mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 03:58:12 +00:00
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:
parent
a04ab219d1
commit
e87756424d
12 changed files with 166 additions and 0 deletions
20
DevTools/HackStudio/CursorTool.cpp
Normal file
20
DevTools/HackStudio/CursorTool.cpp
Normal 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";
|
||||||
|
}
|
18
DevTools/HackStudio/CursorTool.h
Normal file
18
DevTools/HackStudio/CursorTool.h
Normal 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;
|
||||||
|
};
|
|
@ -1,9 +1,11 @@
|
||||||
#include "FormEditorWidget.h"
|
#include "FormEditorWidget.h"
|
||||||
|
#include "CursorTool.h"
|
||||||
#include "FormWidget.h"
|
#include "FormWidget.h"
|
||||||
#include <LibGUI/GPainter.h>
|
#include <LibGUI/GPainter.h>
|
||||||
|
|
||||||
FormEditorWidget::FormEditorWidget(GWidget* parent)
|
FormEditorWidget::FormEditorWidget(GWidget* parent)
|
||||||
: GScrollableWidget(parent)
|
: GScrollableWidget(parent)
|
||||||
|
, m_tool(make<CursorTool>(*this))
|
||||||
{
|
{
|
||||||
set_fill_with_background_color(true);
|
set_fill_with_background_color(true);
|
||||||
set_background_color(Color::MidGray);
|
set_background_color(Color::MidGray);
|
||||||
|
@ -26,3 +28,10 @@ void FormEditorWidget::paint_event(GPaintEvent& event)
|
||||||
GPainter painter(*this);
|
GPainter painter(*this);
|
||||||
painter.add_clip_rect(event.rect());
|
painter.add_clip_rect(event.rect());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FormEditorWidget::set_tool(NonnullOwnPtr<Tool> tool)
|
||||||
|
{
|
||||||
|
m_tool->detach();
|
||||||
|
m_tool = move(tool);
|
||||||
|
m_tool->attach();
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <LibGUI/GScrollableWidget.h>
|
#include <LibGUI/GScrollableWidget.h>
|
||||||
|
|
||||||
class FormWidget;
|
class FormWidget;
|
||||||
|
class Tool;
|
||||||
|
|
||||||
class FormEditorWidget final : public GScrollableWidget {
|
class FormEditorWidget final : public GScrollableWidget {
|
||||||
C_OBJECT(FormEditorWidget)
|
C_OBJECT(FormEditorWidget)
|
||||||
|
@ -12,10 +13,17 @@ public:
|
||||||
FormWidget& form_widget() { return *m_form_widget; }
|
FormWidget& form_widget() { return *m_form_widget; }
|
||||||
const FormWidget& form_widget() const { 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:
|
private:
|
||||||
virtual void paint_event(GPaintEvent&) override;
|
virtual void paint_event(GPaintEvent&) override;
|
||||||
|
|
||||||
explicit FormEditorWidget(GWidget* parent);
|
explicit FormEditorWidget(GWidget* parent);
|
||||||
|
|
||||||
RefPtr<FormWidget> m_form_widget;
|
RefPtr<FormWidget> m_form_widget;
|
||||||
|
|
||||||
|
NonnullOwnPtr<Tool> m_tool;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "FormWidget.h"
|
#include "FormWidget.h"
|
||||||
#include "FormEditorWidget.h"
|
#include "FormEditorWidget.h"
|
||||||
|
#include "Tool.h"
|
||||||
#include <LibGUI/GPainter.h>
|
#include <LibGUI/GPainter.h>
|
||||||
|
|
||||||
FormWidget::FormWidget(FormEditorWidget& parent)
|
FormWidget::FormWidget(FormEditorWidget& parent)
|
||||||
|
@ -8,12 +9,24 @@ FormWidget::FormWidget(FormEditorWidget& parent)
|
||||||
set_fill_with_background_color(true);
|
set_fill_with_background_color(true);
|
||||||
set_background_color(Color::WarmGray);
|
set_background_color(Color::WarmGray);
|
||||||
set_relative_rect(5, 5, 400, 300);
|
set_relative_rect(5, 5, 400, 300);
|
||||||
|
|
||||||
|
set_greedy_for_hits(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
FormWidget::~FormWidget()
|
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)
|
void FormWidget::paint_event(GPaintEvent& event)
|
||||||
{
|
{
|
||||||
GPainter painter(*this);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,9 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void paint_event(GPaintEvent&) override;
|
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);
|
explicit FormWidget(FormEditorWidget& parent);
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,9 @@ OBJS = \
|
||||||
Editor.o \
|
Editor.o \
|
||||||
EditorWrapper.o \
|
EditorWrapper.o \
|
||||||
Locator.o \
|
Locator.o \
|
||||||
|
Tool.o \
|
||||||
|
CursorTool.o \
|
||||||
|
WidgetTool.o \
|
||||||
main.o
|
main.o
|
||||||
|
|
||||||
APP = HackStudio
|
APP = HackStudio
|
||||||
|
|
0
DevTools/HackStudio/Tool.cpp
Normal file
0
DevTools/HackStudio/Tool.cpp
Normal file
30
DevTools/HackStudio/Tool.h
Normal file
30
DevTools/HackStudio/Tool.h
Normal 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;
|
||||||
|
};
|
20
DevTools/HackStudio/WidgetTool.cpp
Normal file
20
DevTools/HackStudio/WidgetTool.cpp
Normal 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";
|
||||||
|
}
|
23
DevTools/HackStudio/WidgetTool.h
Normal file
23
DevTools/HackStudio/WidgetTool.h
Normal 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;
|
||||||
|
};
|
|
@ -1,4 +1,6 @@
|
||||||
#include "CppLexer.h"
|
#include "CppLexer.h"
|
||||||
|
#include "CursorTool.h"
|
||||||
|
#include "WidgetTool.h"
|
||||||
#include "Editor.h"
|
#include "Editor.h"
|
||||||
#include "EditorWrapper.h"
|
#include "EditorWrapper.h"
|
||||||
#include "FindInFilesWidget.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->set_preferred_size(38, 0);
|
||||||
|
|
||||||
form_widgets_toolbar->add_action(GAction::create("Cursor", GraphicsBitmap::load_from_file("/res/icons/widgets/Cursor.png"), [&](auto&) {
|
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) {
|
GWidgetClassRegistration::for_each([&](const GWidgetClassRegistration& reg) {
|
||||||
auto icon_path = String::format("/res/icons/widgets/%s.png", reg.class_name().characters());
|
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&) {
|
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());
|
auto widget = reg.construct(&g_form_editor_widget->form_widget());
|
||||||
widget->set_relative_rect(30, 30, 30, 30);
|
widget->set_relative_rect(30, 30, 30, 30);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue