Debian Codegraph @

Descent3/CtlCfgElem.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/CtlCfgElem.cpp $
21 * $Revision: 26 $
22 * $Date: 3/20/00 12:03p $
23 * $Author: Matt $
24 *
25 * Control config element gadgets
26 *
27 * $Log: /DescentIII/main/CtlCfgElem.cpp $
28 *
29 * 26 3/20/00 12:03p Matt
30 * Merge of Duane's post-1.3 changes.
31 * Added Mac international keys (Mac only)
32 *
33 * 25 10/22/99 10:47a Matt
34 * Mac merge
35 *
36 * 24 7/30/99 1:05p Samir
37 * read POVs before buttons since most controllers map hat positions to
38 * buttons as well.
39 *
40 * 23 7/16/99 11:15a Samir
41 * multiple hat support
42 *
43 * 22 6/11/99 1:15a Samir
44 * localization issues.
45 *
46 * 21 5/20/99 9:11p Samir
47 * no bind '-' and '=' keys.
48 *
49 * 20 5/06/99 1:40a Samir
50 * adjusted some text.
51 *
52 * 19 4/29/99 2:59p Samir
53 * added help and made CTRL-C clear for controller screens.
54 *
55 * 18 4/29/99 2:23a Samir
56 * moved binding text functions to wincontroller.cpp and new text for
57 * multiple joysticks.
58 *
59 * 17 4/15/99 1:38a Jeff
60 * changes for linux compile
61 *
62 * 16 4/14/99 12:35p Samir
63 * localization issues.
64 *
65 * 15 4/14/99 2:50a Jeff
66 * fixed some case mismatched #includes
67 *
68 * 14 3/23/99 9:04p Samir
69 * moved mouse binding text to mouse library.
70 *
71 * 13 3/09/99 6:26p Samir
72 * flush mouse queue after configuring button.
73 *
74 * 12 3/05/99 4:34p Samir
75 * multiplayer bug in controller config.
76 *
77 * 11 3/02/99 1:17p Samir
78 * resume and suspend controls for config instead of directly going
79 * through controller. this is done so mask_controllers gets called with
80 * the latest Current_pillot.read_controller values...
81 *
82 * 10 2/26/99 2:09a Samir
83 * added '?' button.
84 *
85 * 9 2/21/99 6:36p Samir
86 * focusing changes and key input changes to ui.,
87 *
88 * 8 2/16/99 12:07p Samir
89 * redid controller config with new ui.
90 *
91 * 7 1/28/99 3:58p Jeff
92 * localization update
93 *
94 * 6 11/30/98 4:54p Samir
95 * added rear view config item.
96 *
97 * 5 10/23/98 12:51p Samir
98 * bail out of config if server says so: note there still is a bug with
99 * this system.
100 *
101 * 4 10/18/98 1:07p Samir
102 * tweaked user interface for controller config.
103 *
104 * 3 10/17/98 7:31p Samir
105 * added invertible axes
106 *
107 * 2 9/30/98 4:37p Samir
108 * 'incremental checkin'
109 *
110 * 1 9/28/98 3:47p Samir
111 * initial revision.
112 *
113 * $NoKeywords: $
114 */
115
116 #include "CtlCfgElem.h"
117 #include "descent.h"
118
119 #include "Macros.h"
120 #include "ddio.h"
121 #include "application.h"
122 #include "renderer.h"
123 #include "stringtable.h"
124 #include "gamefont.h"
125 #include "localization.h"
126
127 #include <string.h>
128 #include "joystick.h"
129
130 // all controller binding texts
131 static char Ctltext_KeyBindings[][16] = {"",
132 "",
133 "",
134 "",
135 "",
136 "",
137 "",
138 "",
139 "",
140 "",
141 "",
142 "",
143 "",
144 "",
145 "bspc\0\0\0\0\0\0",
146 "tab\0\0\0\0\0\0",
147 "q",
148 "w",
149 "e",
150 "r",
151 "t",
152 "y",
153 "u",
154 "i",
155 "o",
156 "p",
157 "[",
158 "]",
159 "enter\0\0\0\0\0",
160 "lctrl\0\0\0\0\0",
161 "a",
162 "s",
163 "d",
164 "f",
165 "g",
166 "h",
167 "j",
168 "k",
169 "l",
170 ";",
171 "'",
172 "`",
173 "lshft\0\0\0\0\0",
174 "\\",
175 "z",
176 "x",
177 "c",
178 "v",
179 "b",
180 "n",
181 "m",
182 ",",
183 ".",
184 "/",
185 "rshft\0\0\0\0\0",
186 "pad*\0\0\0\0\0",
187 "lalt\0\0\0\0\0",
188 "spc\0\0\0\0\0",
189 "caps\0\0\0\0\0",
190 "",
191 "",
192 "",
193 "",
194 "",
195 "",
196 "",
197 "",
198 "",
199 "",
200 "num\0\0\0\0\0",
201 "sclk\0\0\0\0\0",
202 "pad7\0\0\0\0\0",
203 "pad8\0\0\0\0\0",
204 "pad9\0\0\0\0\0",
205 "pad-\0\0\0\0\0",
206 "pad4\0\0\0\0\0",
207 "pad5\0\0\0\0\0",
208 "pad6\0\0\0\0\0",
209 "pad+\0\0\0\0\0",
210 "pad1\0\0\0\0\0",
211 "pad2\0\0\0\0\0",
212 "pad3\0\0\0\0\0",
213 "pad0\0\0\0\0\0",
214 "pad.\0\0\0\0\0",
215 "",
216 "",
217 "",
218 "",
219 "",
220 "",
221 "",
222 "",
223 "",
224 "",
225 "",
226 "",
227 "",
228 "",
229 "",
230 "",
231 "",
232 "",
233 "",
234 "",
235 "",
236 "",
237 "",
238 "",
239 "",
240 "",
241 "",
242 "",
243 "",
244 "",
245 "",
246 "",
247 "",
248 "",
249 "",
250 "",
251 "",
252 "",
253 "",
254 "",
255 "",
256 "",
257 "",
258 "",
259 "",
260 "",
261 "",
262 "",
263 "",
264 "",
265 "",
266 "",
267 "",
268 "",
269 "",
270 "",
271 "",
272 "",
273 "",
274 "",
275 "",
276 "",
277 "",
278 "",
279 "",
280 "",
281 "",
282 "",
283 "",
284 "",
285 "",
286 "",
287 "pad�\0\0\0\0\0\0",
288 "rctrl\0\0\0\0\0",
289 "",
290 "",
291 "",
292 "",
293 "",
294 "",
295 "",
296 "",
297 "",
298 "",
299 "",
300 "",
301 "",
302 "",
303 "",
304 "",
305 "",
306 "",
307 "",
308 "",
309 "",
310 "",
311 "",
312 "pad/\0\0\0\0\0\0",
313 "",
314 "",
315 "ralt\0\0\0\0\0\0",
316 "",
317 "",
318 "",
319 "",
320 "",
321 "",
322 "",
323 "",
324 "",
325 "",
326 "",
327 "",
328 "",
329 "",
330 "home\0\0\0\0\0\0",
331 "up\0\0\0\0\0\0\0",
332 "pgup\0\0\0\0\0\0",
333 "",
334 "left\0\0\0\0\0\0",
335 "",
336 "right\0\0\0\0\0\0",
337 "",
338 "end\0\0\0\0\0\0\0",
339 "down\0\0\0\0\0\0",
340 "pgdn\0\0\0\0\0\0",
341 "ins\0\0\0\0\0\0",
342 "del\0\0\0\0\0\0",
343 "",
344 "",
345 "",
346 "",
347 "",
348 "",
349 "",
350 "",
351 "",
352 "",
353 "",
354 "",
355 "",
356 "",
357 "",
358 "",
359 "",
360 "",
361 "",
362 "",
363 "",
364 "",
365 "",
366 "",
367 "",
368 "",
369 "",
370 "",
371 "",
372 "",
373 "",
374 "",
375 "",
376 "",
377 "",
378 "",
379 "",
380 "",
381 "",
382 "",
383 "",
384 "",
385 "",
386 ""};
387
388 static int16_t key_binding_indices[] = {
389 KEY_BACKSP, KEY_TAB, KEY_ENTER, KEY_LCTRL, KEY_LSHIFT, KEY_RSHIFT, KEY_PADMULTIPLY, KEY_LALT,
390 KEY_SPACEBAR, KEY_CAPSLOCK, 0x45, KEY_SCROLLOCK, KEY_PAD7, KEY_PAD8, KEY_PAD9, KEY_PADMINUS,
391 KEY_PAD4, KEY_PAD5, KEY_PAD6, KEY_PADPLUS, KEY_PAD1, KEY_PAD2, KEY_PAD3, KEY_PAD0,
392 KEY_PADPERIOD, KEY_PADENTER, KEY_RCTRL, KEY_PADDIVIDE, KEY_RALT, KEY_HOME, KEY_UP, KEY_PAGEUP,
393 KEY_LEFT, KEY_RIGHT, KEY_END, KEY_DOWN, KEY_PAGEDOWN, KEY_INSERT, KEY_DELETE, 0xff};
394
395 #define NUM_KEYBINDSTRINGS (sizeof(Ctltext_KeyBindings) / sizeof(char *))
396
397 #define F_ELEM_HILITE 0x1
398 #define F_ELEM_ACTIVE 0x2
399 #define F_ELEM_INVERTED 0x4
400
401 #define CFGELEM_BTN_X 132
402
403 /*
404 $$TABLE_GAMEFILE "SmallButton.ogf"
405 $$TABLE_GAMEFILE "SmallButtonLit.ogf"
406 $$TABLE_GAMEFILE "TinyButton.ogf"
407 $$TABLE_GAMEFILE "TinyButtonLit.ogf"
408 */
409 #define NEWUI_BTN_FILE "SmallButton.ogf"
410 #define NEWUI_BTNLIT_FILE "SmallButtonLit.ogf"
411 #define NEWUI_TINYBTN_FILE "TinyButton.ogf"
412 #define NEWUI_TINYBTNLIT_FILE "TinyButtonLit.ogf"
413
414 extern char Ctltext_AxisBindings[][16];
415 extern char Ctltext_PovBindings[][16];
416 extern char Ctltext_MseBtnBindings[][32];
417 extern char Ctltext_MseAxisBindings[][32];
418
419 void Localize_ctl_bindings() {
420 char **strtable;
421 int n_strings, i;
422
423 // keyboard translations.
424 // no need to do for english!
425 if (Localization_GetLanguage() == LANGUAGE_ENGLISH)
426 return;
427
428 if (CreateStringTable("bindkey.str", &strtable, &n_strings)) {
429 i = 0;
430 while (key_binding_indices[i] != 0xff) {
431 if (i >= n_strings) {
432 break;
433 }
434 strcpy(Ctltext_KeyBindings[key_binding_indices[i]], strtable[i]);
435 i++;
436 }
437 DestroyStringTable(strtable, n_strings);
438 }
439
440 // mouse translations.
441 if (CreateStringTable("bindmse.str", &strtable, &n_strings)) {
442 for (i = 0; i < 6; i++) {
443 if (i >= n_strings) {
444 break;
445 }
446 strcpy(Ctltext_MseBtnBindings[i], strtable[i]);
447 }
448 for (i = 0; i < 3; i++) {
449 if ((i + 6) >= n_strings) {
450 break;
451 }
452 strcpy(Ctltext_MseAxisBindings[i], strtable[i + 6]);
453 }
454 DestroyStringTable(strtable, n_strings);
455 }
456
457 // joystick translations.
458 if (CreateStringTable("bindjoy.str", &strtable, &n_strings)) {
459 for (i = 0; i < 6; i++) {
460 if (i >= n_strings) {
461 break;
462 }
463 strcpy(Ctltext_AxisBindings[i + 1], strtable[i]);
464 }
465 for (i = 0; i < 4; i++) {
466 if ((i + 6) >= n_strings) {
467 break;
468 }
469 strcpy(Ctltext_PovBindings[i + 1], strtable[i + 6]);
470 }
471 DestroyStringTable(strtable, n_strings);
472 }
473 }
474
475 //////////////////////////////////////////////////////////////////////////////
476 const char *cfg_binding_text(ct_type ctype, uint8_t ctrl, uint8_t binding) {
477 const char *str;
478
479 if (ctrl == NULL_CONTROLLER) {
480 return NULL;
481 }
482
483 switch (ctype) {
484 case ctKey: {
485 ASSERT(binding < NUM_KEYBINDSTRINGS);
486 str = Ctltext_KeyBindings[binding];
487 break;
488 }
489
490 case ctPOV:
491 case ctPOV2:
492 case ctPOV3:
493 case ctPOV4:
494 case ctMouseButton:
495 case ctButton:
496 case ctMouseAxis:
497 case ctAxis:
498 str = Controller->get_binding_text(ctype, ctrl, binding);
499 break;
500
501 default:
502 if (ctype == ctNone) {
503 Int3();
504 }
505 return ("");
506 }
507
508 return str;
509 }
510
511 //////////////////////////////////////////////////////////////////////////////
512 //
513 class cfg_element_ui : public newuiMessageBox {
514 uint8_t m_element; // element passed and returned.
515 uint8_t m_controller; // controller.
516 int8_t m_alpha; // used for fx.
517 ct_type m_type;
518
519 public:
520 void Create(const char *title, ct_type type, uint8_t controller, uint8_t element);
521 uint8_t GetElement() const { return m_element; };
522 uint8_t GetController() const { return m_controller; };
523 ct_type GetType() const { return m_type; };
524 int DoUI();
525
526 protected:
527 virtual void OnDraw();
528 };
529
530 //////////////////////////////////////////////////////////////////////////////
531 //
532
533 UIBitmapItem *cfg_element::m_btn_bmp_lit = NULL;
534 UIBitmapItem *cfg_element::m_btn_bmp = NULL;
535 UIBitmapItem *cfg_element::m_xbtn_bmp_lit = NULL;
536 UIBitmapItem *cfg_element::m_xbtn_bmp = NULL;
537 int16_t cfg_element::m_count = 0;
538
539 // MTS: unused?
540 bool key_cfg_element(uint8_t *element, uint8_t *flags);
541
542 //////////////////////////////////////////////////////////////////////////////
543 //
544
545 void cfg_element::Create(UIWindow *wnd, int str_i, int x, int y, int fnid, int id) {
546 m_title = TXT(str_i);
547 m_fnid = (int8_t)fnid;
548 m_slot = 0;
549 m_curslot = -1;
550 m_flags = 0;
551 m_slot_alpha = 255;
552 m_blink_state = 0;
553
554 if (cfg_element::m_count == 0) {
555 cfg_element::m_btn_bmp_lit = newui_LoadBitmap(IGNORE_TABLE(NEWUI_BTNLIT_FILE));
556 cfg_element::m_btn_bmp = newui_LoadBitmap(IGNORE_TABLE(NEWUI_BTN_FILE));
557 cfg_element::m_xbtn_bmp_lit = newui_LoadBitmap(IGNORE_TABLE(NEWUI_TINYBTNLIT_FILE));
558 cfg_element::m_xbtn_bmp = newui_LoadBitmap(IGNORE_TABLE(NEWUI_TINYBTN_FILE));
559 }
560 cfg_element::m_count++;
561
562 UIGadget::Create(wnd, id, x, y, 10, 10, 0);
563
564 if (id == -1) {
565 UIGadget::Disable();
566 }
567 }
568
569 void cfg_element::OnFormat() {
570 UITextItem title(MONITOR9_NEWUI_FONT, m_title, NEWUI_MONITORFONT_COLOR);
571 m_W = CFGELEM_BTN_X + (N_CFGELEM_SLOTS * (m_btn_bmp->width() + 2));
572 m_H = title.height();
573 }
574
575 void cfg_element::OnDraw() {
576 // draw text, then buttons, then...
577 UITextItem title(MONITOR9_NEWUI_FONT, m_title, NEWUI_MONITORFONT_COLOR);
578
579 int x = 0, i;
580
581 if (m_infocus) {
582 title.set_alpha(m_slot_alpha);
583 }
584
585 if (m_fnid != -1) {
586 UITextItem text(GADGET9_NEWUI_FONT, "?", NEWUI_GADGETFONT_COLOR);
587 if (m_curslot == CFGELEM_SLOT_CLEAR && CHECK_FLAG(m_flags, F_ELEM_HILITE)) {
588 m_xbtn_bmp_lit->draw(x, 0);
589 } else {
590 m_xbtn_bmp->draw(x, 0);
591 }
592 text.draw(x + (m_xbtn_bmp->width() - text.width()) / 2, 1 + (m_xbtn_bmp->height() - text.height()) / 2);
593 x += 28;
594 }
595
596 title.draw(x, 0);
597
598 if (m_fnid != -1) {
599 ct_type ctype[CTLBINDS_PER_FUNC];
600 uint8_t cfgflags[CTLBINDS_PER_FUNC];
601 ct_config_data cfgdata;
602 tCfgDataParts cfgparts;
603
604 x = CFGELEM_BTN_X;
605
606 Controller->get_controller_function(m_fnid, ctype, &cfgdata, cfgflags);
607 parse_config_data(&cfgparts, ctype[0], ctype[1], cfgdata);
608
609 for (i = 0; i < N_CFGELEM_SLOTS; i++) {
610 const char *txt;
611 uint8_t one_binding = (i == 0) ? cfgparts.bind_0 : cfgparts.bind_1;
612 uint8_t one_ctrlbind = (i == 0) ? cfgparts.ctrl_0 : cfgparts.ctrl_1;
613
614 txt = cfg_binding_text(ctype[i], one_ctrlbind, one_binding);
615
616 // draw button
617 if (CHECK_FLAG(m_flags, F_ELEM_HILITE) && m_curslot == i) {
618 m_btn_bmp_lit->draw(x, 0);
619 } else {
620 m_btn_bmp->draw(x, 0);
621 }
622
623 // draw text
624 if (txt) {
625 UITextItem text(GADGET9_NEWUI_FONT, txt, NEWUI_GADGETFONT_COLOR);
626
627 if (m_infocus) {
628 if (m_curslot == i) {
629 text.set_alpha(m_slot_alpha);
630 }
631 }
632
633 text.draw(x + (m_btn_bmp->width() - text.width()) / 2, (m_btn_bmp->height() - text.height()) / 2);
634 }
635
636 x += (m_btn_bmp->width() + 2);
637 }
638
639 m_slot_alpha += m_blink_state;
640 if (m_slot_alpha == 25) {
641 m_blink_state = 25;
642 } else if (m_slot_alpha == 250) {
643 m_blink_state = -25;
644 }
645 }
646 }
647
648 void cfg_element::OnKeyDown(int key) {
649 if (key == KEY_RIGHT && m_curslot < (N_CFGELEM_SLOTS - 1)) {
650 m_curslot++;
651 } else if (key == KEY_LEFT && m_curslot > 0) {
652 m_curslot--;
653 } else if (key == (KEY_CTRLED + KEY_C)) {
654 // clear binding.
655 tCfgDataParts cfgparts;
656 ct_type ctype_fn[CTLBINDS_PER_FUNC];
657 uint8_t cfgflags_fn[CTLBINDS_PER_FUNC];
658 ct_config_data ccfgdata_fn;
659
660 Controller->get_controller_function(m_fnid, ctype_fn, &ccfgdata_fn, cfgflags_fn);
661 parse_config_data(&cfgparts, ctype_fn[0], ctype_fn[1], ccfgdata_fn);
662 if (m_curslot == 0) {
663 cfgparts.bind_0 = NULL_BINDING;
664 cfgparts.ctrl_0 = NULL_CONTROLLER;
665 } else if (m_curslot == 1) {
666 cfgparts.bind_1 = NULL_BINDING;
667 cfgparts.ctrl_1 = NULL_CONTROLLER;
668 }
669 ccfgdata_fn = unify_config_data(&cfgparts);
670 Controller->set_controller_function(m_fnid, ctype_fn, ccfgdata_fn, cfgflags_fn);
671
672 } else if (key == (KEY_SHIFTED + KEY_SLASH)) {
673 m_slot = CFGELEM_SLOT_CLEAR;
674 OnSelect();
675 }
676 }
677
678 void cfg_element::OnMouseBtnDown(int btn) {
679 if (btn == UILMSEBTN) {
680 int gadmx = SCREEN_TO_GAD_X(this, UI_input.mx), gadmy = SCREEN_TO_GAD_Y(this, UI_input.my);
681 int x = 0;
682 int8_t curslot;
683
684 // determine what is the current slot
685 curslot = -1;
686
687 if (PT_IN_RECT(gadmx, gadmy, x, 0, x + m_xbtn_bmp->width(), m_xbtn_bmp->height())) {
688 curslot = CFGELEM_SLOT_CLEAR;
689 }
690
691 x = CFGELEM_BTN_X;
692 if (PT_IN_RECT(gadmx, gadmy, x, 0, x + m_btn_bmp->width(), m_btn_bmp->height())) {
693 curslot = 0;
694 }
695
696 x += m_btn_bmp->width() + 2;
697 if (PT_IN_RECT(gadmx, gadmy, x, 0, x + m_btn_bmp->width(), m_btn_bmp->height())) {
698 curslot = 1;
699 }
700
701 // if not active and mouse button is down in a box, flag as active if not already active.
702 if (!CHECK_FLAG(m_flags, F_ELEM_ACTIVE)) {
703 m_slot = -1;
704 if (curslot != -1) {
705 m_flags |= F_ELEM_ACTIVE;
706 LOCK_FOCUS(this);
707 m_curslot = curslot;
708 }
709 }
710
711 // if gadget is active, then check if we should be highlighted
712 if (CHECK_FLAG(m_flags, F_ELEM_ACTIVE)) {
713 if (curslot != -1 && m_curslot == curslot) {
714 m_flags |= F_ELEM_HILITE;
715 } else {
716 m_flags &= (~F_ELEM_HILITE);
717 }
718 }
719 }
720 }
721
722 void cfg_element::OnMouseBtnUp(int btn) {
723 if (btn == UILMSEBTN) {
724 // only do something if active.
725 if (CHECK_FLAG(m_flags, F_ELEM_ACTIVE)) {
726 UNLOCK_FOCUS(this);
727 if (CHECK_FLAG(m_flags, F_ELEM_HILITE)) {
728 OnSelect();
729 m_slot = m_curslot;
730 } else {
731 m_slot = -1;
732 }
733 }
734 m_flags &= ~(F_ELEM_HILITE + F_ELEM_ACTIVE);
735 // m_curslot = -1;
736 }
737 }
738
739 void cfg_element::OnLostFocus() {
740 m_blink_state = 0;
741 m_slot_alpha = 250;
742 }
743
744 void cfg_element::OnGainFocus() {
745 m_slot_alpha = 250;
746 m_blink_state = -25;
747 m_curslot = 0;
748 }
749
750 void cfg_element::OnDestroy() {
751 cfg_element::m_count--;
752 if (cfg_element::m_count <= 0) {
753 cfg_element::m_count = 0;
754 newui_FreeBitmap(cfg_element::m_btn_bmp);
755 newui_FreeBitmap(cfg_element::m_btn_bmp_lit);
756 newui_FreeBitmap(cfg_element::m_xbtn_bmp);
757 newui_FreeBitmap(cfg_element::m_xbtn_bmp_lit);
758 }
759 }
760
761 // calls configuration routines
762 bool cfg_element::Configure(ct_type *new_elem_type, uint8_t *controller, uint8_t *new_cfg_element, int8_t *cfg_slot) {
763 cfg_element_ui cfg_box;
764 ct_type ctype_fn[CTLBINDS_PER_FUNC];
765 uint8_t cfgflags_fn[CTLBINDS_PER_FUNC];
766 ct_config_data ccfgdata_fn;
767 tCfgDataParts cfgparts;
768 uint8_t element, ctrl;
769 bool configure = false;
770
771 int8_t fnid = m_fnid;
772 int8_t slot = m_slot;
773
774 if (m_fnid == -1) {
775 Int3(); // get samir
776 return false;
777 }
778
779 // determine which configuration pipe to go down
780 Controller->get_controller_function(fnid, ctype_fn, &ccfgdata_fn, cfgflags_fn);
781
782 if (!new_elem_type) {
783 cfgparts.bind_0 = NULL_BINDING;
784 cfgparts.bind_1 = NULL_BINDING;
785 cfgparts.ctrl_0 = NULL_CONTROLLER;
786 cfgparts.ctrl_1 = NULL_CONTROLLER;
787
788 ccfgdata_fn = unify_config_data(&cfgparts);
789 cfgflags_fn[0] = 0;
790 cfgflags_fn[1] = 0;
791
792 Controller->set_controller_function(fnid, ctype_fn, ccfgdata_fn, cfgflags_fn);
793 return false;
794 }
795
796 ASSERT(slot < N_CFGELEM_SLOTS && slot >= 0);
797
798 parse_config_data(&cfgparts, ctype_fn[0], ctype_fn[1], ccfgdata_fn);
799 element = (slot == 0) ? cfgparts.bind_0 : cfgparts.bind_1;
800 ctrl = (slot == 0) ? cfgparts.ctrl_0 : cfgparts.ctrl_1;
801
802 // check if we can configure this slot.
803 switch (ctype_fn[slot]) {
804 case ctNone:
805 break;
806 case ctKey:
807 configure = true;
808 break;
809 case ctMouseButton:
810 case ctButton:
811 case ctPOV:
812 case ctPOV2:
813 case ctPOV3:
814 case ctPOV4:
815 case ctAxis:
816 case ctMouseAxis:
817 configure = true;
818 break;
819 }
820
821 // do configuration requested
822 *new_elem_type = ctNone;
823 *new_cfg_element = NULL_BINDING;
824 *controller = NULL_CONTROLLER;
825 *cfg_slot = -1;
826 if (configure) {
827 const char *txt = m_title;
828 cfg_box.Create(txt, ctype_fn[slot], ctrl, element);
829 cfg_box.Open();
830 if (cfg_box.DoUI() == UID_OK) {
831 *new_elem_type = cfg_box.GetType();
832 *new_cfg_element = cfg_box.GetElement();
833 *controller = cfg_box.GetController();
834 *cfg_slot = m_slot;
835 configure = true;
836 } else {
837 configure = false;
838 }
839 cfg_box.Close();
840 cfg_box.Destroy();
841 }
842
843 return configure;
844 }
845
846 //////////////////////////////////////////////////////////////////////////////
847 bool key_cfg_element(uint8_t *element, uint8_t *flags) {
848 // put up configuration dialog
849
850 return true;
851 }
852
853 void cfg_element_ui::Create(const char *title, ct_type type, uint8_t controller, uint8_t element) {
854 m_controller = controller;
855 m_element = element;
856 m_type = type;
857
858 newuiMessageBox::Create(title, MSGBOX_NULL);
859
860 // put appropriate instructions in messagebox
861 newuiSheet *sheet = newuiMessageBox::GetSheet();
862
863 switch (type) {
864 case ctNone:
865 break;
866 case ctKey:
867 sheet->AddText(TXT_CTLBINDHELP1);
868 break;
869 case ctButton:
870 case ctMouseButton:
871 case ctPOV:
872 case ctPOV2:
873 case ctPOV3:
874 case ctPOV4:
875 sheet->AddText(TXT_CTLBINDHELP2_0);
876 sheet->AddText(TXT_CTLBINDHELP2_1);
877 break;
878
879 case ctAxis:
880 case ctMouseAxis:
881 sheet->AddText(TXT_CTLBINDHELP3_0);
882 sheet->AddText(TXT_CTLBINDHELP3_1);
883 break;
884 }
885 }
886
887 #define GCV_CONTROLLER(_r) (CONTROLLER_CTL1_INFO(CONTROLLER_INFO(ccfgdata)))
888 #define GCV_VALUE(_r) (CONTROLLER_CTL1_VALUE(CONTROLLER_VALUE(ccfgdata)))
889 #define GCV_VALID_RESULT(_r) (CONTROLLER_CTL1_INFO(CONTROLLER_INFO(ccfgdata)) != (int8_t)NULL_CONTROLLER)
890
891 int cfg_element_ui::DoUI() {
892 extern void ddio_MouseQueueFlush();
893
894 int retval = UID_OK;
895 bool quit = false, catch_press = false;
896 int8_t adj;
897
898 m_alpha = 16;
899 adj = -1;
900
901 ui_HideCursor();
902
903 Descent->delay(0.3f);
904 ResumeControls();
905 Control_poll_flag = false; // under multiplayer, the game frame is still running, so don't let
906 // assignments make it down to game frame (HACK)
907 newuiMessageBox::GetSheet()->Realize();
908 while (!quit) {
909 int key;
910
911 Descent->defer();
912 key = ddio_KeyInKey();
913
914 // quit if escape pressed
915 if (key == KEY_ESC) {
916 retval = UID_CANCEL;
917 quit = true;
918 }
919
920 // input must go here.
921 switch (m_type) {
922 case ctKey: {
923 // perform simple key processing.
924 if (key > 0) {
925 if (Ctltext_KeyBindings[key & 0xff]) {
926 m_element = (key & 0xff);
927 quit = true;
928 } else {
929 // here tell user that this key can't be bound.
930 }
931 }
932 break;
933 }
934 case ctButton:
935 case ctMouseButton:
936 case ctPOV:
937 case ctPOV2:
938 case ctPOV3:
939 case ctPOV4: {
940 ct_config_data ccfgdata;
941 ct_type new_type;
942
943 // get each type of input.
944 Controller->poll();
945
946 ccfgdata = Controller->get_controller_value(ctMouseButton);
947 new_type = ctMouseButton;
948 if (!GCV_VALID_RESULT(ccfgdata)) {
949 ccfgdata = Controller->get_controller_value(ctPOV);
950 new_type = ctPOV;
951 if (!GCV_VALID_RESULT(ccfgdata)) {
952 ccfgdata = Controller->get_controller_value(ctPOV2);
953 new_type = ctPOV2;
954 if (!GCV_VALID_RESULT(ccfgdata)) {
955 ccfgdata = Controller->get_controller_value(ctPOV3);
956 new_type = ctPOV3;
957 if (!GCV_VALID_RESULT(ccfgdata)) {
958 ccfgdata = Controller->get_controller_value(ctPOV4);
959 new_type = ctPOV4;
960 if (!GCV_VALID_RESULT(ccfgdata)) {
961 ccfgdata = Controller->get_controller_value(ctButton); // read hats before buttons
962 new_type = ctButton;
963 }
964 }
965 }
966 }
967 }
968 if (GCV_VALID_RESULT(ccfgdata) && !catch_press) {
969 m_type = new_type;
970 m_element = GCV_VALUE(ccfgdata);
971 m_controller = GCV_CONTROLLER(ccfgdata);
972 catch_press = true;
973 // mprintf(0, "HERE?\n");
974 } else if (catch_press) {
975 quit = true;
976 // mprintf(0, "THERE?\n");
977 }
978 break;
979 }
980
981 case ctAxis:
982 case ctMouseAxis: {
983 ct_config_data ccfgdata;
984 ct_type new_type;
985
986 // get each type of input.
987 Controller->poll();
988
989 ccfgdata = Controller->get_controller_value(ctAxis);
990 new_type = ctAxis;
991
992 if (!GCV_VALID_RESULT(ccfgdata)) {
993 ccfgdata = Controller->get_controller_value(ctMouseAxis);
994 new_type = ctMouseAxis;
995 }
996 if (GCV_VALID_RESULT(ccfgdata)) {
997 m_type = new_type;
998 m_element = GCV_VALUE(ccfgdata);
999 m_controller = GCV_CONTROLLER(ccfgdata);
1000 quit = true;
1001 }
1002 break;
1003 }
1004
1005 case ctNone:
1006 quit = true;
1007 break;
1008
1009 default:
1010 Int3();
1011 quit = true;
1012 }
1013
1014 DoUIFrameWithoutInput();
1015 rend_Flip();
1016
1017 if (GetUIFrameResult() == NEWUIRES_FORCEQUIT) {
1018 quit = true;
1019 }
1020
1021 m_alpha += adj;
1022 if (m_alpha < -15) {
1023 m_alpha = -15;
1024 adj = 1;
1025 } else if (m_alpha > 16) {
1026 m_alpha = 16;
1027 adj = -1;
1028 }
1029 }
1030 newuiMessageBox::GetSheet()->Unrealize();
1031
1032 SuspendControls();
1033
1034 ui_ShowCursor();
1035 ddio_MouseQueueFlush();
1036 ui_Flush();
1037
1038 return UID_OK;
1039 }
1040
1041 void cfg_element_ui::OnDraw() {
1042 UITextItem prompt(MONITOR9_NEWUI_FONT, "?", NEWUI_MONITORFONT_COLOR, (uint8_t)(m_alpha * 8) + 127);
1043
1044 newuiMessageBox::OnDraw();
1045
1046 prompt.draw(16, 60);
1047 }