Skip to content

Commit

Permalink
PixelPaint: Add new selection moving modes
Browse files Browse the repository at this point in the history
If you press "spacebar" while moving a selection, it will now move the
origin point of the selection; and if you press "control" it will move
it relatively to the center.
  • Loading branch information
noftaly authored and awesomekling committed Jun 17, 2021
1 parent e2215cc commit 9087378
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Userland/Applications/PixelPaint/RectangleSelectTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ void RectangleSelectTool::on_mousemove(Layer&, GUI::MouseEvent&, GUI::MouseEvent
if (!m_selecting)
return;

if (m_moving_mode != MovingMode::None) {
auto delta = m_selection_end - image_event.position();
if (m_moving_mode == MovingMode::MovingOrigin)
m_selection_start -= delta;
else if (m_moving_mode == MovingMode::AroundCenter)
m_selection_start += delta;
}

m_selection_end = image_event.position();
m_editor->update();
}
Expand All @@ -55,6 +63,22 @@ void RectangleSelectTool::on_mouseup(Layer&, GUI::MouseEvent&, GUI::MouseEvent&
m_editor->selection().set(rect_in_image);
}

void RectangleSelectTool::on_keydown(GUI::KeyEvent& key_event)
{
if (key_event.key() == KeyCode::Key_Space)
m_moving_mode = MovingMode::MovingOrigin;
else if (key_event.key() == KeyCode::Key_Control)
m_moving_mode = MovingMode::AroundCenter;
}

void RectangleSelectTool::on_keyup(GUI::KeyEvent& key_event)
{
if (key_event.key() == KeyCode::Key_Space && m_moving_mode == MovingMode::MovingOrigin)
m_moving_mode = MovingMode::None;
else if (key_event.key() == KeyCode::Key_Control && m_moving_mode == MovingMode::AroundCenter)
m_moving_mode = MovingMode::None;
}

void RectangleSelectTool::on_second_paint(Layer const&, GUI::PaintEvent& event)
{
if (!m_selecting)
Expand Down
9 changes: 9 additions & 0 deletions Userland/Applications/PixelPaint/RectangleSelectTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ class RectangleSelectTool final : public Tool {
virtual void on_mousedown(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) override;
virtual void on_mousemove(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) override;
virtual void on_keydown(GUI::KeyEvent&) override;
virtual void on_keyup(GUI::KeyEvent&) override;
virtual void on_second_paint(Layer const&, GUI::PaintEvent&) override;

private:
enum class MovingMode {
MovingOrigin,
AroundCenter,
None,
};

bool m_selecting { false };
MovingMode m_moving_mode { MovingMode::None };
Gfx::IntPoint m_selection_start;
Gfx::IntPoint m_selection_end;
};
Expand Down

0 comments on commit 9087378

Please sign in to comment.