/pingus-0.7.6/src/pingus/components/menu_button.cpp

# · C++ · 132 lines · 95 code · 18 blank · 19 comment · 4 complexity · c0b6f3486af7e754973678c70bc2fc64 MD5 · raw file

  1. // Pingus - A free Lemmings clone
  2. // Copyright (C) 1999 Ingo Ruhnke <grumbel@gmx.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 "pingus/components/menu_button.hpp"
  17. #include "engine/display/drawing_context.hpp"
  18. #include "engine/sound/sound.hpp"
  19. #include "math/vector2i.hpp"
  20. #include "pingus/screens/pingus_menu.hpp"
  21. MenuButton::MenuButton(PingusMenu* menu_,
  22. const Vector2i& pos_,
  23. const std::string& text_, const std::string& desc_) :
  24. menu(menu_),
  25. surface_p(),
  26. highlight(),
  27. font(),
  28. font_large(),
  29. x_pos(),
  30. y_pos(),
  31. desc(),
  32. text(),
  33. mouse_over(),
  34. pressed()
  35. {
  36. surface_p = Sprite("core/menu/menuitem");
  37. highlight = Sprite("core/menu/menuitem_highlight");
  38. text = text_;
  39. desc = desc_;
  40. x_pos = pos_.x;
  41. y_pos = pos_.y;
  42. font = Fonts::pingus_small;
  43. font_large = Fonts::chalk_large;
  44. mouse_over = false;
  45. pressed = false;
  46. }
  47. MenuButton::~MenuButton ()
  48. {
  49. }
  50. void
  51. MenuButton::on_click ()
  52. {
  53. //log_info("MenuButton: Click");
  54. menu->on_click(this);
  55. }
  56. void
  57. MenuButton::draw (DrawingContext& gc)
  58. {
  59. if (mouse_over) // pressed
  60. {
  61. gc.draw(surface_p,Vector2i(x_pos, y_pos));
  62. gc.draw(highlight, Vector2i(x_pos, y_pos));
  63. gc.print_center(font_large, Vector2i(x_pos, y_pos - 28), text);
  64. }
  65. else
  66. {
  67. gc.draw(surface_p, Vector2i(x_pos, y_pos));
  68. gc.print_center(font_large, Vector2i(x_pos, y_pos - 28), text);
  69. }
  70. }
  71. void
  72. MenuButton::update (float delta)
  73. {
  74. }
  75. void
  76. MenuButton::on_pointer_enter ()
  77. {
  78. mouse_over = true;
  79. Sound::PingusSound::play_sound ("tick");
  80. //log_info("X: " << this << "enter");
  81. menu->set_hint(desc);
  82. }
  83. void
  84. MenuButton::on_pointer_leave ()
  85. {
  86. //log_info("X: " << this << "leave");
  87. mouse_over = false;
  88. menu->set_hint("");
  89. }
  90. void
  91. MenuButton::on_pointer_press ()
  92. {
  93. pressed = true;
  94. }
  95. void
  96. MenuButton::on_pointer_release ()
  97. {
  98. pressed = false;
  99. }
  100. bool
  101. MenuButton::is_at(int x, int y)
  102. {
  103. return (x > x_pos - int(surface_p.get_width()) / 2
  104. && x < x_pos + int(surface_p.get_width()) / 2
  105. && y > y_pos - int(surface_p.get_height()) / 2
  106. && y < y_pos + int(surface_p.get_height()) / 2);
  107. }
  108. void
  109. MenuButton::set_pos(int x, int y)
  110. {
  111. x_pos = x;
  112. y_pos = y;
  113. }
  114. /* EOF */