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.
This commit is contained in:
Andreas Kling 2019-11-10 21:45:32 +01:00
parent a04ab219d1
commit e87756424d
12 changed files with 166 additions and 0 deletions

View file

@ -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";
}

View file

@ -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;
};

View file

@ -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();
}

View file

@ -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;
};

View file

@ -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);
}

View file

@ -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);

View file

@ -12,6 +12,9 @@ OBJS = \
Editor.o \
EditorWrapper.o \
Locator.o \
Tool.o \
CursorTool.o \
WidgetTool.o \
main.o
APP = HackStudio

View file

View file

@ -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;
};

View file

@ -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";
}

View file

@ -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;
};

View file

@ -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), [&reg](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);
});