slider.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <ClanLib/Display/keys.h>
  18. #include "math.hpp"
  19. #include "slider.hpp"
  20. Slider::Slider(const CL_Rect& rect, CL_Component* parent)
  21. : CL_Component(rect, parent)
  22. {
  23. start = 0.0f;
  24. end = 100.0f;
  25. value = 50.0f;
  26. pressed = false;
  27. slots.push_back(sig_mouse_down().connect(this, &Slider::on_mouse_down));
  28. slots.push_back(sig_mouse_up().connect(this, &Slider::on_mouse_up));
  29. slots.push_back(sig_mouse_move().connect(this, &Slider::on_mouse_move));
  30. slots.push_back(sig_paint().connect(this, &Slider::draw));
  31. }
  32. Slider::~Slider()
  33. {
  34. }
  35. void
  36. Slider::draw()
  37. {
  38. CL_Display::push_modelview();
  39. CL_Display::add_translate(get_screen_x(), get_screen_y());
  40. CL_Display::fill_rect(CL_Rect(CL_Point(0, get_height()/2 - 2),
  41. CL_Size(get_width(), 5)),
  42. CL_Color(255, 255, 255, 255));
  43. CL_Display::fill_rect(CL_Rect(CL_Point(int(-2 + (value/(end-start)) * get_width()), 0),
  44. CL_Size(5, get_height())),
  45. CL_Color(0, 0, 0, 255));
  46. CL_Display::pop_modelview();
  47. }
  48. void
  49. Slider::set_range(float start_, float end_)
  50. {
  51. start = start_;
  52. end = end_;
  53. }
  54. CL_Signal_v1<float>&
  55. Slider::sig_on_change()
  56. {
  57. return on_change;
  58. }
  59. void
  60. Slider::set_value(float value_)
  61. {
  62. value = value_;
  63. on_change(value);
  64. }
  65. void
  66. Slider::update_mouse(const CL_InputEvent& event)
  67. {
  68. set_value(Math::mid(start, (float(event.mouse_pos.x) / get_width()) * (end - start), end));
  69. }
  70. void
  71. Slider::on_mouse_down(const CL_InputEvent& event)
  72. {
  73. if (event.id == CL_MOUSE_LEFT)
  74. {
  75. pressed = true;
  76. capture_mouse();
  77. update_mouse(event);
  78. }
  79. }
  80. void
  81. Slider::on_mouse_up (const CL_InputEvent& event)
  82. {
  83. if (event.id == CL_MOUSE_LEFT)
  84. {
  85. pressed = false;
  86. release_mouse();
  87. update_mouse(event);
  88. }
  89. }
  90. void
  91. Slider::on_mouse_move(const CL_InputEvent& event)
  92. {
  93. if (pressed)
  94. {
  95. update_mouse(event);
  96. }
  97. }
  98. /* EOF */