MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
EventAwaiter.cpp
Go to the documentation of this file.
1#include "EventAwaiter.hpp"
2
3namespace MayaFlux::Kriya {
4
10
12{
13 if (auto ev = m_source.pop_event(m_filter)) {
14 m_result = *ev;
15 return true;
16 }
17 return false;
18}
19
20void WindowEventAwaiter::await_suspend(std::coroutine_handle<> handle)
21{
22 m_handle = handle;
23 m_is_suspended = true;
25}
26
32
33void WindowEventAwaiter::try_resume(const void* event)
34{
35 if (!filter_matches(event))
36 return;
37
38 if (auto ev = m_source.pop_event(m_filter)) {
39 m_result = *ev;
41 m_is_suspended = false;
42 m_handle.resume();
43 }
44}
45
46bool WindowEventAwaiter::filter_matches(const void* event) const
47{
48 const auto* ev = static_cast<const Core::WindowEvent*>(event);
49
50 if (m_filter.event_type && ev->type != *m_filter.event_type)
51 return false;
52
53 if (m_filter.key_code) {
54 const auto* kd = std::get_if<Core::WindowEvent::KeyData>(&ev->data);
55 if (!kd || kd->key != static_cast<int16_t>(*m_filter.key_code))
56 return false;
57 }
58
59 if (m_filter.button) {
60 const auto* bd = std::get_if<Core::WindowEvent::MouseButtonData>(&ev->data);
61 if (!bd || bd->button != static_cast<int>(*m_filter.button))
62 return false;
63 }
64
65 return true;
66}
67
68} // namespace MayaFlux::Kriya
std::coroutine_handle m_handle
bool filter_matches(const void *event) const override
Casts event to Core::WindowEvent and checks against stored filter.
void await_suspend(std::coroutine_handle<> handle)
Registers with the source and suspends the coroutine.
Core::WindowEvent await_resume()
Returns the event that caused resumption.
bool await_ready()
Returns true if a matching event is already queued; stores it if so.
void try_resume(const void *event) override
Casts event to Core::WindowEvent, checks filter, resumes if matched.
Vruta::WindowEventFilter m_filter
Vruta::WindowEventSource & m_source
void register_waiter(Kriya::EventAwaiter *awaiter)
void unregister_waiter(Kriya::EventAwaiter *awaiter)
std::optional< Core::WindowEvent > pop_event(const WindowEventFilter &filter)
Removes and returns the first pending event matching the filter.
Event data for window and input events.
std::optional< Core::WindowEventType > event_type
std::optional< IO::MouseButtons > button