1 ///
2 module fluid.button;
3 
4 import fluid.node;
5 import fluid.frame;
6 import fluid.input;
7 import fluid.label;
8 import fluid.utils;
9 import fluid.style;
10 import fluid.backend;
11 
12 alias button = simpleConstructor!Button;
13 deprecated("Use vframeButton instead")
14 alias frameButton = simpleConstructor!FrameButton;
15 alias hframeButton = simpleConstructor!(FrameButton, (a) {
16     a.isHorizontal = true;
17 });
18 alias vframeButton = simpleConstructor!FrameButton;
19 alias Button = ButtonImpl!Label;
20 alias FrameButton = ButtonImpl!Frame;
21 
22 @safe:
23 
24 /// A button can be pressed by the user to trigger an action.
25 class ButtonImpl(T : Node = Label) : InputNode!T {
26 
27     mixin enableInputActions;
28 
29     /// Callback to run when the button is pressed.
30     alias pressed = submitted;
31 
32     // Button status
33     public {
34 
35         // If true, this button is currenly held down.
36         bool isPressed;
37 
38     }
39 
40     /// Create a new button.
41     /// Params:
42     ///     pressed = Action to perform when the button is pressed.
43     this(T...)(T sup, void delegate() @trusted pressed) {
44 
45         super(sup);
46         this.pressed = pressed;
47 
48     }
49 
50     protected override void drawImpl(Rectangle outer, Rectangle inner) {
51 
52         // Check if pressed
53         isPressed = checkIsPressed;
54         // TODO this should be *false* if key is held down, but wasn't pressed while in focus
55 
56         // Draw the button
57         super.drawImpl(outer, inner);
58 
59     }
60 
61     /// Handle mouse input. By default, this will call the `pressed` delegate if the button is pressed.
62     @(FluidInputAction.press)
63     protected void press() @trusted {
64 
65         // Run the callback
66         if (pressed) pressed();
67 
68     }
69 
70     static if (is(typeof(text) : string))
71     override string toString() const {
72 
73         import std.format;
74         return format!"button(%s)"(text);
75 
76     }
77 
78 }