Scene picker: Added option to add groups of scenes to open

This commit is contained in:
Anders Ejlersen 2023-08-31 17:41:44 +02:00
parent 41604b98e7
commit a86f76904e
28 changed files with 1052 additions and 225 deletions

View file

@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
namespace Module.NavigationTool.Editor.Toolbar
{
internal abstract class AbstractReordableListDrawer<T>
{
public readonly List<T> list;
private readonly ReorderableList reorderableList;
public event Action onChanged;
public bool IsDirty { get; private set; }
protected AbstractReordableListDrawer(List<T> list, string header)
{
this.list = list;
reorderableList = new ReorderableList(list, typeof(string), true, true, true, true);
reorderableList.drawElementCallback += (rect, index, isActive, isFocused) =>
{
EditorGUI.BeginChangeCheck();
OnDrawElement(rect, index, isActive, isFocused);
if (EditorGUI.EndChangeCheck())
IsDirty = true;
};
reorderableList.drawHeaderCallback += rect => EditorGUI.LabelField(rect, header);
reorderableList.onAddCallback += _ =>
{
OnAddElementToList();
InvokeChanged();
};
reorderableList.onRemoveCallback += rl =>
{
list.RemoveAt(rl.index);
OnRemovedElementFromList(rl.index);
InvokeChanged();
};
reorderableList.onReorderCallback += _ => InvokeChanged();;
reorderableList.elementHeightCallback += OnElementHeight;
}
public void DoList(Rect rect)
{
IsDirty = false;
reorderableList.DoList(rect);
}
public void DoLayoutList()
{
IsDirty = false;
reorderableList.DoLayoutList();
}
protected abstract void OnDrawElement(Rect rect, int index, bool isActive, bool isFocused);
protected abstract void OnAddElementToList();
protected virtual void OnRemovedElementFromList(int index)
{
}
protected virtual float OnElementHeight(int index)
{
return EditorGUIUtility.singleLineHeight;
}
protected void InvokeChanged()
{
onChanged?.Invoke();
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ad769ac53dd3476b82e6a2b62117c2d3
timeCreated: 1693332886

View file

@ -0,0 +1,81 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Module.NavigationTool.Editor.Toolbar
{
internal sealed class SceneGroupReordableListDrawer : AbstractReordableListDrawer<SceneGroup>
{
private readonly List<StringReordableListDrawer> elements = new List<StringReordableListDrawer>();
public SceneGroupReordableListDrawer(List<SceneGroup> list, string header)
: base(list, header)
{
for (var i = 0; i < list.Count; i++)
{
elements.Add(new StringReordableListDrawer(list[i].filters, "Filters"));
}
}
protected override void OnDrawElement(Rect rect, int index, bool isActive, bool isFocused)
{
rect.height -= 2;
rect.y += 1;
SceneGroup group = list[index];
var rectName = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
var rectType = new Rect(rect.x, rectName.yMax + 2f, rect.width, EditorGUIUtility.singleLineHeight);
var rectOptional = new Rect(rect.x, rectType.yMax + 2f, rect.width - 80f, EditorGUIUtility.singleLineHeight);
var rectOptionalBtn = new Rect(rectOptional.xMax, rectType.yMax + 2f, 80f, EditorGUIUtility.singleLineHeight);
var rectLabels = new Rect(rect.x, rectOptional.yMax + 2f, rect.width, rect.height - (rectName.y - rectOptional.yMax) - 4f);
EditorGUI.BeginChangeCheck();
group.name = EditorGUI.TextField(rectName, "Name", group.name);
group.optionalMainScenePath = EditorGUI.TextField(rectOptional, "Optional Main Scene Path", group.optionalMainScenePath);
if (GUI.Button(rectOptionalBtn, "Find"))
{
string mainScenePath = EditorUtility.OpenFilePanel("Scene", "Assets", "unity");
if (!string.IsNullOrEmpty(mainScenePath))
{
mainScenePath = mainScenePath.Substring(Application.dataPath.Length - 6);
group.optionalMainScenePath = mainScenePath;
}
}
group.filterType = (ESceneGroupFilterType)EditorGUI.EnumPopup(rectType, "Filter Type", group.filterType);
elements[index].DoList(rectLabels);
if (EditorGUI.EndChangeCheck())
InvokeChanged();
}
protected override void OnAddElementToList()
{
var filters = new List<string>();
list.Add(new SceneGroup
{
name = "New Scene Group",
filters = filters
});
elements.Add(new StringReordableListDrawer(filters, "Filters"));
}
protected override void OnRemovedElementFromList(int index)
{
elements.RemoveAt(index);
}
protected override float OnElementHeight(int index)
{
int labelCount = list[index].filters.Count;
return base.OnElementHeight(index) * 3f
+ Mathf.Max(0, labelCount - 1) * (EditorGUIUtility.singleLineHeight + 2f)
+ 78f;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4923054c7113444ab1b696d769cdae00
timeCreated: 1693333828

View file

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace Module.NavigationTool.Editor.Toolbar
{
internal sealed class StringReordableListDrawer : AbstractReordableListDrawer<string>
{
public StringReordableListDrawer(List<string> list, string header)
: base(list, header)
{
}
protected override void OnDrawElement(Rect rect, int index, bool isActive, bool isFocused)
{
rect.height -= 2;
rect.y += 1;
string text = list[index];
string temp = EditorGUI.TextField(rect, text);
if (string.Equals(text, temp, StringComparison.Ordinal))
return;
list[index] = temp;
InvokeChanged();
}
protected override void OnAddElementToList()
{
list.Add(string.Empty);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e4b2250a66d548f1aacb916c7b5b6204
timeCreated: 1693333142