1 // Written in the D programming language.
2 /++
3  + Authors: KanzakiKino
4  + Copyright: KanzakiKino 2018
5  + License: LGPL-3.0
6 ++/
7 module w4d.widget.button;
8 import w4d.layout.placer.fill,
9        w4d.layout.gravity,
10        w4d.parser.colorset,
11        w4d.style.rect,
12        w4d.style.scalar,
13        w4d.task.window,
14        w4d.widget.panel,
15        w4d.widget.text,
16        w4d.event;
17 import g4d.ft.font,
18        g4d.glfw.cursor;
19 import gl3n.linalg;
20 
21 /// A handler that handles pressing buttons.
22 alias ButtonPressHandler = EventHandler!( void );
23 
24 /// A widget of button.
25 class ButtonWidget : PanelWidget
26 {
27     protected TextWidget _text;
28     /// TextWidget.
29     inout @property inout(TextWidget) text () { return _text; }
30 
31     ///
32     ButtonPressHandler onButtonPress;
33 
34     ///
35     override bool handleMouseButton ( MouseButton btn, bool status, vec2 pos )
36     {
37         if ( super.handleMouseButton( btn, status, pos ) ) {
38             return true;
39         }
40         if ( _status.disabled || !style.isPointInside(pos) ) {
41             return false;
42         }
43         if ( btn == MouseButton.Left && !status ) {
44             if ( !_status.locked ) handleButtonPress();
45             return true;
46         }
47         return false;
48     }
49 
50     ///
51     void handleButtonPress ()
52     {
53         onButtonPress.call();
54     }
55 
56     ///
57     override @property const(Cursor) cursor ()
58     {
59         return Cursor.Hand;
60     }
61 
62     ///
63     this ()
64     {
65         super();
66 
67         _text = new TextWidget;
68         _text.textOriginRate = vec2(0.5,0.5);
69         _text.textPosRate    = vec2(0.5,0.5);
70         super.addChild( _text );
71 
72         parseColorSetsFromFile!"colorset/button.yaml"( style );
73         style.box.margins     = Rect(1.mm);
74         style.box.paddings    = Rect(2.mm);
75         style.box.borderWidth = Rect(1.pixel);
76     }
77     mixin DisableModifyChildren;
78 
79     /// Changes the text.
80     void loadText ( dstring text, FontFace face )
81     {
82         _text.loadText( text, face );
83     }
84 
85     ///
86     override @property bool trackable () { return true; }
87     ///
88     override @property bool focusable () { return true; }
89 }