1 /**
2  * Button
3  */
4 module d2d.Button;
5 
6 import d2d;
7 
8 /**
9  * A predefined component that may be used as one would normally expect
10  * Ensures that the mouse was clicked and released over the button
11  * Button doesn't handle timing of press or anything like that
12  */
13 abstract class Button : Component {
14 
15     private bool _isClicked; ///An internally used flag to store whether the mouse has clicked (not released) over the button
16     iRectangle _location; ///Where the button is on the screen
17 
18     /**
19      * Gets whether the mouse button is held down over this button
20      */
21     @property bool isClicked() {
22         return this._isClicked;
23     }
24 
25     /**
26      * Gets whether the mouse is hovering over this button
27      */
28     @property bool isHovered() {
29         return this.location.contains(this.container.mouse.location);
30     }
31 
32     /**
33      * Gets where the button is on the screen
34      */
35     override @property iRectangle location() {
36         return _location;
37     }
38 
39     /**
40      * Sets where the button is on the screen
41      */
42     @property void location(iRectangle newLocation) {
43         this._location = newLocation;
44     }
45 
46     /**
47      * Makes a button given its location
48      */
49     this(Display container, iRectangle location) {
50         super(container);
51         this._location = location;
52     }
53 
54     /**
55      * How the button determines when it has been pressed
56      * Collects events and if the events signify the button has been pressed, calls the button's action
57      */
58     void handleEvent(SDL_Event event) {
59         if (!this.isHovered) {
60             this._isClicked = false;
61             return;
62         }
63         if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT)
64             this._isClicked = true;
65         else if (this._isClicked && event.type == SDL_MOUSEBUTTONUP
66                 && event.button.button == SDL_BUTTON_LEFT) {
67             this._isClicked = false;
68             this.action();
69         }
70     }
71 
72     void action(); //What the button should do when clicked
73 
74 }