MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
WindowEventSource.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "EventSource.hpp"
4
7
8#include <queue>
9
10namespace MayaFlux::Kriya {
11class WindowEventAwaiter;
12}
13
14namespace MayaFlux::Vruta {
15
16// =============================================================================
17// EventFilter
18// =============================================================================
19
20/**
21 * @struct WindowEventFilter
22 * @brief Filter criteria for GLFW window input events.
23 *
24 * Multiple criteria are ANDed. An empty filter matches any event.
25 */
26struct MAYAFLUX_API WindowEventFilter : public EventFilter {
27 std::optional<Core::WindowEventType> event_type;
28 std::optional<IO::Keys> key_code;
29 std::optional<IO::MouseButtons> button;
30
31 WindowEventFilter() = default;
32
33 /**
34 * @brief Constructs a filter matching a specific event type.
35 * @param type Event type to match.
36 */
38 : event_type(type)
39 {
40 }
41
42 /**
43 * @brief Constructs a filter matching a specific key.
44 * @param key Key to match.
45 */
47 : key_code(key)
48 {
49 }
50};
51
52// =============================================================================
53// WindowEventSource
54// =============================================================================
55
56/**
57 * @class WindowEventSource
58 * @brief Awaitable stream of GLFW window input events.
59 *
60 * Signals are produced by GLFW callbacks on the main thread. Coroutines
61 * suspend via WindowEventAwaiter and are resumed synchronously from signal().
62 * Tracks input state for immediate polling via is_key_pressed(),
63 * is_mouse_pressed(), and get_mouse_position().
64 *
65 * @see EventSource
66 * @see WindowEventAwaiter
67 * @see EventFilter
68 */
69class MAYAFLUX_API WindowEventSource : public EventSource {
70public:
71 WindowEventSource() = default;
72 ~WindowEventSource() override = default;
73
76 WindowEventSource(WindowEventSource&&) noexcept = default;
77 WindowEventSource& operator=(WindowEventSource&&) noexcept = default;
78
79 /**
80 * @brief Enqueue a window event and resume matching waiters.
81 * @param event Event produced by the GLFW backend.
82 *
83 * Updates input state caches before dispatching.
84 */
85 void signal(Core::WindowEvent event);
86
87 /**
88 * @brief Creates an awaiter that resumes on the next event of any type.
89 */
90 Kriya::WindowEventAwaiter next_event();
91
92 /**
93 * @brief Creates an awaiter that resumes on the next event matching a type.
94 * @param type Event type to wait for.
95 */
96 Kriya::WindowEventAwaiter await_event(Core::WindowEventType type);
97
98 [[nodiscard]] bool has_pending() const override { return !m_pending_events.empty(); }
99 [[nodiscard]] size_t pending_count() const { return m_pending_events.size(); }
100 void clear() override { m_pending_events = {}; }
101
102 /**
103 * @brief Returns true if the given key is currently held.
104 * @param key Key to query.
105 */
106 [[nodiscard]] bool is_key_pressed(IO::Keys key) const;
107
108 /**
109 * @brief Returns true if the given mouse button is currently held.
110 * @param button Button index.
111 */
112 [[nodiscard]] bool is_mouse_pressed(int button) const;
113
114 /**
115 * @brief Returns the last known cursor position in screen coordinates.
116 */
117 [[nodiscard]] std::pair<double, double> get_mouse_position() const;
118
119private:
120 std::queue<Core::WindowEvent> m_pending_events;
121 std::unordered_map<int16_t, bool> m_key_states;
122 std::unordered_map<int, bool> m_button_states;
123 double m_mouse_x {};
124 double m_mouse_y {};
125
126 /**
127 * @brief Removes and returns the first pending event matching the filter.
128 * @param filter Filter criteria.
129 */
130 std::optional<Core::WindowEvent> pop_event(const WindowEventFilter& filter);
131
133};
134
135} // namespace MayaFlux::Vruta
Awaiter for suspending on GLFW window input events with optional filtering.
Base for event filters used by EventSources to match signals to awaiters.
Abstract base for all awaitable signal sources.
std::queue< Core::WindowEvent > m_pending_events
WindowEventSource & operator=(const WindowEventSource &)=delete
WindowEventSource(WindowEventSource &&) noexcept=default
std::unordered_map< int16_t, bool > m_key_states
void clear() override
Discards all buffered signals.
~WindowEventSource() override=default
WindowEventSource(const WindowEventSource &)=delete
std::unordered_map< int, bool > m_button_states
Awaitable stream of GLFW window input events.
WindowEventType
Types of window and input events.
std::optional< Core::WindowEventType > event_type
WindowEventFilter(Core::WindowEventType type)
Constructs a filter matching a specific event type.
WindowEventFilter(IO::Keys key)
Constructs a filter matching a specific key.
std::optional< IO::MouseButtons > button
Filter criteria for GLFW window input events.