💿🐜 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.

262 lines
9.1 KiB

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