Skip to content

Commit

Permalink
Remove unnecessary delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
vanjac committed Nov 10, 2023
1 parent 9036a1e commit 0de0430
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 82 deletions.
58 changes: 17 additions & 41 deletions Assets/Base/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
using System.Xml.Serialization;
using UnityEngine;

public delegate object GetProperty();
public delegate void SetProperty(object value);
public delegate void PropertyGUI(Property property);

public struct Property
{
public string id;
public string name;
public GetProperty getter;
public SetProperty setter;
public Func<object> getter;
public Action<object> setter;
public PropertyGUI gui;
public object value
{
Expand All @@ -29,7 +27,7 @@ public object value
}
public bool explicitType; // store object type with property in file

public Property(string id, string name, GetProperty getter, SetProperty setter,
public Property(string id, string name, Func<object> getter, Action<object> setter,
PropertyGUI gui, bool explicitType = false)
{
this.id = id;
Expand Down Expand Up @@ -67,8 +65,6 @@ public class PropertiesObjectType
{
public static readonly PropertiesObjectType NONE = new PropertiesObjectType("None", null);

public delegate PropertiesObject PropertiesObjectConstructor();

public string fullName; // not readonly so it can be serialized
[XmlIgnore]
public readonly string description;
Expand All @@ -78,7 +74,7 @@ public class PropertiesObjectType
public readonly string iconName;
[XmlIgnore]
public readonly Type type;
private readonly PropertiesObjectConstructor constructor;
private readonly Func<PropertiesObject> constructor;

private Texture _icon;
public Texture icon
Expand All @@ -105,7 +101,7 @@ public PropertiesObjectType(string fullName, Type type)
}

public PropertiesObjectType(string fullName, string description, string iconName,
Type type, PropertiesObjectConstructor constructor = null)
Type type, Func<PropertiesObject> constructor = null)
{
this.fullName = fullName;
this.description = description;
Expand All @@ -119,7 +115,7 @@ public PropertiesObjectType(string fullName, Type type)
}

public PropertiesObjectType(string fullName, string description, string longDescription,
string iconName, Type type, PropertiesObjectConstructor constructor = null)
string iconName, Type type, Func<PropertiesObject> constructor = null)
{
this.fullName = fullName;
this.description = description;
Expand All @@ -132,7 +128,7 @@ public PropertiesObjectType(string fullName, Type type)
this.constructor = constructor;
}

public PropertiesObjectType(PropertiesObjectType baseType, PropertiesObjectConstructor newConstructor)
public PropertiesObjectType(PropertiesObjectType baseType, Func<PropertiesObject> newConstructor)
{
this.fullName = baseType.fullName;
this.description = baseType.description;
Expand Down Expand Up @@ -595,9 +591,7 @@ public virtual void OnDisable()

public class BehaviorType : PropertiesObjectType
{
public delegate bool BehaviorRule(Entity checkEntity);

public readonly BehaviorRule rule;
public readonly Predicate<Entity> rule;

public BehaviorType(string fullName, Type type)
: base(fullName, type)
Expand All @@ -606,7 +600,7 @@ public BehaviorType(string fullName, Type type)
}

public BehaviorType(string fullName, string description, string iconName, Type type,
BehaviorRule rule = null)
Predicate<Entity> rule = null)
: base(fullName, description, iconName, type)
{
if (rule == null)
Expand All @@ -616,7 +610,7 @@ public BehaviorType(string fullName, Type type)
}

public BehaviorType(string fullName, string description, string longDescription, string iconName, Type type,
BehaviorRule rule = null)
Predicate<Entity> rule = null)
: base(fullName, description, longDescription, iconName, type)
{
if (rule == null)
Expand All @@ -625,34 +619,16 @@ public BehaviorType(string fullName, Type type)
this.rule = rule;
}

private static bool DefaultRule(Entity checkEntity)
{
return true;
}
private static bool DefaultRule(Entity checkEntity) => true;

public static BehaviorRule AndRule(BehaviorRule r1, BehaviorRule r2)
{
return (Entity checkEntity) =>
{
return r1(checkEntity) && r2(checkEntity);
};
}
public static Predicate<Entity> AndRule(Predicate<Entity> r1, Predicate<Entity> r2) =>
(Entity checkEntity) => r1(checkEntity) && r2(checkEntity);

public static BehaviorRule BaseTypeRule(Type baseType)
{
return (Entity checkEntity) =>
{
return baseType.IsAssignableFrom(checkEntity.GetType());
};
}
public static Predicate<Entity> BaseTypeRule(Type baseType) =>
(Entity checkEntity) => baseType.IsAssignableFrom(checkEntity.GetType());

public static BehaviorRule NotBaseTypeRule(Type baseType)
{
return (Entity checkEntity) =>
{
return !baseType.IsAssignableFrom(checkEntity.GetType());
};
}
public static Predicate<Entity> NotBaseTypeRule(Type baseType) =>
(Entity checkEntity) => !baseType.IsAssignableFrom(checkEntity.GetType());
}


Expand Down
4 changes: 1 addition & 3 deletions Assets/VoxelEditor/GUI/ColorPickerGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ public class ColorPickerGUI : GUIPanel
{
private const int PREVIEW_SIZE = 250;

public delegate void ColorChangeHandler(Color color);

private Color color;
private float hue, saturation, value;
private Texture2D colorTexture = null;
private Texture2D hueTexture, saturationTexture, valueTexture, alphaTexture;
private GUIStyle hueSliderStyle = null, saturationSliderStyle = null, valueSliderStyle = null, alphaSliderStyle = null;
public ColorChangeHandler handler;
public System.Action<Color> handler;
public bool includeAlpha = false;

public override Rect GetRect(Rect safeRect, Rect screenRect)
Expand Down
17 changes: 5 additions & 12 deletions Assets/VoxelEditor/GUI/DialogGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

public class DialogGUI : GUIPanel
{
public delegate void ButtonHandler();

public string message;
public string yesButtonText;
public string noButtonText;
public ButtonHandler yesButtonHandler;
public ButtonHandler noButtonHandler;
public System.Action yesButtonHandler;
public System.Action noButtonHandler;

private bool calledHandler = false;

Expand Down Expand Up @@ -68,12 +66,9 @@ public void OnDestroy()

public class TextInputDialogGUI : GUIPanel
{
public delegate void TextHandler(string text);
public delegate void CancelHandler();

public TextHandler handler;
public System.Action<string> handler;
// TODO not called when touch keyboard not supported
public CancelHandler cancelHandler;
public System.Action cancelHandler;
public string prompt;
public string text = "";

Expand Down Expand Up @@ -153,10 +148,8 @@ public override void WindowGUI()

public class LargeMessageGUI : GUIPanel
{
public delegate void ButtonHandler();

public string message;
public ButtonHandler closeHandler;
public System.Action closeHandler;

public static LargeMessageGUI ShowLargeMessageDialog(GameObject gameObject, string message)
{
Expand Down
3 changes: 1 addition & 2 deletions Assets/VoxelEditor/GUI/EntityPickerGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

public class EntityPickerGUI : ActionBarGUI
{
public delegate void EntityPickerHanlder(ICollection<Entity> entities);
public EntityPickerHanlder handler;
public System.Action<ICollection<Entity>> handler;
public bool allowNone = true, allowMultiple = true, allowNull = false;
public string nullName = "None";

Expand Down
4 changes: 1 addition & 3 deletions Assets/VoxelEditor/GUI/FilterGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

public class FilterGUI : GUIPanel
{
public delegate void FilterHandler(ActivatedSensor.Filter filter);

public ActivatedSensor.Filter current;
public FilterHandler handler;
public System.Action<ActivatedSensor.Filter> handler;
public VoxelArrayEditor voxelArray;

public override Rect GetRect(Rect safeRect, Rect screenRect)
Expand Down
4 changes: 1 addition & 3 deletions Assets/VoxelEditor/GUI/MaterialSelectorGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public class MaterialSelectorGUI : GUIPanel
private const string CUSTOM_CATEGORY = "CUSTOM";
private const string WORLD_LIST_CATEGORY = "Import from world...";

public delegate void MaterialSelectHandler(Material material);

public MaterialSelectHandler handler;
public System.Action<Material> handler;
public string rootDirectory = "Materials";
public bool isOverlay = false;
public bool allowNullMaterial = false;
Expand Down
4 changes: 1 addition & 3 deletions Assets/VoxelEditor/GUI/PaintGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public class PaintGUI : GUIPanel

private static List<VoxelFace> recentPaints = new List<VoxelFace>(); // most recent first

public delegate void PaintHandler(VoxelFace paint);

public PaintHandler handler;
public System.Action<VoxelFace> handler;
public VoxelFace paint;
public VoxelArrayEditor voxelArray;

Expand Down
3 changes: 1 addition & 2 deletions Assets/VoxelEditor/GUI/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
public static class PropertyGUIs
{
private static TouchScreenKeyboard numberKeyboard = null;
private delegate void KeyboardHandler(string text);
private static KeyboardHandler keyboardHandler;
private static Action<string> keyboardHandler;

private static readonly Lazy<GUIStyle> alignedLabelStyle = new Lazy<GUIStyle>(() =>
{
Expand Down
7 changes: 3 additions & 4 deletions Assets/VoxelEditor/GUI/PropertiesGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public StoredPropertiesObject(PropertiesObject[] objects)
int _propertyI = propertyI; // for use in lambda functions -- won't change
Property firstProperty = objectPropertiesSets[0][propertyI];

GetProperty getter = () =>
System.Func<object> getter = () =>
{
object value = objectPropertiesSets[0][_propertyI].getter();
for (int objectI = 0; objectI < numObjects; objectI++)
Expand All @@ -63,7 +63,7 @@ public StoredPropertiesObject(PropertiesObject[] objects)
return value;
};

SetProperty setter = value =>
System.Action<object> setter = value =>
{
for (int objectI = 0; objectI < numObjects; objectI++)
objectPropertiesSets[objectI][_propertyI].setter(value);
Expand Down Expand Up @@ -524,8 +524,7 @@ private void DeleteButton()

public class NewBehaviorGUI : GUIPanel
{
public delegate void BehaviorTypeHandler(PropertiesObjectType behavior);
public BehaviorTypeHandler handler;
public System.Action<PropertiesObjectType> handler;
public Entity self;
public VoxelArrayEditor voxelArray;

Expand Down
3 changes: 1 addition & 2 deletions Assets/VoxelEditor/GUI/TagPickerGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

public class TagPickerGUI : GUIPanel
{
public delegate void TagHandler(byte tag);
public TagHandler handler;
public System.Action<byte> handler;
public bool multiple;
public byte multiSelection;

Expand Down
4 changes: 1 addition & 3 deletions Assets/VoxelEditor/GUI/TargetGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

public class TargetGUI : GUIPanel
{
public delegate void TargetHandler(Target target);

public TargetHandler handler;
public System.Action<Target> handler;
public VoxelArrayEditor voxelArray;
public bool allowObjectTarget = true, allowNullTarget = false, allowVertical = true;
public bool alwaysWorld = false, allowRandom = true;
Expand Down
4 changes: 1 addition & 3 deletions Assets/VoxelEditor/GUI/TypePickerGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

public class TypePickerGUI : GUIPanel
{
public delegate void TypeHandler(PropertiesObjectType type);

public TypeHandler handler;
public System.Action<PropertiesObjectType> handler;
public PropertiesObjectType[][] categories;
public string[] categoryNames = new string[0];

Expand Down
2 changes: 1 addition & 1 deletion Assets/VoxelEditor/VoxelArrayEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ private void SingleAdjust(Vector3Int adjustDirection, HashSet<Voxel> voxelsToUpd
bool negativeAdjustAxis = adjustDirFaceI % 2 == 0;

// sort selectedThings in order along the adjustDirection vector
selectedThings.Sort(delegate (Selectable a, Selectable b)
selectedThings.Sort((Selectable a, Selectable b) =>
{
// positive means A is greater than B
// so positive means B will be adjusted before A
Expand Down

0 comments on commit 0de0430

Please sign in to comment.