PaintBrush: Add move tool context menu with "move to back/front"

Tools can now have an in-image context menu which you get when right-
clicking inside the image editing area. Our first use of this is to
provide the ability to move layers to the back/front. :^)
This commit is contained in:
Andreas Kling 2020-05-13 22:03:29 +02:00
parent 02d9cf2039
commit 720ebee1be
Notes: sideshowbarker 2024-07-19 06:39:46 +09:00
7 changed files with 62 additions and 0 deletions

View file

@ -79,4 +79,29 @@ GUI::Model& Image::layer_model()
return *m_layer_model;
}
size_t Image::index_of(const Layer& layer) const
{
for (size_t i = 0; i < m_layers.size(); ++i) {
if (&m_layers.at(i) == &layer)
return i;
}
ASSERT_NOT_REACHED();
}
void Image::move_layer_to_back(Layer& layer)
{
NonnullRefPtr<Layer> protector(layer);
auto index = index_of(layer);
m_layers.remove(index);
m_layers.prepend(layer);
}
void Image::move_layer_to_front(Layer& layer)
{
NonnullRefPtr<Layer> protector(layer);
auto index = index_of(layer);
m_layers.remove(index);
m_layers.append(layer);
}
}

View file

@ -55,9 +55,14 @@ public:
GUI::Model& layer_model();
void move_layer_to_front(Layer&);
void move_layer_to_back(Layer&);
private:
explicit Image(const Gfx::Size&);
size_t index_of(const Layer&) const;
Gfx::Size m_size;
NonnullRefPtrVector<Layer> m_layers;
RefPtr<GUI::Model> m_layer_model;

View file

@ -117,6 +117,13 @@ void ImageEditor::mouseup_event(GUI::MouseEvent& event)
m_active_tool->on_mouseup(*m_active_layer, layer_event, event);
}
void ImageEditor::context_menu_event(GUI::ContextMenuEvent& event)
{
if (!m_active_layer || !m_active_tool)
return;
m_active_tool->on_context_menu(*m_active_layer, event);
}
void ImageEditor::keydown_event(GUI::KeyEvent& event)
{
if (m_active_tool)

View file

@ -79,6 +79,7 @@ private:
virtual void mouseup_event(GUI::MouseEvent&) override;
virtual void keydown_event(GUI::KeyEvent&) override;
virtual void keyup_event(GUI::KeyEvent&) override;
virtual void context_menu_event(GUI::ContextMenuEvent&) override;
RefPtr<Image> m_image;
RefPtr<Layer> m_active_layer;

View file

@ -25,8 +25,11 @@
*/
#include "MoveTool.h"
#include "Image.h"
#include "ImageEditor.h"
#include "Layer.h"
#include <LibGUI/Action.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
@ -101,4 +104,21 @@ void MoveTool::on_keydown(GUI::KeyEvent& event)
m_editor->layers_did_change();
}
void MoveTool::on_context_menu(Layer& layer, GUI::ContextMenuEvent& event)
{
if (!m_context_menu) {
m_context_menu = GUI::Menu::construct();
m_context_menu->add_action(GUI::CommonActions::make_move_to_front_action([this](auto&) {
m_editor->image()->move_layer_to_front(*m_context_menu_layer);
m_editor->layers_did_change();
}));
m_context_menu->add_action(GUI::CommonActions::make_move_to_back_action([this](auto&) {
m_editor->image()->move_layer_to_back(*m_context_menu_layer);
m_editor->layers_did_change();
}));
}
m_context_menu_layer = layer;
m_context_menu->popup(event.screen_position());
}
}

View file

@ -39,6 +39,7 @@ public:
virtual void on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_mouseup(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent& original_event) override;
virtual void on_keydown(GUI::KeyEvent&) override;
virtual void on_context_menu(Layer&, GUI::ContextMenuEvent&) override;
private:
virtual const char* class_name() const override { return "MoveTool"; }
@ -47,6 +48,8 @@ private:
RefPtr<Layer> m_layer_being_moved;
Gfx::Point m_event_origin;
Gfx::Point m_layer_origin;
RefPtr<GUI::Menu> m_context_menu;
RefPtr<Layer> m_context_menu_layer;
};
}

View file

@ -43,6 +43,7 @@ public:
virtual void on_mousedown(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) {}
virtual void on_mousemove(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) {}
virtual void on_mouseup(Layer&, GUI::MouseEvent&, GUI::MouseEvent&) {}
virtual void on_context_menu(Layer&, GUI::ContextMenuEvent&) {}
virtual void on_tool_button_contextmenu(GUI::ContextMenuEvent&) {}
virtual void on_second_paint(const Layer&, GUI::PaintEvent&) {}
virtual void on_keydown(GUI::KeyEvent&) {}