Debian Codegraph @

common/ui/keypad.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 #include <memory>
21 #include <stdlib.h>
22 #include <math.h>
23 #include <string.h>
24 #include <ranges>
25 #include "physfsx.h"
26 #include "maths.h"
27 #include "pstypes.h"
28 #include "gr.h"
29 #include "key.h"
30 #include "ui.h"
31 #include "u_mem.h"
32 #include "func.h"
33 #include "dxxerror.h"
34
35 #include "compiler-range_for.h"
36 #include "d_range.h"
37 #include <memory>
38
39 namespace dcx {
40 namespace {
41
42 #define MAX_NUM_PADS 20
43
44 static std::array<std::unique_ptr<UI_GADGET_BUTTON>, 17> Pad;
45 static std::array<std::unique_ptr<UI_KEYPAD>, MAX_NUM_PADS> KeyPad;
46 static int active_pad;
47
48 static int desc_x, desc_y;
49
50 static std::array<int, 17> HotKey, HotKey1;
51
52 }
53
54 int ui_pad_get_current()
55 {
56 return active_pad;
57 }
58
59 void ui_pad_init()
60 {
61 KeyPad = {};
62 active_pad = 0;
63 }
64
65 void ui_pad_close()
66 {
67 KeyPad = {};
68 }
69
70 namespace {
71
72 typedef PHYSFSX_gets_line_t<100>::line_t keypad_input_line_t;
73
74 static keypad_input_line_t::const_iterator find_fake_comma(keypad_input_line_t::const_iterator i, keypad_input_line_t::const_iterator e)
75 {
76 const auto is_fake_comma = [](const char c) {
77 return !c || static_cast<uint8_t>(c) == 179;
78 };
79 return std::ranges::find_if(i, e, is_fake_comma);
80 }
81
82 template <bool append, char eor>
83 static keypad_input_line_t::const_iterator set_row(keypad_input_line_t::const_iterator i, const keypad_input_line_t::const_iterator e, UI_KEYPAD::buttontext_element_t &r)
84 {
85 const auto oe = r.end();
86 auto ob = r.begin();
87 if (append)
88 ob = std::ranges::find(ob, oe, 0);
89 auto comma0 = find_fake_comma(i, e);
90 if (comma0 == e)
91 /* Start not found */
92 return comma0;
93 const auto comma1 = find_fake_comma(++ comma0, e);
94 std::size_t id = std::distance(comma0, comma1);
95 std::size_t od = std::distance(ob, oe);
96 if (!od)
97 /* Output buffer full */
98 return comma1;
99 -- od;
100 std::size_t md = std::min(id, od);
101 std::copy_n(comma0, md, ob);
102 std::advance(ob, md);
103 assert(ob != oe);
104 if (ob == oe)
105 -- ob;
106 if (eor)
107 {
108 /* Add EOR if room */
109 auto on = std::next(ob);
110 if (on != oe)
111 *ob++ = eor;
112 }
113 *ob = 0;
114 return comma1;
115 }
116
117 template <bool append, char eor, typename... T>
118 static keypad_input_line_t::const_iterator set_row(keypad_input_line_t::const_iterator i, const keypad_input_line_t::const_iterator e, UI_KEYPAD::buttontext_element_t &r, T &... t)
119 {
120 return set_row<append, eor>(set_row<append, eor>(i, e, r), e, t...);
121 }
122
123 static void set_short_row(keypad_input_line_t::const_iterator i, const keypad_input_line_t::const_iterator e, UI_KEYPAD::buttontext_element_t &r)
124 {
125 typedef std::reverse_iterator<keypad_input_line_t::const_iterator> reverse_iterator;
126 const auto oe = r.end();
127 auto ob = std::find(r.begin(), oe, 0);
128 std::size_t od = std::distance(ob, oe);
129 if (!od)
130 return;
131 -- od;
132 auto ie = std::find(i, e, 0);
133 auto ri = reverse_iterator(i);
134 auto comma0 = std::find(reverse_iterator(ie), ri, 179);
135 if (comma0 == ri)
136 return;
137 auto comma1 = std::find(++ comma0, ri, 180);
138 if (comma1 == ri)
139 return;
140 auto bcomma1 = comma1.base();
141 std::size_t id = std::distance(comma0.base(), bcomma1);
142 std::size_t md = std::min(id, od);
143 std::copy_n(bcomma1, md, ob);
144 std::advance(ob, md);
145 assert(ob != oe);
146 if (ob == oe)
147 -- ob;
148 auto on = std::next(ob);
149 if (on != oe)
150 *ob++ = '\n';
151 *ob = 0;
152 }
153
154 static std::unique_ptr<UI_GADGET_BUTTON> ui_create_pad_gadget(UI_DIALOG &dlg, uint_fast32_t x, uint_fast32_t y, uint_fast32_t w, uint_fast32_t h, const grs_font &font)
155 {
156 auto r = ui_add_gadget_button(dlg, x, y, w, h, nullptr, nullptr);
157 r->canvas->cv_font = &font;
158 return r;
159 }
160
161 }
162
163 void ui_pad_activate(UI_DIALOG &dlg, uint_fast32_t x, uint_fast32_t y)
164 {
165 const uint_fast32_t bw = 56;
166 const uint_fast32_t bh = 30;
167 const uint_fast32_t x5 = x + 5;
168 const uint_fast32_t y20 = y + 20;
169 const auto &font = *ui_small_font.get();
170 const auto fx = [=](uint_fast32_t col) {
171 return x5 + (bw * col);
172 };
173 const auto fy = [=](uint_fast32_t row) {
174 return y20 + (bh * row);
175 };
176 const auto fw = [=](uint_fast32_t w) {
177 return bw * w;
178 };
179 const auto fh = [=](uint_fast32_t h) {
180 return bh * h;
181 };
182 const auto ui_add_pad_gadget = [&dlg, &font](uint_fast32_t n, uint_fast32_t gx, uint_fast32_t gy, uint_fast32_t w, uint_fast32_t h) {
183 Pad[n] = ui_create_pad_gadget(dlg, gx, gy, w, h, font);
184 };
185
186 int w,h,row,col, n;
187
188 desc_x = x+2;
189 desc_y = y-17;
190
191 n=0; row = 0; col = 0; w = 1; h = 1;
192 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
193 n=1; row = 0; col = 1; w = 1; h = 1;
194 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
195 n=2; row = 0; col = 2; w = 1; h = 1;
196 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
197 n=3; row = 0; col = 3; w = 1; h = 1;
198 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
199 n=4; row = 1; col = 0; w = 1; h = 1;
200 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
201 n=5; row = 1; col = 1; w = 1; h = 1;
202 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
203 n=6; row = 1; col = 2; w = 1; h = 1;
204 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
205 n=7; row = 1; col = 3; w = 1; h = 2;
206 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
207 n=8; row = 2; col = 0; w = 1; h = 1;
208 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
209 n=9; row = 2; col = 1; w = 1; h = 1;
210 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
211 n=10; row = 2; col = 2; w = 1; h = 1;
212 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
213 n=11; row = 3; col = 0; w = 1; h = 1;
214 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
215 n=12; row = 3; col = 1; w = 1; h = 1;
216 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
217 n=13; row = 3; col = 2; w = 1; h = 1;
218 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
219 n=14; row = 3; col = 3; w = 1; h = 2;
220 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
221 n=15; row = 4; col = 0; w = 2; h = 1;
222 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
223 n=16; row = 4; col = 2; w = 1; h = 1;
224 ui_add_pad_gadget(n, fx(col), fy(row), fw(w), fh(h));
225
226 HotKey[0] = KEY_CTRLED + KEY_NUMLOCK;
227 HotKey[1] = KEY_CTRLED + KEY_PADDIVIDE;
228 HotKey[2] = KEY_CTRLED + KEY_PADMULTIPLY;
229 HotKey[3] = KEY_CTRLED + KEY_PADMINUS;
230 HotKey[4] = KEY_CTRLED + KEY_PAD7;
231 HotKey[5] = KEY_CTRLED + KEY_PAD8;
232 HotKey[6] = KEY_CTRLED + KEY_PAD9;
233 HotKey[7] = KEY_CTRLED + KEY_PADPLUS;
234 HotKey[8] = KEY_CTRLED + KEY_PAD4;
235 HotKey[9] = KEY_CTRLED + KEY_PAD5;
236 HotKey[10] = KEY_CTRLED + KEY_PAD6;
237 HotKey[11] = KEY_CTRLED + KEY_PAD1;
238 HotKey[12] = KEY_CTRLED + KEY_PAD2;
239 HotKey[13] = KEY_CTRLED + KEY_PAD3;
240 HotKey[14] = KEY_CTRLED + KEY_PADENTER;
241 HotKey[15] = KEY_CTRLED + KEY_PAD0;
242 HotKey[16] = KEY_CTRLED + KEY_PADPERIOD;
243
244 HotKey1[0] = KEY_SHIFTED + KEY_CTRLED + KEY_NUMLOCK;
245 HotKey1[1] = KEY_SHIFTED + KEY_CTRLED + KEY_PADDIVIDE;
246 HotKey1[2] = KEY_SHIFTED + KEY_CTRLED + KEY_PADMULTIPLY;
247 HotKey1[3] = KEY_SHIFTED + KEY_CTRLED + KEY_PADMINUS;
248 HotKey1[4] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD7;
249 HotKey1[5] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD8;
250 HotKey1[6] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD9;
251 HotKey1[7] = KEY_SHIFTED + KEY_CTRLED + KEY_PADPLUS;
252 HotKey1[8] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD4;
253 HotKey1[9] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD5;
254 HotKey1[10] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD6;
255 HotKey1[11] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD1;
256 HotKey1[12] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD2;
257 HotKey1[13] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD3;
258 HotKey1[14] = KEY_SHIFTED + KEY_CTRLED + KEY_PADENTER;
259 HotKey1[15] = KEY_SHIFTED + KEY_CTRLED + KEY_PAD0;
260 HotKey1[16] = KEY_SHIFTED + KEY_CTRLED + KEY_PADPERIOD;
261
262 active_pad = 0;
263
264 }
265
266
267 void ui_pad_deactivate()
268 {
269 range_for (auto &i, Pad)
270 {
271 i->text.clear();
272 }
273 }
274
275 void ui_pad_draw(UI_DIALOG *dlg, int x, int y)
276 {
277 int bh, bw;
278
279 bw = 56; bh = 30;
280
281 ui_dialog_set_current_canvas(*dlg);
282 ui_draw_box_in(*grd_curcanv, x, y, x+(bw * 4)+10 + 200, y+(bh * 5)+45);
283
284 gr_set_default_canvas();
285 auto &canvas = *grd_curcanv;
286 const auto color{CWHITE};
287 gr_urect(canvas, desc_x, desc_y, desc_x+ 56*4-1, desc_y+15, color);
288 gr_set_fontcolor(canvas, CBLACK, CWHITE);
289 gr_ustring(canvas, *canvas.cv_font, desc_x, desc_y, KeyPad[active_pad]->description.data());
290 }
291
292 namespace {
293
294 static void ui_pad_set_active( int n )
295 {
296 int np;
297 const char * name;
298
299
300 range_for (const int i, xrange(17u))
301 {
302 Pad[i]->text = KeyPad[n]->buttontext[i].data();
303 Pad[i]->user_function = NULL;
304 Pad[i]->dim_if_no_function = 1;
305 Pad[i]->hotkey = -1;
306
307 for (int j=0; j< KeyPad[n]->numkeys; j++ )
308 {
309 if (HotKey[i] == KeyPad[n]->keycode[j] )
310 {
311 Pad[i]->hotkey = HotKey[i];
312 Pad[i]->user_function = func_nget( KeyPad[n]->function_number[j], &np, &name );
313 }
314 if (HotKey1[i] == KeyPad[n]->keycode[j] )
315 {
316 Pad[i]->hotkey1 = HotKey1[i];
317 Pad[i]->user_function1 = func_nget( KeyPad[n]->function_number[j], &np, &name );
318 }
319 }
320 }
321
322 active_pad = n;
323 }
324
325 }
326
327 void ui_pad_goto(int n)
328 {
329 if ( KeyPad[n] != NULL )
330 ui_pad_set_active(n);
331 }
332
333 void ui_pad_goto_next()
334 {
335 int i, si;
336
337 i = active_pad + 1;
338 si = i;
339
340 while( KeyPad[i]==NULL )
341 {
342 i++;
343 if (i >= MAX_NUM_PADS)
344 i = 0;
345 if (i == si )
346 break;
347 }
348 ui_pad_set_active(i);
349 }
350
351 void ui_pad_goto_prev()
352 {
353 int i;
354
355 if (active_pad == -1 )
356 active_pad = MAX_NUM_PADS;
357
358 i = active_pad - 1;
359 if (i<0) i= MAX_NUM_PADS - 1;
360
361 while( KeyPad[i]==NULL )
362 {
363 i--;
364 if (i < 0)
365 i = MAX_NUM_PADS-1;
366 if (i == active_pad )
367 break;
368 }
369 ui_pad_set_active(i);
370 }
371
372 UI_KEYPAD::UI_KEYPAD() :
373 numkeys(0)
374 {
375 description.front() = 0;
376 range_for (auto &i, buttontext)
377 i[0] = 0;
378 }
379
380 int ui_pad_read( int n, const char * filename )
381 {
382 int linenumber{0};
383 int keycode, functionnumber;
384
385 auto &&[infile, physfserr] = PHYSFSX_openReadBuffered(filename);
386 if (!infile) {
387 Warning("UI pad: failed to open \"%s\": %s", filename, PHYSFS_getErrorByCode(physfserr));
388 return 0;
389 }
390 auto &kpn = *(KeyPad[n] = std::make_unique<UI_KEYPAD>());
391
392 PHYSFSX_gets_line_t<100> buffer;
393 while (linenumber < 22 && PHYSFSX_fgets(buffer, infile))
394 {
395 auto &line = buffer.line();
396 const auto lb = line.begin();
397 const auto le = line.end();
398 switch( linenumber+1 )
399 {
400 case 1:
401 kpn.description.copy_if(line);
402 break;
403 //===================== ROW 0 ==============================
404 case 3:
405 set_row<false, '\n'>(lb, le, kpn.buttontext[0], kpn.buttontext[1], kpn.buttontext[2], kpn.buttontext[3]);
406 break;
407 case 4:
408 set_row<true, '\n'>(lb, le, kpn.buttontext[0], kpn.buttontext[1], kpn.buttontext[2], kpn.buttontext[3]);
409 break;
410 case 5:
411 set_row<true, 0>(lb, le, kpn.buttontext[0], kpn.buttontext[1], kpn.buttontext[2], kpn.buttontext[3]);
412 break;
413 //===================== ROW 1 ==============================
414 case 7:
415 set_row<false, '\n'>(lb, le, kpn.buttontext[4], kpn.buttontext[5], kpn.buttontext[6], kpn.buttontext[7]);
416 break;
417 case 8:
418 set_row<true, '\n'>(lb, le, kpn.buttontext[4], kpn.buttontext[5], kpn.buttontext[6], kpn.buttontext[7]);
419 break;
420 case 9:
421 set_row<true, '\n'>(set_row<true, 0>(lb, le, kpn.buttontext[4], kpn.buttontext[5], kpn.buttontext[6]), le, kpn.buttontext[7]);
422 break;
423 case 10:
424 set_short_row(lb, le, kpn.buttontext[7]);
425 break;
426 //======================= ROW 2 ==============================
427 case 11:
428 set_row<true, '\n'>(set_row<false, '\n'>(lb, le, kpn.buttontext[8], kpn.buttontext[9], kpn.buttontext[10]), le, kpn.buttontext[7]);
429 break;
430 case 12:
431 set_row<true, '\n'>(lb, le, kpn.buttontext[8], kpn.buttontext[9], kpn.buttontext[10], kpn.buttontext[7]);
432 break;
433 case 13:
434 set_row<true, 0>(lb, le, kpn.buttontext[8], kpn.buttontext[9], kpn.buttontext[10], kpn.buttontext[7]);
435 break;
436 // ====================== ROW 3 =========================
437 case 15:
438 set_row<false, '\n'>(lb, le, kpn.buttontext[11], kpn.buttontext[12], kpn.buttontext[13], kpn.buttontext[14]);
439 break;
440 case 16:
441 set_row<true, '\n'>(lb, le, kpn.buttontext[11], kpn.buttontext[12], kpn.buttontext[13], kpn.buttontext[14]);
442 break;
443 case 17:
444 set_row<true, '\n'>(set_row<true, 0>(lb, le, kpn.buttontext[11], kpn.buttontext[12], kpn.buttontext[13]), le, kpn.buttontext[14]);
445 break;
446 case 18:
447 set_short_row(lb, le, kpn.buttontext[14]);
448 break;
449 //======================= ROW 4 =========================
450 case 19:
451 set_row<true, '\n'>(set_row<false, '\n'>(lb, le, kpn.buttontext[15], kpn.buttontext[16]), le, kpn.buttontext[14]);
452 break;
453 case 20:
454 set_row<true, '\n'>(lb, le, kpn.buttontext[15], kpn.buttontext[16], kpn.buttontext[14]);
455 break;
456 case 21:
457 set_row<true, 0>(lb, le, kpn.buttontext[15], kpn.buttontext[16], kpn.buttontext[14]);
458 break;
459 }
460
461 linenumber++;
462 }
463
464 // Get the keycodes...
465
466 for (PHYSFSX_gets_line_t<200> line_buffer; PHYSFSX_fgets(line_buffer, infile);)
467 {
468 if (!line_buffer[0])
469 continue;
470 PHYSFSX_gets_line_t<100> text;
471 sscanf(line_buffer, " %99s %99s ", text.next().data(), buffer.next().data());
472 keycode = DecodeKeyText(text);
473 functionnumber = func_get_index(buffer);
474 if (functionnumber==-1)
475 {
476 UserError( "Unknown function, %s, in %s\n", static_cast<const char *>(buffer), filename );
477 } else if (keycode==-1)
478 {
479 UserError( "Unknown keystroke, %s, in %s\n", static_cast<const char *>(text), filename );
480 //ui_messagebox( -2, -2, 1, buffer, "Ok" );
481
482 } else {
483 kpn.keycode[kpn.numkeys] = keycode;
484 kpn.function_number[kpn.numkeys] = functionnumber;
485 kpn.numkeys++;
486 }
487 }
488
489 return 1;
490 }
491
492 }