Slider.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/StdLibExtras.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Slider.h>
  30. #include <LibGfx/Palette.h>
  31. #include <LibGfx/StylePainter.h>
  32. namespace GUI {
  33. Slider::Slider(Orientation orientation)
  34. : m_orientation(orientation)
  35. {
  36. }
  37. Slider::~Slider()
  38. {
  39. }
  40. void Slider::set_range(int min, int max)
  41. {
  42. ASSERT(min <= max);
  43. if (m_min == min && m_max == max)
  44. return;
  45. m_min = min;
  46. m_max = max;
  47. m_value = clamp(m_value, m_min, m_max);
  48. update();
  49. }
  50. void Slider::set_value(int value)
  51. {
  52. value = clamp(value, m_min, m_max);
  53. if (m_value == value)
  54. return;
  55. m_value = value;
  56. update();
  57. if (on_value_changed)
  58. on_value_changed(m_value);
  59. }
  60. void Slider::paint_event(PaintEvent& event)
  61. {
  62. Painter painter(*this);
  63. painter.add_clip_rect(event.rect());
  64. Gfx::Rect track_rect;
  65. if (orientation() == Orientation::Horizontal) {
  66. track_rect = { inner_rect().x(), 0, inner_rect().width(), track_size() };
  67. track_rect.center_vertically_within(inner_rect());
  68. } else {
  69. track_rect = { 0, inner_rect().y(), track_size(), inner_rect().height() };
  70. track_rect.center_horizontally_within(inner_rect());
  71. }
  72. Gfx::StylePainter::paint_frame(painter, track_rect, palette(), Gfx::FrameShape::Panel, Gfx::FrameShadow::Sunken, 1);
  73. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_knob_hovered);
  74. }
  75. Gfx::Rect Slider::knob_rect() const
  76. {
  77. auto inner_rect = this->inner_rect();
  78. Gfx::Rect rect;
  79. rect.set_secondary_offset_for_orientation(orientation(), 0);
  80. rect.set_secondary_size_for_orientation(orientation(), knob_secondary_size());
  81. if (knob_size_mode() == KnobSizeMode::Fixed) {
  82. if (m_max - m_min) {
  83. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min);
  84. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)) - (knob_fixed_primary_size() / 2));
  85. } else
  86. rect.set_primary_size_for_orientation(orientation(), 0);
  87. rect.set_primary_size_for_orientation(orientation(), knob_fixed_primary_size());
  88. } else {
  89. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min + 1);
  90. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)));
  91. if (m_max - m_min)
  92. rect.set_primary_size_for_orientation(orientation(), ::max((int)(scale), knob_fixed_primary_size()));
  93. else
  94. rect.set_primary_size_for_orientation(orientation(), inner_rect.primary_size_for_orientation(orientation()));
  95. }
  96. if (orientation() == Orientation::Horizontal)
  97. rect.center_vertically_within(inner_rect);
  98. else
  99. rect.center_horizontally_within(inner_rect);
  100. return rect;
  101. }
  102. void Slider::mousedown_event(MouseEvent& event)
  103. {
  104. if (!is_enabled())
  105. return;
  106. if (event.button() == MouseButton::Left) {
  107. if (knob_rect().contains(event.position())) {
  108. m_dragging = true;
  109. m_drag_origin = event.position();
  110. m_drag_origin_value = m_value;
  111. return;
  112. } else {
  113. if (event.position().primary_offset_for_orientation(orientation()) > knob_rect().last_edge_for_orientation(orientation()))
  114. set_value(m_value + 1);
  115. else if (event.position().primary_offset_for_orientation(orientation()) < knob_rect().first_edge_for_orientation(orientation()))
  116. set_value(m_value - 1);
  117. }
  118. }
  119. return Widget::mousedown_event(event);
  120. }
  121. void Slider::mousemove_event(MouseEvent& event)
  122. {
  123. if (!is_enabled())
  124. return;
  125. set_knob_hovered(knob_rect().contains(event.position()));
  126. if (m_dragging) {
  127. float delta = event.position().primary_offset_for_orientation(orientation()) - m_drag_origin.primary_offset_for_orientation(orientation());
  128. float scrubbable_range = inner_rect().primary_size_for_orientation(orientation());
  129. float value_steps_per_scrubbed_pixel = (m_max - m_min) / scrubbable_range;
  130. float new_value = m_drag_origin_value + (value_steps_per_scrubbed_pixel * delta);
  131. set_value((int)new_value);
  132. return;
  133. }
  134. return Widget::mousemove_event(event);
  135. }
  136. void Slider::mouseup_event(MouseEvent& event)
  137. {
  138. if (!is_enabled())
  139. return;
  140. if (event.button() == MouseButton::Left) {
  141. m_dragging = false;
  142. return;
  143. }
  144. return Widget::mouseup_event(event);
  145. }
  146. void Slider::mousewheel_event(MouseEvent& event)
  147. {
  148. if (!is_enabled())
  149. return;
  150. if (orientation() == Orientation::Horizontal)
  151. set_value(value() - event.wheel_delta() * m_step);
  152. else
  153. set_value(value() + event.wheel_delta() * m_step);
  154. Widget::mousewheel_event(event);
  155. }
  156. void Slider::leave_event(Core::Event& event)
  157. {
  158. if (!is_enabled())
  159. return;
  160. set_knob_hovered(false);
  161. Widget::leave_event(event);
  162. }
  163. void Slider::change_event(Event& event)
  164. {
  165. if (event.type() == Event::Type::EnabledChange) {
  166. if (!is_enabled())
  167. m_dragging = false;
  168. }
  169. Widget::change_event(event);
  170. }
  171. void Slider::set_knob_hovered(bool hovered)
  172. {
  173. if (m_knob_hovered == hovered)
  174. return;
  175. m_knob_hovered = hovered;
  176. update(knob_rect());
  177. }
  178. }