From 197ed1bb2a56677c6311d440d6246c9cd4b0a767 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 16 Nov 2019 22:26:46 +0100 Subject: [PATCH] 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. --- DevTools/HackStudio/CursorTool.cpp | 62 +++++++++++++++++++++++++++--- DevTools/HackStudio/CursorTool.h | 8 ++++ DevTools/HackStudio/FormWidget.cpp | 61 +---------------------------- DevTools/HackStudio/FormWidget.h | 11 ------ DevTools/HackStudio/Tool.h | 3 ++ 5 files changed, 68 insertions(+), 77 deletions(-) diff --git a/DevTools/HackStudio/CursorTool.cpp b/DevTools/HackStudio/CursorTool.cpp index 33e8b802ee..637e601f33 100644 --- a/DevTools/HackStudio/CursorTool.cpp +++ b/DevTools/HackStudio/CursorTool.cpp @@ -29,8 +29,10 @@ void CursorTool::on_mousedown(GMouseEvent& event) } } else { m_editor.selection().clear(); - form_widget.set_rubber_banding({}, true); - form_widget.set_rubber_band_origin({}, event.position()); + m_rubber_banding = true; + m_rubber_band_origin = event.position(); + m_rubber_band_position = event.position(); + form_widget.update(); } // FIXME: Do we need to update any part of the FormEditorWidget outside the FormWidget? form_widget.update(); @@ -51,7 +53,8 @@ void CursorTool::on_mouseup(GMouseEvent& event) } } m_dragging = false; - form_widget.set_rubber_banding({}, false); + m_rubber_banding = false; + form_widget.update(); } } @@ -60,8 +63,8 @@ void CursorTool::on_mousemove(GMouseEvent& event) dbg() << "CursorTool::on_mousemove"; auto& form_widget = m_editor.form_widget(); - if (form_widget.is_rubber_banding({})) { - form_widget.set_rubber_band_position({}, event.position()); + if (m_rubber_banding) { + set_rubber_band_position(event.position()); return; } @@ -90,7 +93,6 @@ void CursorTool::on_mousemove(GMouseEvent& event) m_editor.model().update(); return; } - } void CursorTool::on_keydown(GKeyEvent& event) @@ -121,3 +123,51 @@ void CursorTool::on_keydown(GKeyEvent& event) } } } + +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(); +} + +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 CursorTool::rubber_band_rect() const +{ + if (!m_rubber_banding) + return {}; + return rect_from_two_points(m_rubber_band_origin, m_rubber_band_position); +} + +void CursorTool::on_second_paint(GPainter& painter, GPaintEvent&) +{ + if (!m_rubber_banding) + return; + auto rect = rubber_band_rect(); + painter.fill_rect(rect, Color(244, 202, 158, 60)); + painter.draw_rect(rect, Color(110, 34, 9)); +} diff --git a/DevTools/HackStudio/CursorTool.h b/DevTools/HackStudio/CursorTool.h index 931b9eaa2a..5d84e500a7 100644 --- a/DevTools/HackStudio/CursorTool.h +++ b/DevTools/HackStudio/CursorTool.h @@ -20,8 +20,16 @@ private: virtual void on_mouseup(GMouseEvent&) override; virtual void on_mousemove(GMouseEvent&) override; virtual void on_keydown(GKeyEvent&) override; + virtual void on_second_paint(GPainter&, GPaintEvent&) override; + + void set_rubber_band_position(const Point&); + Rect rubber_band_rect() const; Point m_drag_origin; HashMap m_positions_before_drag; bool m_dragging { false }; + + bool m_rubber_banding { false }; + Point m_rubber_band_origin; + Point m_rubber_band_position; }; diff --git a/DevTools/HackStudio/FormWidget.cpp b/DevTools/HackStudio/FormWidget.cpp index df042b27ce..7ae3c74096 100644 --- a/DevTools/HackStudio/FormWidget.cpp +++ b/DevTools/HackStudio/FormWidget.cpp @@ -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, bool value) -{ - if (m_rubber_banding == value) - return; - m_rubber_banding = value; - update(); -} - -void FormWidget::set_rubber_band_origin(Badge, 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, 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); -} diff --git a/DevTools/HackStudio/FormWidget.h b/DevTools/HackStudio/FormWidget.h index f9b41bccee..c5fe565d5f 100644 --- a/DevTools/HackStudio/FormWidget.h +++ b/DevTools/HackStudio/FormWidget.h @@ -17,11 +17,6 @@ public: // FIXME: This should be an app-wide preference instead. int grid_size() const { return m_grid_size; } - bool is_rubber_banding(Badge) const { return m_rubber_banding; } - void set_rubber_banding(Badge, bool); - void set_rubber_band_position(Badge, const Point&); - void set_rubber_band_origin(Badge, const Point&); - private: virtual bool accepts_focus() const override { return true; } @@ -34,11 +29,5 @@ private: explicit FormWidget(FormEditorWidget& parent); - Rect rubber_band_rect() const; - int m_grid_size { 5 }; - - bool m_rubber_banding { false }; - Point m_rubber_band_origin; - Point m_rubber_band_position; }; diff --git a/DevTools/HackStudio/Tool.h b/DevTools/HackStudio/Tool.h index 6a9bb1c44a..28a5120ed0 100644 --- a/DevTools/HackStudio/Tool.h +++ b/DevTools/HackStudio/Tool.h @@ -5,6 +5,8 @@ class FormEditorWidget; class GKeyEvent; class GMouseEvent; +class GPaintEvent; +class GPainter; class Tool { AK_MAKE_NONCOPYABLE(Tool) @@ -16,6 +18,7 @@ public: virtual void on_mouseup(GMouseEvent&) = 0; virtual void on_mousemove(GMouseEvent&) = 0; virtual void on_keydown(GKeyEvent&) = 0; + virtual void on_second_paint(GPainter&, GPaintEvent&) {} virtual const char* class_name() const = 0;