Debian Codegraph @

common/ui/dialog.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Windowing functions and controller.
23 *
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29
30 #include "window.h"
31 #include "u_mem.h"
32 #include "maths.h"
33 #include "pstypes.h"
34 #include "gr.h"
35 #include "ui.h"
36 #include "key.h"
37 #include "mouse.h"
38 #include "timer.h"
39 #include "dxxerror.h"
40 #include <memory>
41
42 namespace dcx {
43 namespace {
44
45 static void ui_dialog_draw(UI_DIALOG *dlg)
46 {
47 int w, h;
48 int req_w, req_h;
49
50 w = dlg->d_width;
51 h = dlg->d_height;
52
53 req_w = w;
54 req_h = h;
55
56 ui_dialog_set_current_canvas(*dlg);
57
58 if (dlg->d_flags & DF_FILLED)
59 ui_draw_box_out(*grd_curcanv, 0, 0, req_w-1, req_h-1);
60
61 gr_set_fontcolor(*grd_curcanv, CBLACK, CWHITE);
62 }
63 }
64
65 // The dialog handler borrows heavily from the newmenu_handler
66 window_event_result UI_DIALOG::event_handler(const d_event &event)
67 {
68 if (event.type == event_type::window_activated ||
69 event.type == event_type::window_deactivated)
70 return window_event_result::ignored;
71 auto rval = callback_handler(event);
72 if (rval != window_event_result::ignored)
73 return rval; // event handled
74
75 switch (event.type)
76 {
77 case event_type::idle:
78 timer_delay2(50);
79 [[fallthrough]];
80 case event_type::mouse_button_down:
81 case event_type::mouse_button_up:
82 case event_type::mouse_moved:
83 case event_type::key_command:
84 case event_type::key_release:
85 return ui_dialog_do_gadgets(*this, event);
86 case event_type::window_draw:
87 {
88 ui_dialog_draw(this);
89 rval = ui_dialog_do_gadgets(*this, event);
90 if (rval != window_event_result::close)
91 this->send_event(d_event{event_type::ui_dialog_draw});
92 return rval;
93 }
94
95 default:
96 return window_event_result::ignored;
97 }
98 }
99
100 namespace {
101
102 static short adjust_starting_coordinate(short value, const int limit)
103 {
104 if (value < 0)
105 value = 0;
106 if (value - 1 >= limit)
107 value = limit;
108 return value;
109 }
110
111 }
112
113 UI_DIALOG::UI_DIALOG(short x, short y, const short w, const short h, const enum dialog_flags flags) :
114 window(grd_curscreen->sc_canvas, adjust_starting_coordinate(x, grd_curscreen->get_screen_width() - w), adjust_starting_coordinate(y, grd_curscreen->get_screen_height() - h), w, h),
115 d_width(w), d_height(h), d_flags(flags)
116 {
117 selected_gadget = NULL;
118
119 if (!(flags & DF_MODAL))
120 set_modal(0); // make this window modeless, allowing events to propogate through the window stack
121 }
122
123 void ui_dialog_set_current_canvas(UI_DIALOG &dlg)
124 {
125 gr_set_current_canvas(dlg.w_canv);
126 }
127
128 UI_DIALOG::~UI_DIALOG()
129 {
130 selected_gadget = NULL;
131 }
132
133 void ui_close_dialog(UI_DIALOG &dlg)
134 {
135 window_close(&dlg);
136 }
137
138 #if 0
139 void restore_state()
140 {
141 _disable();
142 range_for (const int i, xrange(256u))
143 {
144 keyd_pressed[i] = SavedState[i];
145 }
146 _enable();
147 }
148
149
150 void ui_mega_process()
151 {
152 unsigned char k;
153
154 event_process();
155
156 switch( Record )
157 {
158 case 0:
159 {
160 const auto [mx, my, mz] = mouse_get_delta();
161 Mouse.new_dx = mx;
162 Mouse.new_dy = my;
163 Mouse.new_buttons = mouse_get_btns();
164 last_keypress = key_inkey();
165
166 if ( Mouse.new_buttons || last_keypress || Mouse.new_dx || Mouse.new_dy ) {
167 last_event = timer_query();
168 }
169
170 break;
171 case 1:
172 {
173
174 if (ui_event_counter==0 )
175 {
176 EventBuffer[ui_event_counter].frame = 0;
177 EventBuffer[ui_event_counter].type = 7;
178 EventBuffer[ui_event_counter].data = ui_number_of_events;
179 ui_event_counter++;
180 }
181
182
183 if (ui_event_counter==1 && (RecordFlags & UI_RECORD_MOUSE) )
184 {
185 Mouse.new_buttons = 0;
186 EventBuffer[ui_event_counter].frame = FrameCount;
187 EventBuffer[ui_event_counter].type = 6;
188 EventBuffer[ui_event_counter].data = ((Mouse.y & 0xFFFF) << 16) | (Mouse.x & 0xFFFF);
189 ui_event_counter++;
190 }
191
192 const auto [mx, my, mz] = mouse_get_delta();
193 MouseDX = mx;
194 MouseDY = my;
195 MouseButtons = mouse_get_btns();
196
197 Mouse.new_dx = MouseDX;
198 Mouse.new_dy = MouseDY;
199
200 if ((MouseDX != 0 || MouseDY != 0) && (RecordFlags & UI_RECORD_MOUSE) )
201 {
202 if (ui_event_counter < ui_number_of_events-1 )
203 {
204 EventBuffer[ui_event_counter].frame = FrameCount;
205 EventBuffer[ui_event_counter].type = 1;
206 EventBuffer[ui_event_counter].data = ((MouseDY & 0xFFFF) << 16) | (MouseDX & 0xFFFF);
207 ui_event_counter++;
208 } else {
209 Record = 0;
210 }
211
212 }
213
214 if ( (MouseButtons != Mouse.new_buttons) && (RecordFlags & UI_RECORD_MOUSE) )
215 {
216 Mouse.new_buttons = MouseButtons;
217
218 if (ui_event_counter < ui_number_of_events-1 )
219 {
220 EventBuffer[ui_event_counter].frame = FrameCount;
221 EventBuffer[ui_event_counter].type = 2;
222 EventBuffer[ui_event_counter].data = MouseButtons;
223 ui_event_counter++;
224 } else {
225 Record = 0;
226 }
227
228 }
229
230
231 if ( keyd_last_pressed && (RecordFlags & UI_RECORD_KEYS) )
232 {
233 _disable();
234 k = keyd_last_pressed;
235 keyd_last_pressed= 0;
236 _enable();
237
238 if (ui_event_counter < ui_number_of_events-1 )
239 {
240 EventBuffer[ui_event_counter].frame = FrameCount;
241 EventBuffer[ui_event_counter].type = 3;
242 EventBuffer[ui_event_counter].data = k;
243 ui_event_counter++;
244 } else {
245 Record = 0;
246 }
247
248 }
249
250 if ( keyd_last_released && (RecordFlags & UI_RECORD_KEYS) )
251 {
252 _disable();
253 k = keyd_last_released;
254 keyd_last_released= 0;
255 _enable();
256
257 if (ui_event_counter < ui_number_of_events-1 )
258 {
259 EventBuffer[ui_event_counter].frame = FrameCount;
260 EventBuffer[ui_event_counter].type = 4;
261 EventBuffer[ui_event_counter].data = k;
262 ui_event_counter++;
263 } else {
264 Record = 0;
265 }
266 }
267
268 last_keypress = key_inkey();
269
270 if (last_keypress == KEY_F12 )
271 {
272 ui_number_of_events = ui_event_counter;
273 last_keypress = 0;
274 Record = 0;
275 break;
276 }
277
278 if ((last_keypress != 0) && (RecordFlags & UI_RECORD_KEYS) )
279 {
280 if (ui_event_counter < ui_number_of_events-1 )
281 {
282 EventBuffer[ui_event_counter].frame = FrameCount;
283 EventBuffer[ui_event_counter].type = 5;
284 EventBuffer[ui_event_counter].data = last_keypress;
285 ui_event_counter++;
286 } else {
287 Record = 0;
288 }
289 }
290 }
291
292 FrameCount++;
293
294 break;
295 case 2:
296 case 3:
297 Mouse.new_dx = 0;
298 Mouse.new_dy = 0;
299 Mouse.new_buttons = 0;
300 last_keypress = 0;
301
302 if ( keyd_last_pressed ) {
303 _disable();
304 k = keyd_last_pressed;
305 keyd_last_pressed = 0;
306 _disable();
307 SavedState[k] = 1;
308 }
309
310 if ( keyd_last_released )
311 {
312 _disable();
313 k = keyd_last_released;
314 keyd_last_released = 0;
315 _disable();
316 SavedState[k] = 0;
317 }
318
319 if (key_inkey() == KEY_F12 )
320 {
321 restore_state();
322 Record = 0;
323 break;
324 }
325
326 if (EventBuffer==NULL) {
327 restore_state();
328 Record = 0;
329 break;
330 }
331
332 while( (ui_event_counter < ui_number_of_events) && (EventBuffer[ui_event_counter].frame <= FrameCount) )
333 {
334 switch ( EventBuffer[ui_event_counter].type )
335 {
336 case 1: // Mouse moved
337 Mouse.new_dx = EventBuffer[ui_event_counter].data & 0xFFFF;
338 Mouse.new_dy = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
339 break;
340 case 2: // Mouse buttons changed
341 Mouse.new_buttons = EventBuffer[ui_event_counter].data;
342 break;
343 case 3: // Key moved down
344 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 1;
345 break;
346 case 4: // Key moved up
347 keyd_pressed[ EventBuffer[ui_event_counter].data ] = 0;
348 break;
349 case 5: // Key pressed
350 last_keypress = EventBuffer[ui_event_counter].data;
351 break;
352 case 6: // Initial Mouse X position
353 Mouse.x = EventBuffer[ui_event_counter].data & 0xFFFF;
354 Mouse.y = (EventBuffer[ui_event_counter].data >> 16) & 0xFFFF;
355 break;
356 case 7:
357 break;
358 }
359 ui_event_counter++;
360 if (ui_event_counter >= ui_number_of_events )
361 {
362 Record = 0;
363 restore_state();
364 //( 0, "Done playing %d events.\n", ui_number_of_events );
365 }
366 }
367
368 switch (Record)
369 {
370 case 2:
371 {
372 int next_frame;
373
374 if ( ui_event_counter < ui_number_of_events )
375 {
376 next_frame = EventBuffer[ui_event_counter].frame;
377
378 if ( (FrameCount+PlaybackSpeed) < next_frame )
379 FrameCount = next_frame - PlaybackSpeed;
380 else
381 FrameCount++;
382 } else {
383 FrameCount++;
384 }
385 }
386 break;
387
388 case 3:
389 if ( ui_event_counter < ui_number_of_events )
390 FrameCount = EventBuffer[ui_event_counter].frame;
391 else
392 FrameCount++;
393 break;
394 default:
395 FrameCount++;
396 }
397 }
398
399 ui_mouse_process();
400
401 }
402 #endif // 0
403
404 void (ui_dprintf_at)( UI_DIALOG * dlg, short x, short y, const char * format, ... )
405 {
406 char buffer[1000];
407 va_list args;
408
409 va_start(args, format );
410 vsnprintf(buffer,sizeof(buffer),format,args);
411 va_end(args);
412
413 ui_dputs_at(dlg, x, y, buffer);
414 }
415
416 void ui_dputs_at( UI_DIALOG * dlg, short x, short y, const char * buffer )
417 {
418 ui_dialog_set_current_canvas(*dlg);
419 gr_string(*grd_curcanv, *grd_curcanv->cv_font, x, y, buffer);
420 }
421
422 }