Debian Codegraph @

ui/UISystem.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ui/UISystem.cpp $
21 * $Revision: 44 $
22 * $Date: 3/20/00 12:30p $
23 * $Author: Matt $
24 *
25 * UI system code.
26 *
27 * $Log: /DescentIII/Main/ui/UISystem.cpp $
28 *
29 * 44 3/20/00 12:30p Matt
30 * Merge of Duane's post-1.3 changes.
31 * Mac-only debugging change.
32 *
33 * 43 10/21/99 3:33p Kevin
34 * Mac merge!
35 *
36 * 42 7/28/99 2:03p Kevin
37 * Macintosh Changes
38 *
39 * 41 5/05/99 6:48p Samir
40 * hack to get pause key to properly register in UI system.
41 *
42 * 40 5/01/99 1:14a Samir
43 * fixed mouse sequencing probs with selections being canceled out by
44 * UISystem.
45 *
46 * 39 4/14/99 1:54a Jeff
47 * fixed case mismatched #includes
48 *
49 * 38 4/01/99 11:28a Samir
50 * never flush mouse during ui_Flush. This will screw up emulated mouse
51 * input and maybe mess up some hardware mouse support from the ddio
52 * library.
53 *
54 * 37 3/23/99 9:02p Samir
55 * added mouse flush for ui flush. also made keyboard flush too.
56 *
57 * 36 2/21/99 6:38p Samir
58 * mouse and key input better. buffered mouse.
59 *
60 * 35 1/28/99 6:49p Samir
61 * frame limit ui system.
62 *
63 * 34 1/25/99 11:02a Samir
64 * revamped mouse and key controls.
65 *
66 * 33 10/19/98 2:50p Samir
67 * added function to cleanup ui windows.
68 *
69 * 32 10/16/98 5:54p Samir
70 * to prevent infinite loops, error check.
71 *
72 * 31 10/16/98 5:53p Samir
73 * redid add and remove window mechanisms.
74 *
75 * 30 10/13/98 8:23p Samir
76 * added more keyboard input options.
77 *
78 * 29 10/13/98 3:47p Samir
79 * added function to check if a cursor is visible
80 *
81 * 28 10/06/98 7:31p Samir
82 * changed behavior of mouse b1_count calculation.
83 *
84 * 27 10/02/98 5:47p Samir
85 * changed behavior of mouse cursor.
86 *
87 * 26 9/30/98 4:32p Samir
88 * added frametime value.
89 *
90 * 25 9/27/98 3:52p Samir
91 * made mouse input delay 25 fps.
92 *
93 * 24 9/01/98 4:12p Samir
94 * added screenshot caparbilities.
95 *
96 * 23 8/31/98 4:08p Samir
97 * don't reset mouse position when flushing ui.
98 *
99 * 22 8/24/98 3:13p Samir
100 * fixed swimming of mouse.
101 *
102 * 21 6/30/98 6:13p Samir
103 * call ddio_MouseReset on ui_SetScreenMode.
104 *
105 * 20 6/29/98 6:44p Samir
106 * redid ui input polling.
107 *
108 * 19 6/05/98 5:35p Samir
109 * massive improvement in UI keyboard interface.
110 *
111 * 18 5/06/98 4:34p Samir
112 * added function to set ui screen resolution.
113 *
114 * 17 5/05/98 5:15p Samir
115 * ui system now takes a width and height too
116 *
117 * 16 4/10/98 7:51p Samir
118 * added ui_Flush
119 *
120 * 15 3/24/98 4:27p Samir
121 * To check key states in ui_Poll, calls ddio_GetAdjKeyValue.
122 *
123 * 14 3/18/98 6:25p Samir
124 * Don't turn Z-buffering back on.
125 *
126 * 13 3/10/98 12:50p Samir
127 * Made UI_screen_width & height accessable.
128 *
129 * 12 2/18/98 2:39p Samir
130 * fixed mouse cursor clipping.
131 *
132 * 11 2/17/98 2:22p Jason
133 * fixed some potential z sorting problems
134 *
135 * 10 2/13/98 6:36p Samir
136 * Added ability to prevent input polling and mouse input.
137 *
138 * 9 2/11/98 5:55p Samir
139 * Hide and Show cursor aren't cumulative now.
140 *
141 * 8 2/02/98 7:35p Samir
142 * Added show/hide cursor functions.
143 *
144 * 7 1/30/98 7:07p Samir
145 * Cleaned up Window handling code.
146 *
147 * 6 1/18/98 4:22p Samir
148 * Changed some UI initialization stuff.
149 *
150 * 5 1/16/98 1:27p Jeff
151 * Poll always at least once.
152 *
153 * 4 1/13/98 4:32p Samir
154 * Made tUIInput a shared structure to be used by ui_DoFrame to return UI
155 * input values.
156 *
157 * 3 1/08/98 12:17p Samir
158 * Added TitledWindow and modified the UI interface for polling.
159 *
160 * 2 1/02/98 12:48p Samir
161 * Better keyboard support and full mouse cursor support.
162 *
163 * 1 12/30/97 4:36p Samir
164 * Initial revision
165 *
166 * $NoKeywords: $
167 */
168
169 #include "UIlib.h"
170 #include "application.h"
171 #include "bitmap.h"
172 #include "ddvid.h"
173 #include "renderer.h"
174 #include "Macros.h"
175
176 #define UI_MOUSE_HOTX 2
177 #define UI_MOUSE_HOTY 2
178 #define UI_FRAMETIME 0.05
179 //////////////////////////////////////////////////////////////////////////////
180 // VARIABLES
181 struct tUIWindowNode {
182 UIWindow *wnd;
183 struct tUIWindowNode *next;
184 struct tUIWindowNode *prev;
185 };
186 // user input structure
187 tUIInput UI_input;
188 // user output structure
189 tUIOutput UI_output;
190 int UI_screen_width, UI_screen_height;
191 float UI_aspect_x = 1.0f;
192 float UI_aspect_y = 1.0f;
193 // application object
194 static bool UI_init = false;
195 static oeApplication *UI_app = NULL;
196 static int UI_cursor_bm = -1;
197 static tUIWindowNode *UIWindowList = NULL;
198 static tUIWindowNode *UIWindowTail = NULL;
199 static UIWindow *UIWindowFocus = NULL;
200 static int UI_cursor_show = 0;
201 float UIFrameTime = 0.0f;
202 bool ui_MousePoll(bool buttons);
203 bool ui_KeyPoll();
204 void ui_UpdateWindows();
205 //////////////////////////////////////////////////////////////////////////////
206 // FUNCTIONS
207 // call this function to initialize the UI system. pass a surface where all UI will occur
208 void ui_Init(oeApplication *app, tUIInitInfo *init_info) {
209 // initialize variables
210 if (UI_app) {
211 ui_Close();
212 }
213 // clear out input poller.
214 ui_Flush();
215 UI_cursor_bm = -1;
216 UI_cursor_show = 0;
217 UI_app = app;
218 UIWindowList = NULL;
219 UIWindowTail = NULL;
220 UITextItem::SetDefaultFont(init_info->window_font);
221 ui_SetScreenMode(init_info->w, init_info->h);
222 UI_init = true;
223 }
224 // frees ui input cache
225 void ui_Flush() {
226 // UI_input.mx = 0;
227 // UI_input.last_mx = 0;
228 // UI_input.my = 0;
229 // UI_input.last_my = 0;
230 UI_input.b1_status = 0;
231 UI_input.b1_last_status = 0;
232 UI_input.b1_count = 0;
233 UIFrameTime = 0.0f;
234 UI_input.cur_time = timer_GetTime();
235 ui_KeyFlush();
236 }
237 // flushes out key input info currently in ui.
238 void ui_KeyFlush() {
239 UI_input.key = 0;
240 UI_input.key_first_press = false;
241 UI_input.key_status = 0;
242 UI_input.last_key = 0;
243 UI_input.last_key_status = 0;
244 UI_input.printscreen = false;
245 ddio_KeyFlush();
246 }
247 // closes UI system. do this when setting a new surface.
248 void ui_Close() {
249 if (UI_cursor_bm > -1) {
250 bm_FreeBitmap(UI_cursor_bm);
251 UI_cursor_bm = -1;
252 }
253 // delete window node list.
254 ui_RemoveAllWindows();
255 UI_app = NULL;
256 UI_init = false;
257 }
258 // retrieves input for user interface
259 // mouse
260 // keyboard
261 // possibly joystick?
262 bool ui_MousePoll(bool buttons) {
263 int mx, my;
264 static int btn_mask = 0;
265 int msebtn;
266 bool state;
267 if (!buttons) {
268 // get all input, mouse maintains persistent button info. key doesn't.
269 btn_mask = ddio_MouseGetState(&mx, &my, NULL, NULL);
270 UI_input.last_mx = UI_input.mx;
271 UI_input.last_my = UI_input.my;
272 UI_input.mx = mx;
273 UI_input.my = my;
274 } else if (UI_cursor_show) {
275 // if bX_count is 0, then repeat processing can occur, otherwise only real mouse events are processed.
276 if (ddio_MouseGetEvent(&msebtn, &state)) {
277 // mprintf(2, "mouse #%d state %d at %04d %04d\n", msebtn, UI_input.b1_status, UI_input.mx, UI_input.my);
278 if (msebtn == 0) {
279 UI_input.b1_last_status = UI_input.b1_status;
280 UI_input.b1_status = state ? UIMSEBTN_PRESSED : UIMSEBTN_RELEASED;
281 UI_input.b1_count = 1;
282 // mprintf(0, "M");
283 return true;
284 } else {
285 UI_input.b1_status = 0;
286 }
287 } else {
288 if (UI_input.b1_count == 0) {
289 // this frame, there was no down or up event, so now we can check if we should
290 // report button 1's state.
291 UI_input.b1_last_status = UI_input.b1_status;
292 UI_input.b1_count = 1;
293 if (CHECK_FLAG(btn_mask, MOUSE_LB)) {
294 // process repeat key events.
295 UI_input.b1_status = UIMSEBTN_PRESSED;
296 // mprintf(0, "m");
297 return true;
298 }
299 UI_input.b1_status = 0;
300 }
301 }
302 }
303 return false;
304 }
305 bool ui_KeyPoll() {
306 int key;
307 key = ddio_KeyInKey();
308 if (key == KEY_PRINT_SCREEN)
309 UI_input.printscreen = true;
310 UI_input.last_key = UI_input.key;
311 UI_input.last_key_status = UI_input.key_status;
312 // messy hack for pause key
313 if ((key & 0x00ff) == KEY_PAUSE) {
314 key = key & 0xff;
315 }
316 UI_input.key = key;
317 if (UI_input.key) {
318 if (ddio_GetAdjKeyState(UI_input.key)) {
319 UI_input.key_status = UIKEY_PRESSED;
320 UI_input.key_first_press = true;
321 } else {
322 UI_input.key_status = UIKEY_CLICKED;
323 UI_input.key_first_press = true;
324 }
325 } else if (UI_input.last_key && UI_input.last_key_status == UIKEY_PRESSED) {
326 if (ddio_GetAdjKeyState(UI_input.last_key)) {
327 UI_input.key = UI_input.last_key;
328 UI_input.key_status = UIKEY_PRESSED;
329 UI_input.key_first_press = false;
330 } else {
331 UI_input.key_status = UIKEY_RELEASED;
332 UI_input.key = UI_input.last_key;
333 UI_input.key_first_press = false;
334 }
335 } else {
336 UI_input.key_status = 0;
337 UI_input.key_first_press = false;
338 }
339 // mprintf(0, "key=%d status=%d.\n", UI_input.key, UI_input.key_status);
340 return UI_input.key ? true : false;
341 }
342 // ability to load/use mouse cursors
343 void ui_UseCursor(const char *fname) {
344 if (UI_cursor_bm > -1)
345 bm_FreeBitmap(UI_cursor_bm);
346 UI_cursor_bm = bm_AllocLoadFileBitmap(fname, 0);
347 ASSERT(UI_cursor_bm > -1);
348 }
349 // adds a window to the ui list.
350 void ui_AddWindow(UIWindow *wnd) {
351 tUIWindowNode *newnode = new tUIWindowNode;
352
353 if (!UIWindowList) {
354 // first item to add to list.
355 UIWindowList = newnode;
356 newnode->prev = NULL;
357 } else {
358 // add to existing list.
359 tUIWindowNode *node = UIWindowList;
360 while (node->next) {
361 node = node->next;
362 }
363 node->next = newnode;
364 newnode->prev = node;
365 }
366 newnode->next = NULL;
367 newnode->wnd = wnd;
368 UIWindowTail = newnode;
369 }
370 // removes a window from ui list.if topmost, the next topmost window has focus.
371 void ui_RemoveAllWindows() {
372 int count = 0;
373 while (UIWindowTail) {
374 ui_RemoveWindow(UIWindowTail->wnd);
375 count++;
376 ASSERT(count < 50);
377 }
378 if (count) {
379 mprintf(0, "%d UI windows left open. Closed them!\n", count);
380 }
381 }
382 // removes a window from ui list.
383 void ui_RemoveWindow(UIWindow *wnd) {
384 tUIWindowNode *node = UIWindowList;
385 bool found_wnd = false;
386 while (node) {
387 if (node->wnd == wnd) {
388 // we found a node.
389 if (node->prev) {
390 node->prev->next = node->next;
391 }
392 if (node->next) {
393 node->next->prev = node->prev;
394 }
395 if (node == UIWindowList) {
396 UIWindowList = NULL;
397 UIWindowList = node->next;
398 }
399 if (node == UIWindowTail) {
400 UIWindowTail = NULL;
401 UIWindowTail = node->prev;
402 }
403 delete node;
404 found_wnd = true;
405 break;
406 }
407 node = node->next;
408 }
409 ASSERT(found_wnd);
410 }
411 // Procedure to find which window should gain focus in the
412 // first check what window contains mouse pointer and save that window
413 // check if keystroke,
414 // then adjust focus and save as current focus
415 // else
416 // set current focus = saved mouse focus.
417 void ui_DoWindowFocus() {
418 // right now, the topmost window always has focus!
419 tUIWindowNode *wndnode = UIWindowTail;
420 if (wndnode)
421 UIWindowFocus = wndnode->wnd;
422 else
423 UIWindowFocus = NULL;
424 }
425 // returns a result and processes input of a window in focus
426 int ui_ProcessFocusedWindow() {
427 int res = -1;
428 int res2;
429 // no input processing, just render frame
430 // process the window in focus.
431 // process at least once regardless of input (for user processes.)
432 // get keys. get mouse while real events (non ui manuipulated) are there.
433 ui_MousePoll(false);
434 if (UIWindowFocus) {
435 UI_input.b1_count = 0; // button one state reset.
436 ui_KeyPoll();
437 ui_MousePoll(true);
438 do {
439 res2 = UIWindowFocus->Process();
440 if (res2 != -1)
441 res = res2;
442 } while (ui_MousePoll(true));
443 }
444 return res;
445 }
446 // updates all visible windows.
447 void ui_UpdateWindows() {
448 tUIWindowNode *wndnode = UIWindowList;
449 while (wndnode) {
450 if (wndnode->next == NULL) {
451 }
452 wndnode->wnd->Render();
453 wndnode = wndnode->next;
454 }
455 }
456 // do cursor display and update
457 void ui_DoCursor() {
458 ui_StartDraw(0, 0, UI_screen_width, UI_screen_height);
459
460 rend_SetOverlayType(OT_NONE);
461 rend_SetLighting(LS_NONE);
462 rend_SetColorModel(CM_MONO);
463 rend_SetZBufferState(0);
464 rend_SetAlphaType(AT_CONSTANT + AT_TEXTURE);
465 rend_SetAlphaValue(255);
466
467 // This function needs to get called do if there are no windows, stuff gets drawn using
468 // the ui_StartDraw and ui_EndDraw
469 if (UI_cursor_show && UI_cursor_bm > -1) {
470 ui_MousePoll(false); // DAJ
471
472 float u0 = 0.0f, v0 = 0.0f, u1 = 1.0f, v1 = 1.0f;
473 int cur_w = bm_w(UI_cursor_bm, 0);
474 int cur_h = bm_h(UI_cursor_bm, 0);
475 if (UI_input.mx > (UI_screen_width - cur_w))
476 u1 = ((float)(UI_screen_width - UI_input.mx)) / ((float)cur_w);
477 if (UI_input.my > (UI_screen_height - cur_h))
478 v1 = ((float)(UI_screen_height - UI_input.my)) / ((float)cur_h);
479 if (UI_input.mx < UI_MOUSE_HOTX)
480 u0 = (float)(UI_MOUSE_HOTX - UI_input.mx) / (float)cur_w;
481 if (UI_input.my < UI_MOUSE_HOTY)
482 v0 = (float)(UI_MOUSE_HOTY - UI_input.my) / (float)cur_h;
483 // mprintf_at(1,5,30,"mx=%d my=%d ", UI_input.mx,UI_input.my);
484 rend_DrawScaledBitmap(UI_input.mx - UI_MOUSE_HOTX, UI_input.my - UI_MOUSE_HOTY,
485 UI_input.mx + (int)((float)cur_w * u1) - UI_MOUSE_HOTX,
486 UI_input.my + (int)((float)cur_h * v1) - UI_MOUSE_HOTY, UI_cursor_bm, u0, v0, u1, v1);
487 }
488 ui_EndDraw();
489 }
490 // ui_DoFrame
491 // polls input
492 // does a ui frame given a list of windows
493 int ui_DoFrame(bool input) {
494 int res = -1;
495 if (!UI_init)
496 return res;
497
498 // reset this flag here, so that print screen is valid only until next call to ui_DoFrame
499 if (input)
500 UI_input.printscreen = false;
501 if (input) {
502 ui_DoWindowFocus(); // determine window with current input focus.
503 res = ui_ProcessFocusedWindow(); // process focused window
504
505 // int64_t cur_time = timer_GetMSTime();
506 while ((timer_GetTime() - UI_input.cur_time) < UI_FRAMETIME) {
507 };
508
509 float temp_time = timer_GetTime();
510 UIFrameTime = temp_time - UI_input.cur_time;
511 UI_input.cur_time = temp_time;
512 }
513 ui_UpdateWindows();
514 ui_DoCursor(); // update mouse cursor position
515
516 return res;
517 }
518 // does a ui frame and gets mouse and key information.
519 int ui_DoFrame(tUIInput *input, bool doinput) {
520 int res;
521 res = ui_DoFrame(doinput);
522 *input = UI_input;
523 return res;
524 }
525 // hide and show cursor. effects are cumulative
526 bool ui_ShowCursor() {
527 if (!UI_cursor_show) {
528 UI_cursor_show = 1;
529 return false;
530 }
531 return true;
532 }
533 bool ui_HideCursor() {
534 if (UI_cursor_show) {
535 UI_cursor_show = 0;
536 return false;
537 }
538 return true; // cursor was hidden before.
539 }
540 // set user interface screen resolution
541 void ui_SetScreenMode(int w, int h) {
542 int old_w = UI_screen_width;
543 int old_h = UI_screen_height;
544 UI_screen_width = w;
545 UI_screen_height = h;
546 UI_aspect_x = (float)w / (float)FIXED_SCREEN_WIDTH;
547 UI_aspect_y = (float)h / (float)FIXED_SCREEN_HEIGHT;
548 ddio_MouseReset();
549 // ddio_MouseSetLimits(0,0, UI_screen_width, UI_screen_height);
550 ddio_MouseSetVCoords(UI_screen_width, UI_screen_height);
551 UI_input.cur_time = timer_GetTime();
552 // reposition all active open windows to their correct locations in this new resolution
553 //@@ tUIWindowNode *wndnode = UIWindowList;
554 //@@ float scalar_x = -1.0f + (float)w/(float)old_w;
555 //@@ float scalar_y = -1.0f + (float)h/(float)old_h;
556 //@@
557 //@@ while (wndnode)
558 //@@ {
559 //@@ wndnode->wnd->Move(wndnode->wnd->X() + (int)(scalar_x*old_w), wndnode->wnd->Y() + (int)(scalar_y*old_h),
560 //@@ wndnode->wnd->W(), wndnode->wnd->H());
561 //@@ wndnode = wndnode->next;
562 //@@ }
563 }
564 bool ui_IsCursorVisible() { return UI_cursor_show ? true : false; }