Antkeeper  0.0.1
options-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 
27 #include "game/menu.hpp"
28 #include "game/controls.hpp"
32 #include <engine/scene/text.hpp>
33 #include <engine/debug/log.hpp>
34 #include "game/strings.hpp"
36 
37 using namespace hash::literals;
38 
40  game_state(ctx)
41 {
42  debug::log_trace("Entering options menu state...");
43 
44  // Construct menu item texts
45  controls_text = std::make_unique<scene::text>();
46  graphics_text = std::make_unique<scene::text>();
47  sound_text = std::make_unique<scene::text>();
48  language_text = std::make_unique<scene::text>();
49  back_text = std::make_unique<scene::text>();
50 
51  // Set content of menu item texts
52  controls_text->set_content(get_string(ctx, "options_menu_controls"));
53  graphics_text->set_content(get_string(ctx, "options_menu_graphics"));
54  sound_text->set_content(get_string(ctx, "options_menu_sound"));
55  language_text->set_content(get_string(ctx, "options_menu_language"));
56  back_text->set_content(get_string(ctx, "back"));
57 
58  // Build list of menu item texts
59  ctx.menu_item_texts.push_back({controls_text.get(), nullptr});
60  ctx.menu_item_texts.push_back({graphics_text.get(), nullptr});
61  ctx.menu_item_texts.push_back({sound_text.get(), nullptr});
62  ctx.menu_item_texts.push_back({language_text.get(), nullptr});
63  ctx.menu_item_texts.push_back({back_text.get(), nullptr});
64 
65  // Init menu item index
67 
70  ::menu::align_text(ctx, true);
73 
74  // Construct menu item callbacks
75  auto select_controls_callback = [&ctx]()
76  {
77  // Disable menu controls
78  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
79 
80  // Return to main menu
82  (
83  ctx,
84  [&ctx]()
85  {
86  // Queue change to controls menu state
87  ctx.function_queue.push
88  (
89  [&ctx]()
90  {
91  ctx.state_machine.pop();
92  ctx.state_machine.emplace(std::make_unique<controls_menu_state>(ctx));
93  }
94  );
95  }
96  );
97  };
98  auto select_graphics_callback = [&ctx]()
99  {
100  // Disable menu controls
101  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
102 
103  // Return to main menu
105  (
106  ctx,
107  [&ctx]()
108  {
109  // Queue change to graphics menu state
110  ctx.function_queue.push
111  (
112  [&ctx]()
113  {
114  ctx.state_machine.pop();
115  ctx.state_machine.emplace(std::make_unique<graphics_menu_state>(ctx));
116  }
117  );
118  }
119  );
120  };
121  auto select_sound_callback = [&ctx]()
122  {
123  // Disable menu controls
124  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
125 
126  // Return to main menu
128  (
129  ctx,
130  [&ctx]()
131  {
132  // Queue change to sound menu state
133  ctx.function_queue.push
134  (
135  [&ctx]()
136  {
137  ctx.state_machine.pop();
138  ctx.state_machine.emplace(std::make_unique<sound_menu_state>(ctx));
139  }
140  );
141  }
142  );
143  };
144  auto select_language_callback = [&ctx]()
145  {
146  // Disable menu controls
147  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
148 
149  // Return to main menu
151  (
152  ctx,
153  [&ctx]()
154  {
155  // Queue change to language menu state
156  ctx.function_queue.push
157  (
158  [&ctx]()
159  {
160  ctx.state_machine.pop();
161  ctx.state_machine.emplace(std::make_unique<language_menu_state>(ctx));
162  }
163  );
164  }
165  );
166  };
167  auto select_back_callback = [&ctx]()
168  {
169  // Disable menu controls
170  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
171 
172  // Save config
173  //::save::config(ctx);
174 
176  (
177  ctx,
178  [&ctx]()
179  {
180  // Queue change to main menu state
181  ctx.function_queue.push
182  (
183  [&ctx]()
184  {
185  ctx.state_machine.pop();
186  if (ctx.resume_callback)
187  ctx.state_machine.emplace(std::make_unique<pause_menu_state>(ctx));
188  else
189  ctx.state_machine.emplace(std::make_unique<main_menu_state>(ctx, false));
190  }
191  );
192  }
193  );
194  };
195 
196  // Build list of menu select callbacks
197  ctx.menu_select_callbacks.push_back(select_controls_callback);
198  ctx.menu_select_callbacks.push_back(select_graphics_callback);
199  ctx.menu_select_callbacks.push_back(select_sound_callback);
200  ctx.menu_select_callbacks.push_back(select_language_callback);
201  ctx.menu_select_callbacks.push_back(select_back_callback);
202 
203  // Build list of menu right callbacks
204  ctx.menu_right_callbacks.resize(5, nullptr);
205 
206  // Build list of menu left callbacks
207  ctx.menu_left_callbacks.resize(5, nullptr);
208 
209  // Set menu back callback
210  ctx.menu_back_callback = select_back_callback;
211 
212  // Fade in menu
213  ::menu::fade_in(ctx, nullptr);
214 
215  // Queue enable menu controls
216  ctx.function_queue.push(std::bind(::enable_menu_controls, std::ref(ctx)));
217 
218  debug::log_trace("Entered options menu state");
219 }
220 
222 {
223  debug::log_trace("Exiting options menu state...");
224 
225  // Destruct menu
231 
232  debug::log_trace("Exited options menu state");
233 }
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
std::function< void()> resume_callback
Definition: game.hpp:300
options_menu_state(::game &ctx)
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