Antkeeper  0.0.1
controls-menu-state.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
24 #include "game/controls.hpp"
25 #include <engine/scene/text.hpp>
26 #include <engine/debug/log.hpp>
27 #include "game/menu.hpp"
28 #include "game/strings.hpp"
30 
31 using namespace hash::literals;
32 
34  game_state(ctx)
35 {
36  debug::log_trace("Entering controls menu state...");
37 
38  // Construct menu item texts
39  keyboard_text = std::make_unique<scene::text>();
40  gamepad_text = std::make_unique<scene::text>();
41  back_text = std::make_unique<scene::text>();
42 
43  // Build list of menu item texts
44  ctx.menu_item_texts.push_back({keyboard_text.get(), nullptr});
45  ctx.menu_item_texts.push_back({gamepad_text.get(), nullptr});
46  ctx.menu_item_texts.push_back({back_text.get(), nullptr});
47 
48  // Set content of menu item texts
49  keyboard_text->set_content(get_string(ctx, "controls_menu_keyboard"));
50  gamepad_text->set_content(get_string(ctx, "controls_menu_gamepad"));
51  back_text->set_content(get_string(ctx, "back"));
52 
53  // Init menu item index
54  ::menu::init_menu_item_index(ctx, "controls");
55 
58  ::menu::align_text(ctx, true);
61 
62  // Construct menu item callbacks
63  auto select_keyboard_callback = [&ctx]()
64  {
65  // Disable menu controls
66  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
67 
69  (
70  ctx,
71  [&ctx]()
72  {
73  // Queue change to keyboard config menu state
74  ctx.function_queue.push
75  (
76  [&ctx]()
77  {
78  ctx.state_machine.pop();
79  ctx.state_machine.emplace(std::make_unique<keyboard_config_menu_state>(ctx));
80  }
81  );
82  }
83  );
84  };
85  auto select_gamepad_callback = [&ctx]()
86  {
87  // Disable menu controls
88  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
89 
91  (
92  ctx,
93  [&ctx]()
94  {
95  // Queue change to gamepad config menu state
96  ctx.function_queue.push
97  (
98  [&ctx]()
99  {
100  ctx.state_machine.pop();
101  ctx.state_machine.emplace(std::make_unique<gamepad_config_menu_state>(ctx));
102  }
103  );
104  }
105  );
106  };
107  auto select_back_callback = [&ctx]()
108  {
109  // Disable menu controls
110  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
111 
113  (
114  ctx,
115  [&ctx]()
116  {
117  // Queue change to options menu state
118  ctx.function_queue.push
119  (
120  [&ctx]()
121  {
122  ctx.state_machine.pop();
123  ctx.state_machine.emplace(std::make_unique<options_menu_state>(ctx));
124  }
125  );
126  }
127  );
128  };
129 
130  // Build list of menu select callbacks
131  ctx.menu_select_callbacks.push_back(select_keyboard_callback);
132  ctx.menu_select_callbacks.push_back(select_gamepad_callback);
133  ctx.menu_select_callbacks.push_back(select_back_callback);
134 
135  // Build list of menu left callbacks
136  ctx.menu_left_callbacks.push_back(nullptr);
137  ctx.menu_left_callbacks.push_back(nullptr);
138  ctx.menu_left_callbacks.push_back(nullptr);
139 
140  // Build list of menu right callbacks
141  ctx.menu_right_callbacks.push_back(nullptr);
142  ctx.menu_right_callbacks.push_back(nullptr);
143  ctx.menu_right_callbacks.push_back(nullptr);
144 
145  // Set menu back callback
146  ctx.menu_back_callback = select_back_callback;
147 
148  // Queue menu control setup
149  ctx.function_queue.push(std::bind(::enable_menu_controls, std::ref(ctx)));
150 
151  // Fade in menu
152  ::menu::fade_in(ctx, nullptr);
153 
154  debug::log_trace("Entered controls menu state");
155 }
156 
158 {
159  debug::log_trace("Exiting options menu state...");
160 
161  // Destruct menu
167 
168  debug::log_trace("Exited controls menu state");
169 }
controls_menu_state(::game &ctx)
Abstract base class for game states.
Definition: game-state.hpp:29
::game & ctx
Definition: game-state.hpp:44
Definition: game.hpp:121
std::vector< std::function< void()> > menu_left_callbacks
Definition: game.hpp:348
std::vector< std::function< void()> > menu_right_callbacks
Definition: game.hpp:349
hsm::state_machine< game_state > state_machine
Definition: game.hpp:299
std::vector< std::function< void()> > menu_select_callbacks
Definition: game.hpp:347
std::vector< std::tuple< scene::text *, scene::text * > > menu_item_texts
Definition: game.hpp:351
std::queue< std::function< void()> > function_queue
Definition: game.hpp:303
std::function< void()> menu_back_callback
Definition: game.hpp:350
void enable_menu_controls(::game &ctx)
void disable_menu_controls(::game &ctx)
log_message< log_message_severity::trace, Args... > log_trace
Formats and logs a trace message.
Definition: log.hpp:88
User-defined literals for compile-time string hashing.
Definition: fnv1a.hpp:232
void init_menu_item_index(::game &ctx, hash::fnv1a32_t menu_name)
Definition: menu.cpp:31
void update_text_color(::game &ctx)
Definition: menu.cpp:59
void clear_callbacks(::game &ctx)
Definition: menu.cpp:197
void delete_animations(::game &ctx)
Definition: menu.cpp:191
void align_text(::game &ctx, bool center, bool has_back, float anchor_y)
Definition: menu.cpp:73
void setup_animations(::game &ctx)
Definition: menu.cpp:206
void add_text_to_ui(::game &ctx)
Definition: menu.cpp:166
void fade_out(::game &ctx, const std::function< void()> &end_callback)
Definition: menu.cpp:263
void update_text_font(::game &ctx)
Definition: menu.cpp:44
void fade_in(::game &ctx, const std::function< void()> &end_callback)
Definition: menu.cpp:233
void remove_text_from_ui(::game &ctx)
Definition: menu.cpp:176
void delete_text(::game &ctx)
Definition: menu.cpp:186
std::string get_string(const ::game &ctx, hash::fnv1a32_t key)
Returns a localized string.
Definition: strings.cpp:23