Tanoda
DisableIf.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10#if UNITY_EDITOR
11using UnityEditor;
12#endif
13using System;
14
15namespace Leap.Unity.Attributes {
16
28 public readonly string[] propertyNames;
29 public readonly object testValue;
30 public readonly bool disableResult;
31 public readonly bool isAndOperation;
32 public readonly bool nullIsValid;
33 public readonly bool equalToComparison;
34
35 public DisableIfBase(object isEqualTo, object isNotEqualTo, bool isAndOperation,
36 bool nullIsValid, bool equalToComparison, params string[] propertyNames) {
37 this.propertyNames = propertyNames;
38 this.isAndOperation = isAndOperation;
39 this.nullIsValid = nullIsValid;
40 this.equalToComparison = equalToComparison;
41
42 if (nullIsValid) {
44 testValue = isEqualTo;
45 disableResult = true;
46 } else {
47 testValue = isNotEqualTo;
48 disableResult = false;
49 }
50 } else {
51 if ((isEqualTo != null) == (isNotEqualTo != null)) {
52 throw new ArgumentException("Must specify exactly one of 'equalTo' or 'notEqualTo'.");
53 }
54
55 if (isEqualTo != null) {
56 testValue = isEqualTo;
57 disableResult = true;
58 } else if (isNotEqualTo != null) {
59 testValue = isNotEqualTo;
60 disableResult = false;
61 }
62
63 if (!(testValue is bool) && !(testValue is Enum)) {
64 throw new ArgumentException("Only values of bool or Enum are allowed in comparisons using DisableIf.");
65 }
66 }
67
68 }
69
70#if UNITY_EDITOR
71 public bool ShouldDisable(SerializedProperty property) {
72 foreach (var name in propertyNames) {
73 var prop = property.serializedObject.FindProperty(name);
74
75 bool result = shouldDisable(prop);
76 if (isAndOperation) {
77 if (!result) {
78 return false;
79 }
80 } else {
81 if (result) {
82 return true;
83 }
84 }
85 }
86
87 if (isAndOperation) {
88 return true;
89 } else {
90 return false;
91 }
92 }
93
94 private bool shouldDisable(SerializedProperty property) {
95 if (property == null) {
96 throw new System.NullReferenceException(
97 "Property was null. Expected one of " + propertyNames.ToArrayString());
98 }
99 if (nullIsValid && property.propertyType == SerializedPropertyType.ObjectReference) {
100 return (property.objectReferenceValue == (UnityEngine.Object)testValue) == disableResult;
101 } else if (property.propertyType == SerializedPropertyType.Boolean) {
102 return (property.boolValue == (bool)testValue) == disableResult;
103 } else if (property.propertyType == SerializedPropertyType.Enum) {
104 return (property.intValue == (int)testValue) == disableResult;
105 } else {
106 Debug.LogError("Can only conditionally disable based on boolean or enum types.");
107 return false;
108 }
109 }
110#endif
111 }
112
113 public class DisableIf : DisableIfBase {
114 public DisableIf(string propertyName, object isEqualTo = null, object isNotEqualTo = null) :
115 base(isEqualTo, isNotEqualTo, true, false, false, propertyName) { }
116 }
117
119 public DisableIfEqual(string propertyName, object To) :
120 base(To, null, true, true, true, propertyName) { }
121 }
122
124 public DisableIfNotEqual(string propertyName, object To) :
125 base(null, To, true, true, false, propertyName) { }
126 }
127
129
130 public DisableIfAny(string propertyName1, string propertyName2, object areEqualTo = null, object areNotEqualTo = null) :
131 base(areEqualTo, areNotEqualTo, false, false, false, propertyName1, propertyName2) { }
132
133 public DisableIfAny(string propertyName1, string propertyName2, string propertyName3, object areEqualTo = null, object areNotEqualTo = null) :
134 base(areEqualTo, areNotEqualTo, false, false, false, propertyName1, propertyName2, propertyName3) { }
135
136 public DisableIfAny(string propertyName1, string propertyName2, string propertyName3, string propertyName4, object areEqualTo = null, object areNotEqualTo = null) :
137 base(areEqualTo, areNotEqualTo, false, false, false, propertyName1, propertyName2, propertyName3, propertyName4) { }
138 }
139
141
142 public DisableIfAll(string propertyName1, string propertyName2, object areEqualTo = null, object areNotEqualTo = null) :
143 base(areEqualTo, areNotEqualTo, true, false, false, propertyName1, propertyName2) { }
144
145 public DisableIfAll(string propertyName1, string propertyName2, string propertyName3, object areEqualTo = null, object areNotEqualTo = null) :
146 base(areEqualTo, areNotEqualTo, true, false, false, propertyName1, propertyName2, propertyName3) { }
147
148 public DisableIfAll(string propertyName1, string propertyName2, string propertyName3, string propertyName4, object areEqualTo = null, object areNotEqualTo = null) :
149 base(areEqualTo, areNotEqualTo, true, false, false, propertyName1, propertyName2, propertyName3, propertyName4) { }
150 }
151}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
DisableIfAll(string propertyName1, string propertyName2, string propertyName3, object areEqualTo=null, object areNotEqualTo=null)
Definition: DisableIf.cs:145
DisableIfAll(string propertyName1, string propertyName2, object areEqualTo=null, object areNotEqualTo=null)
Definition: DisableIf.cs:142
DisableIfAll(string propertyName1, string propertyName2, string propertyName3, string propertyName4, object areEqualTo=null, object areNotEqualTo=null)
Definition: DisableIf.cs:148
DisableIfAny(string propertyName1, string propertyName2, object areEqualTo=null, object areNotEqualTo=null)
Definition: DisableIf.cs:130
DisableIfAny(string propertyName1, string propertyName2, string propertyName3, string propertyName4, object areEqualTo=null, object areNotEqualTo=null)
Definition: DisableIf.cs:136
DisableIfAny(string propertyName1, string propertyName2, string propertyName3, object areEqualTo=null, object areNotEqualTo=null)
Definition: DisableIf.cs:133
Conditionally disables a property based on the value of another property. The only condition types th...
Definition: DisableIf.cs:27
readonly string[] propertyNames
Definition: DisableIf.cs:28
DisableIfBase(object isEqualTo, object isNotEqualTo, bool isAndOperation, bool nullIsValid, bool equalToComparison, params string[] propertyNames)
Definition: DisableIf.cs:35
DisableIfEqual(string propertyName, object To)
Definition: DisableIf.cs:119
DisableIf(string propertyName, object isEqualTo=null, object isNotEqualTo=null)
Definition: DisableIf.cs:114
DisableIfNotEqual(string propertyName, object To)
Definition: DisableIf.cs:124