diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-11-10 21:45:32 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-11-10 21:45:32 +0100 |
commit | e87756424d3fb0724930425e7cffa83fce073e7a (patch) | |
tree | da32d508315211123e5df6b3e06e43a1add6e287 | |
parent | a04ab219d16d3dc800a693ee1ff1db62cb84f709 (diff) | |
download | serenity-e87756424d3fb0724930425e7cffa83fce073e7a.zip |
HackStudio: Introduce a Tool class with subs CursorTool and WidgetTool
These will be used to draw out new widgets on a FormWidget, or in the
case of CursorTool, to select and manipulate existing widgets.
-rw-r--r-- | DevTools/HackStudio/CursorTool.cpp | 20 | ||||
-rw-r--r-- | DevTools/HackStudio/CursorTool.h | 18 | ||||
-rw-r--r-- | DevTools/HackStudio/FormEditorWidget.cpp | 9 | ||||
-rw-r--r-- | DevTools/HackStudio/FormEditorWidget.h | 8 | ||||
-rw-r--r-- | DevTools/HackStudio/FormWidget.cpp | 28 | ||||
-rw-r--r-- | DevTools/HackStudio/FormWidget.h | 3 | ||||
-rw-r--r-- | DevTools/HackStudio/Makefile | 3 | ||||
-rw-r--r-- | DevTools/HackStudio/Tool.cpp | 0 | ||||
-rw-r--r-- | DevTools/HackStudio/Tool.h | 30 | ||||
-rw-r--r-- | DevTools/HackStudio/WidgetTool.cpp | 20 | ||||
-rw-r--r-- | DevTools/HackStudio/WidgetTool.h | 23 | ||||
-rw-r--r-- | DevTools/HackStudio/main.cpp | 4 |
12 files changed, 166 insertions, 0 deletions
diff --git a/DevTools/HackStudio/CursorTool.cpp b/DevTools/HackStudio/CursorTool.cpp new file mode 100644 index 0000000000..9eff4102de --- /dev/null +++ b/DevTools/HackStudio/CursorTool.cpp @@ -0,0 +1,20 @@ +#include "CursorTool.h" +#include <AK/LogStream.h> + +void CursorTool::on_mousedown(GMouseEvent& event) +{ + (void)event; + dbg() << "CursorTool::on_mousedown"; +} + +void CursorTool::on_mouseup(GMouseEvent& event) +{ + (void)event; + dbg() << "CursorTool::on_mouseup"; +} + +void CursorTool::on_mousemove(GMouseEvent& event) +{ + (void)event; + dbg() << "CursorTool::on_mousemove"; +} diff --git a/DevTools/HackStudio/CursorTool.h b/DevTools/HackStudio/CursorTool.h new file mode 100644 index 0000000000..2b9cbcc3bc --- /dev/null +++ b/DevTools/HackStudio/CursorTool.h @@ -0,0 +1,18 @@ +#pragma once + +#include "Tool.h" + +class CursorTool final : public Tool { +public: + explicit CursorTool(FormEditorWidget& editor) + : Tool(editor) + { + } + virtual ~CursorTool() override {} + +private: + virtual const char* class_name() const override { return "CursorTool"; } + virtual void on_mousedown(GMouseEvent&) override; + virtual void on_mouseup(GMouseEvent&) override; + virtual void on_mousemove(GMouseEvent&) override; +}; diff --git a/DevTools/HackStudio/FormEditorWidget.cpp b/DevTools/HackStudio/FormEditorWidget.cpp index 80cade9366..894454b2d9 100644 --- a/DevTools/HackStudio/FormEditorWidget.cpp +++ b/DevTools/HackStudio/FormEditorWidget.cpp @@ -1,9 +1,11 @@ #include "FormEditorWidget.h" +#include "CursorTool.h" #include "FormWidget.h" #include <LibGUI/GPainter.h> FormEditorWidget::FormEditorWidget(GWidget* parent) : GScrollableWidget(parent) + , m_tool(make<CursorTool>(*this)) { set_fill_with_background_color(true); set_background_color(Color::MidGray); @@ -26,3 +28,10 @@ void FormEditorWidget::paint_event(GPaintEvent& event) GPainter painter(*this); painter.add_clip_rect(event.rect()); } + +void FormEditorWidget::set_tool(NonnullOwnPtr<Tool> tool) +{ + m_tool->detach(); + m_tool = move(tool); + m_tool->attach(); +} diff --git a/DevTools/HackStudio/FormEditorWidget.h b/DevTools/HackStudio/FormEditorWidget.h index 7f04cf4248..f760031da7 100644 --- a/DevTools/HackStudio/FormEditorWidget.h +++ b/DevTools/HackStudio/FormEditorWidget.h @@ -3,6 +3,7 @@ #include <LibGUI/GScrollableWidget.h> class FormWidget; +class Tool; class FormEditorWidget final : public GScrollableWidget { C_OBJECT(FormEditorWidget) @@ -12,10 +13,17 @@ public: FormWidget& form_widget() { return *m_form_widget; } const FormWidget& form_widget() const { return *m_form_widget; } + Tool& tool() { return *m_tool; } + const Tool& tool() const { return *m_tool; } + + void set_tool(NonnullOwnPtr<Tool>); + private: virtual void paint_event(GPaintEvent&) override; explicit FormEditorWidget(GWidget* parent); RefPtr<FormWidget> m_form_widget; + + NonnullOwnPtr<Tool> m_tool; }; diff --git a/DevTools/HackStudio/FormWidget.cpp b/DevTools/HackStudio/FormWidget.cpp index 8892e45f44..a798680909 100644 --- a/DevTools/HackStudio/FormWidget.cpp +++ b/DevTools/HackStudio/FormWidget.cpp @@ -1,5 +1,6 @@ #include "FormWidget.h" #include "FormEditorWidget.h" +#include "Tool.h" #include <LibGUI/GPainter.h> FormWidget::FormWidget(FormEditorWidget& parent) @@ -8,12 +9,24 @@ FormWidget::FormWidget(FormEditorWidget& parent) set_fill_with_background_color(true); set_background_color(Color::WarmGray); set_relative_rect(5, 5, 400, 300); + + set_greedy_for_hits(true); } FormWidget::~FormWidget() { } +FormEditorWidget& FormWidget::editor() +{ + return static_cast<FormEditorWidget&>(*parent()); +} + +const FormEditorWidget& FormWidget::editor() const +{ + return static_cast<const FormEditorWidget&>(*parent()); +} + void FormWidget::paint_event(GPaintEvent& event) { GPainter painter(*this); @@ -25,3 +38,18 @@ void FormWidget::paint_event(GPaintEvent& event) } } } + +void FormWidget::mousedown_event(GMouseEvent& event) +{ + editor().tool().on_mousedown(event); +} + +void FormWidget::mouseup_event(GMouseEvent& event) +{ + editor().tool().on_mouseup(event); +} + +void FormWidget::mousemove_event(GMouseEvent& event) +{ + editor().tool().on_mousemove(event); +} diff --git a/DevTools/HackStudio/FormWidget.h b/DevTools/HackStudio/FormWidget.h index c309b7e6a1..cb60006fc5 100644 --- a/DevTools/HackStudio/FormWidget.h +++ b/DevTools/HackStudio/FormWidget.h @@ -14,6 +14,9 @@ public: private: virtual void paint_event(GPaintEvent&) override; + virtual void mousedown_event(GMouseEvent&) override; + virtual void mouseup_event(GMouseEvent&) override; + virtual void mousemove_event(GMouseEvent&) override; explicit FormWidget(FormEditorWidget& parent); diff --git a/DevTools/HackStudio/Makefile b/DevTools/HackStudio/Makefile index 964f5dd86c..ca2c4fabec 100644 --- a/DevTools/HackStudio/Makefile +++ b/DevTools/HackStudio/Makefile @@ -12,6 +12,9 @@ OBJS = \ Editor.o \ EditorWrapper.o \ Locator.o \ + Tool.o \ + CursorTool.o \ + WidgetTool.o \ main.o APP = HackStudio diff --git a/DevTools/HackStudio/Tool.cpp b/DevTools/HackStudio/Tool.cpp new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/DevTools/HackStudio/Tool.cpp diff --git a/DevTools/HackStudio/Tool.h b/DevTools/HackStudio/Tool.h new file mode 100644 index 0000000000..75911209e1 --- /dev/null +++ b/DevTools/HackStudio/Tool.h @@ -0,0 +1,30 @@ +#pragma once + +#include <AK/Noncopyable.h> + +class FormEditorWidget; +class GMouseEvent; + +class Tool { + AK_MAKE_NONCOPYABLE(Tool) + AK_MAKE_NONMOVABLE(Tool) +public: + virtual ~Tool() {} + + virtual void on_mousedown(GMouseEvent&) = 0; + virtual void on_mouseup(GMouseEvent&) = 0; + virtual void on_mousemove(GMouseEvent&) = 0; + + virtual const char* class_name() const = 0; + + virtual void attach() {} + virtual void detach() {} + +protected: + explicit Tool(FormEditorWidget& editor) + : m_editor(editor) + { + } + + FormEditorWidget& m_editor; +}; diff --git a/DevTools/HackStudio/WidgetTool.cpp b/DevTools/HackStudio/WidgetTool.cpp new file mode 100644 index 0000000000..032e3688df --- /dev/null +++ b/DevTools/HackStudio/WidgetTool.cpp @@ -0,0 +1,20 @@ +#include "WidgetTool.h" +#include <AK/LogStream.h> + +void WidgetTool::on_mousedown(GMouseEvent& event) +{ + (void)event; + dbg() << "WidgetTool::on_mousedown"; +} + +void WidgetTool::on_mouseup(GMouseEvent& event) +{ + (void)event; + dbg() << "WidgetTool::on_mouseup"; +} + +void WidgetTool::on_mousemove(GMouseEvent& event) +{ + (void)event; + dbg() << "WidgetTool::on_mousemove"; +} diff --git a/DevTools/HackStudio/WidgetTool.h b/DevTools/HackStudio/WidgetTool.h new file mode 100644 index 0000000000..24b9946ebf --- /dev/null +++ b/DevTools/HackStudio/WidgetTool.h @@ -0,0 +1,23 @@ +#pragma once + +#include "Tool.h" + +class GWidgetClassRegistration; + +class WidgetTool final : public Tool { +public: + explicit WidgetTool(FormEditorWidget& editor, const GWidgetClassRegistration& meta_class) + : Tool(editor) + , m_meta_class(meta_class) + { + } + virtual ~WidgetTool() override {} + +private: + virtual const char* class_name() const override { return "WidgetTool"; } + virtual void on_mousedown(GMouseEvent&) override; + virtual void on_mouseup(GMouseEvent&) override; + virtual void on_mousemove(GMouseEvent&) override; + + const GWidgetClassRegistration& m_meta_class; +}; diff --git a/DevTools/HackStudio/main.cpp b/DevTools/HackStudio/main.cpp index 201ecc6d1f..0972676931 100644 --- a/DevTools/HackStudio/main.cpp +++ b/DevTools/HackStudio/main.cpp @@ -1,4 +1,6 @@ #include "CppLexer.h" +#include "CursorTool.h" +#include "WidgetTool.h" #include "Editor.h" #include "EditorWrapper.h" #include "FindInFilesWidget.h" @@ -129,11 +131,13 @@ int main(int argc, char** argv) form_widgets_toolbar->set_preferred_size(38, 0); form_widgets_toolbar->add_action(GAction::create("Cursor", GraphicsBitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) { + g_form_editor_widget->set_tool(make<CursorTool>(*g_form_editor_widget)); })); GWidgetClassRegistration::for_each([&](const GWidgetClassRegistration& reg) { auto icon_path = String::format("/res/icons/widgets/%s.png", reg.class_name().characters()); auto action = GAction::create(reg.class_name(), GraphicsBitmap::load_from_file(icon_path), [®](auto&) { + g_form_editor_widget->set_tool(make<WidgetTool>(*g_form_editor_widget, reg)); auto widget = reg.construct(&g_form_editor_widget->form_widget()); widget->set_relative_rect(30, 30, 30, 30); }); |