MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
WindowEventSource.cpp
Go to the documentation of this file.
2
4
5namespace MayaFlux::Vruta {
6
8{
9 if (auto* kd = std::get_if<Core::WindowEvent::KeyData>(&event.data)) {
11 m_key_states[kd->key] = true;
12 } else if (event.type == Core::WindowEventType::KEY_RELEASED) {
13 m_key_states[kd->key] = false;
14 }
15 }
16
17 if (auto* bd = std::get_if<Core::WindowEvent::MouseButtonData>(&event.data)) {
19 m_button_states[bd->button] = true;
21 m_button_states[bd->button] = false;
22 }
23 }
24
25 if (auto* pd = std::get_if<Core::WindowEvent::MousePosData>(&event.data)) {
26 m_mouse_x = pd->x;
27 m_mouse_y = pd->y;
28 }
29
30 m_pending_events.push(event);
31 dispatch(&event);
32}
33
38
43
44std::optional<Core::WindowEvent> WindowEventSource::pop_event(const WindowEventFilter& filter)
45{
46 if (m_pending_events.empty())
47 return std::nullopt;
48
49 if (!filter.event_type && !filter.key_code && !filter.button) {
50 auto ev = m_pending_events.front();
51 m_pending_events.pop();
52 return ev;
53 }
54
55 std::queue<Core::WindowEvent> tmp;
56 std::optional<Core::WindowEvent> result;
57
58 while (!m_pending_events.empty()) {
59 auto ev = m_pending_events.front();
60 m_pending_events.pop();
61
62 bool matches = true;
63
64 if (filter.event_type && ev.type != *filter.event_type)
65 matches = false;
66
67 if (matches && filter.key_code) {
68 if (auto* kd = std::get_if<Core::WindowEvent::KeyData>(&ev.data)) {
69 matches = kd->key == static_cast<int16_t>(*filter.key_code);
70 } else {
71 matches = false;
72 }
73 }
74
75 if (matches && filter.button) {
76 if (auto* bd = std::get_if<Core::WindowEvent::MouseButtonData>(&ev.data)) {
77 matches = bd->button == static_cast<int>(*filter.button);
78 } else {
79 matches = false;
80 }
81 }
82
83 if (matches && !result) {
84 result = ev;
85 } else {
86 tmp.push(ev);
87 }
88 }
89
90 m_pending_events = std::move(tmp);
91 return result;
92}
93
95{
96 auto it = m_key_states.find(static_cast<int16_t>(key));
97 return it != m_key_states.end() && it->second;
98}
99
101{
102 auto it = m_button_states.find(button);
103 return it != m_button_states.end() && it->second;
104}
105
106std::pair<double, double> WindowEventSource::get_mouse_position() const
107{
108 return { m_mouse_x, m_mouse_y };
109}
110
111} // namespace MayaFlux::Vruta
Awaiter for suspending on GLFW window input events with optional filtering.
void dispatch(const void *event)
Iterates the waiter list, passing the type-erased signal to each.
bool is_key_pressed(IO::Keys key) const
Returns true if the given key is currently held.
bool is_mouse_pressed(int button) const
Returns true if the given mouse button is currently held.
std::queue< Core::WindowEvent > m_pending_events
Kriya::WindowEventAwaiter await_event(Core::WindowEventType type)
Creates an awaiter that resumes on the next event matching a type.
std::unordered_map< int16_t, bool > m_key_states
void signal(Core::WindowEvent event)
Enqueue a window event and resume matching waiters.
std::pair< double, double > get_mouse_position() const
Returns the last known cursor position in screen coordinates.
std::optional< Core::WindowEvent > pop_event(const WindowEventFilter &filter)
Removes and returns the first pending event matching the filter.
Kriya::WindowEventAwaiter next_event()
Creates an awaiter that resumes on the next event of any type.
std::unordered_map< int, bool > m_button_states
WindowEventType
Types of window and input events.
Event data for window and input events.
std::optional< Core::WindowEventType > event_type
std::optional< IO::MouseButtons > button
Filter criteria for GLFW window input events.