Debian Codegraph @

Descent3/ConfigItem.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/ConfigItem.cpp $
21 * $Revision: 19 $
22 * $Date: 4/26/99 9:11p $
23 * $Author: Matt $
24 *
25 * Contains the implementation for ConfigItem [config dialog UI objects]
26 *
27 * $Log: /DescentIII/main/ConfigItem.cpp $
28 *
29 * 19 4/26/99 9:11p Matt
30 * Use small font instead of old UI font.
31 *
32 * 18 4/14/99 2:50a Jeff
33 * fixed some case mismatched #includes
34 *
35 * 17 10/23/98 11:09a Jeff
36 * fixed percent sign
37 *
38 * 16 10/20/98 12:14p Jeff
39 * added color to the UIGroup
40 *
41 * 15 10/19/98 6:30p Jeff
42 * changes made for detail variables. Put in preset values. Preset
43 * options. Removed terrain_cast from detail. Put new callbacks in
44 * UIListBox and UISlider
45 *
46 * 14 10/16/98 1:42p Jeff
47 * added CIF_USEGROUP and general dialog look
48 *
49 * 13 10/07/98 2:54p Jeff
50 * General UI fixes and additions
51 *
52 * 12 10/06/98 5:34p Jeff
53 * various UI changes/improvements
54 *
55 * 11 9/23/98 3:07p Jeff
56 * updated the colors and various other items of config and UI
57 *
58 * 10 9/08/98 11:40a Jeff
59 * fixed radio button bug
60 *
61 * 9 9/04/98 3:52p Jeff
62 * changes made from UI meeting
63 *
64 * 8 9/02/98 2:54p Jeff
65 * added defines for text colors to be used throughout the game...fixed up
66 * buddy menu too
67 *
68 * 7 6/16/98 10:38a Jeff
69 * localization, strings pulled out to stringtable.h and d3.str
70 *
71 * 6 6/11/98 12:08p Jeff
72 * fixed memory leak in CIT_CHECKBOX
73 *
74 * 5 6/05/98 5:59p Jeff
75 * Added a CIT_CHECKBOX style
76 *
77 * 4 5/23/98 6:33p Jeff
78 * Added some flags for the slider, and fixed it up a bit
79 *
80 * 3 5/22/98 5:02p Jeff
81 * Increased functionality
82 *
83 * 2 5/21/98 10:35p Jeff
84 * Initial creation
85 *
86 * $NoKeywords: $
87 */
88
89 #include "ConfigItem.h"
90 #include "ddio.h"
91 #include "newui.h"
92 #include "application.h"
93 #include "descent.h"
94 #include "mono.h"
95 #include "ddio.h"
96 #include "gamefont.h"
97 #include "multi_ui.h"
98 #include "cfile.h"
99 #include "mem.h"
100 #include "game.h"
101 #include "stringtable.h"
102
103 #include <string.h>
104 #include <stdlib.h>
105 #include <stdarg.h>
106 #include "stringtable.h"
107
108 #define UI_CHECKBOX_OFF_CHAR 28
109 #define UI_CHECKBOX_ON_CHAR 29
110
111 ConfigItem::ConfigItem() {
112 m_fInitial = 0;
113 m_X = m_Y = m_W = m_H = 0;
114 curr_x = curr_y = 0;
115 m_fCurrentValue = 0;
116 m_iFlags = 0;
117 m_iType = 0;
118 m_iID = NULL;
119 m_iNumIDs = 0;
120 m_iItems = 0;
121 m_iValueType = 0;
122 m_hWnd = NULL;
123 m_tiList = NULL;
124 m_tiCount = 0;
125 m_hsList = NULL;
126 m_hsCount = 0;
127 m_sList = NULL;
128 m_sCount = 0;
129 m_rbList = NULL;
130 m_rbCount = 0;
131 m_bList = NULL;
132 m_bCount = 0;
133 m_lbList = NULL;
134 m_lbCount = 0;
135 m_fMin = 0;
136 m_fMax = 100;
137 m_iRange = 100;
138 m_fUnit = 1;
139 m_auxX = 0;
140 m_bAlive = false;
141 m_labeltext = NULL;
142 m_fCallback = NULL;
143 m_iCallback = NULL;
144 m_bCallback = NULL;
145 }
146
147 ConfigItem::~ConfigItem() {
148 if (m_bAlive)
149 Destroy();
150 }
151
152 bool ConfigItem::Create(NewUIGameWindow *parentwnd, int type, int flags, int x, int y, int w, int h, char *label,
153 int item_x) {
154 if (m_bAlive)
155 return false;
156 if (!parentwnd)
157 return false;
158 switch (type) {
159 case CIT_HOTSPOTLIST:
160 case CIT_SLIDER:
161 case CIT_ONOFFBUTTON:
162 case CIT_LISTBOX:
163 case CIT_YESNOBUTTON:
164 case CIT_RADIOBUTTON:
165 case CIT_CHECKBOX:
166 m_iType = type;
167 break;
168 default:
169 mprintf(0, "Bad config item type in Create\n");
170 return false;
171 }
172 if (!label)
173 return false;
174
175 m_hWnd = parentwnd;
176 m_iFlags = flags;
177
178 if ((m_iFlags & CIF_USEGROUP) && (type != CIT_RADIOBUTTON)) {
179 // only radiobutton items support the CIF_USEGROUP
180 mprintf(0, "CONFIGITEM: Only CIT_RADIOBUTTON supports CIF_USEGROUP flag\n");
181 m_iFlags &= ~CIF_USEGROUP;
182 Int3();
183 }
184
185 if (type == CIT_SLIDER) { // slider's need to have the label centered vertically
186 UITextItem item{label, UICOL_TEXT_NORMAL};
187 m_tLabel.Create(parentwnd, &item, x, y + 7, UIF_FIT);
188 } else {
189 if (!(m_iFlags & CIF_USEGROUP)) {
190 // create a regular label
191 UITextItem item{label, UICOL_TEXT_NORMAL};
192 m_tLabel.Create(parentwnd, &item, x, y, UIF_FIT);
193 } else {
194 // save the label text
195 m_labeltext = mem_strdup(label);
196 }
197 }
198
199 m_auxX = x;
200 m_auxY = y;
201 m_X = item_x;
202 m_Y = y;
203 m_W = w;
204 m_H = h;
205 return true;
206 }
207
208 bool ConfigItem::Create(NewUIGameWindow *parentwnd, int type, char *label, int x, int y, int w, int h, int initialvalue,
209 int flags, int item_x) {
210 if (!Create(parentwnd, type, flags, x, y, w, h, label, item_x))
211 return false;
212
213 m_iValueType = CIV_INT;
214 m_iInitial = m_iCurrentValue = initialvalue;
215
216 m_bAlive = true;
217 return true;
218 }
219
220 bool ConfigItem::Create(NewUIGameWindow *parentwnd, int type, char *label, int x, int y, int w, int h,
221 bool initialvalue, int flags, int item_x) {
222 if (!Create(parentwnd, type, flags, x, y, w, h, label, item_x))
223 return false;
224
225 m_iValueType = CIV_BOOL;
226 m_bInitial = m_bCurrentValue = initialvalue;
227
228 m_bAlive = true;
229 return true;
230 }
231
232 bool ConfigItem::Create(NewUIGameWindow *parentwnd, int type, char *label, int x, int y, int w, int h,
233 float initialvalue, int flags, int item_x) {
234 if (!Create(parentwnd, type, flags, x, y, w, h, label, item_x))
235 return false;
236
237 m_iValueType = CIV_FLOAT;
238 m_fInitial = m_fCurrentValue = initialvalue;
239
240 m_bAlive = true;
241 return true;
242 }
243
244 void ConfigItem::Destroy(void) {
245 if (!m_bAlive)
246 return;
247
248 int i;
249
250 if (m_labeltext) {
251 mem_free(m_labeltext);
252 m_labeltext = NULL;
253 }
254
255 if ((m_tiCount) && (m_tiList)) {
256 for (i = 0; i < m_tiCount; i++)
257 if (m_tiList[i])
258 delete m_tiList[i];
259 mem_free(m_tiList);
260 }
261
262 if ((m_hsCount) && (m_hsList)) {
263 for (i = 0; i < m_hsCount; i++)
264 if (m_hsList[i])
265 delete m_hsList[i];
266 mem_free(m_hsList);
267 }
268
269 if ((m_sCount) && (m_sList)) {
270 for (i = 0; i < m_sCount; i++)
271 if (m_sList[i])
272 delete m_sList[i];
273 mem_free(m_sList);
274 }
275
276 if ((m_rbCount) && (m_rbList)) {
277 for (i = 0; i < m_rbCount; i++)
278 if (m_rbList[i])
279 delete m_rbList[i];
280 mem_free(m_rbList);
281 }
282
283 if ((m_bCount) && (m_bList)) {
284 for (i = 0; i < m_bCount; i++)
285 if (m_bList[i])
286 delete m_bList[i];
287 mem_free(m_bList);
288 }
289
290 if ((m_lbCount) && (m_lbList)) {
291 for (i = 0; i < m_lbCount; i++)
292 if (m_lbList[i])
293 delete m_lbList[i];
294 mem_free(m_lbList);
295 }
296
297 if ((m_iNumIDs) && (m_iID))
298 mem_free(m_iID);
299
300 m_bAlive = false;
301 }
302
303 bool ConfigItem::GetCurrentValue(int *currvalue) {
304 if (!m_bAlive)
305 return false;
306 if (m_iValueType != CIV_INT)
307 return false;
308 if (m_iType == CIT_LISTBOX) // we need to get the current index since it only gets updated on a double click
309 m_iCurrentValue = m_lbList[0]->GetSelectedIndex();
310 *currvalue = m_iCurrentValue;
311 return true;
312 }
313
314 bool ConfigItem::GetCurrentValue(bool *currvalue) {
315 if (!m_bAlive)
316 return false;
317 if (m_iValueType != CIV_BOOL)
318 return false;
319 *currvalue = m_bCurrentValue;
320 return true;
321 }
322
323 bool ConfigItem::GetCurrentValue(float *currvalue) {
324 if (!m_bAlive)
325 return false;
326 if (m_iValueType != CIV_FLOAT)
327 return false;
328 *currvalue = m_fCurrentValue;
329 return true;
330 }
331
332 bool ConfigItem::SetCurrentValue(int currvalue, bool call_back) {
333 if (!m_bAlive)
334 return false;
335 if (m_iValueType != CIV_INT)
336 return false;
337
338 float f_cv = (float)currvalue;
339 int i_cv = currvalue;
340
341 // do some error checking
342 switch (m_iType) {
343 case CIT_HOTSPOTLIST:
344 case CIT_RADIOBUTTON:
345 if (i_cv < 0)
346 i_cv = 0;
347 if (i_cv >= m_iNumIDs)
348 i_cv = m_iNumIDs - 1;
349 break;
350 case CIT_SLIDER:
351 if (f_cv < m_fMin)
352 f_cv = m_fMin;
353 if (f_cv >= m_fMax)
354 f_cv = m_fMax;
355 i_cv = (int)(((float)(f_cv - m_fMin)) / m_fUnit);
356 break;
357 case CIT_LISTBOX:
358 // handles error checking in itself
359 break;
360 case CIT_CHECKBOX:
361 case CIT_ONOFFBUTTON:
362 case CIT_YESNOBUTTON:
363 Int3();
364 break;
365 }
366
367 switch (m_iType) {
368 case CIT_HOTSPOTLIST:
369 case CIT_RADIOBUTTON:
370 m_rbList[i_cv]->Activate();
371 break;
372 case CIT_SLIDER:
373 m_sList[0]->SetPos(i_cv);
374 UpdateSlider(0, call_back);
375 break;
376 case CIT_LISTBOX:
377 m_lbList[0]->SetSelectedIndex(i_cv);
378 break;
379 case CIT_CHECKBOX:
380 case CIT_ONOFFBUTTON:
381 case CIT_YESNOBUTTON:
382 Int3();
383 break;
384 }
385
386 // set the value than update
387 m_iCurrentValue = currvalue;
388
389 // Update(int result);
390
391 return true;
392 }
393
394 bool ConfigItem::SetCurrentValue(bool currvalue, bool call_back) {
395 if (!m_bAlive)
396 return false;
397 if (m_iValueType != CIV_BOOL)
398 return false;
399
400 // do some error checking
401 switch (m_iType) {
402 case CIT_HOTSPOTLIST:
403 case CIT_RADIOBUTTON:
404 case CIT_SLIDER:
405 case CIT_LISTBOX:
406 Int3();
407 break;
408 case CIT_CHECKBOX:
409 case CIT_ONOFFBUTTON:
410 case CIT_YESNOBUTTON:
411 break;
412 }
413
414 switch (m_iType) {
415 case CIT_HOTSPOTLIST:
416 case CIT_RADIOBUTTON:
417 case CIT_SLIDER:
418 case CIT_LISTBOX:
419 Int3();
420 break;
421 case CIT_CHECKBOX:
422 if (currvalue)
423 m_hsList[0]->SetStates(m_tiList[2], m_tiList[3]); // Checked
424 else
425 m_hsList[0]->SetStates(m_tiList[0], m_tiList[1]); // Unchecked
426 break;
427 case CIT_ONOFFBUTTON:
428 case CIT_YESNOBUTTON:
429 if (currvalue)
430 m_bList[0]->SetTitle(m_tiList[1]); // Yes
431 else
432 m_bList[0]->SetTitle(m_tiList[0]); // No
433 break;
434 }
435
436 // set the value than update
437 m_bCurrentValue = currvalue;
438
439 // Update(int result);
440
441 return true;
442 }
443
444 bool ConfigItem::SetCurrentValue(float currvalue, bool call_back) {
445 if (!m_bAlive)
446 return false;
447 if (m_iValueType != CIV_FLOAT)
448 return false;
449
450 // do some error checking
451 switch (m_iType) {
452 case CIT_SLIDER:
453 if (currvalue < m_fMin)
454 currvalue = m_fMin;
455 if (currvalue >= m_fMax)
456 currvalue = m_fMax;
457 currvalue = ((currvalue - m_fMin) / m_fUnit);
458 break;
459 case CIT_LISTBOX:
460 case CIT_HOTSPOTLIST:
461 case CIT_RADIOBUTTON:
462 case CIT_CHECKBOX:
463 case CIT_ONOFFBUTTON:
464 case CIT_YESNOBUTTON:
465 Int3();
466 break;
467 }
468
469 switch (m_iType) {
470 case CIT_SLIDER:
471 m_sList[0]->SetPos((int)currvalue);
472 UpdateSlider(0, call_back);
473 break;
474 case CIT_HOTSPOTLIST:
475 case CIT_RADIOBUTTON:
476 case CIT_LISTBOX:
477 case CIT_CHECKBOX:
478 case CIT_ONOFFBUTTON:
479 case CIT_YESNOBUTTON:
480 Int3();
481 break;
482 }
483
484 // Update(int result);
485
486 // set the value than update
487 m_fCurrentValue = currvalue;
488
489 return true;
490 }
491
492 bool ConfigItem::GetInitialValue(int *initvalue) {
493 if (!m_bAlive)
494 return false;
495 if (m_iValueType != CIV_INT)
496 return false;
497 *initvalue = m_iInitial;
498 return true;
499 }
500
501 bool ConfigItem::GetInitialValue(bool *initvalue) {
502 if (!m_bAlive)
503 return false;
504 if (m_iValueType != CIV_BOOL)
505 return false;
506 *initvalue = m_bInitial;
507 return true;
508 }
509
510 bool ConfigItem::GetInitialValue(float *initvalue) {
511 if (!m_bAlive)
512 return false;
513 if (m_iValueType != CIV_FLOAT)
514 return false;
515 *initvalue = m_fInitial;
516 return true;
517 }
518
519 void ConfigItem::Update(int result) {
520 if (!m_bAlive)
521 return;
522 if (!m_iID)
523 return;
524
525 // first see if this is one of our items
526 int index = -1;
527 for (int i = 0; i < m_iNumIDs; i++) {
528 if (m_iID[i] == result) {
529 index = i;
530 break;
531 }
532 }
533
534 if (index == -1) // it isn't one of ours
535 return;
536
537 // now determine which type of update to call
538 switch (m_iType) {
539 case CIT_HOTSPOTLIST:
540 UpdateHotSpotList(index);
541 break;
542 case CIT_SLIDER:
543 UpdateSlider(index);
544 break;
545 case CIT_ONOFFBUTTON:
546 UpdateOnOffButton(index);
547 break;
548 case CIT_LISTBOX:
549 UpdateListBox(index);
550 break;
551 case CIT_YESNOBUTTON:
552 UpdateYesNoButton(index);
553 break;
554 case CIT_RADIOBUTTON:
555 UpdateRadioButton(index);
556 break;
557 case CIT_CHECKBOX:
558 UpdateCheckBox(index);
559 break;
560 default:
561 mprintf(0, "Bad ConfigItem Type in Update\n");
562 }
563 }
564
565 void ConfigItem::UpdateHotSpotList(int index) {
566 ASSERT((index >= 0) && (index < m_rbCount));
567 m_iCurrentValue = index;
568 m_rbList[m_iCurrentValue]->Activate();
569 if (m_iCallback)
570 (*m_iCallback)(m_iCurrentValue);
571 }
572
573 void ConfigItem::UpdateSlider(int index, bool call_callback) {
574 ASSERT((index >= 0) && (index < m_sCount));
575 // determine which type of variables int or float
576 char temp[10];
577
578 switch (m_iValueType) {
579 case CIV_INT:
580 m_iCurrentValue = (int)(((float)m_sList[index]->GetPos()) * m_fUnit + m_fMin + 0.5f);
581 if (m_iCurrentValue > m_fMax)
582 m_iCurrentValue = m_fMax;
583 snprintf(temp, sizeof(temp), "%d", m_iCurrentValue);
584 if (m_iFlags & CIF_PERCENT)
585 strncat(temp, "%", sizeof(temp) - strlen(temp) - 1);
586 if (m_iCallback && call_callback)
587 (*m_iCallback)(m_iCurrentValue);
588 break;
589 case CIV_FLOAT:
590 m_fCurrentValue = ((float)m_sList[index]->GetPos()) * m_fUnit + m_fMin;
591 if (m_fCurrentValue > m_fMax)
592 m_fCurrentValue = m_fMax;
593 if (m_iFlags & CIF_NODECIMAL)
594 snprintf(temp, sizeof(temp), "%.0f", m_fCurrentValue);
595 else
596 snprintf(temp, sizeof(temp), "%.2f", m_fCurrentValue);
597 if (m_iFlags & CIF_PERCENT)
598 strncat(temp, "%", sizeof(temp) - strlen(temp) - 1);
599 if (m_fCallback && call_callback)
600 (*m_fCallback)(m_fCurrentValue);
601 break;
602 default:
603 Int3(); // Get Jeff
604 }
605 UITextItem item{temp, UICOL_TEXT_NORMAL};
606 m_tLabel2.SetTitle(&item);
607 }
608
609 void ConfigItem::UpdateOnOffButton(int index) {
610 ASSERT((index >= 0) && (index < m_bCount));
611 m_bCurrentValue = !m_bCurrentValue;
612 if (m_bCurrentValue)
613 m_bList[index]->SetTitle(m_tiList[1]); // On
614 else
615 m_bList[index]->SetTitle(m_tiList[0]); // Off
616 if (m_bCallback)
617 (*m_bCallback)(m_bCurrentValue);
618 }
619
620 void ConfigItem::UpdateListBox(int index) {
621 ASSERT((index >= 0) && (index < m_lbCount));
622 m_iCurrentValue = m_lbList[index]->GetSelectedIndex();
623 if (m_iCallback)
624 (*m_iCallback)(m_iCurrentValue);
625 }
626
627 void ConfigItem::UpdateYesNoButton(int index) {
628 ASSERT((index >= 0) && (index < m_bCount));
629 m_bCurrentValue = !m_bCurrentValue;
630 if (m_bCurrentValue)
631 m_bList[index]->SetTitle(m_tiList[1]); // Yes
632 else
633 m_bList[index]->SetTitle(m_tiList[0]); // No
634 if (m_bCallback)
635 (*m_bCallback)(m_bCurrentValue);
636 }
637
638 void ConfigItem::UpdateCheckBox(int index) {
639 ASSERT((index >= 0) && (index < m_hsCount));
640 m_bCurrentValue = !m_bCurrentValue;
641 if (m_bCurrentValue)
642 m_hsList[index]->SetStates(m_tiList[2], m_tiList[3]); // Checked
643 else
644 m_hsList[index]->SetStates(m_tiList[0], m_tiList[1]); // Unchecked
645
646 if (m_bCallback)
647 (*m_bCallback)(m_bCurrentValue);
648 }
649
650 void ConfigItem::UpdateRadioButton(int index) {
651 ASSERT((index >= 0) && (index < m_rbCount));
652 m_rbList[index]->Activate();
653 m_iCurrentValue = index;
654
655 if (m_iCallback)
656 (*m_iCallback)(m_iCurrentValue);
657 }
658
659 void ConfigItem::Add(int count, ...) {
660 if (!m_bAlive)
661 return;
662
663 ASSERT(m_tiCount == 0);
664
665 va_list marker;
666 va_start(marker, count);
667 int i;
668 UITextItem *uitemp;
669
670 switch (m_iType) {
671 case CIT_HOTSPOTLIST:
672 ASSERT(count > 0);
673 ASSERT(m_rbCount == 0);
674 // we need count text items
675 // we need count radiobuttons
676 // we need count IDs
677 m_tiCount = count;
678 m_tiList = (UITextItem **)mem_malloc(sizeof(UITextItem *) * m_tiCount);
679 m_rbCount = count;
680 m_rbList = (UIRadioButton **)mem_malloc(sizeof(UIRadioButton *) * m_rbCount);
681 m_iNumIDs = count;
682 m_iID = (int *)mem_malloc(sizeof(int) * m_iNumIDs);
683 // make sure mallocs are ok
684 ASSERT((m_tiList) && (m_rbList) && (m_iID));
685 // initialize variables
686 // adjust the curr x/y for the list
687 curr_x = 0;
688 curr_y = 0;
689 for (i = 0; i < m_iNumIDs; i++) {
690 // get unique ID
691 m_iID[i] = GetUniqueID();
692 // allocate text item
693 m_tiList[i] = new UITextItem;
694 // make sure allocation ok
695 ASSERT(m_tiList[i]);
696 uitemp = va_arg(marker, UITextItem *);
697 (*m_tiList[i]) = *uitemp;
698 (*m_tiList[i]).set_alpha(255);
699 // allocate radio button
700 m_rbList[i] = new UIRadioButton;
701 // make sure allocation ok
702 ASSERT(m_rbList[i]);
703 if (i)
704 m_rbList[i]->Create(m_hWnd, m_rbList[i - 1], m_iID[i], m_tiList[i], m_X + curr_x, m_Y + curr_y,
705 m_tiList[i]->width(), m_tiList[i]->height(), UIF_FIT | UIRB_NOBUTTON);
706 else
707 m_rbList[i]->Create(m_hWnd, NULL, m_iID[i], m_tiList[i], m_X + curr_x, m_Y + curr_y, m_tiList[i]->width(),
708 m_tiList[i]->height(), UIF_FIT | UIRB_NOBUTTON);
709
710 (*m_tiList[i]).set_color(UICOL_HOTSPOT_HI);
711 m_rbList[i]->SetStateItem(UI_BTS_ACTIVATED, m_tiList[i]);
712 (*m_tiList[i]).set_color(UICOL_HOTSPOT_LO);
713 m_rbList[i]->SetStateItem(UI_BTS_INACTIVE, m_tiList[i]);
714 (*m_tiList[i]).set_color(UICOL_HOTSPOT_HI);
715 m_rbList[i]->SetStateItem(UI_BTS_HILITE, m_tiList[i]);
716
717 // adjust curr x/y
718 curr_x += (m_rbList[i]->W() + HOTSPOTLIST_GAP);
719 }
720 // Initialize Config Item to initial value
721 ASSERT(m_iValueType == CIV_INT);
722 ASSERT((m_iInitial >= 0) && (m_iInitial < m_iNumIDs));
723 m_rbList[m_iInitial]->Activate();
724 break;
725 case CIT_SLIDER: {
726 // only 1 slider per config item
727 ASSERT(m_sCount == 0);
728 // we need 1 slider
729 // we need 1 ID
730 m_sCount = 1;
731 m_sList = (NewUISlider **)mem_malloc(sizeof(NewUISlider *) * 1);
732 m_iNumIDs = 1;
733 m_iID = (int *)mem_malloc(sizeof(int) * 1);
734 // make sure mallocs are ok
735 ASSERT((m_sList) && (m_iID));
736 // get unique ID
737 m_iID[0] = GetUniqueID();
738 // allocate slider
739 m_sList[0] = new NewUISlider;
740 // make sure allocation ok
741 ASSERT(m_sList[0]);
742 m_sList[0]->Create(m_hWnd, m_iID[0], m_X, m_Y, UIF_FIT);
743 char temp[10];
744 switch (m_iValueType) {
745 case CIV_INT:
746 snprintf(temp, sizeof(temp), "%d", m_iInitial);
747 if (m_iFlags & CIF_PERCENT)
748 strncat(temp, "%", sizeof(temp) - strlen(temp) - 1);
749 break;
750 case CIV_FLOAT:
751 if (m_iFlags & CIF_NODECIMAL)
752 snprintf(temp, sizeof(temp), "%.0f", m_fInitial);
753 else
754 snprintf(temp, sizeof(temp), "%.2f", m_fInitial);
755 if (m_iFlags & CIF_PERCENT)
756 strncat(temp, "%", sizeof(temp) - strlen(temp) - 1);
757 break;
758 }
759 UITextItem item{temp, UICOL_TEXT_NORMAL};
760 m_tLabel2.Create(m_hWnd, &item, m_X + 155, m_Y + 7, UIF_FIT);
761 m_sList[0]->SetSelectChangeCallback(CISliderCallback, this);
762 break;
763 }
764 case CIT_ONOFFBUTTON:
765 // only 1 on/off button per config item
766 ASSERT(m_bCount == 0);
767 // we need 2 text items ("On" and "Off")
768 // we need 1 button
769 // we need 1 ID
770 m_bCount = 1;
771 m_bList = (NewUIButton **)mem_malloc(sizeof(NewUIButton *) * 1);
772 m_tiCount = 2;
773 m_tiList = (UITextItem **)mem_malloc(sizeof(UITextItem *) * 2);
774 m_iNumIDs = 1;
775 m_iID = (int *)mem_malloc(sizeof(int) * 1);
776 // make sure mallocs mallocs are ok
777 ASSERT((m_tiList) && (m_bList) && (m_iID));
778 // adjust the curr x/y for the button
779 curr_x = 0;
780 curr_y = 0;
781 // get unique ID
782 m_iID[0] = GetUniqueID();
783 // allocate text items
784 m_tiList[0] = new UITextItem(TXT_OFF, UICOL_BUTTON_LO);
785 m_tiList[1] = new UITextItem(TXT_ON, UICOL_BUTTON_HI);
786 // make sure allocation ok
787 ASSERT((m_tiList[0]) && (m_tiList[1]));
788 // allocate buttonCreate
789 m_bList[0] = new NewUIButton;
790 // make sure allocation ok
791 ASSERT(m_bList[0]);
792 m_bList[0]->Create(m_hWnd, m_iID[0], m_tiList[0], m_X + curr_x, m_Y + curr_y, m_tiList[0]->width(),
793 m_tiList[0]->height(), UIF_FIT);
794 // Initialize Config Item to initial value
795 ASSERT(m_iValueType == CIV_BOOL);
796 if (m_bInitial)
797 m_bList[0]->SetTitle(m_tiList[1]); // On
798 else
799 m_bList[0]->SetTitle(m_tiList[0]); // Off
800 break;
801 case CIT_LISTBOX:
802 ASSERT(count > 0);
803 ASSERT(m_lbCount == 0);
804 // we need count text items (one for each item in list box)
805 // we need 1 listbox
806 // we need 1 ID
807 m_lbCount = 1;
808 m_lbList = (NewUIListBox **)mem_malloc(sizeof(NewUIListBox *) * 1);
809 m_tiCount = count;
810 m_tiList = (UITextItem **)mem_malloc(sizeof(UITextItem *) * m_tiCount);
811 m_iNumIDs = 1;
812 m_iID = (int *)mem_malloc(sizeof(int) * 1);
813 // make sure mallocs ok
814 ASSERT((m_lbList) && (m_tiList) && (m_iID));
815 // adjust the curr x/y for the listbox
816 curr_x = 0;
817 curr_y = m_tLabel.H() + LISTBOX_LABELGAP;
818 // get unique ID
819 m_iID[0] = GetUniqueID();
820 // allocate list box
821 m_lbList[0] = new NewUIListBox;
822 // check allocation
823 ASSERT(m_lbList[0]);
824 m_lbList[0]->Create(m_hWnd, m_iID[0], m_X + curr_x, m_Y + curr_y, m_W, m_H, UIF_FIT);
825 // allocate text items and put them in
826 for (i = 0; i < m_tiCount; i++) {
827 // create the text item
828 m_tiList[i] = new UITextItem;
829 // make sure allocation ok
830 ASSERT(m_tiList[i]);
831 (*m_tiList[i]) = (*(va_arg(marker, UITextItem *)));
832 // add textitem to listbox
833 m_lbList[0]->AddItem(m_tiList[i]);
834 }
835 // Initialize Config Item to initial value
836 ASSERT(m_iValueType == CIV_INT);
837 ASSERT((m_iInitial >= 0) && (m_iInitial < count));
838 m_lbList[0]->SetSelectChangeCallback(CIListBoxCallback, this);
839 m_lbList[0]->SetSelectedIndex(m_iInitial);
840 break;
841 case CIT_YESNOBUTTON:
842 // only 1 yes/no button per config item
843 ASSERT(m_bCount == 0);
844 // we need 2 text items ("Yes" and "No")
845 // we need 1 button
846 // we need 1 ID
847 m_bCount = 1;
848 m_bList = (NewUIButton **)mem_malloc(sizeof(NewUIButton *) * 1);
849 m_tiCount = 2;
850 m_tiList = (UITextItem **)mem_malloc(sizeof(UITextItem *) * 2);
851 m_iNumIDs = 1;
852 m_iID = (int *)mem_malloc(sizeof(int) * 1);
853 // make sure mallocs mallocs are ok
854 ASSERT((m_tiList) && (m_bList) && (m_iID));
855 // adjust the curr x/y for the button
856 curr_x = 0;
857 curr_y = 0;
858 // get unique ID
859 m_iID[0] = GetUniqueID();
860 // allocate text items
861 m_tiList[0] = new UITextItem(TXT_NO, UICOL_BUTTON_LO);
862 m_tiList[1] = new UITextItem(TXT_YES, UICOL_BUTTON_HI);
863 // make sure allocation ok
864 ASSERT((m_tiList[0]) && (m_tiList[1]));
865 // allocate buttonCreate
866 m_bList[0] = new NewUIButton;
867 // make sure allocation ok
868 ASSERT(m_bList[0]);
869 m_bList[0]->Create(m_hWnd, m_iID[0], m_tiList[0], m_X + curr_x, m_Y + curr_y, m_tiList[0]->width(),
870 m_tiList[0]->height(), UIF_FIT);
871 // Initialize Config Item to initial value
872 ASSERT(m_iValueType == CIV_BOOL);
873 if (m_bInitial)
874 m_bList[0]->SetTitle(m_tiList[1]); // Yes
875 else
876 m_bList[0]->SetTitle(m_tiList[0]); // No
877 break;
878 case CIT_CHECKBOX:
879 // only 1 checkbox per config item
880 ASSERT(m_bCount == 0);
881 // we need 4 text items (a checked and unchecked box hightlited and not)
882 // we need 1 hotspot
883 // we need 1 ID
884 m_hsCount = 1;
885 m_hsList = (UIHotspot **)mem_malloc(sizeof(UIHotspot *) * 1);
886 m_tiCount = 4;
887 m_tiList = (UITextItem **)mem_malloc(sizeof(UITextItem *) * 4);
888 m_iNumIDs = 1;
889 m_iID = (int *)mem_malloc(sizeof(int) * 1);
890 // make sure mallocs are ok
891 ASSERT((m_hsList) && (m_tiList) && (m_iID));
892 // adjust the curr x/y for the checkbox
893 curr_x = 0;
894 curr_y = 0;
895 // get unique ID
896 m_iID[0] = GetUniqueID();
897 // allocate text items
898 char buffer[256];
899 snprintf(buffer, sizeof(buffer), "%c %s", UI_CHECKBOX_OFF_CHAR, ((UITextItem *)m_tLabel.GetItem())->GetBuffer());
900 m_tiList[0] = new UITextItem(buffer, UICOL_HOTSPOT_LO);
901 m_tiList[1] = new UITextItem(buffer, UICOL_HOTSPOT_HI);
902 snprintf(buffer, sizeof(buffer), "%c %s", UI_CHECKBOX_ON_CHAR, ((UITextItem *)m_tLabel.GetItem())->GetBuffer());
903 m_tiList[2] = new UITextItem(buffer, UICOL_HOTSPOT_LO);
904 m_tiList[3] = new UITextItem(buffer, UICOL_HOTSPOT_HI);
905 // make sure allocation ok
906 ASSERT((m_tiList[0]) && (m_tiList[1]));
907 // allocate hot spots
908 m_hsList[0] = new UIHotspot;
909 // make sure allocation ok
910 ASSERT(m_hsList[0]);
911 m_hsList[0]->Create(m_hWnd, m_iID[0], -1, m_tiList[0], m_tiList[1], m_X + curr_x, m_Y + curr_y,
912 m_tiList[0]->width(), m_tiList[0]->height(), UIF_FIT);
913 // Initialize Config Item to initial value
914 ASSERT(m_iValueType == CIV_BOOL);
915 if (m_bInitial)
916 m_hsList[0]->SetStates(m_tiList[2], m_tiList[3]);
917 else
918 m_hsList[0]->SetStates(m_tiList[0], m_tiList[1]);
919 // remove the title so it's not drawn
920 m_tLabel.Destroy();
921 break;
922 case CIT_RADIOBUTTON:
923 ASSERT(count > 0);
924 ASSERT(m_rbCount == 0);
925 // we need count text items
926 // we need count radio buttons
927 // we need count IDs
928 m_tiCount = count;
929 m_tiList = (UITextItem **)mem_malloc(sizeof(UITextItem *) * m_tiCount);
930 m_rbCount = count;
931 m_rbList = (UIRadioButton **)mem_malloc(sizeof(UIRadioButton *) * m_rbCount);
932 m_iNumIDs = count;
933 m_iID = (int *)mem_malloc(sizeof(int) * m_iNumIDs);
934 // make sure mallocs are ok
935 ASSERT((m_tiList) && (m_rbList) && (m_iID));
936 // adjust the curr x/y for the list of radio buttons
937 curr_x = 0;
938
939 int offset;
940 bool use_group;
941 use_group = (bool)((m_iFlags & CIF_USEGROUP) != 0);
942
943 if (m_X) {
944 curr_y = 0;
945 offset = 8;
946 } else {
947 m_X = m_auxX;
948 curr_y = RADIOBUTTON_LABELGAP + 10;
949
950 if (use_group) {
951 offset = 5;
952 } else {
953 offset = 0;
954 }
955 }
956
957 int start_y, w, max_width;
958
959 start_y = curr_y;
960 max_width = 0;
961
962 for (i = 0; i < count; i++) {
963 // get unique ID
964 m_iID[i] = GetUniqueID();
965 // allocate text items
966 m_tiList[i] = new UITextItem;
967 // make sure allocation ok
968 ASSERT(m_tiList[i]);
969 (*m_tiList[i]) = (*(va_arg(marker, UITextItem *)));
970
971 m_tiList[i]->set_color(UICOL_HOTSPOT_LO);
972
973 // m_tiList[i]->set_alpha(UIALPHA_HOTSPOT_LO);
974 // allocate radio button
975 m_rbList[i] = new UIRadioButton;
976 ASSERT(m_rbList[i]);
977 if (i)
978 m_rbList[i]->Create(m_hWnd, m_rbList[i - 1], m_iID[i], m_tiList[i], m_X + curr_x + offset, m_Y + curr_y, m_W,
979 m_H, UIF_FIT);
980 else
981 m_rbList[i]->Create(m_hWnd, NULL, m_iID[i], m_tiList[i], m_X + curr_x + offset, m_Y + curr_y, m_W, m_H,
982 UIF_FIT);
983
984 // store the max width
985 w = m_rbList[i]->W();
986 if (w > max_width)
987 max_width = w;
988
989 // adjust curr x/y
990 curr_y += (m_tiList[i]->height() + RADIOBUTTON_GAP);
991 }
992
993 if (m_iFlags & CIF_USEGROUP && m_labeltext) {
994 // create the group box now
995
996 // we may need to update the width depending on width of title
997 grtext_SetFont(SMALL_FONT);
998 int w = grtext_GetTextLineWidth(m_labeltext);
999 if (w > max_width)
1000 max_width = w;
1001
1002 m_H = curr_y - start_y + 15; // generate real height
1003 m_W = max_width + 20; // generate real width
1004
1005 // Create the group box
1006 m_gGroup.Create(m_hWnd, m_labeltext, m_auxX, m_Y, m_W, m_H, UICOL_TEXT_NORMAL, UICOL_HOTSPOT_LO);
1007 }
1008
1009 // Initialize Config Item to initial value
1010 ASSERT(m_iValueType == CIV_INT);
1011 ASSERT((m_iInitial >= 0) && (m_iInitial < m_iNumIDs));
1012 m_rbList[m_iInitial]->Activate();
1013 break;
1014 default:
1015 mprintf(0, "Bad ConfigItem Type in Add\n");
1016 }
1017 va_end(marker);
1018 }
1019 void ConfigItem::SetRange(int min, int max, int range) {
1020 if (!m_bAlive)
1021 return;
1022 ASSERT(m_iType == CIT_SLIDER);
1023 ASSERT(m_iValueType == CIV_INT);
1024 ASSERT(max > min);
1025 ASSERT((m_iCurrentValue >= min) && (m_iCurrentValue <= max));
1026
1027 m_fMin = min;
1028 m_fMax = max;
1029 m_sList[0]->SetRange(range + 1);
1030 m_fUnit = ((float)max - min) / ((float)range);
1031
1032 // figure out how far in is the position
1033 m_sList[0]->SetPos((int)(((float)(m_iInitial - min)) / m_fUnit));
1034 }
1035
1036 void ConfigItem::SetRange(float min, float max, int range) {
1037 if (!m_bAlive)
1038 return;
1039 ASSERT(m_iType == CIT_SLIDER);
1040 ASSERT(m_iValueType == CIV_FLOAT);
1041 ASSERT(max > min);
1042 ASSERT((m_fCurrentValue >= min) && (m_fCurrentValue <= max));
1043
1044 m_fMin = min;
1045 m_fMax = max;
1046 m_sList[0]->SetRange(range + 1);
1047 m_fUnit = (max - min) / ((float)range);
1048
1049 // figure out the real positions
1050 m_sList[0]->SetPos((int)((m_fInitial - min) / m_fUnit));
1051 }
1052
1053 void ConfigItem::SetCallback(void (*callback)(float)) {
1054 if (!m_bAlive)
1055 return;
1056 if (m_iValueType != CIV_FLOAT)
1057 return;
1058 m_fCallback = callback;
1059 }
1060
1061 void ConfigItem::SetCallback(void (*callback)(int)) {
1062 if (!m_bAlive)
1063 return;
1064 if (m_iValueType != CIV_INT)
1065 return;
1066 m_iCallback = callback;
1067 }
1068
1069 void ConfigItem::SetCallback(void (*callback)(bool)) {
1070 if (!m_bAlive)
1071 return;
1072 if (m_iValueType != CIV_BOOL)
1073 return;
1074 m_bCallback = callback;
1075 }
1076
1077 int GetUniqueID(void) {
1078 static int count = 0xB0;
1079 count++;
1080 return count;
1081 }
1082
1083 void CIListBoxCallback(int ID, void *ptr) {}
1084
1085 void CISliderCallback(int ID, void *ptr) {
1086 ConfigItem *ci = (ConfigItem *)ptr;
1087 if (!ci)
1088 return;
1089 ci->Update(ID);
1090 }