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             static import armos.events;
28             static import armos.app;
29             armos.events.addListener(armos.app.currentEvents.update, this, &this.update);
30         }
31 
32         override void update(ref armos.events.EventArg arg){
33             for (int i = 0; i < _bufferSize -1; i++) {
34                 _buffer[i] = _buffer[i+1];
35             }
36             _buffer[$-1] = *_var;
37         };
38 
39         override void draw(){
40             import armos.graphics.renderer:color,
41                                            drawRectangle;
42             color(_style.colors["background"]);
43             drawRectangle(0, 0, _style.width, _style.font.height*16);
44 
45             import std.format:format;
46             import std.conv;
47             string varString = "";
48             static if(__traits(isIntegral, *_var)){
49                 varString = format("%d", *_var).to!string;
50             }else if(__traits(isFloating, *_var)){
51                 varString = format("%f", *_var).to!string;
52             }
53             _style.font.material.uniform("diffuse", _style.colors["font1"]);
54             _style.font.draw(_name ~ " : " ~ varString, _style.font.width, 0);
55 
56             color(_style.colors["base1"]);
57             drawRectangle(_style.font.width, _style.font.height, _style.width - _style.font.width*2, _style.font.height*14);
58 
59             drawLine;
60         }
61     }//public
62 
63     private{
64         string _name;
65         T* _var;
66         T _varMin;
67         T _varMax;
68         enum int _bufferSize = 30;
69         T[_bufferSize] _buffer;
70         Mesh _lines;
71 
72         void drawLine(){
73             import std.conv;
74             import armos.graphics.renderer:color;
75             color(_style.colors["font1"]);
76             foreach (i, v; _buffer) {
77                 import armos.math:map;
78                 immutable x = map( i.to!float, 0f, 30f, _style.font.width.to!float, _style.width.to!float);
79                 immutable y = map( _varMax - v, _varMin, _varMax, _style.font.height.to!float, _style.font.height.to!float*15);
80                 _lines.vertices[i][0] = x;
81                 _lines.vertices[i][1] = y;
82                 // y = map.map( v, _varMin, _varMax, _style.font.width.to!float, _style.width.to!float - _style.font.width.to!float);
83             }
84             _lines.drawWireFrame;
85         }
86     }//private
87 }//class MovingGraph