MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
EventSource.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7#include <queue>
8
9namespace MayaFlux::Kriya {
10class EventAwaiter;
11}
12
13namespace MayaFlux::Vruta {
14
15/**
16 * @struct EventFilter
17 * @brief Filter criteria for window events
18 *
19 * Used to filter events in the pending queue. Multiple criteria can be
20 * combined to create specific filters (e.g., a specific key press).
21 */
22struct MAYAFLUX_API EventFilter {
23 std::optional<Core::WindowEventType> event_type;
24 std::optional<IO::Keys> key_code; // For KEY_PRESSED/RELEASED filtering
25 std::optional<IO::MouseButtons> button; // For MOUSE_BUTTON_* filtering
26
27 EventFilter() = default;
28
29 /**
30 * @brief Constructs filter for specific event type
31 * @param type The window event type to filter
32 */
34 : event_type(type)
35 {
36 }
37
38 /**
39 * @brief Constructs filter for specific key event
40 * @param key The key to filter for
41 */
43 : key_code(key)
44 {
45 }
46};
47
48/**
49 * @class EventSource
50 * @brief Awaitable event stream for window events
51 *
52 * Unlike clocks (SampleClock, FrameClock) which tick periodically,
53 * EventSource is signaled when discrete window events occur.
54 * Coroutines can suspend until events arrive.
55 */
56class MAYAFLUX_API EventSource {
57public:
58 EventSource() = default;
59 ~EventSource() = default;
60
61 EventSource(const EventSource&) = delete;
63 EventSource(EventSource&&) noexcept = default;
64 EventSource& operator=(EventSource&&) noexcept = default;
65
66 /**
67 * @brief Signals that an event occurred
68 * @param event The event data
69 *
70 * Called by GLFW callbacks. Queues event and resumes waiting coroutines.
71 */
72 void signal(Core::WindowEvent event);
73
74 /**
75 * @brief Creates awaiter for next event (any type)
76 */
77 Kriya::EventAwaiter next_event();
78
79 /**
80 * @brief Creates awaiter for specific event type
81 */
82 Kriya::EventAwaiter await_event(Core::WindowEventType type);
83
84 /**
85 * @brief Checks if events are pending
86 */
87 [[nodiscard]] bool has_pending() const { return !m_pending_events.empty(); }
88
89 /**
90 * @brief Gets number of pending events
91 */
92 [[nodiscard]] size_t pending_count() const { return m_pending_events.size(); }
93
94 /**
95 * @brief Clears all pending events
96 */
97 void clear() { m_pending_events = {}; }
98
99 /**
100 * @brief Query if a specific key is currently pressed
101 * @param key The key to check
102 * @return True if key is currently pressed
103 */
104 [[nodiscard]] bool is_key_pressed(IO::Keys key) const;
105
106 /**
107 * @brief Query if a specific mouse button is currently pressed
108 * @param button Mouse button to check
109 * @return True if button is currently pressed
110 */
111 [[nodiscard]] bool is_mouse_pressed(int button) const;
112
113 /**
114 * @brief Get current mouse position
115 * @return Pair of (x, y) coordinates
116 */
117 [[nodiscard]] std::pair<double, double> get_mouse_position() const;
118
119private:
120 std::queue<Core::WindowEvent> m_pending_events;
121 std::vector<Kriya::EventAwaiter*> m_waiters;
122
123 std::unordered_map<int16_t, bool> m_key_states;
124 std::unordered_map<int, bool> m_button_states;
125 double m_mouse_x {};
126 double m_mouse_y {};
127
128 /**
129 * @brief Pop event matching filter from queue
130 * @param filter Filter criteria
131 * @return Event if found, nullopt otherwise
132 *
133 * Searches the queue for an event matching all filter criteria.
134 * Removes and returns the first matching event, preserving order
135 * of non-matching events.
136 */
137 std::optional<Core::WindowEvent> pop_event(const EventFilter& filter);
138
139 void register_waiter(Kriya::EventAwaiter* awaiter);
140 void unregister_waiter(Kriya::EventAwaiter* awaiter);
141
143};
144
145}
Awaiter for suspending on window events with optional filtering.
void clear()
Clears all pending events.
size_t pending_count() const
Gets number of pending events.
EventSource(const EventSource &)=delete
std::unordered_map< int, bool > m_button_states
std::vector< Kriya::EventAwaiter * > m_waiters
std::queue< Core::WindowEvent > m_pending_events
EventSource & operator=(const EventSource &)=delete
EventSource(EventSource &&) noexcept=default
std::unordered_map< int16_t, bool > m_key_states
Awaitable event stream for window events.
WindowEventType
Types of window and input events.
std::optional< IO::Keys > key_code
EventFilter(IO::Keys key)
Constructs filter for specific key event.
EventFilter(Core::WindowEventType type)
Constructs filter for specific event type.
std::optional< IO::MouseButtons > button
std::optional< Core::WindowEventType > event_type
Filter criteria for window events.