1 ///
2 module fluid.hover_button;
3
4 import fluid.node;
5 import fluid.frame;
6 import fluid.input;
7 import fluid.label;
8 import fluid.style;
9 import fluid.utils;
10 import fluid.button;
11
12 @safe:
13 deprecated("`fluid.hover_button` is deprecated, because it is legacy code and has no known usecase. "
14 ~ "Please create your own `Button` node subclass and override `mouseImpl`. "
15 ~ "`hover_button` will be removed in Fluid 0.9.0."):
16
17
18 alias hoverButton = simpleConstructor!HoverButton;
19 alias frameHoverButton = simpleConstructor!FrameHoverButton;
20
21 alias HoverButton = HoverButtonImpl!Label;
22 alias FrameHoverButton = HoverButtonImpl!Frame;
23
24 /// An button that triggers every frame as long as the button is hovered. Useful for advanced buttons which react to
25 /// more than just left button click.
26 ///
27 /// Note, this is a somewhat low-level node and the hover event, as stated, triggers every frame. There are no hover
28 /// entry nor hover leave events. Make sure you know what you're doing when using this node!
29 class HoverButtonImpl(T : Node = Label) : ButtonImpl!T {
30
31 mixin enableInputActions;
32
33 /// Create a new hover button.
34 /// Params:
35 /// pressed = Action to perform when the button is hovered.
36 this(T...)(T sup) {
37
38 super(sup);
39
40 }
41
42 // Disable action on `press`.
43 protected override void press() {
44
45 }
46
47 /// Check events
48 protected override void mouseImpl() {
49
50 // Simple enough
51 submitted();
52
53 }
54
55 protected override bool keyboardImpl() {
56
57 return false;
58
59 }
60
61 }
62
63 @("Legacy: HoverButton works (abandoned)")
64 unittest {
65
66 import fluid.backend;
67
68 int hoverFrameCount;
69
70 auto io = new HeadlessBackend;
71 auto root = hoverButton(.nullTheme, "Hello, World!", delegate { hoverFrameCount += 1; });
72
73 root.io = io;
74
75 // Move the mouse away from the button
76 io.mousePosition = io.windowSize;
77 root.draw();
78
79 assert(hoverFrameCount == 0);
80
81 // Hover the button now
82 io.nextFrame;
83 io.mousePosition = Vector2(0, 0);
84 root.draw();
85
86 assert(hoverFrameCount == 1);
87
88 // Press the button
89 io.nextFrame;
90 io.press(MouseButton.left);
91 root.draw();
92
93 assert(io.isDown(MouseButton.left));
94 assert(hoverFrameCount == 2);
95
96 // Wait while the button is pressed
97 io.nextFrame;
98 root.draw();
99
100 assert(io.isDown(MouseButton.left));
101 assert(hoverFrameCount == 3);
102
103 // Release the button
104 io.nextFrame;
105 io.release(MouseButton.left);
106 root.draw();
107
108 assert(io.isUp(MouseButton.left));
109 assert(hoverFrameCount == 4);
110
111 // Move the mouse elsewhere
112 io.nextFrame;
113 io.mousePosition = Vector2(-1, -1);
114 root.draw();
115
116 assert(hoverFrameCount == 4);
117
118 // Press the button outside
119 io.nextFrame;
120 io.press(MouseButton.left);
121 root.draw();
122
123 assert(hoverFrameCount == 4);
124
125
126 }