Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Merge pull request #114 from scellecs/bugfix/conditional-attributes
Browse files Browse the repository at this point in the history
Bugfix/conditional attributes
  • Loading branch information
elleyer authored Jan 31, 2022
2 parents a749e04 + f66fe84 commit fa24e79
Show file tree
Hide file tree
Showing 22 changed files with 185 additions and 129 deletions.
File renamed without changes.
29 changes: 29 additions & 0 deletions Attributes/Conditional/ConditionalAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Frigg {
using System;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method,
Inherited = false, AllowMultiple = true)]
public abstract class ConditionAttribute : Attribute, IAttribute {
public EConditionType ConditionType { get; }
public string MemberName { get; }
public string Expression { get; }

public bool ExpectedValue { get; private set; }

protected ConditionAttribute(string expression) {
this.Expression = expression;
this.ConditionType = EConditionType.ByExpression;
}

protected ConditionAttribute(string memberName, bool expectedValue) {
this.ConditionType = EConditionType.ByCondition;
this.MemberName = memberName;
this.ExpectedValue = expectedValue;
}
}

public enum EConditionType {
ByExpression = 0,
ByCondition = 1
}
}
9 changes: 9 additions & 0 deletions Attributes/Conditional/DisableIfAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Frigg {
public class DisableIfAttribute : ConditionAttribute {
public DisableIfAttribute(string expression) : base(expression) {
}

public DisableIfAttribute(string name, bool expected) : base(name, expected) {
}
}
}
9 changes: 9 additions & 0 deletions Attributes/Conditional/EnableIfAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Frigg {
public class EnableIfAttribute : ConditionAttribute {
public EnableIfAttribute(string expression) : base(expression) {
}

public EnableIfAttribute(string name, bool expected) : base(name, expected) {
}
}
}
9 changes: 9 additions & 0 deletions Attributes/Conditional/HideIfAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Frigg {
public class HideIfAttribute : ConditionAttribute {
public HideIfAttribute(string expression) : base(expression) {
}

public HideIfAttribute(string name, bool expected) : base(name, expected) {
}
}
}
9 changes: 9 additions & 0 deletions Attributes/Conditional/ShowIfAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Frigg {
public class ShowIfAttribute : ConditionAttribute {
public ShowIfAttribute(string expression) : base(expression) {
}

public ShowIfAttribute(string name, bool expected) : base(name, expected) {
}
}
}
22 changes: 0 additions & 22 deletions Attributes/Validators/ConditionalAttribute.cs

This file was deleted.

9 changes: 0 additions & 9 deletions Attributes/Validators/DisableIfAttribute.cs

This file was deleted.

9 changes: 0 additions & 9 deletions Attributes/Validators/EnableIfAttribute.cs

This file was deleted.

9 changes: 0 additions & 9 deletions Attributes/Validators/HideIfAttribute.cs

This file was deleted.

9 changes: 0 additions & 9 deletions Attributes/Validators/ShowIfAttribute.cs

This file was deleted.

11 changes: 5 additions & 6 deletions Editor/DecoratorDrawers/InfoBoxDecoratorDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ public override void DrawLayout() {
break;
}

if (CoreUtilities.IsPropertyVisible(this.property)) {
if (!string.IsNullOrEmpty(attr.Member)) {
//todo: move this code into another class. This should be checked only before calling "Property.Draw"
if (!string.IsNullOrEmpty(attr.Member)) {
//todo: get "member" name & call directly.
}
else {
EditorGUILayout.HelpBox(attr.Text, messageType);
}
}
else {
EditorGUILayout.HelpBox(attr.Text, messageType);
}

this.property.CallNextDrawer();
Expand Down
36 changes: 23 additions & 13 deletions Editor/FriggProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Groups;
Expand Down Expand Up @@ -41,12 +42,12 @@ public class FriggProperty {
/// <summary>
/// Property's actual value.
/// </summary>
public PropertyValue<object> NativeValue { get; private set; }
public PropertyValue<object> NativeValue { get; }

/// <summary>
/// Indicates whether property is root(e.g base property).
/// </summary>
public bool IsRootProperty { get; private set; }
public bool IsRootProperty { get; }

/// <summary>
/// Indicates whether property is Expanded in Unity Editor.
Expand All @@ -56,26 +57,26 @@ public class FriggProperty {
public bool IsReadonly { get; private set; }

//Don't need to use this? Check for an attribute and then remove from list?
public bool IsLayoutMember { get; set; }
public bool IsLayoutMember { get; private set; }

/// <summary>
/// Property's reflected name.
/// </summary>
public string Name { get; set; }
public string Name { get; private set; }

/// <summary>
/// Property's displayable name.
/// </summary>
public string NiceName { get; set; }
public string NiceName { get; private set; }

/// <summary>
/// Parent property of target property.
/// </summary>
public FriggProperty ParentProperty { get; set; }
public FriggProperty ParentProperty { get; private set; }
/// <summary>
/// Main property tree for target property.
/// </summary>
public PropertyTree PropertyTree { get; set; }
public PropertyTree PropertyTree { get; }

/// <summary>
/// Children properties of target property.
Expand All @@ -87,16 +88,18 @@ public class FriggProperty {
/// </summary>
///
public IEnumerable<Attribute> FixedAttributes { get; private set; }
public List<Attribute> ConditionAttributes { get; private set; }
//All applied attributes on target property.
//private List<Attribute> fixedAttributes = new List<Attribute>();

public FriggProperty(PropertyTree propertyTree, PropertyValue<object> value, bool isRoot = false) {
this.NativeValue = value;
this.IsRootProperty = isRoot;
this.PropertyTree = propertyTree;
this.Name = value.MetaInfo.Name;
this.Path = propertyTree.TargetType.ToString();
this.FixedAttributes = value.MetaInfo.MemberType.GetCustomAttributes();
this.NativeValue = value;
this.IsRootProperty = isRoot;
this.PropertyTree = propertyTree;
this.Name = value.MetaInfo.Name;
this.Path = propertyTree.TargetType.ToString();
this.FixedAttributes = value.MetaInfo.MemberType.GetCustomAttributes();
this.ConditionAttributes = this.FixedAttributes.Where(x => x is ConditionalAttribute).ToList();
}

/// <summary>
Expand Down Expand Up @@ -131,6 +134,8 @@ internal static FriggProperty DoProperty(FriggProperty parent, [NotNull] Propert
property.FixedAttributes = !property.IsRootProperty
? property.MetaInfo.MemberInfo.GetCustomAttributes()
: property.MetaInfo.MemberType.GetCustomAttributes();

property.ConditionAttributes = property.FixedAttributes.Where(x => x is ConditionAttribute).ToList();

property.SetNativeProperty();
property.Path = GetFriggPath(property);
Expand Down Expand Up @@ -190,6 +195,10 @@ public void SetReadOnly(bool status) {
/// </summary>
/// <param name="rect">Rect that represents draw info</param>
public void Draw(Rect rect = default) {
if (!CoreUtilities.IsPropertyVisible(this))
return;

EditorGUI.BeginDisabledGroup(!CoreUtilities.IsPropertyEnabled(this));
var current = this.DrawersQueue.Current;
if ((current.DrawerType != FriggDrawerType.Custom || current.Drawer != null) && current.IsVisible) {
if (rect == default) {
Expand All @@ -203,6 +212,7 @@ public void Draw(Rect rect = default) {
else {
this.CallNextDrawer(rect);
}
EditorGUI.EndDisabledGroup();
}

/// <summary>
Expand Down
5 changes: 0 additions & 5 deletions Editor/PropertyDrawers/BaseDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ protected BaseDrawer(FriggProperty prop) : base(prop) {
}
}

protected T GetTargetValue<T>() {
this.drawerType = typeof(T);
return DrawerUtils.GetTargetValue<T>(this.property);
}

protected void UpdateAndCallNext(object value, Rect rect = default) =>
DrawerUtils.UpdateAndCallNext(this.drawerType, this.property, value, rect);

Expand Down
Loading

0 comments on commit fa24e79

Please sign in to comment.