item_stringselect.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SuperTux
  2. // Copyright (C) 2015 Hume2 <teratux.mail@gmail.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 "gui/item_stringselect.hpp"
  17. #include "gui/menu_manager.hpp"
  18. #include "supertux/colorscheme.hpp"
  19. #include "supertux/gameconfig.hpp"
  20. #include "supertux/globals.hpp"
  21. #include "supertux/resources.hpp"
  22. #include "video/drawing_context.hpp"
  23. #include "video/surface.hpp"
  24. ItemStringSelect::ItemStringSelect(const std::string& text, std::vector<std::string> items, int* selected, int id) :
  25. MenuItem(text, id),
  26. m_items(std::move(items)),
  27. m_selected(std::move(selected)),
  28. m_pointer_provided(true),
  29. m_callback(),
  30. m_width(calculate_width())
  31. {
  32. }
  33. ItemStringSelect::ItemStringSelect(const std::string& text, std::vector<std::string> items, int default_item, int id) :
  34. MenuItem(text, id),
  35. m_items(std::move(items)),
  36. m_selected(new int(default_item)),
  37. m_pointer_provided(false),
  38. m_callback(),
  39. m_width(calculate_width())
  40. {
  41. }
  42. ItemStringSelect::~ItemStringSelect()
  43. {
  44. if (!m_pointer_provided)
  45. delete m_selected;
  46. }
  47. void
  48. ItemStringSelect::draw(DrawingContext& context, const Vector& pos, int menu_width, bool active)
  49. {
  50. float roff = static_cast<float>(Resources::arrow_left->get_width()) * 1.0f;
  51. float sel_width = Resources::normal_font->get_text_width(m_items[*m_selected]);
  52. // Draw left side
  53. context.color().draw_text(Resources::normal_font, get_text(),
  54. Vector(pos.x + 16.0f,
  55. pos.y - Resources::normal_font->get_height() / 2.0f),
  56. ALIGN_LEFT, LAYER_GUI, active ? g_config->activetextcolor : get_color());
  57. // Draw right side
  58. context.color().draw_surface(Resources::arrow_left,
  59. Vector(pos.x + static_cast<float>(menu_width) - sel_width - 2.0f * roff - 8.0f,
  60. pos.y - 8.0f),
  61. LAYER_GUI);
  62. context.color().draw_surface(Resources::arrow_right,
  63. Vector(pos.x + static_cast<float>(menu_width) - roff - 8.0f,
  64. pos.y - 8.0f),
  65. LAYER_GUI);
  66. context.color().draw_text(Resources::normal_font, m_items[*m_selected],
  67. Vector(pos.x + static_cast<float>(menu_width) - roff - 8.0f,
  68. pos.y - Resources::normal_font->get_height() / 2.0f),
  69. ALIGN_RIGHT, LAYER_GUI, active ? g_config->activetextcolor : get_color());
  70. }
  71. int
  72. ItemStringSelect::get_width() const
  73. {
  74. return static_cast<int>(m_width);
  75. }
  76. void
  77. ItemStringSelect::process_action(const MenuAction& action)
  78. {
  79. switch (action) {
  80. case MenuAction::LEFT:
  81. if ( (*m_selected) > 0) {
  82. (*m_selected)--;
  83. } else {
  84. (*m_selected) = static_cast<int>(m_items.size()) - 1;
  85. }
  86. MenuManager::instance().current_menu()->menu_action(*this);
  87. if (m_callback) {
  88. m_callback(*m_selected);
  89. }
  90. break;
  91. case MenuAction::RIGHT:
  92. case MenuAction::HIT:
  93. if ( (*m_selected)+1 < int(m_items.size())) {
  94. (*m_selected)++;
  95. } else {
  96. (*m_selected) = 0;
  97. }
  98. MenuManager::instance().current_menu()->menu_action(*this);
  99. if (m_callback) {
  100. m_callback(*m_selected);
  101. }
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. float
  108. ItemStringSelect::calculate_width() const
  109. {
  110. float max_item_width = 0;
  111. for (auto const& item : m_items) {
  112. max_item_width = std::max(Resources::normal_font->get_text_width(item),
  113. max_item_width);
  114. }
  115. return Resources::normal_font->get_text_width(get_text()) + max_item_width + 64.0f;
  116. }
  117. /* EOF */