1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 // Event header file
8
9 #pragma once
10
11 #include <SDL_version.h>
12 #include "fwd-event.h"
13 #include "maths.h"
14
15 #if SDL_MAJOR_VERSION == 2
16 #include <SDL_video.h>
17 #endif
18
19 #ifdef __cplusplus
20 namespace dcx {
21
22 enum event_type : unsigned
23 {
24 EVENT_IDLE = 0,
25 EVENT_QUIT,
26
27 #if DXX_MAX_BUTTONS_PER_JOYSTICK
28 EVENT_JOYSTICK_BUTTON_DOWN,
29 EVENT_JOYSTICK_BUTTON_UP,
30 #endif
31 #if DXX_MAX_AXES_PER_JOYSTICK
32 EVENT_JOYSTICK_MOVED,
33 #endif
34
35 EVENT_MOUSE_BUTTON_DOWN,
36 EVENT_MOUSE_BUTTON_UP,
37 EVENT_MOUSE_DOUBLE_CLICKED,
38 EVENT_MOUSE_MOVED,
39
40 EVENT_KEY_COMMAND,
41 EVENT_KEY_RELEASE,
42
43 EVENT_WINDOW_CREATED,
44 #if SDL_MAJOR_VERSION == 2
45 EVENT_WINDOW_RESIZE,
46 #endif
47 EVENT_WINDOW_ACTIVATED,
48 EVENT_WINDOW_DEACTIVATED,
49 EVENT_WINDOW_DRAW,
50 EVENT_WINDOW_CLOSE,
51
52 EVENT_NEWMENU_DRAW, // draw after the newmenu stuff is drawn (e.g. savegame previews)
53 EVENT_NEWMENU_CHANGED, // an item had its value/text changed
54 EVENT_NEWMENU_SELECTED, // user chose something - pressed enter/clicked on it
55
56 EVENT_LOOP_BEGIN_LOOP,
57
58 EVENT_UI_DIALOG_DRAW, // draw after the dialog stuff is drawn (e.g. spinning robots)
59 EVENT_UI_GADGET_PRESSED, // user 'pressed' a gadget
60 EVENT_UI_LISTBOX_MOVED,
61 EVENT_UI_LISTBOX_SELECTED,
62 EVENT_UI_USERBOX_DRAGGED
63 };
64
65 enum class window_event_result : uint8_t
66 {
67 // Window ignored event. Bubble up.
68 ignored,
69 // Window handled event.
70 handled,
71 close,
72 // Window handler already deleted window (most likely because it was subclassed), don't attempt to re-delete
73 deleted,
74 };
75
76 // A vanilla event. Cast to the correct type of event according to 'type'.
77 struct d_event
78 {
79 const event_type type;
d_eventd_event80 constexpr d_event(const event_type t) :
81 type(t)
82 {
83 }
84 };
85
86 struct d_create_event : d_event
87 {
d_create_eventd_create_event88 constexpr d_create_event() :
89 d_event{EVENT_WINDOW_CREATED}
90 {
91 }
92 };
93
94 struct d_change_event : d_event
95 {
96 const int citem;
d_change_eventd_change_event97 constexpr d_change_event(const int c) :
98 d_event{EVENT_NEWMENU_CHANGED}, citem(c)
99 {
100 }
101 };
102
103 struct d_select_event : d_event
104 {
105 int citem;
d_select_eventd_select_event106 d_select_event(const int c) :
107 d_event{EVENT_NEWMENU_SELECTED}, citem(c)
108 {
109 }
110 };
111
112 #if SDL_MAJOR_VERSION == 2
113 struct d_window_size_event : d_event
114 {
115 Sint32 width;
116 Sint32 height;
d_window_size_eventd_window_size_event117 d_window_size_event(const Sint32 w, const Sint32 h) :
118 d_event{EVENT_WINDOW_RESIZE}, width(w), height(h)
119 {
120 }
121 };
122 #endif
123
124 struct d_event_begin_loop : d_event
125 {
d_event_begin_loopd_event_begin_loop126 d_event_begin_loop() :
127 d_event{EVENT_LOOP_BEGIN_LOOP}
128 {
129 }
130 };
131
132 #if DXX_USE_EDITOR
133 fix event_get_idle_seconds();
134 #endif
135
136 // Process all events until the front window is deleted
137 // Won't work if there's the possibility of another window on top
138 // without its own event loop
event_process_all(void)139 static inline void event_process_all(void)
140 {
141 while (event_process() != window_event_result::deleted) {}
142 }
143
144 }
145 #endif
146