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