Slider.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(Widget* parent)
  34. : Slider(Orientation::Horizontal, parent)
  35. {
  36. }
  37. Slider::Slider(Orientation orientation, Widget* parent)
  38. : Widget(parent)
  39. , m_orientation(orientation)
  40. {
  41. }
  42. Slider::~Slider()
  43. {
  44. }
  45. void Slider::set_range(int min, int max)
  46. {
  47. ASSERT(min <= max);
  48. if (m_min == min && m_max == max)
  49. return;
  50. m_min = min;
  51. m_max = max;
  52. m_value = clamp(m_value, m_min, m_max);
  53. update();
  54. }
  55. void Slider::set_value(int value)
  56. {
  57. value = clamp(value, m_min, m_max);
  58. if (m_value == value)
  59. return;
  60. m_value = value;
  61. update();
  62. if (on_value_changed)
  63. on_value_changed(m_value);
  64. }
  65. void Slider::paint_event(PaintEvent& event)
  66. {
  67. Painter painter(*this);
  68. painter.add_clip_rect(event.rect());
  69. Gfx::Rect track_rect;
  70. if (orientation() == Orientation::Horizontal) {
  71. track_rect = { inner_rect().x(), 0, inner_rect().width(), track_size() };
  72. track_rect.center_vertically_within(inner_rect());
  73. } else {
  74. track_rect = { 0, inner_rect().y(), track_size(), inner_rect().height() };
  75. track_rect.center_horizontally_within(inner_rect());
  76. }
  77. Gfx::StylePainter::paint_frame(painter, track_rect, palette(), Gfx::FrameShape::Panel, Gfx::FrameShadow::Sunken, 1);
  78. Gfx::StylePainter::paint_button(painter, knob_rect(), palette(), Gfx::ButtonStyle::Normal, false, m_knob_hovered);
  79. }
  80. Gfx::Rect Slider::knob_rect() const
  81. {
  82. auto inner_rect = this->inner_rect();
  83. Gfx::Rect rect;
  84. rect.set_secondary_offset_for_orientation(orientation(), 0);
  85. rect.set_secondary_size_for_orientation(orientation(), knob_secondary_size());
  86. if (knob_size_mode() == KnobSizeMode::Fixed) {
  87. if (m_max - m_min) {
  88. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min);
  89. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)) - (knob_fixed_primary_size() / 2));
  90. } else
  91. rect.set_primary_size_for_orientation(orientation(), 0);
  92. rect.set_primary_size_for_orientation(orientation(), knob_fixed_primary_size());
  93. } else {
  94. float scale = (float)inner_rect.primary_size_for_orientation(orientation()) / (float)(m_max - m_min + 1);
  95. rect.set_primary_offset_for_orientation(orientation(), inner_rect.primary_offset_for_orientation(orientation()) + ((int)(m_value * scale)));
  96. if (m_max - m_min)
  97. rect.set_primary_size_for_orientation(orientation(), ::max((int)(scale), knob_fixed_primary_size()));
  98. else
  99. rect.set_primary_size_for_orientation(orientation(), inner_rect.primary_size_for_orientation(orientation()));
  100. }
  101. if (orientation() == Orientation::Horizontal)
  102. rect.center_vertically_within(inner_rect);
  103. else
  104. rect.center_horizontally_within(inner_rect);
  105. return rect;
  106. }
  107. void Slider::mousedown_event(MouseEvent& event)
  108. {
  109. if (!is_enabled())
  110. return;
  111. if (event.button() == MouseButton::Left) {
  112. if (knob_rect().contains(event.position())) {
  113. m_dragging = true;
  114. m_drag_origin = event.position();
  115. m_drag_origin_value = m_value;
  116. return;
  117. } else {
  118. if (event.position().primary_offset_for_orientation(orientation()) > knob_rect().last_edge_for_orientation(orientation()))
  119. set_value(m_value + 1);
  120. else if (event.position().primary_offset_for_orientation(orientation()) < knob_rect().first_edge_for_orientation(orientation()))
  121. set_value(m_value - 1);
  122. }
  123. }
  124. return Widget::mousedown_event(event);
  125. }
  126. void Slider::mousemove_event(MouseEvent& event)
  127. {
  128. if (!is_enabled())
  129. return;
  130. set_knob_hovered(knob_rect().contains(event.position()));
  131. if (m_dragging) {
  132. float delta = event.position().primary_offset_for_orientation(orientation()) - m_drag_origin.primary_offset_for_orientation(orientation());
  133. float scrubbable_range = inner_rect().primary_size_for_orientation(orientation());
  134. float value_steps_per_scrubbed_pixel = (m_max - m_min) / scrubbable_range;
  135. float new_value = m_drag_origin_value + (value_steps_per_scrubbed_pixel * delta);
  136. set_value((int)new_value);
  137. return;
  138. }
  139. return Widget::mousemove_event(event);
  140. }
  141. void Slider::mouseup_event(MouseEvent& event)
  142. {
  143. if (!is_enabled())
  144. return;
  145. if (event.button() == MouseButton::Left) {
  146. m_dragging = false;
  147. return;
  148. }
  149. return Widget::mouseup_event(event);
  150. }
  151. void Slider::leave_event(Core::Event& event)
  152. {
  153. if (!is_enabled())
  154. return;
  155. set_knob_hovered(false);
  156. Widget::leave_event(event);
  157. }
  158. void Slider::change_event(Event& event)
  159. {
  160. if (event.type() == Event::Type::EnabledChange) {
  161. if (!is_enabled())
  162. m_dragging = false;
  163. }
  164. Widget::change_event(event);
  165. }
  166. void Slider::set_knob_hovered(bool hovered)
  167. {
  168. if (m_knob_hovered == hovered)
  169. return;
  170. m_knob_hovered = hovered;
  171. update(knob_rect());
  172. }
  173. }