Slider.cpp 7.6 KB

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