/
menu.cpp
746 lines (649 loc) · 20.6 KB
/
menu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// SuperTux
// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "gui/menu.hpp"
#include <math.h>
#include <stdexcept>
#include "control/input_manager.hpp"
#include "gui/menu_item.hpp"
#include "gui/menu_manager.hpp"
#include "gui/mousecursor.hpp"
#include "supertux/globals.hpp"
#include "supertux/resources.hpp"
#include "supertux/screen_manager.hpp"
#include "supertux/timer.hpp"
#include "util/gettext.hpp"
#include "video/drawing_context.hpp"
#include "video/font.hpp"
#include "video/renderer.hpp"
static const float MENU_REPEAT_INITIAL = 0.4f;
static const float MENU_REPEAT_RATE = 0.1f;
Menu::Menu() :
hit_item(),
pos(),
menuaction(),
delete_character(),
mn_input_char(),
menu_repeat_time(),
items(),
arrange_left(),
active_item()
{
hit_item = -1;
menuaction = MENU_ACTION_NONE;
delete_character = 0;
mn_input_char = '\0';
pos.x = SCREEN_WIDTH/2;
pos.y = SCREEN_HEIGHT/2;
arrange_left = 0;
active_item = -1;
}
Menu::~Menu()
{
}
void
Menu::set_center_pos(float x, float y)
{
pos.x = x;
pos.y = y;
}
/* Add an item to a menu */
MenuItem*
Menu::add_item(std::unique_ptr<MenuItem> new_item)
{
items.push_back(std::move(new_item));
MenuItem* item = items.back().get();
/* If a new menu is being built, the active item shouldn't be set to
* something that isn't selectable. Set the active_item to the first
* selectable item added.
*/
if (active_item == -1
&& item->kind != MN_HL
&& item->kind != MN_LABEL
&& item->kind != MN_INACTIVE)
{
active_item = items.size() - 1;
}
return item;
}
MenuItem*
Menu::add_hl()
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_HL));
return add_item(std::move(item));
}
MenuItem*
Menu::add_label(const std::string& text)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_LABEL));
item->text = text;
return add_item(std::move(item));
}
MenuItem*
Menu::add_controlfield(int id, const std::string& text,
const std::string& mapping)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_CONTROLFIELD, id));
item->change_text(text);
item->change_input(mapping);
return add_item(std::move(item));
}
MenuItem*
Menu::add_entry(int id, const std::string& text)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_ACTION, id));
item->text = text;
return add_item(std::move(item));
}
MenuItem*
Menu::add_inactive(int id, const std::string& text)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_INACTIVE, id));
item->text = text;
return add_item(std::move(item));
}
MenuItem*
Menu::add_toggle(int id, const std::string& text, bool toogled)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_TOGGLE, id));
item->text = text;
item->toggled = toogled;
return add_item(std::move(item));
}
MenuItem*
Menu::add_string_select(int id, const std::string& text)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_STRINGSELECT, id));
item->text = text;
return add_item(std::move(item));
}
MenuItem*
Menu::add_back(const std::string& text)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_BACK));
item->text = text;
return add_item(std::move(item));
}
MenuItem*
Menu::add_submenu(const std::string& text, int submenu)
{
std::unique_ptr<MenuItem> item(new MenuItem(MN_GOTO));
item->text = text;
item->target_menu = submenu;
return add_item(std::move(item));
}
void
Menu::clear()
{
items.clear();
active_item = -1;
}
/* Process actions done on the menu */
void
Menu::process_input()
{
int menu_height = (int) get_height();
if (menu_height > SCREEN_HEIGHT)
{ // Scrolling
int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
pos.y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
}
Controller* controller = g_input_manager->get_controller();
/** check main input controller... */
if(controller->pressed(Controller::UP)) {
menuaction = MENU_ACTION_UP;
menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
}
if(controller->hold(Controller::UP) &&
menu_repeat_time != 0 && real_time > menu_repeat_time) {
menuaction = MENU_ACTION_UP;
menu_repeat_time = real_time + MENU_REPEAT_RATE;
}
if(controller->pressed(Controller::DOWN)) {
menuaction = MENU_ACTION_DOWN;
menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
}
if(controller->hold(Controller::DOWN) &&
menu_repeat_time != 0 && real_time > menu_repeat_time) {
menuaction = MENU_ACTION_DOWN;
menu_repeat_time = real_time + MENU_REPEAT_RATE;
}
if(controller->pressed(Controller::LEFT)) {
menuaction = MENU_ACTION_LEFT;
menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
}
if(controller->hold(Controller::LEFT) &&
menu_repeat_time != 0 && real_time > menu_repeat_time) {
menuaction = MENU_ACTION_LEFT;
menu_repeat_time = real_time + MENU_REPEAT_RATE;
}
if(controller->pressed(Controller::RIGHT)) {
menuaction = MENU_ACTION_RIGHT;
menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
}
if(controller->hold(Controller::RIGHT) &&
menu_repeat_time != 0 && real_time > menu_repeat_time) {
menuaction = MENU_ACTION_RIGHT;
menu_repeat_time = real_time + MENU_REPEAT_RATE;
}
if(controller->pressed(Controller::ACTION)
|| controller->pressed(Controller::MENU_SELECT)) {
menuaction = MENU_ACTION_HIT;
}
if(controller->pressed(Controller::PAUSE_MENU)
|| controller->pressed(Controller::MENU_BACK)) {
menuaction = MENU_ACTION_BACK;
}
hit_item = -1;
if(items.size() == 0)
return;
int last_active_item = active_item;
switch(menuaction) {
case MENU_ACTION_UP:
do {
if (active_item > 0)
--active_item;
else
active_item = int(items.size())-1;
} while ((items[active_item]->kind == MN_HL
|| items[active_item]->kind == MN_LABEL
|| items[active_item]->kind == MN_INACTIVE)
&& (active_item != last_active_item));
break;
case MENU_ACTION_DOWN:
do {
if(active_item < int(items.size())-1 )
++active_item;
else
active_item = 0;
} while ((items[active_item]->kind == MN_HL
|| items[active_item]->kind == MN_LABEL
|| items[active_item]->kind == MN_INACTIVE)
&& (active_item != last_active_item));
break;
case MENU_ACTION_LEFT:
if(items[active_item]->kind == MN_STRINGSELECT) {
if(items[active_item]->selected > 0)
items[active_item]->selected--;
else
items[active_item]->selected = items[active_item]->list.size()-1;
menu_action(items[active_item].get());
}
break;
case MENU_ACTION_RIGHT:
if(items[active_item]->kind == MN_STRINGSELECT) {
if(items[active_item]->selected+1 < items[active_item]->list.size())
items[active_item]->selected++;
else
items[active_item]->selected = 0;
menu_action(items[active_item].get());
}
break;
case MENU_ACTION_HIT: {
hit_item = active_item;
switch (items[active_item]->kind) {
case MN_GOTO:
assert(items[active_item]->target_menu != 0);
MenuManager::instance().push_menu(items[active_item]->target_menu);
break;
case MN_TOGGLE:
items[active_item]->toggled = !items[active_item]->toggled;
menu_action(items[active_item].get());
break;
case MN_CONTROLFIELD:
menu_action(items[active_item].get());
break;
case MN_ACTION:
menu_action(items[active_item].get());
break;
case MN_STRINGSELECT:
if(items[active_item]->selected+1 < items[active_item]->list.size())
items[active_item]->selected++;
else
items[active_item]->selected = 0;
menu_action(items[active_item].get());
break;
case MN_TEXTFIELD:
case MN_NUMFIELD:
menuaction = MENU_ACTION_DOWN;
process_input();
break;
case MN_BACK:
MenuManager::instance().pop_menu();
break;
default:
break;
}
break;
}
case MENU_ACTION_REMOVE:
if(items[active_item]->kind == MN_TEXTFIELD
|| items[active_item]->kind == MN_NUMFIELD)
{
if(!items[active_item]->input.empty())
{
int i = items[active_item]->input.size();
while(delete_character > 0) /* remove characters */
{
items[active_item]->input.resize(i-1);
delete_character--;
}
}
}
break;
case MENU_ACTION_INPUT:
if(items[active_item]->kind == MN_TEXTFIELD
|| (items[active_item]->kind == MN_NUMFIELD
&& mn_input_char >= '0' && mn_input_char <= '9'))
{
items[active_item]->input.push_back(mn_input_char);
}
break;
case MENU_ACTION_BACK:
MenuManager::instance().pop_menu();
break;
case MENU_ACTION_NONE:
break;
}
menuaction = MENU_ACTION_NONE;
assert(active_item < int(items.size()));
}
int
Menu::check()
{
if (hit_item != -1)
{
int id = items[hit_item]->id;
// Clear event when checked out.. (we would end up in a loop when we try to leave "fake" submenu like Addons or Contrib)
hit_item = -1;
return id;
}
else
return -1;
}
void
Menu::menu_action(MenuItem* )
{}
void
Menu::draw_item(DrawingContext& context, int index)
{
float menu_height = get_height();
float menu_width = get_width();
MenuItem& pitem = *(items[index]);
Color text_color = default_color;
float x_pos = pos.x;
float y_pos = pos.y + 24*index - menu_height/2 + 12;
int text_width = int(Resources::normal_font->get_text_width(pitem.text));
int input_width = int(Resources::normal_font->get_text_width(pitem.input) + 10);
int list_width = 0;
float left = pos.x - menu_width/2 + 16;
float right = pos.x + menu_width/2 - 16;
if(pitem.list.size() > 0) {
list_width = (int) Resources::normal_font->get_text_width(pitem.list[pitem.selected]);
}
if (arrange_left)
x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
if(index == active_item)
{
text_color = active_color;
}
if(active_item == index)
{
float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
Vector(pos.x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
Color(1.0f, 1.0f, 1.0f, blink),
14.0f,
LAYER_GUI-10);
context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10, y_pos - 12),
Vector(pos.x + menu_width/2 - 10, y_pos + 12)),
Color(1.0f, 1.0f, 1.0f, 0.5f),
12.0f,
LAYER_GUI-10);
}
switch (pitem.kind)
{
case MN_INACTIVE:
{
context.draw_text(Resources::normal_font, pitem.text,
Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_CENTER, LAYER_GUI, inactive_color);
break;
}
case MN_HL:
{
// TODO
float x = pos.x - menu_width/2;
float y = y_pos - 12;
/* Draw a horizontal line with a little 3d effect */
context.draw_filled_rect(Vector(x, y + 6),
Vector(menu_width, 4),
Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
context.draw_filled_rect(Vector(x, y + 6),
Vector(menu_width, 2),
Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
break;
}
case MN_LABEL:
{
context.draw_text(Resources::big_font, pitem.text,
Vector(pos.x, y_pos - int(Resources::big_font->get_height()/2)),
ALIGN_CENTER, LAYER_GUI, label_color);
break;
}
case MN_TEXTFIELD:
case MN_NUMFIELD:
case MN_CONTROLFIELD:
{
if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
{
if(active_item == index)
context.draw_text(Resources::normal_font,
pitem.get_input_with_symbol(true),
Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_RIGHT, LAYER_GUI, field_color);
else
context.draw_text(Resources::normal_font,
pitem.get_input_with_symbol(false),
Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_RIGHT, LAYER_GUI, field_color);
}
else
context.draw_text(Resources::normal_font, pitem.input,
Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_RIGHT, LAYER_GUI, field_color);
context.draw_text(Resources::normal_font, pitem.text,
Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_LEFT, LAYER_GUI, text_color);
break;
}
case MN_STRINGSELECT:
{
float roff = Resources::arrow_left->get_width();
// Draw left side
context.draw_text(Resources::normal_font, pitem.text,
Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_LEFT, LAYER_GUI, text_color);
// Draw right side
context.draw_surface(Resources::arrow_left,
Vector(right - list_width - roff - roff, y_pos - 8),
LAYER_GUI);
context.draw_surface(Resources::arrow_right,
Vector(right - roff, y_pos - 8),
LAYER_GUI);
context.draw_text(Resources::normal_font, pitem.list[pitem.selected],
Vector(right - roff, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_RIGHT, LAYER_GUI, text_color);
break;
}
case MN_BACK:
{
context.draw_text(Resources::Resources::normal_font, pitem.text,
Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_CENTER, LAYER_GUI, text_color);
context.draw_surface(Resources::back,
Vector(x_pos + text_width/2 + 16, y_pos - 8),
LAYER_GUI);
break;
}
case MN_TOGGLE:
{
context.draw_text(Resources::normal_font, pitem.text,
Vector(pos.x - menu_width/2 + 16, y_pos - (Resources::normal_font->get_height()/2)),
ALIGN_LEFT, LAYER_GUI, text_color);
if(pitem.toggled)
context.draw_surface(Resources::checkbox_checked,
Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
LAYER_GUI + 1);
else
context.draw_surface(Resources::checkbox,
Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
LAYER_GUI + 1);
break;
}
case MN_ACTION:
context.draw_text(Resources::normal_font, pitem.text,
Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_CENTER, LAYER_GUI, text_color);
break;
case MN_GOTO:
context.draw_text(Resources::normal_font, pitem.text,
Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
ALIGN_CENTER, LAYER_GUI, text_color);
break;
}
}
float
Menu::get_width() const
{
/* The width of the menu has to be more than the width of the text
with the most characters */
float menu_width = 0;
for(unsigned int i = 0; i < items.size(); ++i)
{
FontPtr font = Resources::Resources::normal_font;
if(items[i]->kind == MN_LABEL)
font = Resources::big_font;
float w = font->get_text_width(items[i]->text) +
Resources::big_font->get_text_width(items[i]->input) + 16;
if(items[i]->kind == MN_TOGGLE)
w += 32;
if (items[i]->kind == MN_STRINGSELECT)
w += font->get_text_width(items[i]->list[items[i]->selected]) + 32;
if(w > menu_width)
menu_width = w;
}
return menu_width + 24;
}
float
Menu::get_height() const
{
return items.size() * 24;
}
void
Menu::on_window_resize()
{
pos.x = SCREEN_WIDTH / 2;
pos.y = SCREEN_HEIGHT / 2;
}
void
Menu::draw(DrawingContext& context)
{
if (!items[active_item]->help.empty())
{
int text_width = (int) Resources::normal_font->get_text_width(items[active_item]->help);
int text_height = (int) Resources::normal_font->get_text_height(items[active_item]->help);
Rectf text_rect(pos.x - text_width/2 - 8,
SCREEN_HEIGHT - 48 - text_height/2 - 4,
pos.x + text_width/2 + 8,
SCREEN_HEIGHT - 48 + text_height/2 + 4);
context.draw_filled_rect(Rectf(text_rect.p1 - Vector(4,4),
text_rect.p2 + Vector(4,4)),
Color(0.2f, 0.3f, 0.4f, 0.8f),
16.0f,
LAYER_GUI-10);
context.draw_filled_rect(text_rect,
Color(0.6f, 0.7f, 0.8f, 0.5f),
16.0f,
LAYER_GUI-10);
context.draw_text(Resources::normal_font, items[active_item]->help,
Vector(pos.x, SCREEN_HEIGHT - 48 - text_height/2),
ALIGN_CENTER, LAYER_GUI);
}
for(unsigned int i = 0; i < items.size(); ++i)
{
draw_item(context, i);
}
}
MenuItem&
Menu::get_item_by_id(int id)
{
for (const auto& item : items)
{
if (item->id == id)
{
return *item;
}
}
throw std::runtime_error("MenuItem not found");
}
const MenuItem&
Menu::get_item_by_id(int id) const
{
for (const auto& item : items)
{
if (item->id == id)
{
return *item;
}
}
throw std::runtime_error("MenuItem not found");
}
int Menu::get_active_item_id()
{
return items[active_item]->id;
}
bool
Menu::is_toggled(int id) const
{
return get_item_by_id(id).toggled;
}
void
Menu::set_toggled(int id, bool toggled)
{
get_item_by_id(id).toggled = toggled;
}
/* Check for menu event */
void
Menu::event(const SDL_Event& event)
{
switch(event.type) {
case SDL_MOUSEBUTTONDOWN:
if(event.button.button == SDL_BUTTON_LEFT)
{
Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
int x = int(mouse_pos.x);
int y = int(mouse_pos.y);
if(x > pos.x - get_width()/2 &&
x < pos.x + get_width()/2 &&
y > pos.y - get_height()/2 &&
y < pos.y + get_height()/2)
{
menuaction = MENU_ACTION_HIT;
}
}
break;
case SDL_MOUSEMOTION:
{
Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
float x = mouse_pos.x;
float y = mouse_pos.y;
if(x > pos.x - get_width()/2 &&
x < pos.x + get_width()/2 &&
y > pos.y - get_height()/2 &&
y < pos.y + get_height()/2)
{
int new_active_item
= static_cast<int> ((y - (pos.y - get_height()/2)) / 24);
/* only change the mouse focus to a selectable item */
if ((items[new_active_item]->kind != MN_HL)
&& (items[new_active_item]->kind != MN_LABEL)
&& (items[new_active_item]->kind != MN_INACTIVE))
active_item = new_active_item;
if(MouseCursor::current())
MouseCursor::current()->set_state(MC_LINK);
}
else
{
if(MouseCursor::current())
MouseCursor::current()->set_state(MC_NORMAL);
}
}
break;
default:
break;
}
}
void
Menu::set_active_item(int id)
{
for(size_t i = 0; i < items.size(); ++i) {
if(items[i]->id == id) {
active_item = i;
break;
}
}
}
/* EOF */