ColorInput.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. TextEditor::on_change = [this] {
  36. auto parsed_color = Color::from_string(text());
  37. if (parsed_color.has_value())
  38. set_color_without_changing_text(parsed_color.value());
  39. };
  40. }
  41. ColorInput::~ColorInput()
  42. {
  43. }
  44. Gfx::IntRect ColorInput::color_rect() const
  45. {
  46. auto color_box_padding = 3;
  47. auto color_box_size = height() - color_box_padding - color_box_padding;
  48. return { width() - color_box_size - color_box_padding, color_box_padding, color_box_size, color_box_size };
  49. }
  50. void ColorInput::set_color_without_changing_text(Color color)
  51. {
  52. if (m_color == color)
  53. return;
  54. m_color = color;
  55. update();
  56. if (on_change)
  57. on_change();
  58. }
  59. void ColorInput::set_color(Color color)
  60. {
  61. if (m_color == color)
  62. return;
  63. set_text(m_color_has_alpha_channel ? color.to_string() : color.to_string_without_alpha());
  64. };
  65. void ColorInput::mousedown_event(MouseEvent& event)
  66. {
  67. if (event.button() == MouseButton::Left && color_rect().contains(event.position())) {
  68. m_may_be_color_rect_click = true;
  69. return;
  70. }
  71. TextEditor::mousedown_event(event);
  72. }
  73. void ColorInput::mouseup_event(MouseEvent& event)
  74. {
  75. if (event.button() == MouseButton::Left) {
  76. bool is_color_rect_click = m_may_be_color_rect_click && color_rect().contains(event.position());
  77. m_may_be_color_rect_click = false;
  78. if (is_color_rect_click) {
  79. auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title);
  80. dialog->set_color_has_alpha_channel(m_color_has_alpha_channel);
  81. if (dialog->exec() == GUI::Dialog::ExecOK)
  82. set_color(dialog->color());
  83. event.accept();
  84. return;
  85. }
  86. }
  87. TextEditor::mouseup_event(event);
  88. }
  89. void ColorInput::mousemove_event(MouseEvent& event)
  90. {
  91. if (color_rect().contains(event.position())) {
  92. set_override_cursor(Gfx::StandardCursor::Hand);
  93. event.accept();
  94. return;
  95. } else {
  96. set_override_cursor(Gfx::StandardCursor::IBeam);
  97. }
  98. TextEditor::mousemove_event(event);
  99. }
  100. void ColorInput::paint_event(PaintEvent& event)
  101. {
  102. TextEditor::paint_event(event);
  103. Painter painter(*this);
  104. painter.add_clip_rect(event.rect());
  105. painter.fill_rect(color_rect(), m_color);
  106. painter.draw_rect(color_rect(), Color::Black);
  107. }
  108. }