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

411 lines
13 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/state/graphics-menu.hpp"
  20. #include "game/state/options-menu.hpp"
  21. #include "application.hpp"
  22. #include "scene/text.hpp"
  23. #include "debug/log.hpp"
  24. #include "game/fonts.hpp"
  25. #include "game/menu.hpp"
  26. #include "game/graphics.hpp"
  27. #include "animation/timeline.hpp"
  28. #include "game/strings.hpp"
  29. #include "utility/hash/fnv1a.hpp"
  30. using namespace hash::literals;
  31. namespace game {
  32. namespace state {
  33. static void update_value_text_content(game::context* ctx);
  34. graphics_menu::graphics_menu(game::context& ctx):
  35. game::state::base(ctx)
  36. {
  37. debug::log::trace("Entering graphics menu state...");
  38. // Construct menu item texts
  39. scene::text* fullscreen_name_text = new scene::text();
  40. scene::text* fullscreen_value_text = new scene::text();
  41. scene::text* resolution_name_text = new scene::text();
  42. scene::text* resolution_value_text = new scene::text();
  43. scene::text* v_sync_name_text = new scene::text();
  44. scene::text* v_sync_value_text = new scene::text();
  45. scene::text* aa_method_name_text = new scene::text();
  46. scene::text* aa_method_value_text = new scene::text();
  47. scene::text* font_scale_name_text = new scene::text();
  48. scene::text* font_scale_value_text = new scene::text();
  49. scene::text* dyslexia_font_name_text = new scene::text();
  50. scene::text* dyslexia_font_value_text = new scene::text();
  51. scene::text* back_text = new scene::text();
  52. // Build list of menu item texts
  53. ctx.menu_item_texts.push_back({fullscreen_name_text, fullscreen_value_text});
  54. ctx.menu_item_texts.push_back({resolution_name_text, resolution_value_text});
  55. ctx.menu_item_texts.push_back({v_sync_name_text, v_sync_value_text});
  56. ctx.menu_item_texts.push_back({aa_method_name_text, aa_method_value_text});
  57. ctx.menu_item_texts.push_back({font_scale_name_text, font_scale_value_text});
  58. ctx.menu_item_texts.push_back({dyslexia_font_name_text, dyslexia_font_value_text});
  59. ctx.menu_item_texts.push_back({back_text, nullptr});
  60. // Set content of menu item texts
  61. fullscreen_name_text->set_content(get_string(ctx, "graphics_menu_fullscreen"_fnv1a32));
  62. resolution_name_text->set_content(get_string(ctx, "graphics_menu_resolution"_fnv1a32));
  63. v_sync_name_text->set_content(get_string(ctx, "graphics_menu_v_sync"_fnv1a32));
  64. aa_method_name_text->set_content(get_string(ctx, "graphics_menu_aa_method"_fnv1a32));
  65. font_scale_name_text->set_content(get_string(ctx, "graphics_menu_font_scale"_fnv1a32));
  66. dyslexia_font_name_text->set_content(get_string(ctx, "graphics_menu_dyslexia_font"_fnv1a32));
  67. back_text->set_content(get_string(ctx, "back"_fnv1a32));
  68. update_value_text_content();
  69. // Init menu item index
  70. game::menu::init_menu_item_index(ctx, "graphics");
  71. game::menu::update_text_color(ctx);
  72. game::menu::update_text_font(ctx);
  73. game::menu::align_text(ctx);
  74. game::menu::update_text_tweens(ctx);
  75. game::menu::add_text_to_ui(ctx);
  76. game::menu::setup_animations(ctx);
  77. // Construct menu item callbacks
  78. auto toggle_fullscreen_callback = [this, &ctx]()
  79. {
  80. bool fullscreen = !ctx.app->is_fullscreen();
  81. ctx.app->set_fullscreen(fullscreen);
  82. this->update_value_text_content();
  83. game::menu::align_text(ctx);
  84. game::menu::update_text_tweens(ctx);
  85. // Update fullscreen settings
  86. (*ctx.settings)["fullscreen"_fnv1a32] = fullscreen;
  87. };
  88. auto increase_resolution_callback = [this, &ctx]()
  89. {
  90. // Increase resolution
  91. if (ctx.menu_modifier_control.is_active())
  92. ctx.render_scale += 0.05f;
  93. else
  94. ctx.render_scale += 0.25f;
  95. // Limit resolution
  96. if (ctx.render_scale > 2.0f)
  97. ctx.render_scale = 2.0f;
  98. // Update render scale setting
  99. (*ctx.settings)["render_scale"_fnv1a32] = ctx.render_scale;
  100. // Resize framebuffers
  101. game::graphics::change_render_resolution(ctx, ctx.render_scale);
  102. // Update text
  103. this->update_value_text_content();
  104. game::menu::align_text(ctx);
  105. game::menu::update_text_tweens(ctx);
  106. };
  107. auto decrease_resolution_callback = [this, &ctx]()
  108. {
  109. // Increase resolution
  110. if (ctx.menu_modifier_control.is_active())
  111. ctx.render_scale -= 0.05f;
  112. else
  113. ctx.render_scale -= 0.25f;
  114. // Limit resolution
  115. if (ctx.render_scale < 0.25f)
  116. ctx.render_scale = 0.25f;
  117. // Update render scale setting
  118. (*ctx.settings)["render_scale"_fnv1a32] = ctx.render_scale;
  119. // Resize framebuffers
  120. game::graphics::change_render_resolution(ctx, ctx.render_scale);
  121. // Update text
  122. this->update_value_text_content();
  123. game::menu::align_text(ctx);
  124. game::menu::update_text_tweens(ctx);
  125. };
  126. auto toggle_v_sync_callback = [this, &ctx]()
  127. {
  128. bool v_sync = !ctx.app->get_v_sync();
  129. // Update v-sync setting
  130. (*ctx.settings)["v_sync"_fnv1a32] = v_sync;
  131. ctx.app->set_v_sync(v_sync);
  132. this->update_value_text_content();
  133. game::menu::align_text(ctx);
  134. game::menu::update_text_tweens(ctx);
  135. };
  136. auto next_aa_method_callback = [this, &ctx]()
  137. {
  138. switch (ctx.anti_aliasing_method)
  139. {
  140. case render::anti_aliasing_method::none:
  141. ctx.anti_aliasing_method = render::anti_aliasing_method::fxaa;
  142. break;
  143. case render::anti_aliasing_method::fxaa:
  144. ctx.anti_aliasing_method = render::anti_aliasing_method::none;
  145. break;
  146. }
  147. // Update anti-aliasing method setting
  148. (*ctx.settings)["anti_aliasing_method"_fnv1a32] = std::to_underlying(ctx.anti_aliasing_method);
  149. game::graphics::select_anti_aliasing_method(ctx, ctx.anti_aliasing_method);
  150. // Update value text
  151. this->update_value_text_content();
  152. // Refresh and realign text
  153. game::menu::refresh_text(ctx);
  154. game::menu::align_text(ctx);
  155. game::menu::update_text_tweens(ctx);
  156. };
  157. auto previous_aa_method_callback = [this, &ctx]()
  158. {
  159. switch (ctx.anti_aliasing_method)
  160. {
  161. case render::anti_aliasing_method::none:
  162. ctx.anti_aliasing_method = render::anti_aliasing_method::fxaa;
  163. break;
  164. case render::anti_aliasing_method::fxaa:
  165. ctx.anti_aliasing_method = render::anti_aliasing_method::none;
  166. break;
  167. }
  168. // Update anti-aliasing method setting
  169. (*ctx.settings)["anti_aliasing_method"_fnv1a32] = std::to_underlying(ctx.anti_aliasing_method);
  170. game::graphics::select_anti_aliasing_method(ctx, ctx.anti_aliasing_method);
  171. // Update value text
  172. this->update_value_text_content();
  173. // Refresh and realign text
  174. game::menu::refresh_text(ctx);
  175. game::menu::align_text(ctx);
  176. game::menu::update_text_tweens(ctx);
  177. };
  178. auto increase_font_scale_callback = [this, &ctx]()
  179. {
  180. // Increase font scale
  181. if (ctx.menu_modifier_control.is_active())
  182. ctx.font_scale += 0.01f;
  183. else
  184. ctx.font_scale += 0.1f;
  185. // Limit font scale
  186. if (ctx.font_scale > 2.0f)
  187. ctx.font_scale = 2.0f;
  188. // Update font scale setting
  189. (*ctx.settings)["font_scale"_fnv1a32] = ctx.font_scale;
  190. // Update value text
  191. this->update_value_text_content();
  192. // Reload fonts
  193. debug::log::trace("Reloading fonts...");
  194. game::load_fonts(ctx);
  195. debug::log::trace("Reloaded fonts");
  196. // Refresh and realign text
  197. game::menu::refresh_text(ctx);
  198. game::menu::align_text(ctx);
  199. game::menu::update_text_tweens(ctx);
  200. };
  201. auto decrease_font_scale_callback = [this, &ctx]()
  202. {
  203. // Decrease font scale
  204. if (ctx.menu_modifier_control.is_active())
  205. ctx.font_scale -= 0.01f;
  206. else
  207. ctx.font_scale -= 0.1f;
  208. // Limit font scale
  209. if (ctx.font_scale < 0.1f)
  210. ctx.font_scale = 0.1f;
  211. // Update font scale setting
  212. (*ctx.settings)["font_scale"_fnv1a32] = ctx.font_scale;
  213. // Update value text
  214. this->update_value_text_content();
  215. // Reload fonts
  216. debug::log::trace("Reloading fonts...");
  217. game::load_fonts(ctx);
  218. debug::log::trace("Reloaded fonts");
  219. // Refresh and realign text
  220. game::menu::refresh_text(ctx);
  221. game::menu::align_text(ctx);
  222. game::menu::update_text_tweens(ctx);
  223. };
  224. auto toggle_dyslexia_font_callback = [this, &ctx]()
  225. {
  226. ctx.dyslexia_font = !ctx.dyslexia_font;
  227. // Update value text
  228. this->update_value_text_content();
  229. // Save dyslexia font setting
  230. (*ctx.settings)["dyslexia_font"_fnv1a32] = ctx.dyslexia_font;
  231. // Reload fonts
  232. debug::log::trace("Reloading fonts...");
  233. game::load_fonts(ctx);
  234. debug::log::trace("Reloaded fonts");
  235. // Refresh and realign text
  236. game::menu::refresh_text(ctx);
  237. game::menu::align_text(ctx);
  238. game::menu::update_text_tweens(ctx);
  239. };
  240. auto select_back_callback = [&ctx]()
  241. {
  242. // Disable controls
  243. game::menu::clear_controls(ctx);
  244. game::menu::fade_out
  245. (
  246. ctx,
  247. [&ctx]()
  248. {
  249. // Queue change to options menu state
  250. ctx.function_queue.push
  251. (
  252. [&ctx]()
  253. {
  254. ctx.state_machine.pop();
  255. ctx.state_machine.emplace(new game::state::options_menu(ctx));
  256. }
  257. );
  258. }
  259. );
  260. };
  261. // Build list of menu select callbacks
  262. ctx.menu_select_callbacks.push_back(toggle_fullscreen_callback);
  263. ctx.menu_select_callbacks.push_back(increase_resolution_callback);
  264. ctx.menu_select_callbacks.push_back(toggle_v_sync_callback);
  265. ctx.menu_select_callbacks.push_back(next_aa_method_callback);
  266. ctx.menu_select_callbacks.push_back(increase_font_scale_callback);
  267. ctx.menu_select_callbacks.push_back(toggle_dyslexia_font_callback);
  268. ctx.menu_select_callbacks.push_back(select_back_callback);
  269. // Build list of menu left callbacks
  270. ctx.menu_left_callbacks.push_back(toggle_fullscreen_callback);
  271. ctx.menu_left_callbacks.push_back(decrease_resolution_callback);
  272. ctx.menu_left_callbacks.push_back(toggle_v_sync_callback);
  273. ctx.menu_left_callbacks.push_back(previous_aa_method_callback);
  274. ctx.menu_left_callbacks.push_back(decrease_font_scale_callback);
  275. ctx.menu_left_callbacks.push_back(toggle_dyslexia_font_callback);
  276. ctx.menu_left_callbacks.push_back(nullptr);
  277. // Build list of menu right callbacks
  278. ctx.menu_right_callbacks.push_back(toggle_fullscreen_callback);
  279. ctx.menu_right_callbacks.push_back(increase_resolution_callback);
  280. ctx.menu_right_callbacks.push_back(toggle_v_sync_callback);
  281. ctx.menu_right_callbacks.push_back(next_aa_method_callback);
  282. ctx.menu_right_callbacks.push_back(increase_font_scale_callback);
  283. ctx.menu_right_callbacks.push_back(toggle_dyslexia_font_callback);
  284. ctx.menu_right_callbacks.push_back(nullptr);
  285. // Set menu back callback
  286. ctx.menu_back_callback = select_back_callback;
  287. // Queue menu control setup
  288. ctx.function_queue.push(std::bind(game::menu::setup_controls, std::ref(ctx)));
  289. // Fade in menu
  290. game::menu::fade_in(ctx, nullptr);
  291. debug::log::trace("Entered graphics menu state");
  292. }
  293. graphics_menu::~graphics_menu()
  294. {
  295. debug::log::trace("Exiting graphics menu state...");
  296. // Destruct menu
  297. game::menu::clear_controls(ctx);
  298. game::menu::clear_callbacks(ctx);
  299. game::menu::delete_animations(ctx);
  300. game::menu::remove_text_from_ui(ctx);
  301. game::menu::delete_text(ctx);
  302. debug::log::trace("Exited graphics menu state");
  303. }
  304. void graphics_menu::update_value_text_content()
  305. {
  306. const bool fullscreen = ctx.app->is_fullscreen();
  307. const float render_scale = ctx.render_scale;
  308. const bool v_sync = ctx.app->get_v_sync();
  309. const int aa_method_index = static_cast<int>(ctx.anti_aliasing_method);
  310. const float font_scale = ctx.font_scale;
  311. const bool dyslexia_font = ctx.dyslexia_font;
  312. const std::string string_on = get_string(ctx, "on"_fnv1a32);
  313. const std::string string_off = get_string(ctx, "off"_fnv1a32);
  314. /*
  315. const std::string string_quality[4] =
  316. {
  317. (*ctx.strings)["off"],
  318. (*ctx.strings)["quality_low"],
  319. (*ctx.strings)["quality_medium"],
  320. (*ctx.strings)["quality_high"]
  321. };
  322. */
  323. const std::string string_aa_methods[2] =
  324. {
  325. get_string(ctx, "graphics_menu_aa_method_none"_fnv1a32),
  326. get_string(ctx, "graphics_menu_aa_method_fxaa"_fnv1a32)
  327. };
  328. std::get<1>(ctx.menu_item_texts[0])->set_content((fullscreen) ? string_on : string_off);
  329. std::get<1>(ctx.menu_item_texts[1])->set_content(std::to_string(static_cast<int>(std::round(render_scale * 100.0f))) + "%");
  330. std::get<1>(ctx.menu_item_texts[2])->set_content((v_sync) ? string_on : string_off);
  331. std::get<1>(ctx.menu_item_texts[3])->set_content(string_aa_methods[aa_method_index]);
  332. std::get<1>(ctx.menu_item_texts[5])->set_content(std::to_string(static_cast<int>(std::round(font_scale * 100.0f))) + "%");
  333. std::get<1>(ctx.menu_item_texts[6])->set_content((dyslexia_font) ? string_on : string_off);
  334. }
  335. } // namespace state
  336. } // namespace game