unity-atoms/Source/Editor/GameEventEditor.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
{
public abstract class GameEventEditor<T, E> : UnityEditor.Editor
Cleaned up code readability, naming style to be consistent * Add abstract modifier to all generic classes meant not to be instantiated directly as the intent is to specify a pattern for a closed type that can be serialized. * Added sealed keyword to all closed types. This is an incidental performance improvement for mobile and desktop platforms utilizing il2cpp due to the way unsealed vs sealed virtual methods are handled in generated C++ code. * Also sealed all inspectors to mark them as closed types. * Dropped all where constraints to the next line as most of these class declarations break 120-130 characters. This should help improve readability and make this style consistent as it has been in use for the most-complex generic types already, but not for others. * Dropped all generic type parameters (generally) numbering three or more to the next line to improve readability. * Where found, added empty lines between property/field declarations in classes to improve readability. * Extracted several 2x event classes into their own files to match convention established by other event classes. * Removed unecessary meta files for C# solution. * Added compiler options file (csc.rsp) file for the Unity project and added global warning ignore for 0649 (Field 'field' is never assigned to, and will always have its default value 'value'). This is necessary since 2018.3 switched the compiler to Roslyn and by design they have decided to not suppress this warning for monobehavior fields that are private, but serialized. This is very widely contested as most developers will not make every field needed to be assigned in the Editor as public (breaks encapsulation, opens surface area for bugs) and this can in some cases generate hundreds if not thousands of warnings. Adding the compiler options file suppresses this warning which also may hide legitimate warnings, but is far the lesser of two evils. * Moved example scripts not in a namespace to UnityAtoms.Examples. * Reordered public/private fields and properties to be consistent. Order is as follows; public and private properties followed by public/private fields. * Renamed private fields to use '_' prefix and marked public fields as private where only used internally. Marked these fields with FormerlySerializedAs attribute to preserve older serialized values already in use. * Removed redundant initialization of null to reference types as this is their default value. * Marked implicitly private methods and members without an access modifier as explicitly private. * Updated unit tests to use new private name field when getting private member field info.
2019-04-07 16:03:16 +02:00
where E : GameEvent<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))]
2019-05-06 00:03:43 +02:00
public sealed class BoolEventEditor : GameEventEditor<bool, BoolEvent>
{
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 : GameEventEditor<Collider, ColliderEvent>
{
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))]
2019-05-06 00:03:43 +02:00
public sealed class Collider2DEventEditor : GameEventEditor<Collider2D, Collider2DEvent>
{
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))]
2019-05-06 00:03:43 +02:00
public sealed class ColorEventEditor : GameEventEditor<Color, ColorEvent>
{
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))]
2019-05-06 00:03:43 +02:00
public sealed class FloatEventEditor : GameEventEditor<float, FloatEvent>
{
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))]
2019-05-06 00:03:43 +02:00
public sealed class GameObjectEventEditor : GameEventEditor<GameObject, GameObjectEvent>
{
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))]
2019-05-06 00:03:43 +02:00
public sealed class IntGameEventEditor : GameEventEditor<int, IntEvent>
{
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))]
2019-05-06 00:03:43 +02:00
public sealed class Vector2EventEditor : GameEventEditor<Vector2, Vector2Event>
{
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))]
2019-05-06 00:03:43 +02:00
public sealed class Vector3EventEditor : GameEventEditor<Vector3, Vector3Event>
{
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))]
Cleaned up code readability, naming style to be consistent * Add abstract modifier to all generic classes meant not to be instantiated directly as the intent is to specify a pattern for a closed type that can be serialized. * Added sealed keyword to all closed types. This is an incidental performance improvement for mobile and desktop platforms utilizing il2cpp due to the way unsealed vs sealed virtual methods are handled in generated C++ code. * Also sealed all inspectors to mark them as closed types. * Dropped all where constraints to the next line as most of these class declarations break 120-130 characters. This should help improve readability and make this style consistent as it has been in use for the most-complex generic types already, but not for others. * Dropped all generic type parameters (generally) numbering three or more to the next line to improve readability. * Where found, added empty lines between property/field declarations in classes to improve readability. * Extracted several 2x event classes into their own files to match convention established by other event classes. * Removed unecessary meta files for C# solution. * Added compiler options file (csc.rsp) file for the Unity project and added global warning ignore for 0649 (Field 'field' is never assigned to, and will always have its default value 'value'). This is necessary since 2018.3 switched the compiler to Roslyn and by design they have decided to not suppress this warning for monobehavior fields that are private, but serialized. This is very widely contested as most developers will not make every field needed to be assigned in the Editor as public (breaks encapsulation, opens surface area for bugs) and this can in some cases generate hundreds if not thousands of warnings. Adding the compiler options file suppresses this warning which also may hide legitimate warnings, but is far the lesser of two evils. * Moved example scripts not in a namespace to UnityAtoms.Examples. * Reordered public/private fields and properties to be consistent. Order is as follows; public and private properties followed by public/private fields. * Renamed private fields to use '_' prefix and marked public fields as private where only used internally. Marked these fields with FormerlySerializedAs attribute to preserve older serialized values already in use. * Removed redundant initialization of null to reference types as this is their default value. * Marked implicitly private methods and members without an access modifier as explicitly private. * Updated unit tests to use new private name field when getting private member field info.
2019-04-07 16:03:16 +02:00
public sealed class VoidEventEditor : GameEventEditor<Void, VoidEvent> { }
2019-05-06 00:03:43 +02:00
[CustomEditor(typeof(StringEvent))]
public sealed class StringEventEditor : GameEventEditor<string, StringEvent>
{
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