TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_gesture.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <stdint.h>
6#include <utility>
7
8namespace tachyon
9{
12
13 class GUIStateManager;
14
16 enum class GesturePhase : uint8_t
17 {
21 };
22
24 void TakeFocus(GUIStateManager& gsm, ID element);
25
28 {
30 bool atOnce = false;
31 bool focus = true;
33 bool capture = true;
34 };
35
37 struct NoSnapshot {};
38
39 namespace detail
40 {
42 void TakePress(GUIStateManager& gsm, ID pressed, const PressOptions& options);
44 void ReleaseCaptureOf(GUIStateManager& gsm, ID element);
45 }
46
58 template <class Snapshot = NoSnapshot>
59 struct Gesture
60 {
62 Snapshot before{};
63
64 bool IsHeld() const noexcept { return drag.IsHeld(); }
65 bool IsPressed(ID element) const noexcept { return drag.IsPressed(element); }
66 bool IsDragging(ID element) const noexcept { return drag.IsDragging(element); }
71
74 bool Press(const ElementEvent& e, const ElementStorage& storage, GUIStateManager& gsm,
75 Snapshot snapshot = {}, const PressOptions& options = {})
76 {
77 if (!drag.Press(e, storage, options.button, options.atOnce))
78 return false;
79 before = std::move(snapshot);
80 detail::TakePress(gsm, e.id, options);
81 return true;
82 }
83
85 bool Move(const ElementEvent& e, int32_t distance) { return drag.Move(e, distance); }
89 void Reset() { *this = Gesture{}; }
90
93 bool Cancel(const ElementEvent& e, GUIStateManager& gsm)
94 {
95 if (!IsDragCancelKey(e.state) || !drag.Cancel(e.id))
96 return false;
97 detail::ReleaseCaptureOf(gsm, e.id);
98 return true;
99 }
100
101 bool Cancel(const ElementEvent& e, GUIStateManager& gsm, Snapshot& value)
102 {
103 if (!Cancel(e, gsm))
104 return false;
105 value = before;
106 return true;
107 }
108 };
109
113 template <class Part>
115 {
116 Part part{};
117
120 template <class HitTest>
121 void Track(bool held, HitTest&& hitTest) { if (!held) part = hitTest(); }
123 Part Shown(bool elementHovered) const { return elementHovered ? part : Part{}; }
124 void Clear() { part = Part{}; }
125 };
126
127 namespace detail
128 {
129 inline bool HeldOf(const PointerDrag& d) noexcept { return d.IsHeld(); }
130 template <class S> bool HeldOf(const Gesture<S>& g) noexcept { return g.IsHeld(); }
131 }
132
134 template <class... Presses>
135 bool AnyHeld(const Presses&... presses) noexcept { return (detail::HeldOf(presses) || ...); }
136
138}
The frame's element tree.
Definition gui_element_storage.h:96
Owns the per-frame state objects and runs their BeginFrame / EndFrame in order.
Definition gui_state_manager.h:65
GesturePhase
Where a press is, nested in the element's hover: nothing held, held, or dragging.
Definition gui_gesture.h:17
bool AnyHeld(const Presses &... presses) noexcept
Whether any of a control's presses is held: its hover parts stay as they are then.
Definition gui_gesture.h:135
constexpr bool IsDragCancelKey(const InputState &s) noexcept
Escape going down: cancels the drag of the element that has the event.
Definition gui_pointer_drag.h:28
void TakeFocus(GUIStateManager &gsm, ID element)
A press focuses element: the keyboard goes to it, and a text editor that had it lets go.
MouseButtons
Mouse buttons, combined with |.
Definition gui_input_enums.h:38
@ Dragging
held and moved the drag distance (or a drag from the press on)
Definition gui_gesture.h:20
@ Pressed
held, not yet moved the drag distance: may still be a click
Definition gui_gesture.h:19
@ Idle
no press
Definition gui_gesture.h:18
@ Left
Definition gui_input_enums.h:40
Definition gui_logging.h:24
An input event delivered to an element's EventHandler.
Definition gui_input_handler_func.h:20
const InputState & state
the input snapshot of this event
Definition gui_input_handler_func.h:23
tachyon::ID id
the targeted element
Definition gui_input_handler_func.h:21
One press that may become a drag, with what every control does around it: the focus and the capture o...
Definition gui_gesture.h:60
bool Cancel(const ElementEvent &e, GUIStateManager &gsm)
Escape on the pressed element: drops the press and its capture.
Definition gui_gesture.h:93
bool IsHeld() const noexcept
Definition gui_gesture.h:64
tachyon::KeyModifier before
Definition gui_gesture.h:62
PointerDrag::Ended Release(const ElementEvent &e)
See PointerDrag::Release. before stays for the commit.
Definition gui_gesture.h:87
PointerDrag drag
Definition gui_gesture.h:61
bool IsDragging(ID element) const noexcept
Definition gui_gesture.h:66
GesturePhase Phase() const noexcept
Definition gui_gesture.h:67
void Reset()
Drops any press and what it kept (the control was disabled under it, say).
Definition gui_gesture.h:89
bool IsPressed(ID element) const noexcept
Definition gui_gesture.h:65
bool Cancel(const ElementEvent &e, GUIStateManager &gsm, Snapshot &value)
Escape on the pressed element: as Cancel, and value is put back as it was.
Definition gui_gesture.h:101
bool Press(const ElementEvent &e, const ElementStorage &storage, GUIStateManager &gsm, Snapshot snapshot={}, const PressOptions &options={})
Starts on e when it is the options' button going down: keeps snapshot, takes the focus and the captur...
Definition gui_gesture.h:74
bool Move(const ElementEvent &e, int32_t distance)
See PointerDrag::Move.
Definition gui_gesture.h:85
The part of a drawn control under the pointer (a key, a handle, a clip's end), for its hover look.
Definition gui_gesture.h:115
Part Shown(bool elementHovered) const
The part to draw hovered: none while the element isn't.
Definition gui_gesture.h:123
void Clear()
Definition gui_gesture.h:124
Part part
under the pointer at the last move with nothing held
Definition gui_gesture.h:116
void Track(bool held, HitTest &&hitTest)
On a move: unless a press is held, the part hitTest() finds under the pointer becomes the hovered one...
Definition gui_gesture.h:121
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
No value to keep: a press that changes nothing to put back.
Definition gui_gesture.h:37
One press on an element that may become a drag.
Definition gui_pointer_drag.h:41
Ended
How a press ended.
Definition gui_pointer_drag.h:67
bool Press(const ElementEvent &e, const ElementStorage &storage, MouseButtons button=MouseButtons::Left, bool atOnce=false)
Starts tracking e when it is button going down: true then.
bool IsDragging(ID element) const noexcept
A press of element is held and has become a drag.
Definition gui_pointer_drag.h:53
bool IsHeld() const noexcept
A press is held, on any element.
Definition gui_pointer_drag.h:49
bool Cancel(ID element) noexcept
Drops the press of element (Escape, a lost capture): true when there was one.
bool IsPressed(ID element) const noexcept
A press of element is held.
Definition gui_pointer_drag.h:51
Ended Release(const ElementEvent &e)
For the held button coming up on e.id: how the press ended. There is no press after.
bool Move(const ElementEvent &e, int32_t distance)
For a move of the held press: true when it is a drag move, the one that goes past distance pixels (se...
bool dragging
moved the drag distance (or a drag from the press on)
Definition gui_pointer_drag.h:46
What a press does besides being tracked.
Definition gui_gesture.h:28
bool focus
take the keyboard focus (and leave any text editor)
Definition gui_gesture.h:31
bool atOnce
a drag from the press on, never a click
Definition gui_gesture.h:30
bool capture
take the mouse capture, so the moves and release come here
Definition gui_gesture.h:33
ID focusID
the element to focus; Invalid: the one pressed
Definition gui_gesture.h:32
MouseButtons button
the button that starts it
Definition gui_gesture.h:29