DisableIf.cs
5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System;
namespace Leap.Unity.Attributes {
/// <summary>
/// Conditionally disables a property based on the value of another property. The only condition
/// types that are currently supported are bool types, and enum types. The property has two arguments
/// names 'equalTo' and 'notEqualTo'. Exactly one of them must be specified, like so:
///
/// [DisableIf("myBoolProperty", isEqualTo: true)]
/// [DisableIf("myEnumProperty", isNotEqualTo: MyEnum.Value)]
/// [DisableIfAny("bool1", "bool2", isEqualTo: false)]
/// [DisableIfAll("cond1", "cond2", "cond3", isNotEqualTo: true)]
/// </summary>
public abstract class DisableIfBase : CombinablePropertyAttribute, IPropertyDisabler {
public readonly string[] propertyNames;
public readonly object testValue;
public readonly bool disableResult;
public readonly bool isAndOperation;
public DisableIfBase(object isEqualTo, object isNotEqualTo, bool isAndOperation, params string[] propertyNames) {
this.propertyNames = propertyNames;
this.isAndOperation = isAndOperation;
if ((isEqualTo != null) == (isNotEqualTo != null)) {
throw new ArgumentException("Must specify exactly one of 'equalTo' or 'notEqualTo'.");
}
if (isEqualTo != null) {
testValue = isEqualTo;
disableResult = true;
} else if (isNotEqualTo != null) {
testValue = isNotEqualTo;
disableResult = false;
}
if (!(testValue is bool) && !(testValue is Enum)) {
throw new ArgumentException("Only values of bool or Enum are allowed in comparisons using DisableIf.");
}
}
#if UNITY_EDITOR
public bool ShouldDisable(SerializedProperty property) {
foreach (var name in propertyNames) {
var prop = property.serializedObject.FindProperty(name);
bool result = shouldDisable(prop);
if (isAndOperation) {
if (!result) {
return false;
}
} else {
if (result) {
return true;
}
}
}
if (isAndOperation) {
return true;
} else {
return false;
}
}
private bool shouldDisable(SerializedProperty property) {
if (property.propertyType == SerializedPropertyType.Boolean) {
return (property.boolValue == (bool)testValue) == disableResult;
} else if (property.propertyType == SerializedPropertyType.Enum) {
return (property.intValue == (int)testValue) == disableResult;
} else {
Debug.LogError("Can only conditionally disable based on boolean or enum types.");
return false;
}
}
#endif
}
public class DisableIf : DisableIfBase {
public DisableIf(string propertyName, object isEqualTo = null, object isNotEqualTo = null) :
base(isEqualTo, isNotEqualTo, true, propertyName) { }
}
public class DisableIfAny : DisableIfBase {
public DisableIfAny(string propertyName1, string propertyName2, object areEqualTo = null, object areNotEqualTo = null) :
base(areEqualTo, areNotEqualTo, false, propertyName1, propertyName2) { }
public DisableIfAny(string propertyName1, string propertyName2, string propertyName3, object areEqualTo = null, object areNotEqualTo = null) :
base(areEqualTo, areNotEqualTo, false, propertyName1, propertyName2, propertyName3) { }
public DisableIfAny(string propertyName1, string propertyName2, string propertyName3, string propertyName4, object areEqualTo = null, object areNotEqualTo = null) :
base(areEqualTo, areNotEqualTo, false, propertyName1, propertyName2, propertyName3, propertyName4) { }
}
public class DisableIfAll : DisableIfBase {
public DisableIfAll(string propertyName1, string propertyName2, object areEqualTo = null, object areNotEqualTo = null) :
base(areEqualTo, areNotEqualTo, true, propertyName1, propertyName2) { }
public DisableIfAll(string propertyName1, string propertyName2, string propertyName3, object areEqualTo = null, object areNotEqualTo = null) :
base(areEqualTo, areNotEqualTo, true, propertyName1, propertyName2, propertyName3) { }
public DisableIfAll(string propertyName1, string propertyName2, string propertyName3, string propertyName4, object areEqualTo = null, object areNotEqualTo = null) :
base(areEqualTo, areNotEqualTo, true, propertyName1, propertyName2, propertyName3, propertyName4) { }
}
}