1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:18:11 +00:00

HackStudio: Move the rubber-banding state into CursorTool

I originally put it in FormWidget because CursorTool was clueless about
painting. This patch adds Tool::on_second_paint() which allows all the
tools to hook into the second paint pass.
This commit is contained in:
Andreas Kling 2019-11-16 22:26:46 +01:00
parent 5fc62bcf68
commit 197ed1bb2a
5 changed files with 68 additions and 77 deletions

View file

@ -53,10 +53,7 @@ void FormWidget::second_paint_event(GPaintEvent& event)
});
}
if (m_rubber_banding) {
painter.fill_rect(rubber_band_rect(), Color(244, 202, 158, 60));
painter.draw_rect(rubber_band_rect(), Color(110, 34, 9));
}
editor().tool().on_second_paint(painter, event);
}
void FormWidget::mousedown_event(GMouseEvent& event)
@ -78,59 +75,3 @@ void FormWidget::keydown_event(GKeyEvent& event)
{
editor().tool().on_keydown(event);
}
void FormWidget::set_rubber_banding(Badge<CursorTool>, bool value)
{
if (m_rubber_banding == value)
return;
m_rubber_banding = value;
update();
}
void FormWidget::set_rubber_band_origin(Badge<CursorTool>, const Point& origin)
{
if (m_rubber_band_origin == origin)
return;
m_rubber_band_origin = origin;
m_rubber_band_position = origin;
update();
}
void FormWidget::set_rubber_band_position(Badge<CursorTool>, const Point& position)
{
if (m_rubber_band_position == position)
return;
m_rubber_band_position = position;
auto rubber_band_rect = this->rubber_band_rect();
editor().selection().clear();
for_each_child_widget([&](auto& child) {
if (child.relative_rect().intersects(rubber_band_rect))
editor().selection().add(child);
return IterationDecision::Continue;
});
update();
}
static Rect rect_from_two_points(const Point& a, const Point& b)
{
if (a.x() <= b.x()) {
if (a.y() <= b.y())
return { a, { b.x() - a.x(), b.y() - a.y() } };
int height = a.y() - b.y();
return { a.x(), a.y() - height, b.x() - a.x(), height };
}
if (a.y() >= b.y())
return { b, { a.x() - b.x(), a.y() - b.y() } };
int height = b.y() - a.y();
return { b.x(), b.y() - height, a.x() - b.x(), height };
}
Rect FormWidget::rubber_band_rect() const
{
if (!m_rubber_banding)
return {};
return rect_from_two_points(m_rubber_band_origin, m_rubber_band_position);
}