2020-05-12 22:22:45 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-12 22:22:45 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-05-25 22:49:50 +02:00
|
|
|
#include "Image.h"
|
2020-05-12 22:22:45 +02:00
|
|
|
#include <LibGUI/Frame.h>
|
2020-11-13 13:19:24 +00:00
|
|
|
#include <LibGUI/UndoStack.h>
|
2020-07-25 21:31:47 -07:00
|
|
|
#include <LibGfx/Point.h>
|
2020-05-12 22:22:45 +02:00
|
|
|
|
2020-05-20 20:35:35 +02:00
|
|
|
namespace PixelPaint {
|
2020-05-12 22:22:45 +02:00
|
|
|
|
2020-05-12 23:00:23 +02:00
|
|
|
class Layer;
|
2020-05-12 23:44:46 +02:00
|
|
|
class Tool;
|
2020-05-12 22:22:45 +02:00
|
|
|
|
2020-05-25 22:49:50 +02:00
|
|
|
class ImageEditor final
|
|
|
|
: public GUI::Frame
|
|
|
|
, public ImageClient {
|
2020-05-12 22:22:45 +02:00
|
|
|
C_OBJECT(ImageEditor);
|
|
|
|
|
|
|
|
public:
|
2020-05-25 23:48:09 +02:00
|
|
|
virtual ~ImageEditor() override;
|
|
|
|
|
2020-05-12 22:22:45 +02:00
|
|
|
const Image* image() const { return m_image; }
|
|
|
|
Image* image() { return m_image; }
|
|
|
|
|
|
|
|
void set_image(RefPtr<Image>);
|
|
|
|
|
2020-05-12 23:00:23 +02:00
|
|
|
Layer* active_layer() { return m_active_layer; }
|
|
|
|
void set_active_layer(Layer*);
|
|
|
|
|
2020-05-12 23:44:46 +02:00
|
|
|
Tool* active_tool() { return m_active_tool; }
|
|
|
|
void set_active_tool(Tool*);
|
|
|
|
|
2020-10-17 16:47:34 +00:00
|
|
|
void did_complete_action();
|
|
|
|
bool undo();
|
|
|
|
bool redo();
|
|
|
|
|
2020-05-13 00:21:13 +02:00
|
|
|
void layers_did_change();
|
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Layer* layer_at_editor_position(const Gfx::IntPoint&);
|
2020-05-13 21:46:19 +02:00
|
|
|
|
2021-03-22 14:33:30 -03:00
|
|
|
void scale_centered_on_position(const Gfx::IntPoint&, float);
|
2021-04-15 21:09:25 -04:00
|
|
|
void reset_scale_and_position();
|
2021-04-15 22:06:25 -04:00
|
|
|
void scale_by(float);
|
2021-03-22 14:33:30 -03:00
|
|
|
|
2020-05-13 12:47:47 +02:00
|
|
|
Color primary_color() const { return m_primary_color; }
|
|
|
|
void set_primary_color(Color);
|
|
|
|
|
|
|
|
Color secondary_color() const { return m_secondary_color; }
|
|
|
|
void set_secondary_color(Color);
|
|
|
|
|
|
|
|
Color color_for(const GUI::MouseEvent&) const;
|
|
|
|
Color color_for(GUI::MouseButton) const;
|
|
|
|
|
|
|
|
Function<void(Color)> on_primary_color_change;
|
|
|
|
Function<void(Color)> on_secondary_color_change;
|
|
|
|
|
2020-05-26 09:51:28 +02:00
|
|
|
Function<void(Layer*)> on_active_layer_change;
|
2020-05-13 21:46:19 +02:00
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::FloatRect layer_rect_to_editor_rect(const Layer&, const Gfx::IntRect&) const;
|
|
|
|
Gfx::FloatRect image_rect_to_editor_rect(const Gfx::IntRect&) const;
|
|
|
|
Gfx::FloatRect editor_rect_to_image_rect(const Gfx::IntRect&) const;
|
|
|
|
Gfx::FloatPoint layer_position_to_editor_position(const Layer&, const Gfx::IntPoint&) const;
|
|
|
|
Gfx::FloatPoint image_position_to_editor_position(const Gfx::IntPoint&) const;
|
|
|
|
Gfx::FloatPoint editor_position_to_image_position(const Gfx::IntPoint&) const;
|
2020-05-22 14:45:14 +02:00
|
|
|
|
2020-05-12 22:22:45 +02:00
|
|
|
private:
|
|
|
|
ImageEditor();
|
|
|
|
|
|
|
|
virtual void paint_event(GUI::PaintEvent&) override;
|
2020-05-13 13:41:12 +02:00
|
|
|
virtual void second_paint_event(GUI::PaintEvent&) override;
|
2020-05-12 23:44:46 +02:00
|
|
|
virtual void mousedown_event(GUI::MouseEvent&) override;
|
|
|
|
virtual void mousemove_event(GUI::MouseEvent&) override;
|
|
|
|
virtual void mouseup_event(GUI::MouseEvent&) override;
|
2020-05-20 22:29:04 +02:00
|
|
|
virtual void mousewheel_event(GUI::MouseEvent&) override;
|
2020-05-13 13:20:25 +02:00
|
|
|
virtual void keydown_event(GUI::KeyEvent&) override;
|
|
|
|
virtual void keyup_event(GUI::KeyEvent&) override;
|
2020-05-13 22:03:29 +02:00
|
|
|
virtual void context_menu_event(GUI::ContextMenuEvent&) override;
|
2020-05-20 22:29:04 +02:00
|
|
|
virtual void resize_event(GUI::ResizeEvent&) override;
|
|
|
|
|
2020-05-25 22:49:50 +02:00
|
|
|
virtual void image_did_change() override;
|
2020-10-17 16:47:34 +00:00
|
|
|
virtual void image_select_layer(Layer*) override;
|
2020-05-25 22:49:50 +02:00
|
|
|
|
2020-05-20 22:29:04 +02:00
|
|
|
GUI::MouseEvent event_adjusted_for_layer(const GUI::MouseEvent&, const Layer&) const;
|
|
|
|
GUI::MouseEvent event_with_pan_and_scale_applied(const GUI::MouseEvent&) const;
|
|
|
|
|
2021-04-15 22:06:25 -04:00
|
|
|
void clamped_scale(float);
|
2020-05-20 22:29:04 +02:00
|
|
|
void relayout();
|
2020-05-12 22:22:45 +02:00
|
|
|
|
|
|
|
RefPtr<Image> m_image;
|
2020-05-12 23:00:23 +02:00
|
|
|
RefPtr<Layer> m_active_layer;
|
2020-11-13 13:19:24 +00:00
|
|
|
OwnPtr<GUI::UndoStack> m_undo_stack;
|
2020-05-12 23:44:46 +02:00
|
|
|
|
|
|
|
Tool* m_active_tool { nullptr };
|
2020-05-13 12:47:47 +02:00
|
|
|
|
|
|
|
Color m_primary_color { Color::Black };
|
|
|
|
Color m_secondary_color { Color::White };
|
2020-05-20 22:29:04 +02:00
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntRect m_editor_image_rect;
|
2020-05-20 22:29:04 +02:00
|
|
|
float m_scale { 1 };
|
|
|
|
Gfx::FloatPoint m_pan_origin;
|
|
|
|
Gfx::FloatPoint m_saved_pan_origin;
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntPoint m_click_position;
|
2020-05-12 22:22:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|