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

254 lines
9.1 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 "debug/logger.hpp"
  24. #include "game/menu.hpp"
  25. #include "animation/timeline.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. // Construct menu item texts
  43. scene::text* master_volume_name_text = new scene::text();
  44. scene::text* master_volume_value_text = new scene::text();
  45. scene::text* ambience_volume_name_text = new scene::text();
  46. scene::text* ambience_volume_value_text = new scene::text();
  47. scene::text* effects_volume_name_text = new scene::text();
  48. scene::text* effects_volume_value_text = new scene::text();
  49. scene::text* mono_audio_name_text = new scene::text();
  50. scene::text* mono_audio_value_text = new scene::text();
  51. scene::text* captions_name_text = new scene::text();
  52. scene::text* captions_value_text = new scene::text();
  53. scene::text* captions_size_name_text = new scene::text();
  54. scene::text* captions_size_value_text = new scene::text();
  55. scene::text* back_text = new scene::text();
  56. // Build list of menu item texts
  57. ctx->menu_item_texts.push_back({master_volume_name_text, master_volume_value_text});
  58. ctx->menu_item_texts.push_back({ambience_volume_name_text, ambience_volume_value_text});
  59. ctx->menu_item_texts.push_back({effects_volume_name_text, effects_volume_value_text});
  60. ctx->menu_item_texts.push_back({mono_audio_name_text, mono_audio_value_text});
  61. ctx->menu_item_texts.push_back({captions_name_text, captions_value_text});
  62. ctx->menu_item_texts.push_back({captions_size_name_text, captions_size_value_text});
  63. ctx->menu_item_texts.push_back({back_text, nullptr});
  64. // Set content of menu item texts
  65. master_volume_name_text->set_content((*ctx->strings)["sound_menu_master_volume"]);
  66. ambience_volume_name_text->set_content((*ctx->strings)["sound_menu_ambience_volume"]);
  67. effects_volume_name_text->set_content((*ctx->strings)["sound_menu_effects_volume"]);
  68. mono_audio_name_text->set_content((*ctx->strings)["sound_menu_mono_audio"]);
  69. captions_name_text->set_content((*ctx->strings)["sound_menu_captions"]);
  70. captions_size_name_text->set_content((*ctx->strings)["sound_menu_captions_size"]);
  71. back_text->set_content((*ctx->strings)["back"]);
  72. update_value_text_content(ctx);
  73. // Init menu item index
  74. game::menu::init_menu_item_index(ctx, "sound");
  75. game::menu::update_text_color(ctx);
  76. game::menu::update_text_font(ctx);
  77. game::menu::align_text(ctx);
  78. game::menu::update_text_tweens(ctx);
  79. game::menu::add_text_to_ui(ctx);
  80. game::menu::setup_animations(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. // Disable controls
  155. game::menu::clear_controls(ctx);
  156. game::menu::fade_out
  157. (
  158. ctx,
  159. [ctx]()
  160. {
  161. application::state next_state;
  162. next_state.name = "options_menu";
  163. next_state.enter = std::bind(game::state::options_menu::enter, ctx);
  164. next_state.exit = std::bind(game::state::options_menu::exit, ctx);
  165. ctx->app->queue_state(next_state);
  166. }
  167. );
  168. };
  169. // Build list of menu select callbacks
  170. ctx->menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx->master_volume));
  171. ctx->menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx->ambience_volume));
  172. ctx->menu_select_callbacks.push_back(std::bind(increase_volume_callback, &ctx->effects_volume));
  173. ctx->menu_select_callbacks.push_back(toggle_mono_audio_callback);
  174. ctx->menu_select_callbacks.push_back(toggle_captions_callback);
  175. ctx->menu_select_callbacks.push_back(increase_captions_size_callback);
  176. ctx->menu_select_callbacks.push_back(select_back_callback);
  177. // Build list of menu left callbacks
  178. ctx->menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx->master_volume));
  179. ctx->menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx->ambience_volume));
  180. ctx->menu_left_callbacks.push_back(std::bind(decrease_volume_callback, &ctx->effects_volume));
  181. ctx->menu_left_callbacks.push_back(toggle_mono_audio_callback);
  182. ctx->menu_left_callbacks.push_back(toggle_captions_callback);
  183. ctx->menu_left_callbacks.push_back(decrease_captions_size_callback);
  184. ctx->menu_left_callbacks.push_back(nullptr);
  185. // Build list of menu right callbacks
  186. ctx->menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx->master_volume));
  187. ctx->menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx->ambience_volume));
  188. ctx->menu_right_callbacks.push_back(std::bind(increase_volume_callback, &ctx->effects_volume));
  189. ctx->menu_right_callbacks.push_back(toggle_mono_audio_callback);
  190. ctx->menu_right_callbacks.push_back(toggle_captions_callback);
  191. ctx->menu_right_callbacks.push_back(increase_captions_size_callback);
  192. ctx->menu_right_callbacks.push_back(nullptr);
  193. // Set menu back callback
  194. ctx->menu_back_callback = select_back_callback;
  195. // Schedule menu control setup
  196. timeline* timeline = ctx->timeline;
  197. float t = timeline->get_position();
  198. timeline->add_sequence({{t + game::menu::input_delay, std::bind(game::menu::setup_controls, ctx)}});
  199. // Fade in menu
  200. game::menu::fade_in(ctx, nullptr);
  201. }
  202. void exit(game::context* ctx)
  203. {
  204. // Destruct menu
  205. game::menu::clear_controls(ctx);
  206. game::menu::clear_callbacks(ctx);
  207. game::menu::delete_animations(ctx);
  208. game::menu::remove_text_from_ui(ctx);
  209. game::menu::delete_text(ctx);
  210. // Update config
  211. (*ctx->config)["master_volume"] = ctx->master_volume;
  212. (*ctx->config)["ambience_volume"] = ctx->ambience_volume;
  213. (*ctx->config)["effects_volume"] = ctx->effects_volume;
  214. (*ctx->config)["mono_audio"] = ctx->mono_audio;
  215. (*ctx->config)["captions"] = ctx->captions;
  216. (*ctx->config)["captions_size"] = ctx->captions_size;
  217. }
  218. } // namespace sound_menu
  219. } // namespace state
  220. } // namespace game