label.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SuperTux
  2. // Copyright (C) 2020 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 "interface/label.hpp"
  17. #include "supertux/resources.hpp"
  18. #include "video/video_system.hpp"
  19. #include "video/viewport.hpp"
  20. InterfaceLabel::InterfaceLabel() :
  21. m_rect(),
  22. m_label(),
  23. m_mouse_pos(0.0f, 0.0f)
  24. {
  25. }
  26. InterfaceLabel::InterfaceLabel(const Rectf& rect, std::string label) :
  27. m_rect(rect),
  28. m_label(std::move(label)),
  29. m_mouse_pos(0.0f, 0.0f)
  30. {
  31. }
  32. bool
  33. InterfaceLabel::on_mouse_motion(const SDL_MouseMotionEvent& motion)
  34. {
  35. m_mouse_pos = VideoSystem::current()->get_viewport().to_logical(motion.x, motion.y);
  36. return false;
  37. }
  38. void
  39. InterfaceLabel::draw(DrawingContext& context)
  40. {
  41. context.color().draw_text(Resources::control_font,
  42. get_truncated_text(),
  43. Vector(m_rect.get_left() + 5.f,
  44. (m_rect.get_top() + m_rect.get_bottom()) / 2 -
  45. Resources::control_font->get_height() / 2 + 1.f),
  46. FontAlignment::ALIGN_LEFT,
  47. LAYER_GUI,
  48. Color::WHITE);
  49. if (!fits(m_label) && m_rect.contains(m_mouse_pos)) {
  50. context.color().draw_filled_rect(Rectf(m_mouse_pos, m_mouse_pos + Vector(
  51. Resources::control_font
  52. ->get_text_width(m_label),
  53. Resources::control_font->get_height()))
  54. .grown(5.f).moved(Vector(0, 32)),
  55. Color(0.1f, 0.1f, 0.1f, 0.8f),
  56. LAYER_GUI + 10);
  57. context.color().draw_filled_rect(Rectf(m_mouse_pos, m_mouse_pos + Vector(
  58. Resources::control_font
  59. ->get_text_width(m_label),
  60. Resources::control_font->get_height()))
  61. .grown(3.f).moved(Vector(0, 32)),
  62. Color(1.f, 1.f, 1.f, 0.1f),
  63. LAYER_GUI + 10);
  64. context.color().draw_text(Resources::control_font,
  65. m_label,
  66. m_mouse_pos + Vector(0, 33.f),
  67. FontAlignment::ALIGN_LEFT,
  68. LAYER_GUI + 11,
  69. Color::WHITE);
  70. }
  71. }
  72. bool
  73. InterfaceLabel::fits(const std::string& text) const
  74. {
  75. return Resources::control_font->get_text_width(text) <= m_rect.get_width();
  76. }
  77. std::string
  78. InterfaceLabel::get_truncated_text() const
  79. {
  80. if (fits(m_label)) return m_label;
  81. std::string temp = m_label;
  82. while (!temp.empty() && !fits(temp + "..."))
  83. temp.pop_back();
  84. return temp + "...";
  85. }