Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
Displayer.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
9 using UnityEditor;
10 using UnityEngine;
11 
12 namespace droid.Runtime.Prototyping.Displayers {
16  public abstract class Displayer : PrototypingGameObject,
17  IDisplayer {
20  AbstractPrototypingEnvironment _environment = null;
21 
25  [SerializeField]
26  protected bool _RetainLastPlot = true;
27 
31  protected dynamic _Values = null;
32 
33  [SerializeField] bool clean_all_children = true;
34  [SerializeField] bool clean_before_every_plot = true;
35 
36  #if UNITY_EDITOR
37  [Header("OnGizmo")]
40  [SerializeField]
41  bool _PlotRandomSeries = false;
42 
43  [SerializeField] bool always_random_sample_new = true;
44  #endif
45 
48  public AbstractPrototypingEnvironment ParentEnvironment {
49  get { return this._environment; }
50  set { this._environment = value; }
51  }
52 
56  public override string PrototypingTypeName { get { return "Displayer"; } }
57 
61  protected override void RegisterComponent() {
62  this.ParentEnvironment =
63  NeodroidUtilities.RegisterComponent(this.ParentEnvironment, this);
64  }
65 
69  protected override void UnRegisterComponent() { this.ParentEnvironment?.UnRegister(this); }
70 
73  void Update() {
74  if (this._RetainLastPlot) {
75  if (this.clean_before_every_plot) {
76  this.Clean();
77  }
78 
79  if (this._Values != null) {
80  PlotSeries(this._Values);
81  }
82  }
83  }
84 
85  #if UNITY_EDITOR
86  void OnDrawGizmosSelected() {
87  if (this.enabled && Selection.activeGameObject == this.gameObject) {
88  if (!this._PlotRandomSeries && !this._RetainLastPlot) {
89  this.Clean();
90  }
91 
92  if (this._Values == null || this._Values.Length == 0 || this.always_random_sample_new) {
93  if (this._PlotRandomSeries) {
94  this.Clean();
95  var vs = PlotFunctions.SampleRandomSeries(9);
96  this._Values = vs.Select(v => v._Val).ToArray();
97  this.PlotSeries(vs);
98  }
99  }
100  } else {
101  this.Clean();
102  }
103  }
104 
105  #endif
106 
110  protected virtual void Clean() {
111  if (this.clean_all_children) {
112  foreach (Transform child in this.transform) {
113  if (Application.isPlaying) {
114  Destroy(child.gameObject);
115  } else {
116  DestroyImmediate(child.gameObject);
117  }
118  }
119  }
120 
121  if (this._RetainLastPlot) {
122  this._Values = null;
123  }
124  }
125 
126  void OnDestroy() { this.Clean(); }
127 
128  void OnDisable() { this.Clean(); }
129 
130  void OnEnable() { this.Clean(); }
131 
132  //void OnValidate() { this.Clean(); }
133 
138  public abstract void Display(Single value);
143  public abstract void Display(Double value);
148  public abstract void Display(Single[] values);
153  public abstract void Display(String value);
158  public abstract void Display(Vector3 value);
163  public abstract void Display(Vector3[] value);
168  public abstract void Display(Points.ValuePoint point);
173  public abstract void Display(Points.ValuePoint[] points);
178  public abstract void Display(Points.StringPoint point);
183  public abstract void Display(Points.StringPoint[] points);
188  public abstract void PlotSeries(Points.ValuePoint[] points);
189  }
190 }