OpacitySlider.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 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 <LibGUI/OpacitySlider.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGfx/Palette.h>
  29. #include <LibGfx/StylePainter.h>
  30. REGISTER_WIDGET(GUI, OpacitySlider)
  31. namespace GUI {
  32. OpacitySlider::OpacitySlider(Gfx::Orientation orientation)
  33. : AbstractSlider(orientation)
  34. {
  35. // FIXME: Implement vertical mode.
  36. VERIFY(orientation == Gfx::Orientation::Horizontal);
  37. set_min(0);
  38. set_max(100);
  39. set_value(100);
  40. set_fixed_height(20);
  41. }
  42. OpacitySlider::~OpacitySlider()
  43. {
  44. }
  45. Gfx::IntRect OpacitySlider::frame_inner_rect() const
  46. {
  47. return rect().shrunken(4, 4);
  48. }
  49. void OpacitySlider::paint_event(PaintEvent& event)
  50. {
  51. GUI::Painter painter(*this);
  52. painter.add_clip_rect(event.rect());
  53. auto inner_rect = frame_inner_rect();
  54. // Grid pattern
  55. Gfx::StylePainter::paint_transparency_grid(painter, inner_rect, palette());
  56. // Alpha gradient
  57. for (int x = inner_rect.left(); x <= inner_rect.right(); ++x) {
  58. float relative_offset = (float)x / (float)width();
  59. float alpha = relative_offset * 255.0f;
  60. Color color { 0, 0, 0, (u8)alpha };
  61. painter.fill_rect({ x, inner_rect.y(), 1, inner_rect.height() }, color);
  62. }
  63. constexpr int notch_size = 3;
  64. int notch_y_top = inner_rect.top() + notch_size;
  65. int notch_y_bottom = inner_rect.bottom() - notch_size;
  66. int notch_x = inner_rect.left() + ((float)value() / (float)max() * (float)inner_rect.width());
  67. // Top notch
  68. painter.set_pixel(notch_x, notch_y_top, palette().threed_shadow2());
  69. for (int i = notch_size; i >= 0; --i) {
  70. painter.set_pixel(notch_x - (i + 1), notch_y_top - i - 1, palette().threed_highlight());
  71. for (int j = 0; j < i * 2; ++j) {
  72. painter.set_pixel(notch_x - (i + 1) + j + 1, notch_y_top - i - 1, palette().button());
  73. }
  74. painter.set_pixel(notch_x + (i + 0), notch_y_top - i - 1, palette().threed_shadow1());
  75. painter.set_pixel(notch_x + (i + 1), notch_y_top - i - 1, palette().threed_shadow2());
  76. }
  77. // Bottom notch
  78. painter.set_pixel(notch_x, notch_y_bottom, palette().threed_shadow2());
  79. for (int i = 0; i < notch_size; ++i) {
  80. painter.set_pixel(notch_x - (i + 1), notch_y_bottom + i + 1, palette().threed_highlight());
  81. for (int j = 0; j < i * 2; ++j) {
  82. painter.set_pixel(notch_x - (i + 1) + j + 1, notch_y_bottom + i + 1, palette().button());
  83. }
  84. painter.set_pixel(notch_x + (i + 0), notch_y_bottom + i + 1, palette().threed_shadow1());
  85. painter.set_pixel(notch_x + (i + 1), notch_y_bottom + i + 1, palette().threed_shadow2());
  86. }
  87. // Hairline
  88. // NOTE: If we're in the whiter part of the gradient, the notch is painted as shadow between the notches.
  89. // If we're in the darker part, the notch is painted as highlight.
  90. // We adjust the hairline's x position so it lines up with the shadow/highlight of the notches.
  91. u8 h = ((float)value() / (float)max()) * 255.0f;
  92. if (h < 128)
  93. painter.draw_line({ notch_x, notch_y_top }, { notch_x, notch_y_bottom }, Color(h, h, h, 255));
  94. else
  95. painter.draw_line({ notch_x - 1, notch_y_top }, { notch_x - 1, notch_y_bottom }, Color(h, h, h, 255));
  96. // Text label
  97. auto percent_text = String::formatted("{}%", (int)((float)value() / (float)max() * 100.0f));
  98. painter.draw_text(inner_rect.translated(1, 1), percent_text, Gfx::TextAlignment::Center, Color::Black);
  99. painter.draw_text(inner_rect, percent_text, Gfx::TextAlignment::Center, Color::White);
  100. // Frame
  101. Gfx::StylePainter::paint_frame(painter, rect(), palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  102. }
  103. int OpacitySlider::value_at(const Gfx::IntPoint& position) const
  104. {
  105. auto inner_rect = frame_inner_rect();
  106. if (position.x() < inner_rect.left())
  107. return min();
  108. if (position.x() > inner_rect.right())
  109. return max();
  110. float relative_offset = (float)(position.x() - inner_rect.x()) / (float)inner_rect.width();
  111. return relative_offset * (float)max();
  112. }
  113. void OpacitySlider::mousedown_event(MouseEvent& event)
  114. {
  115. if (event.button() == MouseButton::Left) {
  116. m_dragging = true;
  117. set_value(value_at(event.position()));
  118. return;
  119. }
  120. AbstractSlider::mousedown_event(event);
  121. }
  122. void OpacitySlider::mousemove_event(MouseEvent& event)
  123. {
  124. if (m_dragging) {
  125. set_value(value_at(event.position()));
  126. return;
  127. }
  128. AbstractSlider::mousemove_event(event);
  129. }
  130. void OpacitySlider::mouseup_event(MouseEvent& event)
  131. {
  132. if (event.button() == MouseButton::Left) {
  133. m_dragging = false;
  134. return;
  135. }
  136. AbstractSlider::mouseup_event(event);
  137. }
  138. void OpacitySlider::mousewheel_event(MouseEvent& event)
  139. {
  140. set_value(value() - event.wheel_delta());
  141. }
  142. }