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