diff options
author | Andreas Kling <kling@serenityos.org> | 2021-06-14 17:45:59 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-14 18:25:17 +0200 |
commit | 4cecd7900036554c7d153ca8b876bbc49b3bd31f (patch) | |
tree | 20b9597d09fcf4c682613d12253f99040245d118 /Userland/Applications/PixelPaint/Selection.cpp | |
parent | 1b897ec561f4731353a025ec028e1fbab7f335a2 (diff) | |
download | serenity-4cecd7900036554c7d153ca8b876bbc49b3bd31f.zip |
PixelPaint: Draw the current editor selection as marching ants
This patch moves the marching ants painting code to Selection and
unifies the timer mechanism so that all marching ants are synchronized
which looks neat. :^)
Diffstat (limited to 'Userland/Applications/PixelPaint/Selection.cpp')
-rw-r--r-- | Userland/Applications/PixelPaint/Selection.cpp | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/Userland/Applications/PixelPaint/Selection.cpp b/Userland/Applications/PixelPaint/Selection.cpp index db93e751f0..6e645e9ddb 100644 --- a/Userland/Applications/PixelPaint/Selection.cpp +++ b/Userland/Applications/PixelPaint/Selection.cpp @@ -10,9 +10,50 @@ namespace PixelPaint { +constexpr int marching_ant_length = 4; + void Selection::paint(Gfx::Painter& painter, ImageEditor const& editor) { - painter.draw_rect(editor.image_rect_to_editor_rect(m_rect).to_type<int>(), Color::Magenta); + draw_marching_ants(painter, editor.image_rect_to_editor_rect(m_rect).to_type<int>()); +} + +Selection::Selection(ImageEditor& editor) + : m_editor(editor) +{ + m_marching_ants_timer = Core::Timer::create_repeating(80, [this] { + ++m_marching_ants_offset; + m_marching_ants_offset %= marching_ant_length; + if (!is_empty()) + m_editor.update(); + }); + m_marching_ants_timer->start(); +} + +void Selection::draw_marching_ants(Gfx::Painter& painter, Gfx::IntRect const& rect) const +{ + int offset = m_marching_ants_offset; + + auto draw_pixel = [&](int x, int y) { + if ((offset % marching_ant_length) != 0) + painter.set_pixel(x, y, Color::Black); + offset++; + }; + + // Top line + for (int x = rect.left(); x <= rect.right(); ++x) + draw_pixel(x, rect.top()); + + // Right line + for (int y = rect.top() + 1; y <= rect.bottom(); ++y) + draw_pixel(rect.right(), y); + + // Bottom line + for (int x = rect.right() - 1; x >= rect.left(); --x) + draw_pixel(x, rect.bottom()); + + // Left line + for (int y = rect.bottom() - 1; y > rect.top(); --y) + draw_pixel(rect.left(), y); } } |