VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVRUI_Link_TMPInputfield.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEditor;
5 
6 using AVR.Core;
7 
8 namespace AVR.UI.Link {
9  [ExecuteInEditMode]
10  [RequireComponent(typeof(TMPro.TMP_InputField))]
12  {
13  public override List<System.Type> validTypes => new List<System.Type> {typeof(bool), typeof(float), typeof(int), typeof(double), typeof(string)};
14 
15  private TMPro.TMP_InputField input;
16 
17  protected virtual UnityEngine.Events.UnityAction<string> updateValueListener => delegate { this.updateValue(); };
18 
19  public override void setup() {
20  init();
21 
22  updateOutput();
23 
24  // Make sure we have the adequate persisten listeners by (possibly) removing and re-adding
25  #if UNITY_EDITOR
26  UnityEditor.Events.UnityEventTools.RemovePersistentListener(input.onEndEdit, updateValueListener);
27  UnityEditor.Events.UnityEventTools.AddPersistentListener(input.onEndEdit, updateValueListener);
28 
29  UnityEditor.Events.UnityEventTools.RemovePersistentListener(input.onValueChanged, updateValueListener);
30  UnityEditor.Events.UnityEventTools.AddPersistentListener(input.onValueChanged, updateValueListener);
31  #endif
32 
33  }
34 
35  public override void init() {
36  if(!input) input = GetComponent<TMPro.TMP_InputField>();
37 
38  if(input) old_value = input.text.ToString();
39  }
40 
41  public override void updateValue()
42  {
43  // Do nothing if the user is still typing the value
44  if (input.isFocused) return;
45 
46  // Get the value currently displayed in the inputfield
47  string text = input.text;
48 
49  // Update it to the target, if possible.
50  try {
51  switch (memberType)
52  {
53  case MemberType.FIELD:
54  {
55  target.GetType().GetField(field).SetValue(target,
56  System.Convert.ChangeType(text, target.GetType().GetField(field).FieldType)
57  );
58  break;
59  }
60  case MemberType.PROPERTY:
61  {
62  target.GetType().GetProperty(field).SetValue(target,
63  System.Convert.ChangeType(text, target.GetType().GetProperty(field).PropertyType)
64  );
65  break;
66  }
67  }
68  }
69  catch(System.Exception) {
70  AVR_DevConsole.cwarn("Could not update the given target value to "+text, this);
71  }
72  }
73 
74  private string old_value = "";
75 
76  public override void updateOutput()
77  {
78  // Do nothing if the user is still typing the value
79  if(input.isFocused) return;
80 
81  object current_value = null;
82 
83  switch (memberType)
84  {
85  case MemberType.FIELD:
86  {
87  current_value = target.GetType().GetField(field).GetValue(target);
88  break;
89  }
90  case MemberType.PROPERTY:
91  {
92  current_value = target.GetType().GetProperty(field).GetValue(target);
93  break;
94  }
95  }
96 
97  input.text = current_value.ToString();
98 
99 #if UNITY_EDITOR
100  if (old_value != current_value.ToString() && !Application.isPlaying)
101  {
102  EditorUtility.SetDirty(input);
103  old_value = current_value.ToString();
104  }
105 #endif
106  }
107  }
108 }