| 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/UIWindow.cpp $ |
| 21 | * $Revision: 39 $ |
| 22 | * $Date: 5/02/99 2:09a $ |
| 23 | * $Author: Samir $ |
| 24 | * |
| 25 | * Interface code. |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/ui/UIWindow.cpp $ |
| 28 | * |
| 29 | * 39 5/02/99 2:09a Samir |
| 30 | * don't process input on disabled gadgets. |
| 31 | * |
| 32 | * 38 4/26/99 9:11p Matt |
| 33 | * Took out saturation used by old newui. |
| 34 | * |
| 35 | * 37 4/22/99 4:28p Samir |
| 36 | * made gadget clip check include the right and bottom boundaries of the |
| 37 | * screen. |
| 38 | * |
| 39 | * 36 4/21/99 12:42p Samir |
| 40 | * clip gadgets on screen. |
| 41 | * |
| 42 | * 35 4/14/99 1:54a Jeff |
| 43 | * fixed case mismatched #includes |
| 44 | * |
| 45 | * 34 3/01/99 6:56p Samir |
| 46 | * saturate text for old ui menus (UIF_PROCESS_ALL) |
| 47 | * |
| 48 | * 32 3/01/99 5:00a Samir |
| 49 | * whenever m_GadgetCur is being NULLed, do a lose focus on it first. |
| 50 | * |
| 51 | * 31 2/26/99 2:10a Samir |
| 52 | * added accelerator table. |
| 53 | * |
| 54 | * 30 2/22/99 8:13p Samir |
| 55 | * added slave gadget system that only uses mouse input from window, but |
| 56 | * defers keys to its master. |
| 57 | * |
| 58 | * 29 2/21/99 6:36p Samir |
| 59 | * focusing changes and key input changes to ui., |
| 60 | * |
| 61 | * 28 2/17/99 8:32p Samir |
| 62 | * fixed bugs in remove gadget, added callbacks for attaching and |
| 63 | * detaching gadgets. |
| 64 | * |
| 65 | * 27 2/16/99 11:58a Samir |
| 66 | * small fixes and added gadget notification. |
| 67 | * |
| 68 | * 26 10/19/98 12:18p Samir |
| 69 | * made tabbing between edit controls possible. |
| 70 | * |
| 71 | * 25 10/13/98 8:23p Samir |
| 72 | * communication between gadget and window and edit system to work |
| 73 | * together. |
| 74 | * |
| 75 | * 24 10/06/98 7:31p Samir |
| 76 | * both tab keys and arrow keys scroll through gadgets. |
| 77 | * |
| 78 | * 23 9/10/98 10:20a Samir |
| 79 | * when removing a gadget from a window, check if it's being used by the |
| 80 | * user intefrace, if it is then remove it from that. |
| 81 | * |
| 82 | * 22 8/31/98 12:36p Samir |
| 83 | * implemented dialogversion of window. |
| 84 | * |
| 85 | * 21 8/27/98 2:52p Samir |
| 86 | * changing window system to work in two modes. |
| 87 | * |
| 88 | * 20 8/26/98 12:25p Samir |
| 89 | * fixed the whole editbox, keyboard access mess. |
| 90 | * |
| 91 | * 19 8/25/98 7:08p Samir |
| 92 | * select how keys are sent to a gadget. |
| 93 | * |
| 94 | * 18 8/25/98 6:33p Kevin |
| 95 | * PXO screens |
| 96 | * |
| 97 | * 17 8/25/98 5:27p Samir |
| 98 | * new focusing rules for keyboard. |
| 99 | * |
| 100 | * 16 6/05/98 5:35p Samir |
| 101 | * massive improvement in UI keyboard interface. |
| 102 | * |
| 103 | * 15 3/10/98 12:48p Samir |
| 104 | * Added window centering ability. |
| 105 | * |
| 106 | * 14 3/09/98 2:57p Samir |
| 107 | * Remove gadget from window called from gadget destroy. |
| 108 | * |
| 109 | * 13 3/05/98 6:38p Samir |
| 110 | * Fixed key presses within windows. |
| 111 | * |
| 112 | * 12 2/15/98 7:07p Samir |
| 113 | * Revamped way keys like up and down are handled in window system. |
| 114 | * |
| 115 | * 11 2/13/98 6:35p Samir |
| 116 | * Fixed some gadget creation/destruction inconsistancies |
| 117 | * |
| 118 | * 10 2/02/98 7:36p Samir |
| 119 | * Added UIObject::Destroy. |
| 120 | * |
| 121 | * 9 1/30/98 7:05p Samir |
| 122 | * Each gadget now has their own coordinate system. |
| 123 | * |
| 124 | * 8 1/26/98 10:54a Samir |
| 125 | * Fixed bug in UIWindow returning -2 always. |
| 126 | * |
| 127 | * 7 1/23/98 5:45p Samir |
| 128 | * Call OnUserProcess after default Window Process. |
| 129 | * |
| 130 | * 6 1/18/98 4:23p Samir |
| 131 | * Moved object destruction code to a virtual OnDestroy function for |
| 132 | * gadgets. Must destroy window too. |
| 133 | * |
| 134 | * 5 1/13/98 4:26p Samir |
| 135 | * Changed process of setting and getting focus. |
| 136 | * |
| 137 | * 4 1/08/98 12:17p Samir |
| 138 | * Added TitledWindow and modified the UI interface for polling. |
| 139 | * |
| 140 | * 3 1/05/98 11:10a Jason |
| 141 | * FROM SAMIR: Draw background rectangle for menus |
| 142 | * |
| 143 | * 2 1/02/98 12:48p Samir |
| 144 | * Redid setting and releasing keyboard/mouse focus for gadgets. |
| 145 | * |
| 146 | * 1 12/30/97 4:36p Samir |
| 147 | * Initial revision |
| 148 | * |
| 149 | * $NoKeywords: $ |
| 150 | */ |
| 151 | |
| 152 | #include "UIlib.h" |
| 153 | #include "Macros.h" |
| 154 | |
| 155 | // the window font for all windows |
| 156 | int UIWindow::m_WindowFont = 0; |
| 157 | |
| 158 | // ---------------------------------------------------------------------------- |
| 159 | // UIWindow class |
| 160 | |
| 161 | UIWindow::UIWindow() : UIObject() { |
| 162 | m_GadgetHead = NULL; |
| 163 | m_GadgetTail = NULL; |
| 164 | m_GadgetCur = NULL; |
| 165 | m_BackItem = NULL; |
| 166 | m_HoldHotkeys = false; |
| 167 | m_CurGadgetInGroup = false; |
| 168 | m_ResetCurGadget = false; |
| 169 | m_naccels = 0; |
| 170 | } |
| 171 | |
| 172 | UIWindow::~UIWindow() { |
| 173 | if (m_GadgetTail) { |
| 174 | Destroy(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // create a window at the specified coords. |
| 179 | void UIWindow::Create(int x, int y, int w, int h, int flags) { |
| 180 | m_FontHandle = m_WindowFont; |
| 181 | m_LockedGadget = NULL; |
| 182 | m_BackItem = UIPrimativeItem(TRANSPARENT_COLOR32).CopyUIItem(); |
| 183 | m_BackColor = GR_RGB(0, 0, 0); |
| 184 | m_Flags = flags; |
| 185 | m_GadgetTail = NULL; |
| 186 | m_GadgetHead = NULL; |
| 187 | m_LastKey = 0; |
| 188 | m_LastKeyTime = 0.0f; |
| 189 | m_HoldHotkeys = false; |
| 190 | m_ResetCurGadget = false; |
| 191 | m_CurGadgetInGroup = false; |
| 192 | m_naccels = 0; |
| 193 | |
| 194 | if (m_Flags & UIF_CENTER) { |
| 195 | x = UI_screen_width / 2 - w / 2; |
| 196 | y = UI_screen_height / 2 - h / 2; |
| 197 | } |
| 198 | |
| 199 | UIObject::Create(x, y, w, h); |
| 200 | } |
| 201 | |
| 202 | void UIWindow::Destroy() { |
| 203 | OnDestroy(); |
| 204 | |
| 205 | while (m_GadgetTail) { |
| 206 | UIGadget *gadget = m_GadgetTail; |
| 207 | gadget->Destroy(); // gadget must be destroyed AFTER it's removed from list. |
| 208 | } |
| 209 | m_GadgetTail = NULL; |
| 210 | m_GadgetHead = NULL; |
| 211 | |
| 212 | SetBackItem(NULL); |
| 213 | |
| 214 | UIObject::Destroy(); |
| 215 | } |
| 216 | |
| 217 | // interface functions |
| 218 | // adds a UIObject to the interface's draw list. |
| 219 | void UIWindow::AddGadget(UIGadget *gadget) { |
| 220 | ASSERT(IsCreated()); |
| 221 | ASSERT(gadget->IsCreated()); |
| 222 | |
| 223 | // perform a linked list add. |
| 224 | if (!m_GadgetHead) { |
| 225 | m_GadgetHead = gadget; |
| 226 | m_GadgetTail = m_GadgetHead; |
| 227 | } else { |
| 228 | m_GadgetTail->m_Next = gadget; |
| 229 | gadget->m_Prev = m_GadgetTail; |
| 230 | gadget->m_Next = NULL; |
| 231 | m_GadgetTail = gadget; |
| 232 | } |
| 233 | |
| 234 | if (m_GadgetCur) { |
| 235 | m_GadgetCur->LostFocus(); |
| 236 | m_GadgetCur = NULL; |
| 237 | } |
| 238 | m_ResetCurGadget = true; |
| 239 | m_CurGadgetInGroup = false; |
| 240 | |
| 241 | gadget->m_Wnd = this; |
| 242 | |
| 243 | gadget->OnAttachToWindow(); |
| 244 | } |
| 245 | |
| 246 | // removes a UIObject from the draw list. |
| 247 | void UIWindow::RemoveGadget(UIGadget *gadget) { |
| 248 | if (!gadget) |
| 249 | return; |
| 250 | |
| 251 | ASSERT(IsCreated()); |
| 252 | ASSERT(gadget->IsCreated()); |
| 253 | ASSERT(gadget->m_Wnd == this); |
| 254 | |
| 255 | gadget->OnDetachFromWindow(); |
| 256 | |
| 257 | // remove object from list. |
| 258 | if (gadget->m_Prev) |
| 259 | gadget->m_Prev->m_Next = gadget->m_Next; |
| 260 | if (gadget->m_Next) |
| 261 | gadget->m_Next->m_Prev = gadget->m_Prev; |
| 262 | |
| 263 | if (gadget == m_GadgetTail) |
| 264 | m_GadgetTail = gadget->m_Prev; |
| 265 | |
| 266 | if (gadget == m_GadgetHead) |
| 267 | m_GadgetHead = gadget->m_Next; |
| 268 | |
| 269 | if (gadget == m_GadgetCur) { |
| 270 | m_GadgetCur->LostFocus(); |
| 271 | m_GadgetCur = NULL; |
| 272 | m_ResetCurGadget = true; |
| 273 | m_CurGadgetInGroup = false; |
| 274 | } |
| 275 | |
| 276 | if (gadget == m_LockedGadget) |
| 277 | m_LockedGadget = NULL; |
| 278 | |
| 279 | // break link. |
| 280 | gadget->m_Prev = NULL; |
| 281 | gadget->m_Next = NULL; |
| 282 | gadget->m_Wnd = NULL; |
| 283 | } |
| 284 | |
| 285 | // forces a particular gadget into focus, always until unlocked. |
| 286 | void UIWindow::LockFocusOnGadget(UIGadget *gadget) { m_LockedGadget = gadget; } |
| 287 | |
| 288 | void UIWindow::UnlockFocusOnGadget() { m_LockedGadget = NULL; } |
| 289 | |
| 290 | // forces all keyinput onto one gadget. |
| 291 | void UIWindow::LockKeyFocusOnGadget(UIGadget *gadget) {} |
| 292 | |
| 293 | // Process |
| 294 | // run this to handle user input inside an interface |
| 295 | // sets focus on a control |
| 296 | // m_GadgetCur is gadget in focus. |
| 297 | |
| 298 | int UIWindow::Process() { |
| 299 | ASSERT(IsCreated()); |
| 300 | bool no_process = false; // don't do input for disabled gadgets. |
| 301 | |
| 302 | if (m_ResetCurGadget) { |
| 303 | // find first valid gadget to set focus to. |
| 304 | UIGadget *start_gadget, *gadget; |
| 305 | start_gadget = gadget = m_GadgetHead; |
| 306 | if (gadget) { |
| 307 | bool in_group = false; |
| 308 | do { |
| 309 | if (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_START)) { |
| 310 | in_group = true; |
| 311 | } |
| 312 | if (!gadget->IsDisabled() && !CHECK_FLAG(gadget->GetFlags(), UIF_SLAVE)) { |
| 313 | if (m_GadgetCur) { |
| 314 | m_GadgetCur->LostFocus(); |
| 315 | } |
| 316 | m_GadgetCur = gadget; |
| 317 | m_GadgetCur->GainFocus(); |
| 318 | m_CurGadgetInGroup = in_group; |
| 319 | break; |
| 320 | } |
| 321 | if (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_END)) { |
| 322 | in_group = false; |
| 323 | } |
| 324 | gadget = (gadget->m_Next) ? gadget->m_Next : m_GadgetHead; |
| 325 | } while (gadget != start_gadget); |
| 326 | } |
| 327 | |
| 328 | m_ResetCurGadget = false; |
| 329 | } |
| 330 | |
| 331 | // setup default output values |
| 332 | UI_output.gadget = NULL; |
| 333 | UI_output.id = -1; |
| 334 | |
| 335 | // we can do focus processing if no one gadget has locked the keyboard, mouse focus. |
| 336 | if (!m_LockedGadget) { |
| 337 | UIGadget *test_gadget = NULL; |
| 338 | if (UI_input.b1_status == UIMSEBTN_PRESSED || UI_input.b1_status == UIMSEBTN_CLICKED) { |
| 339 | // start from top most gadget down for mouse focus check. |
| 340 | bool in_group = false; |
| 341 | for (test_gadget = m_GadgetTail; test_gadget != NULL; test_gadget = test_gadget->m_Prev) { |
| 342 | if (CHECK_FLAG(test_gadget->GetFlags(), UIF_GROUP_END)) { // going from end of list to start, so reverse |
| 343 | in_group = true; |
| 344 | } |
| 345 | if (PT_IN_GADGET(this, test_gadget, UI_input.mx, UI_input.my)) { |
| 346 | if (!CHECK_FLAG(test_gadget->GetFlags(), UIF_SLAVE)) { |
| 347 | if (!test_gadget->IsDisabled()) { |
| 348 | break; |
| 349 | } else { |
| 350 | test_gadget = NULL; |
| 351 | no_process = true; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (CHECK_FLAG(test_gadget->GetFlags(), UIF_GROUP_START)) { |
| 358 | in_group = false; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // switch focus from old gadget to new. |
| 363 | if (test_gadget) { |
| 364 | if (m_GadgetCur && m_GadgetCur != test_gadget) { |
| 365 | m_GadgetCur->LostFocus(); |
| 366 | m_GadgetCur = NULL; |
| 367 | } |
| 368 | if (!m_GadgetCur) { |
| 369 | m_GadgetCur = test_gadget; |
| 370 | m_GadgetCur->GainFocus(); |
| 371 | m_CurGadgetInGroup = in_group; |
| 372 | } |
| 373 | test_gadget->CheckFocusOnSlaves(UI_input.mx, UI_input.my); |
| 374 | } |
| 375 | |
| 376 | } else { |
| 377 | // check for keyboard switching. |
| 378 | if (UI_input.key_status == UIKEY_PRESSED) { |
| 379 | if (TrapKey(UI_input.key)) { |
| 380 | OnKeyDown(UI_input.key); |
| 381 | } |
| 382 | } else if (UI_input.key_status == UIKEY_RELEASED) { |
| 383 | UntrapKey(UI_input.key); |
| 384 | OnKeyUp(UI_input.key); |
| 385 | } else if (UI_input.key_status == UIKEY_CLICKED) { |
| 386 | if (TrapKey(UI_input.key)) { |
| 387 | OnKeyDown(UI_input.key); |
| 388 | } |
| 389 | UntrapKey(UI_input.key); |
| 390 | OnKeyUp(UI_input.key); |
| 391 | } |
| 392 | } |
| 393 | } else { |
| 394 | // always assert that current gadget is locked gadget. |
| 395 | // if not, then m_CurGadgetInGroup can be wrong. |
| 396 | // ASSERT(m_GadgetCur == m_LockedGadget); |
| 397 | // m_GadgetCur = m_LockedGadget; |
| 398 | } |
| 399 | |
| 400 | // process input for current gadget in focus. |
| 401 | if (m_LockedGadget) { |
| 402 | m_LockedGadget->Process(true); |
| 403 | } else if (m_GadgetCur && !m_GadgetCur->IsDisabled()) { |
| 404 | // check what slave gadget a mouse event occurs inside m_GadgetCur |
| 405 | if (!no_process) { |
| 406 | if (m_GadgetCur->m_CurrentSlave) { |
| 407 | m_GadgetCur->m_CurrentSlave->Process(false, true); |
| 408 | } |
| 409 | m_GadgetCur->Process(true, m_GadgetCur->m_CurrentSlave ? false : true); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // perform user processing of window input. |
| 414 | OnUserProcess(); |
| 415 | |
| 416 | return UI_output.id; |
| 417 | } |
| 418 | |
| 419 | // overridable: runs after window is processed, to supplement it. |
| 420 | void UIWindow::OnUserProcess() {} |
| 421 | |
| 422 | // behavior when key is pressed. |
| 423 | void UIWindow::OnKeyDown(int key) { |
| 424 | UIGadget *gadget, *start_gadget; |
| 425 | bool in_group; |
| 426 | |
| 427 | // advance down gadget list. |
| 428 | if (key == KEY_TAB) { |
| 429 | if (!m_GadgetCur) { |
| 430 | m_ResetCurGadget = true; |
| 431 | return; |
| 432 | } |
| 433 | start_gadget = gadget = m_GadgetCur; |
| 434 | in_group = m_CurGadgetInGroup; |
| 435 | do { |
| 436 | if (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_START)) { |
| 437 | in_group = true; |
| 438 | } |
| 439 | if (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_END)) { |
| 440 | in_group = false; |
| 441 | } |
| 442 | gadget = (gadget->m_Next) ? gadget->m_Next : m_GadgetHead; |
| 443 | if (!gadget->IsDisabled() && !CHECK_FLAG(gadget->GetFlags(), UIF_SLAVE) && !in_group) { |
| 444 | start_gadget->LostFocus(); |
| 445 | gadget->GainFocus(); |
| 446 | m_CurGadgetInGroup = (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_START) > 0); |
| 447 | m_GadgetCur = gadget; |
| 448 | break; |
| 449 | } |
| 450 | } while (gadget != start_gadget); |
| 451 | } |
| 452 | if (key == (KEY_SHIFTED + KEY_TAB)) { |
| 453 | if (!m_GadgetCur) { |
| 454 | m_ResetCurGadget = true; |
| 455 | return; |
| 456 | } |
| 457 | start_gadget = gadget = m_GadgetCur; |
| 458 | in_group = m_CurGadgetInGroup; |
| 459 | do { |
| 460 | gadget = (gadget->m_Prev) ? gadget->m_Prev : m_GadgetTail; |
| 461 | if (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_END)) { |
| 462 | in_group = true; |
| 463 | } |
| 464 | if (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_START)) { |
| 465 | in_group = false; |
| 466 | } |
| 467 | if (!gadget->IsDisabled() && !CHECK_FLAG(gadget->GetFlags(), UIF_SLAVE) && !in_group) { |
| 468 | start_gadget->LostFocus(); |
| 469 | gadget->GainFocus(); |
| 470 | m_CurGadgetInGroup = |
| 471 | (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_END)) || (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_START)); |
| 472 | m_GadgetCur = gadget; |
| 473 | break; |
| 474 | } |
| 475 | } while (gadget != start_gadget); |
| 476 | } |
| 477 | |
| 478 | // handle direction keys only in a group |
| 479 | if (m_CurGadgetInGroup) { |
| 480 | |
| 481 | if (key == KEY_UP || key == KEY_LEFT) { |
| 482 | if (!m_GadgetCur) { |
| 483 | m_ResetCurGadget = true; |
| 484 | return; |
| 485 | } |
| 486 | start_gadget = gadget = m_GadgetCur; |
| 487 | do { |
| 488 | if (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_START)) { |
| 489 | break; |
| 490 | } |
| 491 | gadget = (gadget->m_Prev) ? gadget->m_Prev : m_GadgetTail; |
| 492 | if (!gadget->IsDisabled() && !CHECK_FLAG(gadget->GetFlags(), UIF_SLAVE)) { |
| 493 | start_gadget->LostFocus(); |
| 494 | gadget->GainFocus(); |
| 495 | m_GadgetCur = gadget; |
| 496 | break; |
| 497 | } |
| 498 | } while (start_gadget != gadget); |
| 499 | } |
| 500 | if (key == KEY_DOWN || key == KEY_RIGHT) { |
| 501 | if (!m_GadgetCur) { |
| 502 | m_ResetCurGadget = true; |
| 503 | return; |
| 504 | } |
| 505 | start_gadget = gadget = m_GadgetCur; |
| 506 | do { |
| 507 | if (CHECK_FLAG(gadget->GetFlags(), UIF_GROUP_END)) { |
| 508 | break; |
| 509 | } |
| 510 | gadget = (gadget->m_Next) ? gadget->m_Next : m_GadgetHead; |
| 511 | if (!gadget->IsDisabled() && !CHECK_FLAG(gadget->GetFlags(), UIF_SLAVE)) { |
| 512 | start_gadget->LostFocus(); |
| 513 | gadget->GainFocus(); |
| 514 | m_GadgetCur = gadget; |
| 515 | break; |
| 516 | } |
| 517 | } while (start_gadget != gadget); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | // determine if any hotkey was pressed |
| 522 | if (!m_HoldHotkeys) { |
| 523 | int i; |
| 524 | |
| 525 | for (gadget = m_GadgetHead; gadget != NULL; gadget = gadget->m_Next) { |
| 526 | if (!gadget->IsDisabled()) { |
| 527 | if (gadget->GetID() == UID_CANCEL && key == KEY_ESC) |
| 528 | gadget->OnSelect(); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // do hotkeys |
| 533 | for (gadget = m_GadgetHead; gadget != NULL; gadget = gadget->m_Next) { |
| 534 | if (!gadget->IsDisabled() && UI_input.key && ISKEYPRESSED(gadget->m_Hotkey)) |
| 535 | gadget->OnSelect(); |
| 536 | } |
| 537 | |
| 538 | // do window accelerators. |
| 539 | for (i = 0; i < m_naccels; i++) { |
| 540 | if (UI_input.key && ISKEYPRESSED(m_Accelerators[i].key) && UI_output.id < 0) { |
| 541 | UI_output.gadget = NULL; |
| 542 | UI_output.id = m_Accelerators[i].id; |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // behavior when key is released. |
| 550 | void UIWindow::OnKeyUp(int key) {} |
| 551 | |
| 552 | // redraw all gadgets |
| 553 | void UIWindow::Render() { |
| 554 | UIGadget *gadget = m_GadgetHead; |
| 555 | |
| 556 | // draw background of window. |
| 557 | ui_StartDraw(m_X, m_Y, m_X + m_W, m_Y + m_H); |
| 558 | ui_DrawSetFont(m_FontHandle); |
| 559 | |
| 560 | OnDraw(); |
| 561 | ui_EndDraw(); |
| 562 | |
| 563 | while (gadget) { |
| 564 | int l = m_X + gadget->X(); |
| 565 | int t = m_Y + gadget->Y(); |
| 566 | int r = l + gadget->W(); |
| 567 | int b = t + gadget->H(); |
| 568 | |
| 569 | // reformat gadget |
| 570 | gadget->OnFormat(); |
| 571 | |
| 572 | // draw gadget |
| 573 | if (l >= 0 && r <= UI_screen_width && t >= 0 && b <= UI_screen_height) { |
| 574 | ui_StartDraw(l, t, r, b); |
| 575 | ui_DrawSetFont(m_FontHandle); |
| 576 | gadget->OnDraw(); |
| 577 | ui_EndDraw(); |
| 578 | } else { |
| 579 | mprintf(0, "WINDOW: Gadget clipped on screen border (%d,%d,%d,%d)\n", l, t, r, b); |
| 580 | // Int3(); |
| 581 | } |
| 582 | |
| 583 | // proeceed... |
| 584 | gadget = gadget->m_Next; |
| 585 | } |
| 586 | |
| 587 | UITextItem::SetSaturationFactor(0); |
| 588 | |
| 589 | // draw ugly looking focus box around current gadget. |
| 590 | #if 0 |
| 591 | if (m_GadgetCur && !CHECK_FLAG(m_GadgetCur->m_Flags, UIF_SLAVE) ) { |
| 592 | int l = m_X+m_GadgetCur->X(); |
| 593 | int t = m_Y+m_GadgetCur->Y(); |
| 594 | int r = l+m_GadgetCur->W(); |
| 595 | int b = t+m_GadgetCur->H(); |
| 596 | |
| 597 | l--; t--; r++; b++; |
| 598 | if (l < 0) l = 0; |
| 599 | if (t < 0) t = 0; |
| 600 | if (r >(m_X+m_W)) r = m_X+m_W; |
| 601 | if (b >(m_Y+m_H)) b = m_Y+m_H; |
| 602 | ui_StartDraw(l,t,r,b); |
| 603 | ui_DrawBox(GR_RGB(128,128,128),0,0,(r-l),(b-t)); |
| 604 | ui_EndDraw(); |
| 605 | } |
| 606 | #endif |
| 607 | } |
| 608 | |
| 609 | // adds window to ui list. |
| 610 | void UIWindow::Open() { ui_AddWindow(this); } |
| 611 | |
| 612 | // removes window from ui list. |
| 613 | void UIWindow::Close() { ui_RemoveWindow(this); } |
| 614 | |
| 615 | // settings |
| 616 | void UIWindow::SetFont(int handle) { m_FontHandle = handle; } |
| 617 | |
| 618 | // overridable draws the window background before gadgets |
| 619 | void UIWindow::OnDraw() { |
| 620 | if (m_BackItem) |
| 621 | m_BackItem->draw(0, 0, m_W, m_H); |
| 622 | } |
| 623 | |
| 624 | // used to manage keypresses so repeating isn't quick. |
| 625 | bool UIWindow::TrapKey(int key) { |
| 626 | float time; |
| 627 | time = timer_GetTime(); |
| 628 | |
| 629 | // take care of multiple keypresses and key down repeats. handle timing |
| 630 | // so it feels 'natural' |
| 631 | if (m_LastKey && m_LastKey == key) { |
| 632 | if (m_LastKeyCount == 1) { |
| 633 | if ((time - m_LastKeyTime) < KEYDOWN_FIRST_DELAY) |
| 634 | return false; |
| 635 | m_LastKeyCount++; |
| 636 | } else if ((time - m_LastKeyTime) < KEYDOWN_REPEAT_DELAY) { |
| 637 | return false; |
| 638 | } |
| 639 | m_LastKeyCount++; |
| 640 | } else { |
| 641 | m_LastKeyCount = 1; |
| 642 | } |
| 643 | |
| 644 | m_LastKey = key; |
| 645 | m_LastKeyTime = time; |
| 646 | |
| 647 | return true; |
| 648 | } |
| 649 | |
| 650 | void UIWindow::UntrapKey(int key) { |
| 651 | // if the key previously pressed went up, then |
| 652 | // turn off key repeat management. |
| 653 | if (key == m_LastKey) { |
| 654 | m_LastKeyCount = 0; |
| 655 | m_LastKey = 0; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | void UIWindow::SetFocusOnGadget(UIGadget *gadget, bool key) { |
| 660 | if (m_GadgetCur) { |
| 661 | m_GadgetCur->LostFocus(); |
| 662 | } |
| 663 | m_GadgetCur = gadget; |
| 664 | if (m_GadgetCur) { |
| 665 | m_GadgetCur->GainFocus(); |
| 666 | m_ResetCurGadget = false; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // used to mark key pressed to id numbers |
| 671 | void UIWindow::AddAcceleratorKey(int key, int id) { |
| 672 | if (m_naccels == N_WINDOW_ACCELS) { |
| 673 | // window has asked for too many accelerators. will not add this one. it's okay to continue, but tell someone |
| 674 | Int3(); |
| 675 | return; |
| 676 | } |
| 677 | m_Accelerators[m_naccels].id = id; |
| 678 | m_Accelerators[m_naccels].key = key; |
| 679 | m_naccels++; |
| 680 | } |
| 681 | |
| 682 | void UIWindow::ResetAcceleratorKey() { m_naccels = 0; } |
| 683 | |
| 684 | // ---------------------------------------------------------------------------- |
| 685 | // UITitledWindow class |
| 686 | |
| 687 | UITitledWindow::UITitledWindow() {} |
| 688 | |
| 689 | UITitledWindow::~UITitledWindow() {} |
| 690 | |
| 691 | void UITitledWindow::Create(UITextItem &title, int x, int y, int w, int h) { |
| 692 | m_Title = title; |
| 693 | m_BorderThickness = 2; |
| 694 | m_CaptionColor = GR_DARKGRAY; |
| 695 | |
| 696 | UIWindow::Create(x, y, w, h); |
| 697 | UIPrimativeItem item{GR_LIGHTGRAY}; |
| 698 | UIWindow::SetBackItem(&item); |
| 699 | } |
| 700 | |
| 701 | // inheritable actions |
| 702 | |
| 703 | // draws the window background before gadgets |
| 704 | void UITitledWindow::OnDraw() { |
| 705 | UIWindow::OnDraw(); |
| 706 | int m_CaptionHeight; // caption height |
| 707 | |
| 708 | // set font for title. |
| 709 | if (m_Title.get_font() > -1) |
| 710 | ui_DrawSetFont(m_Title.get_font()); |
| 711 | |
| 712 | m_CaptionHeight = ui_GetFontHeight() + ui_GetFontHeight() / 2; |
| 713 | |
| 714 | // draw border. |
| 715 | for (int i = 0; i < m_BorderThickness; i++) |
| 716 | ui_DrawBox(GR_RGB(0, 0, 0), m_X + i, m_Y + i, m_X + m_W - i, m_Y + m_H - i); |
| 717 | |
| 718 | // draw caption. |
| 719 | int x, y; |
| 720 | ui_DrawRect(m_CaptionColor, m_BorderThickness, m_BorderThickness, m_W - m_BorderThickness, m_CaptionHeight); |
| 721 | x = m_W / 2 - ui_GetTextWidth(m_Title) / 2; |
| 722 | y = m_BorderThickness + m_CaptionHeight / 2 - ui_GetFontHeight() / 2; |
| 723 | |
| 724 | ui_SetCharAlpha(m_Title.get_alpha()); |
| 725 | ui_DrawString(m_Title.get_color(), x, y, m_Title); |
| 726 | } |