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

242 lines
8.8 KiB

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