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