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