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

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