init
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using RPGCoreCommon.Helpers;
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace RPGCoreCommon.DropTable.Editor
|
||||
{
|
||||
[Serializable]
|
||||
[CanEditMultipleObjects]
|
||||
[CustomEditor(typeof(DropTableSO))]
|
||||
public class DropTableDrawer : UnityEditor.Editor
|
||||
{
|
||||
[SerializeField] private VisualTreeAsset _groupVisualTreeAsset;
|
||||
[SerializeField] private VisualTreeAsset _rowVisualTreeAsset;
|
||||
public static VisualTreeAsset groupVisualTreeAsset { get; private set; }
|
||||
public static VisualTreeAsset rowVisualTreeAsset { get; private set; }
|
||||
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
groupVisualTreeAsset = _groupVisualTreeAsset;
|
||||
rowVisualTreeAsset = _rowVisualTreeAsset;
|
||||
|
||||
if (serializedObject.isEditingMultipleObjects)
|
||||
return new HelpBox($"Multiple {nameof(DropTableSO)} editing is disabled.", HelpBoxMessageType.Warning);
|
||||
|
||||
var property = serializedObject.FindProperty(nameof(DropTableSO.group));
|
||||
|
||||
var wrapper = new VisualElement();
|
||||
wrapper.schedule.Execute(() =>
|
||||
{
|
||||
var mainInspector = wrapper.GetFirstAncestorOfType<InspectorElement>();
|
||||
mainInspector.style.paddingLeft = 4f;
|
||||
mainInspector.style.paddingRight = 4f;
|
||||
});
|
||||
|
||||
var resultBox = new HelpBox();
|
||||
resultBox.style.display = DisplayStyle.None;
|
||||
resultBox.style.unityTextAlign = TextAnchor.MiddleCenter;
|
||||
|
||||
var resultButton = new Button();
|
||||
resultButton.style.marginTop = 20f;
|
||||
resultButton.clicked += () => PrintResult(resultBox, property.boxedValue as DropTableGroup);
|
||||
resultButton.text = "Try your luck...";
|
||||
|
||||
wrapper.Add(new DropTableGroupElement(property));
|
||||
wrapper.Add(resultButton);
|
||||
wrapper.Add(resultBox);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
private void PrintResult(HelpBox resultBox, DropTableGroup value)
|
||||
{
|
||||
resultBox.Clear();
|
||||
resultBox.style.display = DisplayStyle.Flex;
|
||||
var result = new Dictionary<Object, int>();
|
||||
value.Get(ref result);
|
||||
result.DictForEach((item, count) =>
|
||||
{
|
||||
var row = new VisualElement();
|
||||
row.style.flexDirection = FlexDirection.Row;
|
||||
row.Add(new ObjectField{value = item, enabledSelf = false});
|
||||
row.Add(new Label($" -> {count}"));
|
||||
resultBox.Add(row);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user