PaintBrush: Add a "Tool" menu and put all the tools in it

This commit is contained in:
Andreas Kling 2020-05-13 19:30:07 +02:00
parent cbd746e3ec
commit 98cb442edf
Notes: sideshowbarker 2024-07-19 06:40:14 +09:00
5 changed files with 31 additions and 3 deletions

View file

@ -26,6 +26,7 @@
#include "Tool.h"
#include "ImageEditor.h"
#include <LibGUI/Action.h>
namespace PaintBrush {
@ -42,4 +43,9 @@ void Tool::setup(ImageEditor& editor)
m_editor = editor.make_weak_ptr();
}
void Tool::set_action(GUI::Action* action)
{
m_action = action;
}
}

View file

@ -27,6 +27,7 @@
#pragma once
#include <LibGUI/Event.h>
#include <LibGUI/Forward.h>
namespace PaintBrush {
@ -50,9 +51,13 @@ public:
void clear() { m_editor = nullptr; }
void setup(ImageEditor&);
GUI::Action* action() { return m_action; }
void set_action(GUI::Action*);
protected:
Tool();
WeakPtr<ImageEditor> m_editor;
RefPtr<GUI::Action> m_action;
};
}

View file

@ -44,7 +44,7 @@ namespace PaintBrush {
class ToolButton final : public GUI::Button {
C_OBJECT(ToolButton)
public:
ToolButton(ToolboxWidget& toolbox, const String& name, const GUI::Shortcut& shortcut, OwnPtr<Tool>&& tool)
ToolButton(ToolboxWidget& toolbox, const String& name, const GUI::Shortcut& shortcut, OwnPtr<Tool> tool)
: m_toolbox(toolbox)
, m_tool(move(tool))
{
@ -62,6 +62,7 @@ public:
m_toolbox.on_tool_selection(nullptr);
});
m_tool->set_action(m_action);
set_action(*m_action);
m_toolbox.m_action_group.add_action(*m_action);
}
@ -101,6 +102,7 @@ ToolboxWidget::ToolboxWidget()
m_action_group.set_unchecking_allowed(false);
auto add_tool = [&](const StringView& name, const StringView& icon_name, const GUI::Shortcut& shortcut, NonnullOwnPtr<Tool> tool) -> ToolButton& {
m_tools.append(tool.ptr());
auto& button = add<ToolButton>(*this, name, shortcut, move(tool));
button.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
button.set_preferred_size(0, 32);
@ -110,7 +112,7 @@ ToolboxWidget::ToolboxWidget()
};
add_tool("Move", "move", { 0, Key_M }, make<MoveTool>());
add_tool("Pen", "pen", { 0, Key_N }, make<PenTool>()).set_checked(true);
add_tool("Pen", "pen", { 0, Key_N }, make<PenTool>());
add_tool("Bucket Fill", "bucket", { Mod_Shift, Key_B }, make<BucketTool>());
add_tool("Spray", "spray", { Mod_Shift, Key_S }, make<SprayTool>());
add_tool("Color Picker", "picker", { 0, Key_O }, make<PickerTool>());

View file

@ -40,12 +40,19 @@ public:
Function<void(Tool*)> on_tool_selection;
template<typename Callback>
void for_each_tool(Callback callback)
{
for (auto& tool : m_tools)
callback(*tool);
}
private:
friend class ToolButton;
explicit ToolboxWidget();
GUI::ActionGroup m_action_group;
Vector<Tool*> m_tools;
};
}

View file

@ -28,6 +28,7 @@
#include "ImageEditor.h"
#include "Layer.h"
#include "PaletteWidget.h"
#include "Tool.h"
#include "ToolboxWidget.h"
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
@ -114,6 +115,13 @@ int main(int argc, char** argv)
menubar->add_menu("Edit");
auto& tool_menu = menubar->add_menu("Tool");
toolbox.for_each_tool([&](auto& tool) {
if (tool.action())
tool_menu.add_action(*tool.action());
return IterationDecision::Continue;
});
auto& help_menu = menubar->add_menu("Help");
help_menu.add_action(GUI::Action::create("About", [&](auto&) {
GUI::AboutDialog::show("PaintBrush", Gfx::Bitmap::load_from_file("/res/icons/32x32/app-paintbrush.png"), window);