TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_input_manager.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <string_view>
6#include <utility>
7#include <vector>
12
13namespace tachyon
14{
17
18 class ScrollManagement;
19 class ElementStorage;
20 class GUIStateManager;
21 class TabChainManager;
22 class ITextSystem;
23
32
41 {
42 public:
43 InputManager() = default;
44 ~InputManager() = default;
45
47 void SetTextSystem(ITextSystem* textSystem) { m_textSystem = textSystem; }
48
51 void OnKeyboard(tachyon::ID windowId, KeyCode key, int repeat, KeyModifier modifiers, KeyAction action) override;
52 void OnMouseMove(tachyon::ID windowId, tachyon::int2 screenPos, tachyon::MouseButtons buttons, KeyModifier modifiers) override;
53 void OnMouseButton(tachyon::ID windowId, tachyon::int2 screenPos, tachyon::MouseButtons buttons, KeyModifier modifiers, MouseAction action) override;
54 void OnMouseScroll(tachyon::ID windowId, tachyon::int2 screenPos, tachyon::float2 scrollOffset, KeyModifier modifiers) override;
55 void OnTextComposition(tachyon::ID windowId, std::string_view text, int32_t cursor) override;
56 void OnFileDrop(tachyon::ID windowId, tachyon::int2 screenPos, FileDropAction action, std::string_view data) override;
57 void OnPen(tachyon::ID windowId, const PenState& pen) override;
59 void OnPinch(tachyon::ID windowId, float scale, KeyModifier modifiers) override;
61
64 const PenState& GetPen() const { return m_pen; }
65
67 const FileDrag* GetFileDrag() const { return m_fileDrag.windowID != ID::Invalid() ? &m_fileDrag : nullptr; }
70 std::vector<FileDrop> TakeFileDrops() { return std::exchange(m_fileDrops, {}); }
71
75 {
76 if (h) m_globalHandlers.push_back(std::move(h));
77 }
78
81 {
82 if (m_globalHandlers.empty()) return false;
83 m_globalHandlers.pop_back();
84 return true;
85 }
86
88 void ConsumeKey();
89
91 const std::vector<RecordedInputEvent>& GetRecordedEvents() const { return m_recordedEvents; }
93 void ConsumeEvent(size_t index) { if (index < m_recordedEvents.size()) m_recordedEvents[index].consumed = true; }
94
96 MouseButtons GetCurrentButtons() const { return m_currentButtons; }
101
104 void UpdatePreHover(GUIStateManager& guiStateManager);
105
109 void SynthesizeMissingMouseEvents(int2 screenPos,MouseButtons lastPhysicalButtons, MouseButtons physicalButtons);
110
113 void Process(GUIStateManager& guiStateManager,
114 ElementStorage& elementStorage,
115 TabChainManager& tabChainManager,
116 ITextSystem* textSystem,
117 ID overID);
118
121 void Dispatch(GUIStateManager& guiStateManager, ElementStorage& storage);
122
123 private:
124 static bool IsMouseEvent(EventType type);
125 static bool IsKeyboardEvent(EventType type);
126
127 void DispatchSingleEvent(RecordedInputEvent& recorded, GUIStateManager& guiStateManager, ElementStorage& storage);
128
130 void RecordMouseButton(int2 screenPos, MouseButtons buttons, KeyModifier modifiers, MouseAction action);
131
133 struct HeldTextInput
134 {
135 uint32_t beforeEvent = 0;
136 ID windowId = ID::Invalid();
138 KeyModifier modifiers = KeyModifier::None;
140 int32_t cursor = -1;
141 std::string composition;
142 };
143
146 void HandOverHeldTextInput(size_t upTo, size_t& next, GUIStateManager& guiStateManager);
147
150 PenState PointerPen(MouseButtons buttons) const;
151
153 int2 m_prevScreenPos {};
154 MouseButtons m_currentButtons { MouseButtons::None };
155 ID m_prevFocusedID = ID::Invalid();
156 PenState m_pen;
157
158 ITextSystem* m_textSystem = nullptr;
159 bool m_holdTextInput = false;
160
161 std::vector<RecordedInputEvent> m_recordedEvents;
162 std::vector<HeldTextInput> m_heldTextInput;
163 std::vector<EventHandler> m_globalHandlers;
164
165 FileDrag m_fileDrag;
166 FileDrop m_dropping;
167 std::vector<FileDrop> m_fileDrops;
168 };
169
170}
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
Platform-level input interface: receives raw OS events (keyboard, mouse) and forwards them into the G...
Definition gui_platform_input_handler.h:25
Text and icon registration, measurement, rendering and editing.
Definition gui_text_system.h:189
bool IsDragAbandoned(MouseButtons button=MouseButtons::Left) const
Whether a drag held with button was left behind: the button is up and no release is queued for this f...
void Process(GUIStateManager &guiStateManager, ElementStorage &elementStorage, TabChainManager &tabChainManager, ITextSystem *textSystem, ID overID)
Per-frame input processing: tab navigation, text editor focus, dispatch to element handlers,...
void ConsumeEvent(size_t index)
Marks recorded event index as consumed, for something that acted on it while building.
Definition gui_input_manager.h:93
const FileDrag * GetFileDrag() const
The drag from another application over one of the app's windows now; null when there's none.
Definition gui_input_manager.h:67
void UpdatePreHover(GUIStateManager &guiStateManager)
Updates the drag position from the latest recorded mouse event; call before hover resolution and GUIS...
const std::vector< RecordedInputEvent > & GetRecordedEvents() const
This frame's recorded events, e.g. to read them before Process.
Definition gui_input_manager.h:91
void OnTextComposition(tachyon::ID windowId, std::string_view text, int32_t cursor) override
IPlatformInputHandler: record OS events for this frame's dispatch.
void Dispatch(GUIStateManager &guiStateManager, ElementStorage &storage)
Dispatches each recorded event to elements by capture / focus / hover rules, then clears the events.
std::vector< FileDrop > TakeFileDrops()
The drops from other applications since the last call, oldest first.
Definition gui_input_manager.h:70
void OnPen(tachyon::ID windowId, const PenState &pen) override
IPlatformInputHandler: record OS events for this frame's dispatch.
void SetTextSystem(ITextSystem *textSystem)
The text system the events are passed on to; null for none.
Definition gui_input_manager.h:47
void ConsumeKey()
Marks the most recently recorded keyboard event as consumed, so Dispatch skips it.
void SynthesizeMissingMouseEvents(int2 screenPos, MouseButtons lastPhysicalButtons, MouseButtons physicalButtons)
Detects mouse button presses the OS swallowed (e.g.
void OnMouseScroll(tachyon::ID windowId, tachyon::int2 screenPos, tachyon::float2 scrollOffset, KeyModifier modifiers) override
IPlatformInputHandler: record OS events for this frame's dispatch.
void OnKeyboard(tachyon::ID windowId, KeyCode key, int repeat, KeyModifier modifiers, KeyAction action) override
IPlatformInputHandler: record OS events for this frame's dispatch.
MouseButtons GetCurrentButtons() const
Mouse buttons held as of the last recorded button event.
Definition gui_input_manager.h:96
const PenState & GetPen() const
The pen as of the latest OnPen; a default PenState while the mouse has the pointer.
Definition gui_input_manager.h:64
void PushGlobalHandler(EventHandler h)
Pushes a global handler.
Definition gui_input_manager.h:74
void OnMouseMove(tachyon::ID windowId, tachyon::int2 screenPos, tachyon::MouseButtons buttons, KeyModifier modifiers) override
IPlatformInputHandler: record OS events for this frame's dispatch.
void OnFileDrop(tachyon::ID windowId, tachyon::int2 screenPos, FileDropAction action, std::string_view data) override
IPlatformInputHandler: record OS events for this frame's dispatch.
void OnPinch(tachyon::ID windowId, float scale, KeyModifier modifiers) override
Recorded as a MouseScroll with InputState::pinch, where the pointer is.
void OnMouseButton(tachyon::ID windowId, tachyon::int2 screenPos, tachyon::MouseButtons buttons, KeyModifier modifiers, MouseAction action) override
IPlatformInputHandler: record OS events for this frame's dispatch.
bool PopGlobalHandler()
Pops the last pushed global handler; false when there is none.
Definition gui_input_manager.h:80
Scroll containers' offsets (pixels), kept across frames by ID behind handles.
Definition gui_scroll_management.h:33
Tracks the next/prev tab targets around the focused element, rebuilt each frame during construction a...
Definition gui_tab_chain_manager.h:18
KeyCode
Keys by what the keyboard layout makes them, not by position.
Definition gui_input_enums.h:106
EventType
The kind of input event the GUI dispatches to element handlers.
Definition gui_input_enums.h:75
KeyAction
A keyboard event: a key down or up, or a typed character (KeyChar).
Definition gui_input_enums.h:50
std::function< bool(ElementEvent &&input)> EventHandler
Element-level input handler used by the immediate-mode dispatcher; returns true when it consumed the ...
Definition gui_input_handler_func.h:35
KeyModifier
Modifier keys held, combined with |.
Definition gui_input_enums.h:26
FileDropAction
A step of a drag from another application (files, text) over one of the app's windows.
Definition gui_input_enums.h:65
MouseButtons
Mouse buttons, combined with |.
Definition gui_input_enums.h:38
MouseAction
Whether a mouse button went down or up.
Definition gui_input_enums.h:58
@ None
Definition gui_input_enums.h:107
@ None
Definition gui_input_enums.h:76
@ FileDrop
Dropped on the element: state.droppedFiles, state.textInput.
Definition gui_input_enums.h:100
@ KeyDown
Definition gui_input_enums.h:51
@ None
Definition gui_input_enums.h:27
@ None
Definition gui_input_enums.h:39
@ Left
Definition gui_input_enums.h:40
Definition gui_logging.h:24
A drag from another application, of files or text, over one of the app's windows.
Definition gui_input_state.h:134
tachyon::ID windowID
Definition gui_input_state.h:135
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:75
The pen, as it was at a pointer event.
Definition gui_input_state.h:40
A single OS input event recorded for dispatch this frame.
Definition gui_input_manager.h:26
bool consumed
already handled; Dispatch skips it
Definition gui_input_manager.h:30
EventType type
Definition gui_input_manager.h:27
tachyon::ID windowId
OS window that received the event.
Definition gui_input_manager.h:29
InputState state
Definition gui_input_manager.h:28
Definition gui_base_types.h:120
Integer 2D vector.
Definition gui_base_types.h:18