using DunGen.Collision;
using DunGen.Graph;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace DunGen.Editor
{
public static class EditorUtil
{
///
/// Draws a GUI for a game object chance table. Allowing for addition/removal of rows and
/// modification of values and weights
///
/// The table to draw
public static void DrawGameObjectChanceTableGUI(string objectName, GameObjectChanceTable table, List showWeights, bool allowSceneObjects, bool allowAssetObjects, UnityEngine.Object owningObject)
{
string title = string.Format("{0} Weights ({1})", objectName, table.Weights.Count);
EditorGUILayout.LabelField(title, EditorStyles.boldLabel);
EditorGUI.indentLevel = 1;
int toDeleteIndex = -1;
GUILayout.BeginVertical("box");
for (int i = 0; i < table.Weights.Count; i++)
{
var w = table.Weights[i];
GUILayout.BeginVertical("box");
EditorGUILayout.BeginHorizontal();
var obj = (GameObject)EditorGUILayout.ObjectField("", w.Value, typeof(GameObject), allowSceneObjects);
if (obj != null)
{
bool isAsset = EditorUtility.IsPersistent(obj);
if (allowAssetObjects && isAsset || allowSceneObjects && !isAsset)
w.Value = obj;
}
else
w.Value = null;
if (GUILayout.Button("x", EditorStyles.miniButton, InspectorConstants.SmallButtonWidth))
toDeleteIndex = i;
EditorGUILayout.EndHorizontal();
if (i > showWeights.Count - 1)
showWeights.Add(false);
showWeights[i] = EditorGUILayout.Foldout(showWeights[i], "Weights", true);
if (showWeights[i])
{
w.MainPathWeight = EditorGUILayout.FloatField("Main Path", w.MainPathWeight);
w.BranchPathWeight = EditorGUILayout.FloatField("Branch Path", w.BranchPathWeight);
w.DepthWeightScale = EditorGUILayout.CurveField("Depth Scale", w.DepthWeightScale, Color.white, new Rect(0, 0, 1, 1));
}
GUILayout.EndVertical();
}
if (toDeleteIndex >= 0)
{
Undo.RecordObject(owningObject, "Delete Chance Entry");
table.Weights.RemoveAt(toDeleteIndex);
showWeights.RemoveAt(toDeleteIndex);
Undo.FlushUndoRecordObjects();
}
if (GUILayout.Button("Add New " + objectName))
{
Undo.RecordObject(owningObject, "Add Chance Entry");
table.Weights.Add(new GameObjectChance());
showWeights.Add(false);
Undo.FlushUndoRecordObjects();
}
EditorGUILayout.EndVertical();
// Handle dragging objects into the list
var dragTargetRect = GUILayoutUtility.GetLastRect();
var evt = Event.current;
if (evt.type == EventType.DragUpdated || evt.type == EventType.DragPerform)
{
var validDraggingObjects = GetValidGameObjects(DragAndDrop.objectReferences, allowSceneObjects, allowAssetObjects);
if (dragTargetRect.Contains(evt.mousePosition) && validDraggingObjects.Any())
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (evt.type == EventType.DragPerform)
{
Undo.RecordObject(owningObject, "Drag Objects");
DragAndDrop.AcceptDrag();
foreach (var dragObject in validDraggingObjects)
{
table.Weights.Add(new GameObjectChance(dragObject));
showWeights.Add(false);
}
Undo.FlushUndoRecordObjects();
}
}
}
}
public static IEnumerable GetValidGameObjects(IEnumerable