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 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] = Vector!(T, 2)(*_varX, *_varY); 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*18); 47 48 import std.format:format; 49 import std.conv; 50 string varStringX = ""; 51 string varStringY = ""; 52 static if(__traits(isIntegral, T)){ 53 varStringX = format("%d", *_varX).to!string; 54 varStringY = format("%d", *_varY).to!string; 55 }else if(__traits(isFloating, T)){ 56 varStringX = format("%f", *_varX).to!string; 57 varStringY = format("%f", *_varY).to!string; 58 } 59 _style.font.material.uniform("diffuse", _style.colors["font1"]); 60 _style.font.draw(_nameX ~ " : " ~ varStringX, _style.font.width, 0); 61 _style.font.draw(_nameY ~ " : " ~ varStringY, _style.font.width, _style.font.height); 62 63 color(_style.colors["base1"]); 64 drawRectangle(_style.font.width, _style.font.height*2, _style.width - _style.font.width*2, _style.font.height*15); 65 66 drawLine; 67 } 68 }//public 69 70 private{ 71 string _nameX; 72 string _nameY; 73 T* _varX; 74 T* _varY; 75 Vector!(T, 2) _varMin; 76 Vector!(T, 2) _varMax; 77 enum int _bufferSize = 30; 78 Vector!(T, 2)[_bufferSize] _buffer; 79 Mesh _lines; 80 81 void drawLine(){ 82 import std.conv; 83 import armos.graphics.renderer:color; 84 color(_style.colors["font1"]); 85 foreach (i, v; _buffer) { 86 import armos.math:map; 87 immutable x = map( v[0], _varMin[0], _varMax[0], _style.font.width.to!float, _style.width - _style.font.width); 88 immutable y = map( - v[1], _varMin[1], _varMax[1], _style.font.height.to!float*2, _style.font.height.to!float*17); 89 _lines.vertices[i].x = x; 90 _lines.vertices[i].y = y; 91 // y = map.map( v, _varMin, _varMax, _style.font.width.to!float, _style.width.to!float - _style.font.width.to!float); 92 } 93 _lines.drawWireFrame; 94 } 95 }//private 96 }//class MovingGraph