worldmap_cheat_apply_menu.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SuperTux
  2. // Copyright (C) 2021 A. Semphris <semphris@protonmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "supertux/menu/worldmap_cheat_apply_menu.hpp"
  17. #include <fmt/format.h>
  18. #include "gui/menu_item.hpp"
  19. #include "gui/menu_manager.hpp"
  20. #include "object/player.hpp"
  21. #include "scripting/functions.hpp"
  22. #include "supertux/game_session.hpp"
  23. #include "supertux/sector.hpp"
  24. WorldmapCheatApplyMenu::WorldmapCheatApplyMenu(int num_players,
  25. std::function<void(int)> callback) :
  26. m_num_players(num_players),
  27. m_callback_1(callback),
  28. m_callback_2(nullptr),
  29. m_stack_count(-1)
  30. {
  31. add_label(_("Apply cheat to player"));
  32. add_hl();
  33. for (int i = 0; i < m_num_players; i++)
  34. add_entry(i, fmt::format(_("Player {}"), i + 1));
  35. add_hl();
  36. add_back(_("Back"));
  37. }
  38. WorldmapCheatApplyMenu::WorldmapCheatApplyMenu(int num_players,
  39. std::function<void(int, int)> callback) :
  40. m_num_players(num_players),
  41. m_callback_1(nullptr),
  42. m_callback_2(callback),
  43. m_stack_count(1)
  44. {
  45. add_label(_("Apply cheat to player"));
  46. add_hl();
  47. add_intfield("Count", &m_stack_count, -2);
  48. add_hl();
  49. add_entry(-1, _("All Players"));
  50. for (int i = 0; i < m_num_players; i++)
  51. add_entry(i, fmt::format(_("Player {}"), i + 1));
  52. add_hl();
  53. add_back(_("Back"));
  54. }
  55. void
  56. WorldmapCheatApplyMenu::menu_action(MenuItem& item)
  57. {
  58. int id = item.get_id();
  59. if (id < -1)
  60. return;
  61. if (id == -1)
  62. {
  63. for (int i = 0; i < m_num_players; i++)
  64. {
  65. if (m_callback_2)
  66. m_callback_2(i, m_stack_count);
  67. if (m_callback_1)
  68. m_callback_1(i);
  69. }
  70. }
  71. else
  72. {
  73. if (m_callback_2)
  74. m_callback_2(id, m_stack_count);
  75. if (m_callback_1)
  76. m_callback_1(id);
  77. }
  78. MenuManager::instance().clear_menu_stack();
  79. }
  80. /* EOF */