icon.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 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 <ClanLib/Display/display.h>
  17. #include "box.hpp"
  18. #include "icon.hpp"
  19. class IconImpl
  20. {
  21. public:
  22. IconImpl(Icon* p) : parent(p) {}
  23. Icon* parent;
  24. std::vector<CL_Slot> slots;
  25. CL_Sprite sprite;
  26. std::string tooltip;
  27. bool draw_tooltip;
  28. bool down;
  29. /** Parameter to keep the button down all the time, aka togglebutton
  30. like */
  31. bool is_down;
  32. bool is_enabled;
  33. CL_Signal_v0 sig_on_click;
  34. void draw();
  35. void mouse_up (const CL_InputEvent& event);
  36. void mouse_down(const CL_InputEvent& event);
  37. void mouse_move(const CL_InputEvent& event);
  38. };
  39. Icon::Icon(const CL_Rect& rect, const CL_Sprite& sprite, const std::string& tooltip,
  40. CL_Component* parent)
  41. : CL_Component(rect, parent),
  42. impl(new IconImpl(this))
  43. {
  44. impl->sprite = sprite;
  45. impl->tooltip = tooltip;
  46. impl->draw_tooltip = true;
  47. impl->down = false;
  48. impl->is_down = false;
  49. impl->is_enabled = true;
  50. impl->slots.push_back(sig_paint().connect(impl.get(), &IconImpl::draw));
  51. impl->slots.push_back(sig_mouse_down().connect(impl.get(), &IconImpl::mouse_down));
  52. impl->slots.push_back(sig_mouse_up().connect(impl.get(), &IconImpl::mouse_up));
  53. }
  54. CL_Signal_v0&
  55. Icon::sig_clicked()
  56. {
  57. return impl->sig_on_click;
  58. }
  59. void
  60. IconImpl::draw()
  61. {
  62. CL_Display::push_translate(parent->get_screen_x(), parent->get_screen_y());
  63. CL_Rect rect(CL_Point(0, 0), CL_Size(parent->get_width()-4, parent->get_height()-4));
  64. sprite.set_alignment(origin_center);
  65. if (is_enabled)
  66. {
  67. if (is_down)
  68. {
  69. Box::draw_button_down(rect);
  70. }
  71. else
  72. {
  73. if (parent->has_mouse_over())
  74. {
  75. if (down)
  76. Box::draw_button_down(rect);
  77. else
  78. Box::draw_button_up(rect);
  79. }
  80. else
  81. Box::draw_button_neutral(rect);
  82. }
  83. sprite.set_alpha(1.0f);
  84. }
  85. else
  86. {
  87. Box::draw_button_neutral(rect);
  88. sprite.set_alpha(0.3f);
  89. }
  90. sprite.draw((rect.get_width()+1)/2, (rect.get_height()+1)/2);
  91. CL_Display::pop_modelview();
  92. }
  93. void
  94. IconImpl::mouse_up (const CL_InputEvent& event)
  95. {
  96. if (is_enabled)
  97. {
  98. down = false;
  99. parent->release_mouse();
  100. if (parent->has_mouse_over())
  101. {
  102. sig_on_click();
  103. }
  104. }
  105. }
  106. void
  107. IconImpl::mouse_down(const CL_InputEvent& event)
  108. {
  109. if (is_enabled)
  110. {
  111. down = true;
  112. parent->capture_mouse();
  113. }
  114. }
  115. void
  116. IconImpl::mouse_move(const CL_InputEvent& event)
  117. {
  118. //std::cout << "icon: mouse_move: " << event << std::endl;
  119. }
  120. void
  121. Icon::disable()
  122. {
  123. impl->is_enabled = false;
  124. }
  125. void
  126. Icon::enable()
  127. {
  128. impl->is_enabled = true;
  129. }
  130. void
  131. Icon::set_up()
  132. {
  133. impl->is_down = false;
  134. }
  135. void
  136. Icon::set_down()
  137. {
  138. impl->is_down = true;
  139. }
  140. /* EOF */