💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
8.6 KiB

  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. #include "game/states/sound-menu-state.hpp"
  20. #include "game/states/options-menu-state.hpp"
  21. #include "game/controls.hpp"
  22. #include <engine/scene/text.hpp>
  23. #include <engine/debug/log.hpp>
  24. #include "game/menu.hpp"
  25. #include "game/strings.hpp"
  26. #include <engine/utility/hash/fnv1a.hpp>
  27. using namespace hash::literals;
  28. sound_menu_state::sound_menu_state(::game& ctx):
  29. game_state(ctx)
  30. {
  31. debug::log_trace("Entering sound menu state...");
  32. // Construct menu item texts
  33. master_volume_name_text = std::make_unique<scene::text>();
  34. master_volume_value_text = std::make_unique<scene::text>();
  35. ambience_volume_name_text = std::make_unique<scene::text>();
  36. ambience_volume_value_text = std::make_unique<scene::text>();
  37. effects_volume_name_text = std::make_unique<scene::text>();
  38. effects_volume_value_text = std::make_unique<scene::text>();
  39. mono_audio_name_text = std::make_unique<scene::text>();
  40. mono_audio_value_text = std::make_unique<scene::text>();
  41. captions_name_text = std::make_unique<scene::text>();
  42. captions_value_text = std::make_unique<scene::text>();
  43. captions_size_name_text = std::make_unique<scene::text>();
  44. captions_size_value_text = std::make_unique<scene::text>();
  45. back_text = std::make_unique<scene::text>();
  46. // Build list of menu item texts
  47. ctx.menu_item_texts.push_back({master_volume_name_text.get(), master_volume_value_text.get()});
  48. ctx.menu_item_texts.push_back({ambience_volume_name_text.get(), ambience_volume_value_text.get()});
  49. ctx.menu_item_texts.push_back({effects_volume_name_text.get(), effects_volume_value_text.get()});
  50. ctx.menu_item_texts.push_back({mono_audio_name_text.get(), mono_audio_value_text.get()});
  51. ctx.menu_item_texts.push_back({captions_name_text.get(), captions_value_text.get()});
  52. ctx.menu_item_texts.push_back({captions_size_name_text.get(), captions_size_value_text.get()});
  53. ctx.menu_item_texts.push_back({back_text.get(), nullptr});
  54. // Set content of menu item texts
  55. master_volume_name_text->set_content(get_string(ctx, "sound_menu_master_volume"));
  56. ambience_volume_name_text->set_content(get_string(ctx, "sound_menu_ambience_volume"));
  57. effects_volume_name_text->set_content(get_string(ctx, "sound_menu_effects_volume"));
  58. mono_audio_name_text->set_content(get_string(ctx, "sound_menu_mono_audio"));
  59. captions_name_text->set_content(get_string(ctx, "sound_menu_captions"));
  60. captions_size_name_text->set_content(get_string(ctx, "sound_menu_captions_size"));
  61. back_text->set_content(get_string(ctx, "back"));
  62. update_value_text_content();
  63. // Init menu item index
  64. ::menu::init_menu_item_index(ctx, "sound");
  65. ::menu::update_text_color(ctx);
  66. ::menu::update_text_font(ctx);
  67. ::menu::align_text(ctx);
  68. ::menu::add_text_to_ui(ctx);
  69. ::menu::setup_animations(ctx);
  70. // Construct menu item callbacks
  71. auto increase_volume_callback = [this, &ctx](float* volume)
  72. {
  73. // Increase volume
  74. if (ctx.menu_modifier_action.is_active())
  75. *volume += 0.01f;
  76. else
  77. *volume += 0.1f;
  78. // Limit volume
  79. if (*volume > 1.0f)
  80. *volume = 1.0f;
  81. this->update_value_text_content();
  82. ::menu::align_text(ctx);
  83. };
  84. auto decrease_volume_callback = [this, &ctx](float* volume)
  85. {
  86. // Decrease volume
  87. if (ctx.menu_modifier_action.is_active())
  88. *volume -= 0.01f;
  89. else
  90. *volume -= 0.1f;
  91. // Limit volume
  92. if (*volume < 0.0f)
  93. *volume = 0.0f;
  94. this->update_value_text_content();
  95. ::menu::align_text(ctx);
  96. };
  97. auto toggle_mono_audio_callback = [this, &ctx]()
  98. {
  99. ctx.mono_audio = !ctx.mono_audio;
  100. this->update_value_text_content();
  101. ::menu::align_text(ctx);
  102. };
  103. auto toggle_captions_callback = [this, &ctx]()
  104. {
  105. ctx.captions = !ctx.captions;
  106. this->update_value_text_content();
  107. ::menu::align_text(ctx);
  108. };
  109. auto increase_captions_size_callback = [this, &ctx]()
  110. {
  111. // Increase size
  112. if (ctx.menu_modifier_action.is_active())
  113. ctx.captions_size += 0.01f;
  114. else
  115. ctx.captions_size += 0.1f;
  116. // Limit size
  117. if (ctx.captions_size > 2.0f)
  118. ctx.captions_size = 2.0f;
  119. this->update_value_text_content();
  120. ::menu::align_text(ctx);
  121. };
  122. auto decrease_captions_size_callback = [this, &ctx]()
  123. {
  124. // Decrease size
  125. if (ctx.menu_modifier_action.is_active())
  126. ctx.captions_size -= 0.01f;
  127. else
  128. ctx.captions_size -= 0.1f;
  129. // Limit size
  130. if (ctx.captions_size < 0.1f)
  131. ctx.captions_size = 0.1f;
  132. this->update_value_text_content();
  133. ::menu::align_text(ctx);
  134. };
  135. auto select_back_callback = [&ctx]()
  136. {
  137. // Disable menu controls
  138. ctx.function_queue.push(std::bind(::disable_menu_controls, std::ref(ctx)));
  139. ::menu::fade_out
  140. (
  141. ctx,
  142. [&ctx]()
  143. {
  144. // Queue change to options menu state
  145. ctx.function_queue.push
  146. (
  147. [&ctx]()
  148. {
  149. ctx.state_machine.pop();
  150. ctx.state_machine.emplace(std::make_unique<options_menu_state>(ctx));
  151. }
  152. );
  153. }
  154. );
  155. };
  156. // Build list of menu select callbacks
  157. ctx.menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx.master_volume));
  158. ctx.menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx.ambience_volume));
  159. ctx.menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx.effects_volume));
  160. ctx.menu_select_callbacks.push_back(toggle_mono_audio_callback);
  161. ctx.menu_select_callbacks.push_back(toggle_captions_callback);
  162. ctx.menu_select_callbacks.push_back(increase_captions_size_callback);
  163. ctx.menu_select_callbacks.push_back(select_back_callback);
  164. // Build list of menu left callbacks
  165. ctx.menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx.master_volume));
  166. ctx.menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx.ambience_volume));
  167. ctx.menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx.effects_volume));
  168. ctx.menu_left_callbacks.push_back(toggle_mono_audio_callback);
  169. ctx.menu_left_callbacks.push_back(toggle_captions_callback);
  170. ctx.menu_left_callbacks.push_back(decrease_captions_size_callback);
  171. ctx.menu_left_callbacks.push_back(nullptr);
  172. // Build list of menu right callbacks
  173. ctx.menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx.master_volume));
  174. ctx.menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx.ambience_volume));
  175. ctx.menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx.effects_volume));
  176. ctx.menu_right_callbacks.push_back(toggle_mono_audio_callback);
  177. ctx.menu_right_callbacks.push_back(toggle_captions_callback);
  178. ctx.menu_right_callbacks.push_back(increase_captions_size_callback);
  179. ctx.menu_right_callbacks.push_back(nullptr);
  180. // Set menu back callback
  181. ctx.menu_back_callback = select_back_callback;
  182. // Queue menu control setup
  183. ctx.function_queue.push(std::bind(::enable_menu_controls, std::ref(ctx)));
  184. // Fade in menu
  185. ::menu::fade_in(ctx, nullptr);
  186. debug::log_trace("Entered sound menu state");
  187. }
  188. sound_menu_state::~sound_menu_state()
  189. {
  190. debug::log_trace("Exiting sound menu state...");
  191. // Destruct menu
  192. ::disable_menu_controls(ctx);
  193. ::menu::clear_callbacks(ctx);
  194. ::menu::delete_animations(ctx);
  195. ::menu::remove_text_from_ui(ctx);
  196. ::menu::delete_text(ctx);
  197. debug::log_trace("Exited sound menu state");
  198. }
  199. void sound_menu_state::update_value_text_content()
  200. {
  201. const std::string string_on = get_string(ctx, "on");
  202. const std::string string_off = get_string(ctx, "off");
  203. std::get<1>(ctx.menu_item_texts[0])->set_content(std::to_string(static_cast<int>(std::round(ctx.master_volume * 100.0f))) + "%");
  204. std::get<1>(ctx.menu_item_texts[1])->set_content(std::to_string(static_cast<int>(std::round(ctx.ambience_volume * 100.0f))) + "%");
  205. std::get<1>(ctx.menu_item_texts[2])->set_content(std::to_string(static_cast<int>(std::round(ctx.effects_volume * 100.0f))) + "%");
  206. std::get<1>(ctx.menu_item_texts[3])->set_content((ctx.mono_audio) ? string_on : string_off);
  207. std::get<1>(ctx.menu_item_texts[4])->set_content((ctx.captions) ? string_on : string_off);
  208. std::get<1>(ctx.menu_item_texts[5])->set_content(std::to_string(static_cast<int>(std::round(ctx.captions_size * 100.0f))) + "%");
  209. }