1 module armos.utils.gui.widgets.togglebutton; 2 3 public import armos.utils.gui.widgets.widget; 4 5 /// 6 class ToggleButton : Widget{ 7 public{ 8 this(in string name, ref bool v){ 9 _v = &v; 10 _name = name; 11 12 static import armos.events; 13 static import armos.app; 14 armos.events.addListener(armos.app.currentEvents.mouseReleased, this, &this.mouseReleased); 15 armos.events.addListener(armos.app.currentEvents.mousePressed, this, &this.mousePressed); 16 17 _height = 32; 18 } 19 20 /++ 21 +/ 22 override void draw(){ 23 import armos.graphics:color, 24 drawRectangle; 25 color(_style.colors["background"]); 26 drawRectangle(0, 0, _style.width, _style.font.height*4); 27 28 _style.font.material.uniform("diffuse", _style.colors["font1"]); 29 _style.font.draw(_name, _style.font.width, 0); 30 31 if(*_v){ 32 color(_style.colors["base2"]); 33 }else{ 34 color(_style.colors["base1"]); 35 } 36 37 drawRectangle(_style.font.width, _style.font.height, _style.font.width*2, _style.font.height*2); 38 } 39 40 /++ 41 +/ 42 override void mousePressed(ref armos.events.MousePressedEventArg message){ 43 _isPressing = isOnMouse(message.x, message.y); 44 } 45 46 /++ 47 +/ 48 override void mouseReleased(ref armos.events.MouseReleasedEventArg message){ 49 if(_isPressing){ 50 *_v = !(*_v); 51 _isPressing = false; 52 } 53 } 54 }//public 55 56 private{ 57 bool* _v; 58 string _name; 59 bool _isPressing = false; 60 61 bool isOnMouse(in int x, in int y){ 62 immutable int localX = x-_position[0]; 63 immutable int localY = y-_position[1]; 64 if(_style.font.width<localX && localX<_style.font.width*3){ 65 if(_style.font.height<localY && localY<_style.font.height*3){ 66 return true; 67 } 68 } 69 return false; 70 } 71 }//private 72 }//class ToggleButton