info_box.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  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/info_box.hpp"
  17. #include "supertux/globals.hpp"
  18. #include "supertux/info_box_line.hpp"
  19. #include "util/log.hpp"
  20. #include "video/drawing_context.hpp"
  21. #include "video/surface.hpp"
  22. InfoBox::InfoBox(const std::string& text) :
  23. firstline(0),
  24. // Split text string lines into a vector
  25. lines(InfoBoxLine::split(text, 400)),
  26. images(),
  27. arrow_scrollup(),
  28. arrow_scrolldown()
  29. {
  30. try
  31. {
  32. // get the arrow sprites
  33. arrow_scrollup = Surface::from_file("images/engine/menu/scroll-up.png");
  34. arrow_scrolldown = Surface::from_file("images/engine/menu/scroll-down.png");
  35. }
  36. catch (std::exception& e)
  37. {
  38. log_warning << "Could not load scrolling images: " << e.what() << std::endl;
  39. arrow_scrollup.reset();
  40. arrow_scrolldown.reset();
  41. }
  42. }
  43. void
  44. InfoBox::draw(DrawingContext& context)
  45. {
  46. float x1 = context.get_width() / 2.0f - 200.0f;
  47. float y1 = context.get_height() / 2.0f - 200.0f;
  48. float width = 400.0f;
  49. float height = 200.0f;
  50. context.color().draw_filled_rect(Rectf(Vector(x1, y1),
  51. Sizef(width, height)),
  52. Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI - 1);
  53. float y = y1;
  54. bool linesLeft = false;
  55. for (size_t i = firstline; i < lines.size(); ++i) {
  56. if (y >= y1 + height) {
  57. linesLeft = true;
  58. break;
  59. }
  60. lines[i]->draw(context, Rectf(x1, y, x1+width, y), LAYER_GUI);
  61. y += lines[i]->get_height();
  62. }
  63. {
  64. // draw the scrolling arrows
  65. if (arrow_scrollup.get() && firstline > 0)
  66. context.color().draw_surface(arrow_scrollup,
  67. Vector(x1 + width - static_cast<float>(arrow_scrollup->get_width()), // top-right corner of box
  68. y1), LAYER_GUI);
  69. if (arrow_scrolldown.get() && linesLeft && firstline < lines.size()-1)
  70. context.color().draw_surface(arrow_scrolldown,
  71. Vector(x1 + width - static_cast<float>(arrow_scrolldown->get_width()), // bottom-light corner of box
  72. y1 + height - static_cast<float>(arrow_scrolldown->get_height())),
  73. LAYER_GUI);
  74. }
  75. }
  76. void
  77. InfoBox::scrollup()
  78. {
  79. if (firstline > 0)
  80. firstline--;
  81. }
  82. void
  83. InfoBox::scrolldown()
  84. {
  85. if (firstline < lines.size()-1)
  86. firstline++;
  87. }
  88. void
  89. InfoBox::pageup()
  90. {
  91. }
  92. void
  93. InfoBox::pagedown()
  94. {
  95. }
  96. /* EOF */