UnityMol  0.9.6-875
UnityMol viewer / In developement
PlotManager.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 
6 public class PlotManager : MonoBehaviour {
7 
8  public int depth = 2;
9  public Texture2D grid;
10 
11  private Dictionary<String, Plotter> plots = new Dictionary<String, Plotter>();
12  private static PlotManager instance;
13 
17  public static PlotManager Instance
18  {
19  get
20  {
21  return instance;
22  }
23  }
24 
25  void Awake()
26  {
27  instance = this;
28  }
29 
30  void OnGUI()
31  {
32 
33  GUI.depth = depth;
34 
35  // If we have graphs then show them
36  if (plots.Count > 0)
37  {
38  foreach (KeyValuePair<String, Plotter> p in plots)
39  {
40  if (!p.Value.child)
41  {
42  p.Value.Draw();
43  }
44  }
45  }
46  }
47 
53  public void PlotAdd(String plotName, float value)
54  {
55  if (plots.ContainsKey(plotName)) plots[plotName].Add(value);
56  }
57 
64  public void PlotCreate(String plotName, float min, float max, Color plotColor, Vector2 pos)
65  {
66  if (!plots.ContainsKey(plotName))
67  {
68  plots.Add(plotName, new Plotter(plotName, grid, min, max, plotColor, pos));
69  }
70  }
71 
80  public void PlotCreate(String plotName, float min, float max, Color plotColor, String parentName)
81  {
82  if (!plots.ContainsKey(plotName) && plots.ContainsKey(parentName))
83  {
84  plots.Add(plotName, new Plotter(plotName, grid, min, max, plotColor, plots[parentName]));
85  }
86  }
87 
88  public void PlotCreate(String plotName, Color plotColor, String parentName)
89  {
90  if (!plots.ContainsKey(plotName) && plots.ContainsKey(parentName))
91  plots.Add(plotName, new Plotter(plotName, grid, plotColor, plots[parentName]));
92  }
93 
94  public void PlotCreate(String plotName, float min, float max, Color plotColor, ref Texture2D renderTextureReference)
95  {
96  if (!plots.ContainsKey(plotName))
97  {
98  plots.Add(plotName, new Plotter(plotName, grid, min, max, plotColor, ref renderTextureReference));
99  }
100  }
101 
102  public void PlotDestroy(String plotName)
103  {
104  if (plots.ContainsKey(plotName)) plots.Remove(plotName);
105  }
106 
110  public class Plotter
111  {
112 
113  public Boolean child = false;
114  private Plotter Parent;
115  private String name = "";
116  private Color plotColor = Color.green;
117 
118  private Texture2D grid;
119  private int gridWidth = 354;
120  private int gridHeight = 262;
121 
122  private float minValue;
123  private float maxValue;
124  private float scale;
125  private int floor;
126  private int top;
127 
128  private Color[] buffer;
129  private int[] data;
130  private int dataIndex = -1;
131  private bool dataFull = false;
132 
133  private int zeroLine = -1;
134 
135  private Dictionary<String, Plotter> children = new Dictionary<string, Plotter>();
136 
137  public Plotter(String name, Texture2D blankGraph, Color plotColor, Plotter parent)
138  {
139  InitPlotterChild(name, blankGraph, parent.minValue, parent.maxValue, plotColor, parent);
140  }
141 
142  public Plotter(String name, Texture2D blankGraph, float min, float max, Color plotColor, Plotter parent)
143  {
144  InitPlotterChild(name, blankGraph, min, max, plotColor, parent);
145  }
146 
147  public void InitPlotterChild(String name, Texture2D blankGraph, float min, float max, Color plotColor, Plotter parent)
148  {
149 
150  this.name = name;
151  this.plotColor = plotColor;
152 
153  minValue = min;
154  maxValue = max;
155  gridHeight = parent.grid.height;
156  gridWidth = parent.grid.width;
157 
158  data = new int[gridWidth];
159 
160  floor = 0;
161  top = gridHeight + Mathf.RoundToInt(gridHeight * 0.17f) + floor;
162 
163  scale = (max - min) / top;
164 
165  if (max > 0 && min < 0) zeroLine = (int)((-minValue) / scale) + floor;
166 
167  child = true;
168  this.Parent = parent;
169 
170  parent.AddChild(this);
171  }
172 
173  public Plotter(String name, Texture2D blankGraph, float min, float max, Color plotColor, Vector2 pos)
174  {
175 
176  this.name = name;
177  this.plotColor = plotColor;
178 
179  grid = new Texture2D(blankGraph.width, blankGraph.height);
181  gridWidth = grid.width;
182  gridHeight = grid.height;
183 
184 
185  buffer = blankGraph.GetPixels();
186  data = new int[gridWidth];
187 
188  floor = 0;
189  top = gridHeight + Mathf.RoundToInt(gridHeight * 0.17f) + floor;
190 
191  // Calculate verticle scale
192  minValue = min;
193  maxValue = max;
194  scale = (max - min) / top;
195 
196  if (max > 0 && min < 0) zeroLine = (int)((-minValue) / scale) + floor;
197 
198  }
199 
200  // Overload the plot constructor to use a texture passed as reference. This texture can then be used by any GUI system.
201  public Plotter(String name, Texture2D blankGraph, float min, float max, Color plotColor, ref Texture2D renderTextureReference)
202  {
203 
204  this.name = name;
205  this.plotColor = plotColor;
206 
207  grid = new Texture2D(blankGraph.width, blankGraph.height);
208  renderTextureReference = grid;
209  gridWidth = grid.width;
210  gridHeight = grid.height;
211 
212 
213  buffer = blankGraph.GetPixels();
214  data = new int[gridWidth];
215 
216  floor = 0;
217  top = gridHeight + Mathf.RoundToInt(gridHeight * 0.17f) + floor;
218 
219  // Calculate verticle scale
220  minValue = min;
221  maxValue = max;
222  scale = (max - min) / top;
223 
224  if (max > 0 && min < 0) zeroLine = (int)((-minValue) / scale) + floor;
225 
226  }
227 
232  public void Add(float y)
233  {
234 
235  int yPos = floor;
236 
237  // Move to next position in buffer
238  dataIndex++;
239  if (dataIndex == gridWidth) { dataIndex = 0; dataFull = true; }
240 
241  // Add value to buffer. If outside range, then set to min/max
242  if (y > maxValue) yPos = top;
243  else if (y < minValue) yPos = floor;
244  else yPos = (int)((y - minValue) / scale) + floor;
245  data[dataIndex] = yPos;
246  }
247 
248  void DrawLine(Texture2D tex, int x0, int y0, int x1, int y1, Color col)
249  {
250  int dy = (int)(y1-y0);
251  int dx = (int)(x1-x0);
252  int stepx, stepy;
253 
254  if (dy < 0) {dy = -dy; stepy = -1;}
255  else {stepy = 1;}
256  if (dx < 0) {dx = -dx; stepx = -1;}
257  else {stepx = 1;}
258  dy <<= 1;
259  dx <<= 1;
260 
261  float fraction = 0;
262 
263  tex.SetPixel(x0, y0, col);
264  if (dx > dy) {
265  fraction = dy - (dx >> 1);
266  while (Mathf.Abs(x0 - x1) > 1) {
267  if (fraction >= 0) {
268  y0 += stepy;
269  fraction -= dx;
270  }
271  x0 += stepx;
272  fraction += dy;
273  tex.SetPixel(x0, y0, col);
274  }
275  }
276  else {
277  fraction = dx - (dy >> 1);
278  while (Mathf.Abs(y0 - y1) > 1) {
279  if (fraction >= 0) {
280  x0 += stepx;
281  fraction -= dy;
282  }
283  y0 += stepy;
284  fraction += dx;
285  tex.SetPixel(x0, y0, col);
286  }
287  }
288  }
289 
290 
294  public void Draw()
295  {
296  grid.SetPixels(buffer);
297  int x = grid.width;
298  int previousX = x;
299  int previousY;
300  if (dataIndex == -1)
301  {
302  previousY = 0;
303  }
304  else
305  {
306  previousY = data[dataIndex];
307  }
308 
309  // Plot Data in buffer back from current position back to zero
310  for (int i = dataIndex-1; i > 0; i--)
311  {
312  // grid.SetPixel(x, data[i], plotColor);
313  DrawLine(grid, previousX, previousY, x, data[i], plotColor);
314  previousX = x;
315  previousY = data[i];
316  x--;
317  }
318 
319  // Plot data in buffer from last position down to current position
320  if (dataFull)
321  {
322  for (int i = gridWidth - 1; i >= dataIndex; i--)
323  {
324  //grid.SetPixel(x, data[i], plotColor);
325  DrawLine(grid, previousX, previousY, x, data[i], plotColor);
326  previousX = x;
327  previousY = data[i];
328  x--;
329  }
330  }
331 
332  // Draw a line at Zero
333  if (zeroLine > 0)
334  {
335  for (int i = 0; i < (gridWidth - 1); i++) grid.SetPixel(i, zeroLine, Color.yellow);
336  }
337 
338  // Update texture with pixels
339  grid.Apply(false);
340 
341  // Draw all children graphs
342  if (children.Count > 0)
343  {
344  foreach (KeyValuePair<String, Plotter> p in children)
345  {
346  p.Value.DrawChild();
347  }
348 
349  }
350 
351  }
352 
356  private void DrawChild()
357  {
358  int x = this.Parent.grid.width;
359  int previousX = x;
360  int previousY = data[dataIndex];
361  if (dataIndex == -1)
362  {
363  previousY = 0;
364  }
365  else
366  {
367  previousY = data[dataIndex];
368  }
369 
370  // Plot Data in buffer back from current position back to zero
371  for (int i = dataIndex-1; i > 0; i--)
372  {
373  // this.Parent.grid.SetPixel(x, data[i], plotColor);
374  DrawLine(this.Parent.grid, previousX, previousY, x, data[i], plotColor);
375  previousX = x;
376  previousY = data[i];
377  x--;
378  }
379 
380  // Plot data in buffer from last position down to current position
381  if (dataFull)
382  {
383  for (int i = gridWidth - 1; i >= dataIndex; i--)
384  {
385  // this.Parent.grid.SetPixel(x, data[i], plotColor);
386  DrawLine(this.Parent.grid, previousX, previousY, x, data[i], plotColor);
387  previousX = x;
388  previousY = data[i];
389  x--;
390  }
391  }
392 
393  // Update texture with pixels
394  this.Parent.grid.Apply(false);
395 
396  }
397 
402  public void AddChild(Plotter child)
403  {
404  if (!children.ContainsKey(child.name))
405  children.Add(child.name, child);
406  }
407 
408  }
409 }
static Texture2D totalEnergyPlotTexture
Plotter(String name, Texture2D blankGraph, float min, float max, Color plotColor, Plotter parent)
Definition: PlotManager.cs:142
Texture2D grid
Definition: PlotManager.cs:9
Plotter(String name, Texture2D blankGraph, float min, float max, Color plotColor, Vector2 pos)
Definition: PlotManager.cs:173
void Draw()
Draw the graph.
Definition: PlotManager.cs:294
void PlotDestroy(String plotName)
Definition: PlotManager.cs:102
void PlotAdd(String plotName, float value)
Add a value to plot graph
Definition: PlotManager.cs:53
void PlotCreate(String plotName, float min, float max, Color plotColor, Vector2 pos)
Instantiate a new new plot graph
Definition: PlotManager.cs:64
void AddChild(Plotter child)
Link a child Plotter to this
Definition: PlotManager.cs:402
static PlotManager Instance
Instance of object
Definition: PlotManager.cs:18
void Awake()
Definition: PlotManager.cs:25
void DrawChild()
Draw Child Graphs
Definition: PlotManager.cs:356
Dictionary< String, Plotter > children
Definition: PlotManager.cs:135
void PlotCreate(String plotName, float min, float max, Color plotColor, ref Texture2D renderTextureReference)
Definition: PlotManager.cs:94
Dictionary< String, Plotter > plots
Definition: PlotManager.cs:11
Plotter(String name, Texture2D blankGraph, float min, float max, Color plotColor, ref Texture2D renderTextureReference)
Definition: PlotManager.cs:201
void OnGUI()
Definition: PlotManager.cs:30
Plotter(String name, Texture2D blankGraph, Color plotColor, Plotter parent)
Definition: PlotManager.cs:137
void DrawLine(Texture2D tex, int x0, int y0, int x1, int y1, Color col)
Definition: PlotManager.cs:248
void PlotCreate(String plotName, float min, float max, Color plotColor, String parentName)
Create child plotter
Definition: PlotManager.cs:80
void Add(float y)
Add data to buffer
Definition: PlotManager.cs:232
Plotter class for generating graphs
Definition: PlotManager.cs:110
static PlotManager instance
Definition: PlotManager.cs:12
void PlotCreate(String plotName, Color plotColor, String parentName)
Definition: PlotManager.cs:88
void InitPlotterChild(String name, Texture2D blankGraph, float min, float max, Color plotColor, Plotter parent)
Definition: PlotManager.cs:147