1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-08 23:07:35 +00:00

HackStudio: Allow rubber-band selection of widgets

This patch implements basic rubber-banding. Perhaps this mechanism can
be generalized somehow, but it's not clear to me how that would work
at the moment.
This commit is contained in:
Andreas Kling 2019-11-16 19:17:27 +01:00
parent 196b64c0ae
commit 69dee20761
3 changed files with 91 additions and 10 deletions

View file

@ -41,17 +41,20 @@ void FormWidget::paint_event(GPaintEvent& event)
void FormWidget::second_paint_event(GPaintEvent& event)
{
if (editor().selection().is_empty())
return;
GPainter painter(*this);
painter.add_clip_rect(event.rect());
for_each_child_widget([&](auto& child) {
if (editor().selection().contains(child)) {
painter.draw_rect(child.relative_rect(), Color::Blue);
}
return IterationDecision::Continue;
});
if (!editor().selection().is_empty()) {
for_each_child_widget([&](auto& child) {
if (editor().selection().contains(child)) {
painter.draw_rect(child.relative_rect(), Color::Blue);
}
return IterationDecision::Continue;
});
}
if (m_rubber_banding)
painter.draw_rect(rubber_band_rect(), Color::MidMagenta);
}
void FormWidget::mousedown_event(GMouseEvent& event)
@ -73,3 +76,59 @@ 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);
}