Skip to content

Commit 0f6c025

Browse files
more generic menu info handling
1 parent a3426e8 commit 0f6c025

File tree

5 files changed

+96
-24
lines changed

5 files changed

+96
-24
lines changed

game/src/engine.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,9 @@ void Engine::key_tapped(SDL &sdl, GameKey k) {
11191119
case MenuState::start:
11201120
mainMenu.key_tapped(k);
11211121
break;
1122+
case MenuState::editor_menu:
1123+
scenarioMenu.key_tapped(k);
1124+
break;
11221125
case MenuState::singleplayer_game:
11231126
case MenuState::multiplayer_game:
11241127
kbp_game(k);
@@ -1133,8 +1136,14 @@ void Engine::kbp_down(GameKey k) {
11331136
if (k >= GameKey::max)
11341137
return;
11351138

1136-
if (menu_state == MenuState::start)
1139+
switch (menu_state) {
1140+
case MenuState::start:
11371141
mainMenu.key_down(k, keyctl, sfx);
1142+
break;
1143+
case MenuState::editor_menu:
1144+
scenarioMenu.key_down(k, keyctl, sfx);
1145+
break;
1146+
}
11381147
}
11391148

11401149
void Engine::eventloop(SDL &sdl, gfx::GLprogram &prog, GLuint vao) {
@@ -1150,6 +1159,8 @@ void Engine::eventloop(SDL &sdl, gfx::GLprogram &prog, GLuint vao) {
11501159
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
11511160
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
11521161
SDL_Event event;
1162+
const MenuInfo *mi = HasMenuInfo(menu_state);
1163+
11531164
while (SDL_PollEvent(&event)) {
11541165
switch (event.type) {
11551166
case SDL_QUIT:
@@ -1168,23 +1179,23 @@ void Engine::eventloop(SDL &sdl, gfx::GLprogram &prog, GLuint vao) {
11681179
case SDL_KEYDOWN:
11691180
ImGui_ImplSDL2_ProcessEvent(&event);
11701181

1171-
if (menu_state == MenuState::start || (capture_keys() && !io.WantCaptureKeyboard))
1182+
if ((mi && mi->menu) || (capture_keys() && !io.WantCaptureKeyboard))
11721183
kbp_down(keyctl.down(event.key));
11731184
break;
11741185
case SDL_MOUSEBUTTONDOWN:
11751186
ImGui_ImplSDL2_ProcessEvent(&event);
11761187

1177-
if (menu_state == MenuState::start) {
1178-
if (event.button.button = SDL_BUTTON_LEFT)
1179-
mainMenu.mouse_down(event.button.x, event.button.y, sfx);
1188+
if (mi && mi->menu) {
1189+
if (event.button.button == SDL_BUTTON_LEFT)
1190+
mi->menu->mouse_down(event.button.x, event.button.y, sfx);
11801191
}
11811192
break;
11821193
case SDL_MOUSEBUTTONUP:
11831194
ImGui_ImplSDL2_ProcessEvent(&event);
11841195

1185-
if (menu_state == MenuState::start) {
1186-
if (event.button.button = SDL_BUTTON_LEFT)
1187-
mainMenu.mouse_up(event.button.x, event.button.y);
1196+
if (mi && mi->menu) {
1197+
if (event.button.button == SDL_BUTTON_LEFT)
1198+
mi->menu->mouse_up(event.button.x, event.button.y);
11881199
}
11891200
break;
11901201
default:

game/src/engine/menu.cpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#include "menu.hpp"
22
#include "../engine.hpp"
33

4+
#include "../ui/fullscreenmenu.hpp"
5+
46
namespace aoe {
57

68
using namespace io;
79

810
const MenuInfo menu_info[] = {
911
{ false, KeyboardMode::configure, DrsId::bkg_main_menu }, // init
10-
{ true, KeyboardMode::fullscreen_menu, DrsId::bkg_main_menu }, // start
12+
{ true, KeyboardMode::fullscreen_menu, DrsId::bkg_main_menu, &mainMenu }, // start
1113
{ true, KeyboardMode::other, DrsId::bkg_singleplayer },
1214
{ true, KeyboardMode::other, DrsId::bkg_singleplayer },
1315
{ false, KeyboardMode::other, (DrsId)0 }, // sp game
@@ -17,7 +19,7 @@ const MenuInfo menu_info[] = {
1719
{ false, KeyboardMode::other, (DrsId)0 }, // mp settings
1820
{ true, KeyboardMode::other, DrsId::bkg_defeat },
1921
{ true, KeyboardMode::other, DrsId::bkg_victory },
20-
{ true, KeyboardMode::other, DrsId::bkg_editor_menu }, // edit menu
22+
{ true, KeyboardMode::fullscreen_menu, DrsId::bkg_editor_menu, &scenarioMenu }, // edit menu
2123
{ false, KeyboardMode::other, (DrsId)0 }, // edit scn
2224
};
2325

@@ -39,11 +41,15 @@ enum class MainMenuButtonIdx {
3941
quit,
4042
};
4143

42-
void MenuButtonActivate(MenuState state, unsigned idx)
43-
{
44-
if (state != MenuState::start)
45-
return;
44+
enum class EditorMenuButtonIdx {
45+
create_scenario,
46+
load_scenario,
47+
campaign_editor,
48+
cancel
49+
};
4650

51+
void StartMenuButtonActivate(unsigned idx)
52+
{
4753
MainMenuButtonIdx btn = (MainMenuButtonIdx)idx;
4854

4955
switch (btn) {
@@ -64,4 +70,30 @@ void MenuButtonActivate(MenuState state, unsigned idx)
6470
}
6571
}
6672

73+
void EditorMenuButtonActivate(unsigned idx)
74+
{
75+
EditorMenuButtonIdx btn = (EditorMenuButtonIdx)idx;
76+
77+
switch (btn) {
78+
case EditorMenuButtonIdx::create_scenario:
79+
next_menu_state = MenuState::editor_scenario;
80+
break;
81+
default:
82+
next_menu_state = MenuState::start;
83+
break;
84+
}
85+
}
86+
87+
void MenuButtonActivate(MenuState state, unsigned idx)
88+
{
89+
switch (state) {
90+
case MenuState::start:
91+
StartMenuButtonActivate(idx);
92+
break;
93+
case MenuState::editor_menu:
94+
EditorMenuButtonActivate(idx);
95+
break;
96+
}
97+
}
98+
6799
}

game/src/engine/menu.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
namespace aoe {
77

8+
namespace ui {
9+
10+
class FullscreenMenu;
11+
12+
}
13+
814
enum class MenuState {
915
init,
1016
start,
@@ -26,6 +32,7 @@ struct MenuInfo final {
2632
bool draw_border;
2733
KeyboardMode keyboard_mode;
2834
io::DrsId border_col;
35+
ui::FullscreenMenu *menu;
2936
};
3037

3138
extern const MenuInfo menu_info[(unsigned)MenuState::max];

game/src/ui.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ void Engine::show_multiplayer_host() {
637637
}
638638
}
639639

640+
static void DrawScenarioMenu(Frame &f, Audio &sfx, Assets &ass);
641+
640642
void UICache::show_editor_menu() {
641643
ZoneScoped;
642644
ImGuiViewport *vp = ImGui::GetMainViewport();
@@ -658,6 +660,7 @@ void UICache::show_editor_menu() {
658660
f.str2("Scenario Editor", TextHalign::center);
659661
}
660662

663+
#if 0
661664
FontGuard fg(fnt.copper);
662665

663666
ImGui::SetCursorPosY(272.0f / 768.0f * vp->WorkSize.y);
@@ -678,7 +681,9 @@ void UICache::show_editor_menu() {
678681
if (btn(f, "Cancel", TextHalign::center, e->sfx))
679682
next_menu_state = MenuState::start;
680683

681-
ImGui::SetCursorPosX(old_x);
684+
#else
685+
DrawScenarioMenu(f, e->sfx, *e->assets.get());
686+
#endif
682687
}
683688

684689

@@ -1049,9 +1054,32 @@ MenuButton mainMenuButtons[] = {
10491054
{604, "Exit"},
10501055
};
10511056

1057+
MenuButton scenarioMenuButtons[] = {
1058+
{272, "Create Scenario"},
1059+
{352, "Edit Scenario"},
1060+
{432, "Campaign Editor"},
1061+
{512, "Cancel"},
1062+
};
1063+
10521064
static void DrawMainMenu(Frame &f, Audio &sfx, Assets &ass);
1065+
static void DrawScenarioMenu(Frame &f, Audio &sfx, Assets &ass);
10531066

10541067
FullscreenMenu mainMenu(MenuState::start, mainMenuButtons, ARRAY_SIZE(mainMenuButtons), DrawMainMenu);
1068+
FullscreenMenu scenarioMenu(MenuState::editor_menu, scenarioMenuButtons, ARRAY_SIZE(scenarioMenuButtons), DrawScenarioMenu);
1069+
1070+
static void DrawScenarioMenu(Frame &f, Audio &sfx, Assets &ass)
1071+
{
1072+
FullscreenMenu &mm = scenarioMenu;
1073+
ImGuiViewport *vp = ImGui::GetMainViewport();
1074+
1075+
FontGuard fg(fnt.copper);
1076+
mm.reshape(vp);
1077+
1078+
BackgroundColors col = ass.bkg_cols.at(io::DrsId::bkg_editor_menu);
1079+
1080+
for (unsigned i = 0, n = mm.buttonCount; i < n; ++i)
1081+
mm.buttons[i].show(f, sfx, col);
1082+
}
10551083

10561084
static void DrawMainMenu(Frame &f, Audio &sfx, Assets &ass)
10571085
{
@@ -1063,14 +1091,8 @@ static void DrawMainMenu(Frame &f, Audio &sfx, Assets &ass)
10631091

10641092
BackgroundColors col = ass.bkg_cols.at(io::DrsId::bkg_main_menu);
10651093

1066-
bool activated = false;
1067-
1068-
for (unsigned i = 0, n = mm.buttonCount; i < n; ++i) {
1069-
if (mm.buttons[i].show(f, sfx, col) && !activated) {
1070-
activated = true;
1071-
MenuButtonActivate(MenuState::start, i);
1072-
}
1073-
}
1094+
for (unsigned i = 0, n = mm.buttonCount; i < n; ++i)
1095+
mm.buttons[i].show(f, sfx, col);
10741096
}
10751097

10761098
void Engine::show_start() {

game/src/ui/fullscreenmenu.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class FullscreenMenu final {
7878

7979
void MenuButtonActivate(MenuState state, unsigned idx);
8080

81-
extern ui::FullscreenMenu mainMenu;
81+
extern ui::FullscreenMenu mainMenu, scenarioMenu;
8282

8383
}
8484

0 commit comments

Comments
 (0)