PaintBrush: Make the line, rectangle and ellipsis preview work again

You can now see what you're drawing before committing to it. This works
by passing the second_paint_event from the ImageEditor to the tool.
We also pass the active layer which makes it easier for the tool to
keep his logic in layer-relative coordinates even while drawing preview
states directly into the ImageEditor backing bitmap.
This commit is contained in:
Andreas Kling 2020-05-13 13:41:12 +02:00
parent 8318842c7e
commit 96d03546ef
Notes: sideshowbarker 2024-07-19 06:40:51 +09:00
9 changed files with 20 additions and 17 deletions

View file

@ -91,13 +91,14 @@ void EllipseTool::on_mousemove(Layer& layer, GUI::MouseEvent& event, GUI::MouseE
m_editor->update();
}
void EllipseTool::on_second_paint(GUI::PaintEvent& event)
void EllipseTool::on_second_paint(const Layer& layer, GUI::PaintEvent& event)
{
if (m_drawing_button == GUI::MouseButton::None)
return;
GUI::Painter painter(*m_editor);
painter.add_clip_rect(event.rect());
painter.translate(layer.location());
draw_using(painter);
}

View file

@ -41,7 +41,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_contextmenu(GUI::ContextMenuEvent&) override;
virtual void on_second_paint(GUI::PaintEvent&) override;
virtual void on_second_paint(const Layer&, GUI::PaintEvent&) override;
virtual void on_keydown(GUI::KeyEvent&) override;
private:

View file

@ -62,6 +62,12 @@ void ImageEditor::paint_event(GUI::PaintEvent& event)
}
}
void ImageEditor::second_paint_event(GUI::PaintEvent& event)
{
if (m_active_tool && m_active_layer)
m_active_tool->on_second_paint(*m_active_layer, event);
}
static GUI::MouseEvent event_adjusted_for_layer(const GUI::MouseEvent& original_event, const Layer& layer)
{
auto position_in_active_layer_coordinates = original_event.position().translated(-layer.location());

View file

@ -69,6 +69,7 @@ private:
virtual bool accepts_focus() const override { return true; }
virtual void paint_event(GUI::PaintEvent&) override;
virtual void second_paint_event(GUI::PaintEvent&) override;
virtual void mousedown_event(GUI::MouseEvent&) override;
virtual void mousemove_event(GUI::MouseEvent&) override;
virtual void mouseup_event(GUI::MouseEvent&) override;

View file

@ -96,18 +96,15 @@ void LineTool::on_mousemove(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
m_editor->update();
}
void LineTool::on_second_paint(GUI::PaintEvent& event)
void LineTool::on_second_paint(const Layer& layer, GUI::PaintEvent& event)
{
if (m_drawing_button == GUI::MouseButton::None)
return;
(void)event;
#if 0
GUI::Painter painter(*m_widget);
GUI::Painter painter(*m_editor);
painter.add_clip_rect(event.rect());
painter.translate(layer.location());
painter.draw_line(m_line_start_position, m_line_end_position, m_editor->color_for(m_drawing_button), m_thickness);
#endif
}
void LineTool::on_keydown(GUI::KeyEvent& event)

View file

@ -41,7 +41,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_contextmenu(GUI::ContextMenuEvent&) override;
virtual void on_second_paint(GUI::PaintEvent&) override;
virtual void on_second_paint(const Layer&, GUI::PaintEvent&) override;
virtual void on_keydown(GUI::KeyEvent&) override;
virtual void on_keyup(GUI::KeyEvent&) override;

View file

@ -97,17 +97,15 @@ void RectangleTool::on_mousemove(Layer&, GUI::MouseEvent& event, GUI::MouseEvent
m_editor->update();
}
void RectangleTool::on_second_paint(GUI::PaintEvent& event)
void RectangleTool::on_second_paint(const Layer& layer, GUI::PaintEvent& event)
{
if (m_drawing_button == GUI::MouseButton::None)
return;
(void)event;
#if 0
GUI::Painter painter(*m_widget);
GUI::Painter painter(*m_editor);
painter.add_clip_rect(event.rect());
painter.translate(layer.location());
draw_using(painter);
#endif
}
void RectangleTool::on_keydown(GUI::KeyEvent& event)

View file

@ -41,7 +41,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_contextmenu(GUI::ContextMenuEvent&) override;
virtual void on_second_paint(GUI::PaintEvent&) override;
virtual void on_second_paint(const Layer&, GUI::PaintEvent&) override;
virtual void on_keydown(GUI::KeyEvent&) override;
private:
@ -52,7 +52,7 @@ private:
};
virtual const char* class_name() const override { return "RectangleTool"; }
void draw_using(GUI::Painter& painter);
void draw_using(GUI::Painter&);
GUI::MouseButton m_drawing_button { GUI::MouseButton::None };
Gfx::Point m_rectangle_start_position;

View file

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