ColorInput.cpp 4.2 KB

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