forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinValueAttribute.cs
202 lines (167 loc) · 4.24 KB
/
MinValueAttribute.cs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using MyBox.Internal;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using MyBox.EditorTools;
#endif
#pragma warning disable 0414
namespace MyBox
{
public class MinValueAttribute : AttributeBase
{
private readonly float _x;
private readonly float _y;
private readonly float _z;
private readonly bool _vectorValuesSet;
public MinValueAttribute(float value)
{
_x = value;
}
public MinValueAttribute(float x, float y, float z)
{
_x = x;
_y = y;
_z = z;
_vectorValuesSet = true;
}
#if UNITY_EDITOR
private string _warning;
public override void ValidateProperty(SerializedProperty property)
{
if (!property.IsNumerical())
{
if (_warning == null) _warning = property.name + " caused: [MinValueAttribute] used with non-numeric property";
return;
}
bool valueHandled = HandleValues(property);
if (valueHandled) property.serializedObject.ApplyModifiedProperties();
}
public override bool OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (_warning == null) return false;
EditorGUI.HelpBox(position, _warning, MessageType.Warning);
return true;
}
public override float? OverrideHeight()
{
if (_warning == null) return null;
return EditorGUIUtility.singleLineHeight;
}
#region Handle Value
/// <returns>true if fixed</returns>
private bool HandleValues(SerializedProperty property)
{
switch (property.propertyType)
{
case SerializedPropertyType.Float:
case SerializedPropertyType.Integer:
return HandleNumerics(property);
case SerializedPropertyType.Vector2:
case SerializedPropertyType.Vector3:
case SerializedPropertyType.Vector4:
return HandleVectors(property);
case SerializedPropertyType.Vector2Int:
case SerializedPropertyType.Vector3Int:
return HandleIntVectors(property);
}
return false;
}
private bool HandleNumerics(SerializedProperty property)
{
var minValue = _x;
if (property.propertyType == SerializedPropertyType.Integer && property.intValue < minValue)
{
property.intValue = (int) minValue;
return true;
}
if (property.propertyType == SerializedPropertyType.Float && property.floatValue < minValue)
{
property.floatValue = minValue;
return true;
}
return false;
}
private bool HandleVectors(SerializedProperty property)
{
var x = _x;
var y = _vectorValuesSet ? _y : x;
var z = _vectorValuesSet ? _z : x;
Vector4 vector = Vector4.zero;
switch (property.propertyType)
{
case SerializedPropertyType.Vector2:
vector = property.vector2Value;
break;
case SerializedPropertyType.Vector3:
vector = property.vector3Value;
break;
case SerializedPropertyType.Vector4:
vector = property.vector4Value;
break;
}
bool handled = false;
if (vector[0] < x)
{
vector[0] = x;
handled = true;
}
if (vector[1] < y)
{
vector[1] = y;
handled = true;
}
if (vector[2] < z)
{
vector[2] = z;
handled = true;
}
switch (property.propertyType)
{
case SerializedPropertyType.Vector2:
property.vector2Value = vector;
break;
case SerializedPropertyType.Vector3:
property.vector3Value = vector;
break;
case SerializedPropertyType.Vector4:
property.vector4Value = vector;
break;
}
return handled;
}
private bool HandleIntVectors(SerializedProperty property)
{
var x = (int) _x;
var y = _vectorValuesSet ? (int) _y : x;
var z = _vectorValuesSet ? (int) _z : x;
if (property.propertyType == SerializedPropertyType.Vector2Int)
{
var vector = property.vector2IntValue;
if (vector.x < x || vector.y < y)
{
property.vector2IntValue = new Vector2Int(
vector.x < x ? x : vector.x,
vector.y < y ? y : vector.y);
return true;
}
return false;
}
if (property.propertyType == SerializedPropertyType.Vector3Int)
{
var vector = property.vector3IntValue;
if (vector.x < x || vector.y < y || vector.z < z)
{
property.vector3IntValue = new Vector3Int(
vector.x < x ? x : vector.x,
vector.y < y ? y : vector.y,
vector.z < z ? z : vector.z);
return true;
}
return false;
}
return false;
}
#endregion
#endif
}
}