ColorInput.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  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 <LibCore/Timer.h>
  27. #include <LibGUI/ColorInput.h>
  28. #include <LibGUI/ColorPicker.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGfx/Palette.h>
  31. namespace GUI {
  32. ColorInput::ColorInput()
  33. : TextEditor(TextEditor::SingleLine)
  34. {
  35. set_min_width(32);
  36. set_fixed_height(22);
  37. TextEditor::on_change = [this] {
  38. auto parsed_color = Color::from_string(text());
  39. if (parsed_color.has_value())
  40. set_color_without_changing_text(parsed_color.value());
  41. };
  42. }
  43. ColorInput::~ColorInput()
  44. {
  45. }
  46. Gfx::IntRect ColorInput::color_rect() const
  47. {
  48. auto color_box_padding = 3;
  49. auto color_box_size = height() - color_box_padding - color_box_padding;
  50. return { width() - color_box_size - color_box_padding, color_box_padding, color_box_size, color_box_size };
  51. }
  52. void ColorInput::set_color_without_changing_text(Color color)
  53. {
  54. if (m_color == color)
  55. return;
  56. m_color = color;
  57. update();
  58. if (on_change)
  59. on_change();
  60. }
  61. void ColorInput::set_color(Color color)
  62. {
  63. if (m_color == color)
  64. return;
  65. set_text(m_color_has_alpha_channel ? color.to_string() : color.to_string_without_alpha());
  66. };
  67. void ColorInput::mousedown_event(MouseEvent& event)
  68. {
  69. if (event.button() == MouseButton::Left && color_rect().contains(event.position())) {
  70. m_may_be_color_rect_click = true;
  71. return;
  72. }
  73. TextEditor::mousedown_event(event);
  74. }
  75. void ColorInput::mouseup_event(MouseEvent& event)
  76. {
  77. if (event.button() == MouseButton::Left) {
  78. bool is_color_rect_click = m_may_be_color_rect_click && color_rect().contains(event.position());
  79. m_may_be_color_rect_click = false;
  80. if (is_color_rect_click) {
  81. auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title);
  82. dialog->set_color_has_alpha_channel(m_color_has_alpha_channel);
  83. if (dialog->exec() == GUI::Dialog::ExecOK)
  84. set_color(dialog->color());
  85. event.accept();
  86. return;
  87. }
  88. }
  89. TextEditor::mouseup_event(event);
  90. }
  91. void ColorInput::mousemove_event(MouseEvent& event)
  92. {
  93. if (color_rect().contains(event.position())) {
  94. set_override_cursor(Gfx::StandardCursor::Hand);
  95. event.accept();
  96. return;
  97. } else {
  98. set_override_cursor(Gfx::StandardCursor::IBeam);
  99. }
  100. TextEditor::mousemove_event(event);
  101. }
  102. void ColorInput::paint_event(PaintEvent& event)
  103. {
  104. TextEditor::paint_event(event);
  105. Painter painter(*this);
  106. painter.add_clip_rect(event.rect());
  107. painter.fill_rect(color_rect(), m_color);
  108. painter.draw_rect(color_rect(), Color::Black);
  109. }
  110. }