Antkeeper  0.0.1
sound-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 
22 #include "game/controls.hpp"
23 #include <engine/scene/text.hpp>
24 #include <engine/debug/log.hpp>
25 #include "game/menu.hpp"
26 #include "game/strings.hpp"
28 
29 using namespace hash::literals;
30 
31 
33  game_state(ctx)
34 {
35  debug::log_trace("Entering sound menu state...");
36 
37  // Construct menu item texts
38  master_volume_name_text = std::make_unique<scene::text>();
39  master_volume_value_text = std::make_unique<scene::text>();
40  ambience_volume_name_text = std::make_unique<scene::text>();
41  ambience_volume_value_text = std::make_unique<scene::text>();
42  effects_volume_name_text = std::make_unique<scene::text>();
43  effects_volume_value_text = std::make_unique<scene::text>();
44  mono_audio_name_text = std::make_unique<scene::text>();
45  mono_audio_value_text = std::make_unique<scene::text>();
46  captions_name_text = std::make_unique<scene::text>();
47  captions_value_text = std::make_unique<scene::text>();
48  captions_size_name_text = std::make_unique<scene::text>();
49  captions_size_value_text = std::make_unique<scene::text>();
50  back_text = std::make_unique<scene::text>();
51 
52  // Build list of menu item texts
53  ctx.menu_item_texts.push_back({master_volume_name_text.get(), master_volume_value_text.get()});
54  ctx.menu_item_texts.push_back({ambience_volume_name_text.get(), ambience_volume_value_text.get()});
55  ctx.menu_item_texts.push_back({effects_volume_name_text.get(), effects_volume_value_text.get()});
56  ctx.menu_item_texts.push_back({mono_audio_name_text.get(), mono_audio_value_text.get()});
57  ctx.menu_item_texts.push_back({captions_name_text.get(), captions_value_text.get()});
58  ctx.menu_item_texts.push_back({captions_size_name_text.get(), captions_size_value_text.get()});
59  ctx.menu_item_texts.push_back({back_text.get(), nullptr});
60 
61  // Set content of menu item texts
62  master_volume_name_text->set_content(get_string(ctx, "sound_menu_master_volume"));
63  ambience_volume_name_text->set_content(get_string(ctx, "sound_menu_ambience_volume"));
64  effects_volume_name_text->set_content(get_string(ctx, "sound_menu_effects_volume"));
65  mono_audio_name_text->set_content(get_string(ctx, "sound_menu_mono_audio"));
66  captions_name_text->set_content(get_string(ctx, "sound_menu_captions"));
67  captions_size_name_text->set_content(get_string(ctx, "sound_menu_captions_size"));
68  back_text->set_content(get_string(ctx, "back"));
69  update_value_text_content();
70 
71  // Init menu item index
73 
79 
80  // Construct menu item callbacks
81  auto increase_volume_callback = [this, &ctx](float* volume)
82  {
83  // Increase volume
85  *volume += 0.01f;
86  else
87  *volume += 0.1f;
88 
89  // Limit volume
90  if (*volume > 1.0f)
91  *volume = 1.0f;
92 
93  this->update_value_text_content();
95  };
96  auto decrease_volume_callback = [this, &ctx](float* volume)
97  {
98  // Decrease volume
100  *volume -= 0.01f;
101  else
102  *volume -= 0.1f;
103 
104  // Limit volume
105  if (*volume < 0.0f)
106  *volume = 0.0f;
107 
108  this->update_value_text_content();
110  };
111 
112  auto toggle_mono_audio_callback = [this, &ctx]()
113  {
115 
116  this->update_value_text_content();
118  };
119 
120  auto toggle_captions_callback = [this, &ctx]()
121  {
123 
124  this->update_value_text_content();
126  };
127 
128  auto increase_captions_size_callback = [this, &ctx]()
129  {
130  // Increase size
132  ctx.captions_size += 0.01f;
133  else
134  ctx.captions_size += 0.1f;
135 
136  // Limit size
137  if (ctx.captions_size > 2.0f)
138  ctx.captions_size = 2.0f;
139 
140  this->update_value_text_content();
142  };
143 
144  auto decrease_captions_size_callback = [this, &ctx]()
145  {
146  // Decrease size
148  ctx.captions_size -= 0.01f;
149  else
150  ctx.captions_size -= 0.1f;
151 
152  // Limit size
153  if (ctx.captions_size < 0.1f)
154  ctx.captions_size = 0.1f;
155 
156  this->update_value_text_content();
158  };
159  auto select_back_callback = [&ctx]()
160  {
161  // Disable menu controls
162  ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
163 
165  (
166  ctx,
167  [&ctx]()
168  {
169  // Queue change to options menu state
170  ctx.function_queue.push
171  (
172  [&ctx]()
173  {
174  ctx.state_machine.pop();
175  ctx.state_machine.emplace(std::make_unique<options_menu_state>(ctx));
176  }
177  );
178  }
179  );
180  };
181 
182  // Build list of menu select callbacks
183  ctx.menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx.master_volume));
184  ctx.menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx.ambience_volume));
185  ctx.menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx.effects_volume));
186  ctx.menu_select_callbacks.push_back(toggle_mono_audio_callback);
187  ctx.menu_select_callbacks.push_back(toggle_captions_callback);
188  ctx.menu_select_callbacks.push_back(increase_captions_size_callback);
189  ctx.menu_select_callbacks.push_back(select_back_callback);
190 
191  // Build list of menu left callbacks
192  ctx.menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx.master_volume));
193  ctx.menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx.ambience_volume));
194  ctx.menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx.effects_volume));
195  ctx.menu_left_callbacks.push_back(toggle_mono_audio_callback);
196  ctx.menu_left_callbacks.push_back(toggle_captions_callback);
197  ctx.menu_left_callbacks.push_back(decrease_captions_size_callback);
198  ctx.menu_left_callbacks.push_back(nullptr);
199 
200  // Build list of menu right callbacks
201  ctx.menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx.master_volume));
202  ctx.menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx.ambience_volume));
203  ctx.menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx.effects_volume));
204  ctx.menu_right_callbacks.push_back(toggle_mono_audio_callback);
205  ctx.menu_right_callbacks.push_back(toggle_captions_callback);
206  ctx.menu_right_callbacks.push_back(increase_captions_size_callback);
207  ctx.menu_right_callbacks.push_back(nullptr);
208 
209  // Set menu back callback
210  ctx.menu_back_callback = select_back_callback;
211 
212  // Queue menu control setup
213  ctx.function_queue.push(std::bind(::enable_menu_controls, std::ref(ctx)));
214 
215  // Fade in menu
216  ::menu::fade_in(ctx, nullptr);
217 
218  debug::log_trace("Entered sound menu state");
219 }
220 
222 {
223  debug::log_trace("Exiting sound menu state...");
224 
225  // Destruct menu
231 
232  debug::log_trace("Exited sound menu state");
233 }
234 
235 void sound_menu_state::update_value_text_content()
236 {
237  const std::string string_on = get_string(ctx, "on");
238  const std::string string_off = get_string(ctx, "off");
239 
240  std::get<1>(ctx.menu_item_texts[0])->set_content(std::to_string(static_cast<int>(std::round(ctx.master_volume * 100.0f))) + "%");
241  std::get<1>(ctx.menu_item_texts[1])->set_content(std::to_string(static_cast<int>(std::round(ctx.ambience_volume * 100.0f))) + "%");
242  std::get<1>(ctx.menu_item_texts[2])->set_content(std::to_string(static_cast<int>(std::round(ctx.effects_volume * 100.0f))) + "%");
243  std::get<1>(ctx.menu_item_texts[3])->set_content((ctx.mono_audio) ? string_on : string_off);
244  std::get<1>(ctx.menu_item_texts[4])->set_content((ctx.captions) ? string_on : string_off);
245  std::get<1>(ctx.menu_item_texts[5])->set_content(std::to_string(static_cast<int>(std::round(ctx.captions_size * 100.0f))) + "%");
246 }
Abstract base class for game states.
Definition: game-state.hpp:29
::game & ctx
Definition: game-state.hpp:44
Definition: game.hpp:121
bool mono_audio
Definition: game.hpp:384
float ambience_volume
Definition: game.hpp:382
std::vector< std::function< void()> > menu_left_callbacks
Definition: game.hpp:348
input::action menu_modifier_action
Definition: game.hpp:211
float captions_size
Definition: game.hpp:386
std::vector< std::function< void()> > menu_right_callbacks
Definition: game.hpp:349
hsm::state_machine< game_state > state_machine
Definition: game.hpp:299
float master_volume
Definition: game.hpp:381
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
float effects_volume
Definition: game.hpp:383
std::queue< std::function< void()> > function_queue
Definition: game.hpp:303
bool captions
Definition: game.hpp:385
std::function< void()> menu_back_callback
Definition: game.hpp:350
bool is_active() const noexcept
Returns true if the action is active, false otherwise.
Definition: action.hpp:79
sound_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
constexpr vector< T, N > round(const vector< T, N > &x)
Performs a element-wise round operation.
Definition: vector.hpp:1489
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