unity-atoms/Source/Editor/Editors/AtomEventEditor.cs

154 lines
5.7 KiB
C#
Raw Normal View History

2019-05-06 00:03:43 +02:00
#if UNITY_2019_1_OR_NEWER
2018-10-30 20:05:06 +01:00
using UnityEditor;
using UnityEngine;
2019-05-06 00:03:43 +02:00
using UnityEngine.UIElements;
using UnityEditor.UIElements;
2018-10-30 20:05:06 +01:00
namespace UnityAtoms.Editor
2018-10-30 20:05:06 +01:00
{
public abstract class AtomEventEditor<T, E> : UnityEditor.Editor
where E : AtomEvent<T>
2018-10-30 20:05:06 +01:00
{
2019-05-06 00:03:43 +02:00
protected T _raiseValue = default(T);
protected virtual VisualElement GetRaiseValueInput() { return null; }
2018-10-30 20:05:06 +01:00
2019-05-06 00:03:43 +02:00
public override VisualElement CreateInspectorGUI()
{
var wrapper = new VisualElement();
wrapper.SetEnabled(Application.isPlaying);
2018-10-30 20:05:06 +01:00
2019-05-06 00:03:43 +02:00
var input = GetRaiseValueInput();
if (input != null)
2018-10-30 20:05:06 +01:00
{
wrapper.Add(input);
2018-10-30 20:05:06 +01:00
}
2019-05-06 00:03:43 +02:00
wrapper.Add(new Button(() =>
2019-05-06 00:03:43 +02:00
{
E e = target as E;
e.Raise(_raiseValue);
})
{
text = "Raise"
});
return wrapper;
2018-10-30 20:05:06 +01:00
}
}
[CustomEditor(typeof(BoolEvent))]
public sealed class BoolEventEditor : AtomEventEditor<bool, BoolEvent>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Toggle() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<bool>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
[CustomEditor(typeof(ColliderEvent))]
public sealed class ColliderEventEventEditor : AtomEventEditor<Collider, ColliderEvent>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new ObjectField() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value", objectType = typeof(Collider) };
input.RegisterCallback<ChangeEvent<Collider>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
2018-10-30 20:05:06 +01:00
[CustomEditor(typeof(Collider2DEvent))]
public sealed class Collider2DEventEditor : AtomEventEditor<Collider2D, Collider2DEvent>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new ObjectField() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value", objectType = typeof(Collider2D) };
input.RegisterCallback<ChangeEvent<Collider2D>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
2018-10-30 20:05:06 +01:00
[CustomEditor(typeof(ColorEvent))]
public sealed class ColorEventEditor : AtomEventEditor<Color, ColorEvent>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new ColorField() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<Color>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
2018-10-30 20:05:06 +01:00
[CustomEditor(typeof(FloatEvent))]
public sealed class FloatEventEditor : AtomEventEditor<float, FloatEvent>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new FloatField() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<float>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
2018-10-30 20:05:06 +01:00
[CustomEditor(typeof(GameObjectEvent))]
public sealed class GameObjectEventEditor : AtomEventEditor<GameObject, GameObjectEvent>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new ObjectField() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value", objectType = typeof(GameObject) };
input.RegisterCallback<ChangeEvent<GameObject>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
2018-10-30 20:05:06 +01:00
[CustomEditor(typeof(IntEvent))]
public sealed class IntEventEditor : AtomEventEditor<int, IntEvent>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new IntegerField() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<int>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
2018-10-30 20:05:06 +01:00
[CustomEditor(typeof(Vector2Event))]
public sealed class Vector2EventEditor : AtomEventEditor<Vector2, Vector2Event>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Vector2Field() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<Vector2>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
2018-10-30 20:05:06 +01:00
[CustomEditor(typeof(Vector3Event))]
public sealed class Vector3EventEditor : AtomEventEditor<Vector3, Vector3Event>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new Vector3Field() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<Vector3>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
2018-10-30 20:05:06 +01:00
[CustomEditor(typeof(VoidEvent))]
public sealed class VoidEventEditor : AtomEventEditor<Void, VoidEvent> { }
2019-05-06 00:03:43 +02:00
[CustomEditor(typeof(StringEvent))]
public sealed class StringEventEditor : AtomEventEditor<string, StringEvent>
2019-05-06 00:03:43 +02:00
{
protected override VisualElement GetRaiseValueInput()
{
var input = new TextField() { label = "Raise value", name = "Raise value", viewDataKey = "Raise value" };
input.RegisterCallback<ChangeEvent<string>>((evt) => { _raiseValue = evt.newValue; });
return input;
}
}
}
2019-05-06 00:03:43 +02:00
#endif