Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
AutoSetupStatusDisplayer.cs
Go to the documentation of this file.
4 #if UNITY_EDITOR
7 using UnityEditor.Events;
8 using UnityEngine;
9 using UnityEngine.Events;
10 using UnityEngine.UI;
11 
12 namespace droid.Runtime.Utilities.GameObjects.StatusDisplayer {
16  [ExecuteInEditMode]
17  public class AutoSetupStatusDisplayer : MonoBehaviour {
18  [SerializeField] bool _clean_empty_no_target_events = true;
19 
20  [SerializeField] NeodroidEnvironment _environment = null;
21  [SerializeField] TextUpdater _environment_frame = null;
22  [SerializeField] TextUpdater _environment_obs = null;
23 
24  [SerializeField] TextUpdater _environment_text = null;
25  [SerializeField] ObjectiveFunction _evaluation_function = null;
26  [SerializeField] AbstractNeodroidManager _manager = null;
27  [SerializeField] Button _reset_button = null;
28  [SerializeField] TextUpdater _signal = null;
29  [SerializeField] TextUpdater _status_text = null;
30  [SerializeField] ToggleUpdater _terminated = null;
31  [SerializeField] Toggle _testing_toggle = null;
32  [SerializeField] UnityEventCallState _unity_event_call_state = UnityEventCallState.RuntimeOnly;
33 
34  #if NEODROID_DEBUG
35  bool Debugging { get { return this._debugging; } set { this._debugging = value; } }
36  [SerializeField] bool _debugging;
37  #endif
38 
39  void TryRegister(DataPoller poller, UnityAction<DataPoller> f) {
40  if (poller) {
41  var count = poller.PollEvent.GetPersistentEventCount();
42  if (this._clean_empty_no_target_events && count > 0) {
43  //poller.PollEvent.RemoveAllListeners(); // Only non-persistant listeners.
44  for (var i = 0; i < count; i++) {
45  if (poller.PollEvent.GetPersistentTarget(i) == null
46  || poller.PollEvent.GetPersistentMethodName(i) == null) {
47  UnityEventTools.RemovePersistentListener(poller.PollEvent, i);
48  }
49  }
50  }
51 
52  count = poller.PollEvent.GetPersistentEventCount();
53  if (count == 0) {
54  UnityEventTools.AddObjectPersistentListener(poller.PollEvent, f, poller);
55  poller.PollEvent.SetPersistentListenerState(0, this._unity_event_call_state);
56  } else if (count > 0 && poller.PollEvent.GetPersistentTarget(0) != poller) {
57  #if NEODROID_DEBUG
58  if (this.Debugging) {
59  Debug.Log($"PollEvent on {poller} already has a listeners");
60  }
61  #endif
62  }
63  }
64  }
65 
66  void TryRegisterVoid(UnityEventBase poller, UnityAction f) {
67  var count = poller.GetPersistentEventCount();
68  if (this._clean_empty_no_target_events && count > 0) {
69  //poller.PollEvent.RemoveAllListeners(); // Only non-persistant listeners.
70  for (var i = 0; i < count; i++) {
71  if (poller.GetPersistentTarget(i) == null || poller.GetPersistentMethodName(i) == null) {
72  UnityEventTools.RemovePersistentListener(poller, i);
73  }
74  }
75  }
76 
77  count = poller.GetPersistentEventCount();
78  if (count == 0) {
79  UnityEventTools.AddVoidPersistentListener(poller, f);
80  poller.SetPersistentListenerState(0, this._unity_event_call_state);
81  } else if (count > 0) {
82  #if NEODROID_DEBUG
83  if (this.Debugging) {
84  Debug.Log($"PollEvent on {poller} already has a listeners");
85  }
86  #endif
87  }
88  }
89 
90  void TryRegisterProperty(Toggle.ToggleEvent poller, UnityAction<bool> f) {
91  var count = poller.GetPersistentEventCount();
92  if (this._clean_empty_no_target_events && count > 0) {
93  //poller.PollEvent.RemoveAllListeners(); // Only non-persistent listeners.
94  for (var i = 0; i < count; i++) {
95  if (poller.GetPersistentTarget(i) == null || poller.GetPersistentMethodName(i) == null) {
96  UnityEventTools.RemovePersistentListener(poller, i);
97  }
98  }
99  }
100 
101  count = poller.GetPersistentEventCount();
102  if (count == 0) {
103  UnityEventTools.AddPersistentListener(poller, f);
104  poller.SetPersistentListenerState(0, this._unity_event_call_state);
105  } else if (count > 0) {
106  #if NEODROID_DEBUG
107  if (this.Debugging) {
108  Debug.Log($"PollEvent on {poller} already has a listeners");
109  }
110  #endif
111  }
112  }
113 
114  void Start() {
115  if (!this._environment) {
116  this._environment = FindObjectOfType<NeodroidEnvironment>();
117  }
118 
119  var neodroid_environment = this._environment;
120 
121  if (neodroid_environment != null) {
122  this.TryRegister(this._environment_text, neodroid_environment.IdentifierString);
123  this.TryRegister(this._environment_frame, neodroid_environment.FrameString);
124  this.TryRegister(this._environment_obs, neodroid_environment.ObservationsString);
125  this.TryRegister(this._terminated, neodroid_environment.TerminatedBoolean);
126  }
127 
128  if (!this._evaluation_function) {
129  this._evaluation_function = FindObjectOfType<ObjectiveFunction>();
130  }
131 
132  var evaluation_function = this._evaluation_function;
133  if (evaluation_function != null) {
134  this.TryRegister(this._signal, evaluation_function.SignalString);
135  }
136 
137  if (!this._manager) {
138  this._manager = FindObjectOfType<AbstractNeodroidManager>();
139  }
140 
141  if (this._manager) {
142  if (this._status_text) {
143  this.TryRegister(this._status_text, this._manager.StatusString);
144  }
145 
146  if (this._testing_toggle) {
147  this.TryRegisterProperty(this._testing_toggle.onValueChanged, this._manager.SetTesting);
148  }
149  }
150 
151  if (this._reset_button) {
152  this.TryRegisterVoid(this._reset_button.onClick, this._manager.ResetAllEnvironments);
153  }
154  }
155  }
156 }
157 #endif