2019-11-10 21:45:32 +01:00
|
|
|
#include "CursorTool.h"
|
2019-11-10 22:03:39 +01:00
|
|
|
#include "FormEditorWidget.h"
|
|
|
|
#include "FormWidget.h"
|
2019-11-11 19:13:36 +01:00
|
|
|
#include "WidgetTreeModel.h"
|
2019-11-10 21:45:32 +01:00
|
|
|
#include <AK/LogStream.h>
|
|
|
|
|
|
|
|
void CursorTool::on_mousedown(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
dbg() << "CursorTool::on_mousedown";
|
2019-11-10 22:03:39 +01:00
|
|
|
auto& form_widget = m_editor.form_widget();
|
|
|
|
auto result = form_widget.hit_test(event.position(), GWidget::ShouldRespectGreediness::No);
|
2019-11-10 22:31:10 +01:00
|
|
|
|
|
|
|
if (event.button() == GMouseButton::Left) {
|
|
|
|
if (result.widget && result.widget != &form_widget) {
|
|
|
|
if (event.modifiers() & Mod_Ctrl) {
|
|
|
|
m_editor.selection().toggle(*result.widget);
|
|
|
|
} else if (!event.modifiers()) {
|
|
|
|
if (!m_editor.selection().contains(*result.widget)) {
|
|
|
|
dbg() << "Selection didn't contain " << *result.widget << ", making it the only selected one";
|
|
|
|
m_editor.selection().set(*result.widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_drag_origin = event.position();
|
|
|
|
m_positions_before_drag.clear();
|
|
|
|
m_editor.selection().for_each([&](auto& widget) {
|
|
|
|
m_positions_before_drag.set(&widget, widget.relative_position());
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_editor.selection().clear();
|
2019-11-16 22:26:46 +01:00
|
|
|
m_rubber_banding = true;
|
|
|
|
m_rubber_band_origin = event.position();
|
|
|
|
m_rubber_band_position = event.position();
|
|
|
|
form_widget.update();
|
2019-11-10 22:31:10 +01:00
|
|
|
}
|
2019-11-10 22:03:39 +01:00
|
|
|
// FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
|
|
|
|
form_widget.update();
|
|
|
|
}
|
2019-11-10 21:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorTool::on_mouseup(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
dbg() << "CursorTool::on_mouseup";
|
2019-11-10 22:31:10 +01:00
|
|
|
if (event.button() == GMouseButton::Left) {
|
|
|
|
auto& form_widget = m_editor.form_widget();
|
|
|
|
auto result = form_widget.hit_test(event.position(), GWidget::ShouldRespectGreediness::No);
|
|
|
|
if (!m_dragging && !(event.modifiers() & Mod_Ctrl)) {
|
|
|
|
if (result.widget && result.widget != &form_widget) {
|
|
|
|
m_editor.selection().set(*result.widget);
|
|
|
|
// FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
|
|
|
|
form_widget.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_dragging = false;
|
2019-11-16 22:26:46 +01:00
|
|
|
m_rubber_banding = false;
|
|
|
|
form_widget.update();
|
2019-11-10 22:31:10 +01:00
|
|
|
}
|
2019-11-10 21:45:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorTool::on_mousemove(GMouseEvent& event)
|
|
|
|
{
|
|
|
|
dbg() << "CursorTool::on_mousemove";
|
2019-11-16 19:17:27 +01:00
|
|
|
auto& form_widget = m_editor.form_widget();
|
|
|
|
|
2019-11-16 22:26:46 +01:00
|
|
|
if (m_rubber_banding) {
|
|
|
|
set_rubber_band_position(event.position());
|
2019-11-16 19:17:27 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-11-10 22:31:10 +01:00
|
|
|
|
|
|
|
if (!m_dragging && event.buttons() & GMouseButton::Left && event.position() != m_drag_origin) {
|
|
|
|
auto result = form_widget.hit_test(event.position(), GWidget::ShouldRespectGreediness::No);
|
|
|
|
if (result.widget && result.widget != &form_widget) {
|
|
|
|
if (!m_editor.selection().contains(*result.widget)) {
|
|
|
|
m_editor.selection().set(*result.widget);
|
|
|
|
// FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget?
|
|
|
|
form_widget.update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_dragging = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_dragging) {
|
|
|
|
auto movement_delta = event.position() - m_drag_origin;
|
|
|
|
m_editor.selection().for_each([&](auto& widget) {
|
|
|
|
auto new_rect = widget.relative_rect();
|
|
|
|
new_rect.set_location(m_positions_before_drag.get(&widget).value_or({}).translated(movement_delta));
|
|
|
|
new_rect.set_x(new_rect.x() - (new_rect.x() % m_editor.form_widget().grid_size()));
|
|
|
|
new_rect.set_y(new_rect.y() - (new_rect.y() % m_editor.form_widget().grid_size()));
|
|
|
|
widget.set_relative_rect(new_rect);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2019-11-11 19:13:36 +01:00
|
|
|
m_editor.model().update();
|
2019-11-10 22:31:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-11-10 21:45:32 +01:00
|
|
|
}
|
2019-11-10 22:40:58 +01:00
|
|
|
|
|
|
|
void CursorTool::on_keydown(GKeyEvent& event)
|
|
|
|
{
|
|
|
|
dbg() << "CursorTool::on_keydown";
|
|
|
|
|
|
|
|
auto move_selected_widgets_by = [this](int x, int y) {
|
|
|
|
m_editor.selection().for_each([&](auto& widget) {
|
|
|
|
widget.move_by(x, y);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (event.modifiers() == 0) {
|
|
|
|
switch (event.key()) {
|
|
|
|
case Key_Down:
|
|
|
|
move_selected_widgets_by(0, m_editor.form_widget().grid_size());
|
|
|
|
break;
|
|
|
|
case Key_Up:
|
|
|
|
move_selected_widgets_by(0, -m_editor.form_widget().grid_size());
|
|
|
|
break;
|
|
|
|
case Key_Left:
|
|
|
|
move_selected_widgets_by(-m_editor.form_widget().grid_size(), 0);
|
|
|
|
break;
|
|
|
|
case Key_Right:
|
|
|
|
move_selected_widgets_by(m_editor.form_widget().grid_size(), 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 22:26:46 +01:00
|
|
|
|
|
|
|
void CursorTool::set_rubber_band_position(const Point& position)
|
|
|
|
{
|
|
|
|
if (m_rubber_band_position == position)
|
|
|
|
return;
|
|
|
|
m_rubber_band_position = position;
|
|
|
|
|
|
|
|
auto rubber_band_rect = this->rubber_band_rect();
|
|
|
|
|
|
|
|
m_editor.selection().clear();
|
|
|
|
m_editor.form_widget().for_each_child_widget([&](auto& child) {
|
|
|
|
if (child.relative_rect().intersects(rubber_band_rect))
|
|
|
|
m_editor.selection().add(child);
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
m_editor.form_widget().update();
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect CursorTool::rubber_band_rect() const
|
|
|
|
{
|
|
|
|
if (!m_rubber_banding)
|
|
|
|
return {};
|
2019-11-17 16:36:43 +01:00
|
|
|
return Rect::from_two_points(m_rubber_band_origin, m_rubber_band_position);
|
2019-11-16 22:26:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorTool::on_second_paint(GPainter& painter, GPaintEvent&)
|
|
|
|
{
|
|
|
|
if (!m_rubber_banding)
|
|
|
|
return;
|
|
|
|
auto rect = rubber_band_rect();
|
2020-01-06 01:47:55 +02:00
|
|
|
painter.fill_rect(rect, m_editor.palette().rubber_band_fill());
|
|
|
|
painter.draw_rect(rect, m_editor.palette().rubber_band_border());
|
2019-11-16 22:26:46 +01:00
|
|
|
}
|