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