Tanoda
pb_TypeInspector.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.UI;
3using System.Collections;
4using System.Collections.Generic;
5using System.Reflection;
6using GILES;
8
9namespace GILES.Interface
10{
18 public abstract class pb_TypeInspector : MonoBehaviour
19 {
21 public string memberName = null;
22
24 protected object target = null;
25
27 protected PropertyInfo propertyInfo = null;
28
30 protected FieldInfo fieldInfo = null;
31
34 private bool ignoreSetValue = false;
35
37 private int index = -1;
38
40 public System.Type declaringType { get; private set; }
41
45 internal void SetDeclaringType(System.Type type) { declaringType = type; }
46
49 public pb_TypeInspector parent;
50
54 public void Initialize(object target, PropertyInfo prop)
55 {
56 Initialize(null, target, prop);
57 }
58
59
63 public void Initialize(string name, object target, PropertyInfo prop)
64 {
65 if(!string.IsNullOrEmpty(name))
66 this.memberName = name;
67
68 this.target = target;
69 this.propertyInfo = prop;
70 this.fieldInfo = null;
71 this.declaringType = propertyInfo.PropertyType;
72
73 Initialize_INTERNAL();
74 }
75
79 public void Initialize(object target, FieldInfo field)
80 {
81 Initialize(null, target, field);
82 }
83
87 public void Initialize(string name, object target, FieldInfo field)
88 {
89 if(!string.IsNullOrEmpty(name))
90 this.memberName = name;
91
92 this.target = target;
93 this.propertyInfo = null;
94 this.fieldInfo = field;
95 this.declaringType = fieldInfo.FieldType;
96
97 Initialize_INTERNAL();
98 }
99
103 public void Initialize(string name, UpdateValue getStoredValueDelegate, Callback<object> onValueChangedDelegate)
104 {
105 Initialize(name, getStoredValueDelegate, onValueChangedDelegate, null, null);
106 }
107
111 public void Initialize(string name, UpdateValueWithIndex getStoredValueDelegate, Callback<int, object> onValueChangedDelegate)
112 {
113 Initialize(name, null, null, getStoredValueDelegate, onValueChangedDelegate);
114 }
115
120 public void Initialize( string name,UpdateValue getStoredValueDelegate, Callback<object> onValueChangedDelegate, UpdateValueWithIndex getStoredValueDelegateIndexed, Callback<int, object> onValueChangedDelegateIndexed )
121 {
122 if(!string.IsNullOrEmpty(name))
123 this.memberName = name;
124
125 this.updateValue = getStoredValueDelegate;
126 this.onValueChanged = onValueChangedDelegate;
127 this.updateValueWithIndex = getStoredValueDelegateIndexed;
128 this.onValueChangedAtIndex = onValueChangedDelegateIndexed;
129
130 if(declaringType == null)
131 {
132 object o = updateValue != null ? updateValue() : (updateValueWithIndex != null ? updateValueWithIndex(index) : null);
133
134 if(o != null)
135 declaringType = o.GetType();
136 else
137 declaringType = null;
138
139
140
141 }
142 Initialize_INTERNAL();
143 }
144
145 private void Initialize_INTERNAL()
146 {
147 gameObject.name = GetName();
148
149 InitializeGUI();
150 if (useDefaultSkin) {
151 ApplyDefaultSkin();
152 }
153 UpdateGUI();
154 }
155
159 public void SetIndexInCollection(int index)
160 {
161 this.index = index;
162 }
163
164
165 public const int InputField_MinHeight = 30;
166#if DANA
167 public static readonly Color InputField_BackgroundColor = new Color(1f, 1f, 1f, .8f);
168 public static readonly Color InputField_TextColor = new Color(0f, 38f / 255f, 137f / 255f);
169#else
170 public static readonly Color InputField_BackgroundColor = new Color(.32f, .32f, .32f, .8f);
171 public static readonly Color InputField_TextColor = Color.white;
172#endif
173 virtual public bool useDefaultSkin { get { return true; } }
174
178 public void ApplyDefaultSkin()
179 {
180 foreach(InputField inputField in gameObject.GetComponentsInChildren<InputField>())
181 {
182 LayoutElement layoutElement = inputField.gameObject.DemandComponent<LayoutElement>();
183 layoutElement.minHeight = 30f;
184 inputField.textComponent.color = InputField_TextColor;
185 }
186
187 foreach(Button button in gameObject.GetComponentsInChildren<Button>())
188 {
189 LayoutElement layoutElement = button.gameObject.DemandComponent<LayoutElement>();
190 layoutElement.minHeight = 30f;
191 button.GetComponentInChildren<Text>().color = InputField_TextColor;
192 }
193 }
194
197 public delegate object UpdateValueWithIndex(int index);
198
200 public delegate object UpdateValue();
201
204 public UpdateValue updateValue;
205
208 // public delegate void OnValueChangedAtIndex(int index, object newValue);
209
212 public UpdateValueWithIndex updateValueWithIndex;
213
215 public Callback onValueBeginChange = null;
216
218 public Callback<int> onValueBeginChangeAtIndex = null;
219
220
222 public Callback<object> onValueChanged = null;
223
225 public Callback<int, object> onValueChangedAtIndex = null;
226
230 public virtual void InitializeGUI() {
231 Debug.Log("Init GUI");
232 }
233
237 public void UpdateGUI()
238 {
239 ignoreSetValue = true;
240 OnUpdateGUI();
241 ignoreSetValue = false;
242 }
243
251 protected abstract void OnUpdateGUI();
252
257 protected void SetValue(object value)
258 {
259 if( ignoreSetValue )
260 return;
261
263 if(++onValueSetCount == 1 && GetValue<object>() != null && !GetValue<object>().Equals(value))
264 {
265
266 if( onValueBeginChange != null )
267 onValueBeginChange();
268
269 if(onValueBeginChangeAtIndex != null)
270 onValueBeginChangeAtIndex(index);
271
272 if(propertyInfo != null)
273 Undo.RegisterState(new UndoReflection(target, propertyInfo), "Set " + propertyInfo.Name);
274
275 if(fieldInfo != null)
276 Undo.RegisterState(new UndoReflection(target, fieldInfo), "Set " + fieldInfo.Name);
277 }
278
279 if(onValueChanged != null)
280 {
281 onValueChanged(value);
282 }
283 else
284 if(onValueChangedAtIndex != null)
285 {
286 onValueChangedAtIndex(index, value);
287 }
288 else
289 if(propertyInfo != null)
290 {
291 try
292 {
293 if(target == null)
294 target = System.Activator.CreateInstance(propertyInfo.PropertyType);
295
296 propertyInfo.SetValue(target, value, null);
297 }
298 catch (System.Exception e)
299 {
300 Debug.LogError(e.ToString());
301 }
302 }
303 else
304 if(fieldInfo != null && target != null)
305 {
306 try
307 {
308 if(target == null)
309 target = System.Activator.CreateInstance(fieldInfo.FieldType);
310
311 fieldInfo.SetValue(target, value);
312 }
313 catch(System.Exception e)
314 {
315 Debug.LogError(e.ToString());
316 }
317 }
318
319 OnInspectedValueSet();
320 }
321
325 protected virtual void Update()
326 {
327
328 if( Input.GetMouseButtonUp(0) ||
329 Input.GetMouseButtonUp(1) ||
330 Input.GetMouseButtonUp(2) ||
331 Input.GetKeyUp(KeyCode.Tab) ||
332 Input.GetKeyUp(KeyCode.Return))
333 {
334 onValueSetCount = 0;
335 }
336 }
337
341 public Callback onTypeInspectorSetValue = null;
342
344 private int onValueSetCount = 0;
345
351 protected virtual void OnInspectedValueSet()
352 {
353 if( onTypeInspectorSetValue != null )
354 onTypeInspectorSetValue();
355
356 if(parent != null)
357 {
358 parent.OnInspectedValueSet();
359 }
360 else if(target is Component)
361 {
362 if(propertyInfo != null)
363 pb_ComponentDiff.AddDiff((Component)target, propertyInfo.Name, GetValue<object>());
364 else if(fieldInfo != null)
365 pb_ComponentDiff.AddDiff((Component)target, fieldInfo.Name, GetValue<object>());
366 }
367 }
368
373 public T GetValue<T>(object tNull = null)
374 {
375
376 if (updateValue != null)
377 return (T)updateValue();
378
379 if (updateValueWithIndex != null)
380 return (T)updateValueWithIndex(index);
381
382 if(propertyInfo != null && target != null)
383 return (T) propertyInfo.GetValue(target, null);
384
385 if(fieldInfo != null && target != null)
386 return (T) fieldInfo.GetValue(target);
387
388 return default(T);
389 }
390
394 public virtual string GetName()
395 {
396 if( !string.IsNullOrEmpty(memberName) )
397 return memberName;
398 else
399 if(propertyInfo != null)
400 return propertyInfo.Name;
401 else if(fieldInfo != null)
402 return fieldInfo.Name;
403 else
404 return "Generic Type Inspector";
405 }
406 }
407}
UnityEngine.Component Component
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
static void AddDiff(Component component, string name, object value)
static void RegisterState(IUndo target, string message)
Definition: Undo.cs:214
delegate void Callback()