1 module armos.utils.gui.widgets.movinggraph;
2 
3 import armos.utils.gui.widgets.widget;
4 
5 /++
6 +/
7 class MovingGraph(T) : Widget{
8     import armos.graphics.mesh;
9     import std.conv;
10     public{
11         this(in string name, ref T var, in T min, in T max){
12             _name = name;
13             _var = &var;
14             _varMin = min;
15             _varMax= max;
16             _height = 128;
17             _lines = new Mesh();
18 
19             import armos.graphics.renderer:PrimitiveMode;
20 
21             _lines.primitiveMode = PrimitiveMode.LineStrip;
22             foreach (int i, v; _buffer) {
23                 v = _varMin;
24                 _lines.addVertex(i.to!float/_bufferSize.to!float, v, 0);
25                 _lines.addIndex(i);
26             }
27 
28             import armos.events;
29             import armos.app;
30             import rx;
31 
32             currentObservables.update.doSubscribe!(event => this.update(event));
33         }
34 
35         override void update(ref armos.events.UpdateEvent arg){
36             for (int i = 0; i < _bufferSize -1; i++) {
37                 _buffer[i] = _buffer[i+1];
38             }
39             _buffer[$-1] = *_var;
40         };
41 
42         override void draw(){
43             import armos.graphics.renderer:color,
44                                            drawRectangle;
45             color(_style.colors["background"]);
46             drawRectangle(0, 0, _style.width, _style.font.height*16);
47 
48             import std.format:format;
49             import std.conv;
50             string varString = "";
51             static if(__traits(isIntegral, *_var)){
52                 varString = format("%d", *_var).to!string;
53             }else if(__traits(isFloating, *_var)){
54                 varString = format("%f", *_var).to!string;
55             }
56             _style.font.material.uniform("diffuse", _style.colors["font1"]);
57             _style.font.draw(_name ~ " : " ~ varString, _style.font.width, 0);
58 
59             color(_style.colors["base1"]);
60             drawRectangle(_style.font.width, _style.font.height, _style.width - _style.font.width*2, _style.font.height*14);
61 
62             drawLine;
63         }
64     }//public
65 
66     private{
67         string _name;
68         T* _var;
69         T _varMin;
70         T _varMax;
71         enum int _bufferSize = 30;
72         T[_bufferSize] _buffer;
73         Mesh _lines;
74 
75         void drawLine(){
76             import std.conv;
77             import armos.graphics.renderer:color;
78             color(_style.colors["font1"]);
79             foreach (i, v; _buffer) {
80                 import armos.math:map;
81                 immutable x = map( i.to!float, 0f, 30f, _style.font.width.to!float, _style.width.to!float);
82                 immutable y = map( _varMax - v, _varMin, _varMax, _style.font.height.to!float, _style.font.height.to!float*15);
83                 _lines.vertices[i][0] = x;
84                 _lines.vertices[i][1] = y;
85                 // y = map.map( v, _varMin, _varMax, _style.font.width.to!float, _style.width.to!float - _style.font.width.to!float);
86             }
87             _lines.drawWireFrame;
88         }
89     }//private
90 }//class MovingGraph