1 module armos.utils.gui.widgets.movinggraphxy;
2 
3 import armos.utils.gui.widgets.widget;
4 import armos.math.vector;
5 import armos.graphics.mesh;
6 
7 ///
8 class MovingGraphXY(T) : Widget{
9     public{
10         this(in string nameX, ref T varX, in T minX, in T maxX, in string nameY, ref T varY, in T minY, in T maxY){
11             _nameX = nameX;
12             _nameY = nameY;
13             _varX = &varX;
14             _varY = &varY;
15             _varMin = Vector!(T, 2)(minX, minY);
16             _varMax = Vector!(T, 2)(maxX, maxY);
17 
18             _height = 128+16;
19             _lines = new Mesh();
20             import armos.graphics.renderer:PrimitiveMode;
21             _lines.primitiveMode = PrimitiveMode.LineStrip;
22             foreach (int i, v; _buffer) {
23                 v = _varMin;
24                 _lines.addVertex(0, 0, 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] = Vector!(T, 2)(*_varX, *_varY);
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*18);
44 
45             import std.format:format;
46             import std.conv;
47             string varStringX = "";
48             string varStringY = "";
49             static if(__traits(isIntegral, T)){
50                 varStringX = format("%d", *_varX).to!string;
51                 varStringY = format("%d", *_varY).to!string;
52             }else if(__traits(isFloating, T)){
53                 varStringX = format("%f", *_varX).to!string;
54                 varStringY = format("%f", *_varY).to!string;
55             }
56             _style.font.material.uniform("diffuse", _style.colors["font1"]);
57             _style.font.draw(_nameX ~ " : " ~ varStringX, _style.font.width, 0);
58             _style.font.draw(_nameY ~ " : " ~ varStringY, _style.font.width, _style.font.height);
59 
60             color(_style.colors["base1"]);
61             drawRectangle(_style.font.width, _style.font.height*2, _style.width - _style.font.width*2, _style.font.height*15);
62 
63             drawLine;
64         }
65     }//public
66 
67     private{
68         string _nameX;
69         string _nameY;
70         T* _varX;
71         T* _varY;
72         Vector!(T, 2) _varMin;
73         Vector!(T, 2) _varMax;
74         enum int _bufferSize = 30;
75         Vector!(T, 2)[_bufferSize] _buffer;
76         Mesh _lines;
77 
78         void drawLine(){
79             import std.conv;
80             import armos.graphics.renderer:color;
81             color(_style.colors["font1"]);
82             foreach (i, v; _buffer) {
83                 import armos.math:map;
84                 immutable x = map( v[0], _varMin[0], _varMax[0], _style.font.width.to!float, _style.width - _style.font.width);
85                 immutable y = map(  - v[1], _varMin[1], _varMax[1], _style.font.height.to!float*2, _style.font.height.to!float*17);
86                 _lines.vertices[i].x = x;
87                 _lines.vertices[i].y = y;
88                 // y = map.map( v, _varMin, _varMax, _style.font.width.to!float, _style.width.to!float - _style.font.width.to!float);
89             }
90             _lines.drawWireFrame;
91         }
92     }//private
93 }//class MovingGraph