TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_pointer_drag.h
Go to the documentation of this file.
1#pragma once
2
3#include <gui_core/gui_id.h>
8
9#include <stdint.h>
10
11namespace tachyon
12{
15
16 class ElementStorage;
17
21 constexpr bool IsDragTravel(int2 delta, int32_t distance) noexcept
22 {
23 const int64_t dx = delta.x, dy = delta.y;
24 return dx * dx + dy * dy >= int64_t(distance) * distance;
25 }
26
28 constexpr bool IsDragCancelKey(const InputState& s) noexcept
29 {
30 return s.lastKey == KeyCode::Escape && s.lastKeyAction == KeyAction::KeyDown;
31 }
32
41 {
42 ID id = ID::Invalid();
44 int2 pressScreen{ 0, 0 };
45 int2 pressLocal{ 0, 0 };
46 bool dragging = false;
47
49 bool IsHeld() const noexcept { return id != ID::Invalid(); }
51 bool IsPressed(ID element) const noexcept { return id != ID::Invalid() && id == element; }
53 bool IsDragging(ID element) const noexcept { return dragging && IsPressed(element); }
54
57 bool Press(const ElementEvent& e, const ElementStorage& storage,
58 MouseButtons button = MouseButtons::Left, bool atOnce = false);
59
63 bool Move(const ElementEvent& e, int32_t distance);
64
66 enum class Ended : uint8_t
67 {
68 No,
69 Click,
70 Drag
71 };
72
74
76 bool Cancel(ID element) noexcept;
78 void Reset() noexcept { *this = PointerDrag{}; }
79
81 int2 ScreenDelta(const InputState& s) const noexcept { return s.screenPos - pressScreen; }
83 int2 LocalDelta(const ElementEvent& e, const ElementStorage& storage) const;
84 };
85
86}
The frame's element tree.
Definition gui_element_storage.h:100
constexpr bool IsDragTravel(int2 delta, int32_t distance) noexcept
Whether the pointer moving delta pixels from a press makes it a drag: distance pixels or more in a st...
Definition gui_pointer_drag.h:21
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
MouseButtons
Mouse buttons, combined with |.
Definition gui_input_enums.h:38
@ Escape
Definition gui_input_enums.h:110
@ KeyDown
Definition gui_input_enums.h:51
@ None
Definition gui_input_enums.h:39
@ 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
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
Snapshot of input state for a single recorded OS event.
Definition gui_input_state.h:17
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
int2 pressScreen
pointer at the press, screen pixels
Definition gui_pointer_drag.h:44
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
MouseButtons button
the button held
Definition gui_pointer_drag.h:43
int2 ScreenDelta(const InputState &s) const noexcept
Pointer travel since the press, in screen pixels.
Definition gui_pointer_drag.h:81
void Reset() noexcept
Drops any press.
Definition gui_pointer_drag.h:78
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
int2 LocalDelta(const ElementEvent &e, const ElementStorage &storage) const
Pointer travel since the press, in the space of the element's rect (follows zoom).
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...
int2 pressLocal
pointer at the press, in the space of the element's rect
Definition gui_pointer_drag.h:45
bool dragging
moved the drag distance (or a drag from the press on)
Definition gui_pointer_drag.h:46
Integer 2D vector.
Definition gui_base_types.h:19