mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:17:36 +00:00
PixelPaint: Add ability to draw rectangle from center
Essentially the same logic as the prior commit, but now for the `RectangleTool` class. Pressing `alt` lets you draw the rectangle with the starting position as the center.
This commit is contained in:
parent
1a3481f35b
commit
6ccdc018b4
2 changed files with 24 additions and 8 deletions
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -25,8 +26,16 @@ RectangleTool::~RectangleTool()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void RectangleTool::draw_using(GUI::Painter& painter, Gfx::IntRect const& rect)
|
void RectangleTool::draw_using(GUI::Painter& painter, Gfx::IntPoint const& start_position, Gfx::IntPoint const& end_position)
|
||||||
{
|
{
|
||||||
|
Gfx::IntRect rect;
|
||||||
|
if (m_draw_mode == DrawMode::FromCenter) {
|
||||||
|
auto delta = end_position - start_position;
|
||||||
|
rect = Gfx::IntRect::from_two_points(start_position - delta, end_position);
|
||||||
|
} else {
|
||||||
|
rect = Gfx::IntRect::from_two_points(start_position, end_position);
|
||||||
|
}
|
||||||
|
|
||||||
switch (m_mode) {
|
switch (m_mode) {
|
||||||
case Mode::Fill:
|
case Mode::Fill:
|
||||||
painter.fill_rect(rect, m_editor->color_for(m_drawing_button));
|
painter.fill_rect(rect, m_editor->color_for(m_drawing_button));
|
||||||
|
@ -67,8 +76,7 @@ void RectangleTool::on_mouseup(Layer* layer, MouseEvent& event)
|
||||||
|
|
||||||
if (event.layer_event().button() == m_drawing_button) {
|
if (event.layer_event().button() == m_drawing_button) {
|
||||||
GUI::Painter painter(layer->bitmap());
|
GUI::Painter painter(layer->bitmap());
|
||||||
auto rect = Gfx::IntRect::from_two_points(m_rectangle_start_position, m_rectangle_end_position);
|
draw_using(painter, m_rectangle_start_position, m_rectangle_end_position);
|
||||||
draw_using(painter, rect);
|
|
||||||
m_drawing_button = GUI::MouseButton::None;
|
m_drawing_button = GUI::MouseButton::None;
|
||||||
layer->did_modify_bitmap();
|
layer->did_modify_bitmap();
|
||||||
m_editor->did_complete_action();
|
m_editor->did_complete_action();
|
||||||
|
@ -83,6 +91,8 @@ void RectangleTool::on_mousemove(Layer* layer, MouseEvent& event)
|
||||||
if (m_drawing_button == GUI::MouseButton::None)
|
if (m_drawing_button == GUI::MouseButton::None)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
m_draw_mode = event.layer_event().alt() ? DrawMode::FromCenter : DrawMode::FromCorner;
|
||||||
|
|
||||||
m_rectangle_end_position = event.layer_event().position();
|
m_rectangle_end_position = event.layer_event().position();
|
||||||
m_editor->update();
|
m_editor->update();
|
||||||
}
|
}
|
||||||
|
@ -94,10 +104,9 @@ void RectangleTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
|
||||||
|
|
||||||
GUI::Painter painter(*m_editor);
|
GUI::Painter painter(*m_editor);
|
||||||
painter.add_clip_rect(event.rect());
|
painter.add_clip_rect(event.rect());
|
||||||
auto rect = Gfx::IntRect::from_two_points(
|
auto start_position = m_editor->layer_position_to_editor_position(*layer, m_rectangle_start_position).to_type<int>();
|
||||||
m_editor->layer_position_to_editor_position(*layer, m_rectangle_start_position).to_type<int>(),
|
auto end_position = m_editor->layer_position_to_editor_position(*layer, m_rectangle_end_position).to_type<int>();
|
||||||
m_editor->layer_position_to_editor_position(*layer, m_rectangle_end_position).to_type<int>());
|
draw_using(painter, start_position, end_position);
|
||||||
draw_using(painter, rect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RectangleTool::on_keydown(GUI::KeyEvent& event)
|
void RectangleTool::on_keydown(GUI::KeyEvent& event)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -32,13 +33,19 @@ private:
|
||||||
Gradient,
|
Gradient,
|
||||||
};
|
};
|
||||||
|
|
||||||
void draw_using(GUI::Painter&, Gfx::IntRect const&);
|
enum class DrawMode {
|
||||||
|
FromCenter,
|
||||||
|
FromCorner,
|
||||||
|
};
|
||||||
|
|
||||||
|
void draw_using(GUI::Painter&, Gfx::IntPoint const& start_position, Gfx::IntPoint const& end_position);
|
||||||
|
|
||||||
RefPtr<GUI::Widget> m_properties_widget;
|
RefPtr<GUI::Widget> m_properties_widget;
|
||||||
GUI::MouseButton m_drawing_button { GUI::MouseButton::None };
|
GUI::MouseButton m_drawing_button { GUI::MouseButton::None };
|
||||||
Gfx::IntPoint m_rectangle_start_position;
|
Gfx::IntPoint m_rectangle_start_position;
|
||||||
Gfx::IntPoint m_rectangle_end_position;
|
Gfx::IntPoint m_rectangle_end_position;
|
||||||
Mode m_mode { Mode::Outline };
|
Mode m_mode { Mode::Outline };
|
||||||
|
DrawMode m_draw_mode { DrawMode::FromCorner };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue