2020-05-12 22:22:45 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-08-31 21:02:38 +02:00
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
2021-09-10 23:12:25 -04:00
|
|
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@cs.toronto.edu>
|
2020-05-12 22:22:45 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-12 22:22:45 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-08-01 15:54:26 +02:00
|
|
|
#include "Guide.h"
|
2020-05-25 22:49:50 +02:00
|
|
|
#include "Image.h"
|
2021-06-14 17:36:18 +02:00
|
|
|
#include "Selection.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;
|
|
|
|
|
2021-06-15 21:35:04 +02:00
|
|
|
Image const& image() const { return m_image; }
|
|
|
|
Image& image() { return m_image; }
|
2020-05-12 22:22:45 +02:00
|
|
|
|
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();
|
|
|
|
|
2021-08-01 15:54:26 +02:00
|
|
|
void add_guide(NonnullRefPtr<Guide> guide) { m_guides.append(guide); }
|
|
|
|
void remove_guide(Guide const& guide)
|
|
|
|
{
|
|
|
|
m_guides.remove_first_matching([&](auto& entry) { return &guide == entry.ptr(); });
|
|
|
|
}
|
2021-09-10 17:41:28 +02:00
|
|
|
void clear_guides() { m_guides.clear(); }
|
2021-08-01 15:54:26 +02:00
|
|
|
|
2020-05-13 00:21:13 +02:00
|
|
|
void layers_did_change();
|
|
|
|
|
2021-06-11 13:27:47 +02:00
|
|
|
Layer* layer_at_editor_position(Gfx::IntPoint const&);
|
2020-05-13 21:46:19 +02:00
|
|
|
|
2021-06-20 10:48:43 -03:00
|
|
|
float scale() const { return m_scale; }
|
2021-06-11 13:27:47 +02:00
|
|
|
void scale_centered_on_position(Gfx::IntPoint const&, float);
|
2021-09-07 14:14:18 -04:00
|
|
|
void fit_image_to_view();
|
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
|
|
|
|
2021-09-11 23:14:15 -04:00
|
|
|
void set_pan_origin(Gfx::FloatPoint const&);
|
|
|
|
Gfx::FloatPoint pan_origin() const { return m_pan_origin; }
|
|
|
|
|
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);
|
|
|
|
|
2021-06-14 17:36:18 +02:00
|
|
|
Selection& selection() { return m_selection; }
|
|
|
|
Selection const& selection() const { return m_selection; }
|
|
|
|
|
2021-06-11 13:27:47 +02:00
|
|
|
Color color_for(GUI::MouseEvent const&) const;
|
2020-05-13 12:47:47 +02:00
|
|
|
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
|
|
|
|
2021-06-16 12:08:05 +02:00
|
|
|
Function<void(String const&)> on_image_title_change;
|
|
|
|
|
2021-08-01 11:59:47 +02:00
|
|
|
Function<void(Gfx::IntPoint const&)> on_image_mouse_position_change;
|
|
|
|
|
|
|
|
Function<void(void)> on_leave;
|
|
|
|
|
2021-06-11 13:27:47 +02:00
|
|
|
Gfx::FloatRect layer_rect_to_editor_rect(Layer const&, Gfx::IntRect const&) const;
|
|
|
|
Gfx::FloatRect image_rect_to_editor_rect(Gfx::IntRect const&) const;
|
|
|
|
Gfx::FloatRect editor_rect_to_image_rect(Gfx::IntRect const&) const;
|
|
|
|
Gfx::FloatPoint layer_position_to_editor_position(Layer const&, Gfx::IntPoint const&) const;
|
|
|
|
Gfx::FloatPoint image_position_to_editor_position(Gfx::IntPoint const&) const;
|
|
|
|
Gfx::FloatPoint editor_position_to_image_position(Gfx::IntPoint const&) const;
|
2020-05-22 14:45:14 +02:00
|
|
|
|
2021-08-31 20:30:51 +02:00
|
|
|
Result<void, String> save_project_to_fd_and_close(int fd) const;
|
|
|
|
|
2021-08-01 15:54:26 +02:00
|
|
|
NonnullRefPtrVector<Guide> const& guides() const { return m_guides; }
|
2021-08-13 22:40:29 +02:00
|
|
|
bool guide_visibility() { return m_show_guides; }
|
|
|
|
void set_guide_visibility(bool show_guides);
|
|
|
|
Function<void(bool)> on_set_guide_visibility;
|
2021-08-01 15:54:26 +02:00
|
|
|
|
2021-09-05 12:22:27 +02:00
|
|
|
bool ruler_visibility() { return m_show_rulers; }
|
|
|
|
void set_ruler_visibility(bool);
|
|
|
|
Function<void(bool)> on_set_ruler_visibility;
|
|
|
|
|
2021-09-10 23:12:25 -04:00
|
|
|
bool pixel_grid_visibility() const { return m_show_pixel_grid; }
|
|
|
|
void set_pixel_grid_visibility(bool show_pixel_grid);
|
|
|
|
|
2020-05-12 22:22:45 +02:00
|
|
|
private:
|
2021-06-15 21:05:56 +02:00
|
|
|
explicit ImageEditor(NonnullRefPtr<Image>);
|
2020-05-12 22:22:45 +02:00
|
|
|
|
|
|
|
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;
|
2021-08-08 20:50:27 +02:00
|
|
|
virtual void enter_event(Core::Event&) override;
|
2021-08-01 11:59:47 +02:00
|
|
|
virtual void leave_event(Core::Event&) override;
|
2020-05-20 22:29:04 +02:00
|
|
|
|
2021-07-06 20:35:19 +02:00
|
|
|
virtual void image_did_change(Gfx::IntRect const&) override;
|
2021-09-02 19:29:03 -04:00
|
|
|
virtual void image_did_change_rect(Gfx::IntRect const&) override;
|
2020-10-17 16:47:34 +00:00
|
|
|
virtual void image_select_layer(Layer*) override;
|
2021-06-16 12:08:05 +02:00
|
|
|
virtual void image_did_change_title(String const&) override;
|
2020-05-25 22:49:50 +02:00
|
|
|
|
2021-06-11 13:27:47 +02:00
|
|
|
GUI::MouseEvent event_adjusted_for_layer(GUI::MouseEvent const&, Layer const&) const;
|
|
|
|
GUI::MouseEvent event_with_pan_and_scale_applied(GUI::MouseEvent const&) const;
|
2020-05-20 22:29:04 +02:00
|
|
|
|
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
|
|
|
|
2021-09-05 12:22:27 +02:00
|
|
|
int calculate_ruler_step_size() const;
|
2021-09-05 12:26:15 +02:00
|
|
|
Gfx::IntRect mouse_indicator_rect_x() const;
|
|
|
|
Gfx::IntRect mouse_indicator_rect_y() const;
|
2021-09-05 12:22:27 +02:00
|
|
|
|
2021-06-15 21:05:56 +02:00
|
|
|
NonnullRefPtr<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
|
|
|
|
2021-08-01 15:54:26 +02:00
|
|
|
NonnullRefPtrVector<Guide> m_guides;
|
2021-08-05 15:48:59 +02:00
|
|
|
bool m_show_guides { true };
|
2021-09-05 12:22:27 +02:00
|
|
|
bool m_show_rulers { true };
|
2021-09-10 23:12:25 -04:00
|
|
|
bool m_show_pixel_grid { true };
|
2021-08-01 15:54:26 +02:00
|
|
|
|
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;
|
2021-09-05 12:26:15 +02:00
|
|
|
Gfx::IntPoint m_mouse_position;
|
2021-06-14 17:36:18 +02:00
|
|
|
|
2021-09-05 12:22:27 +02:00
|
|
|
int m_ruler_thickness { 20 };
|
2021-09-05 12:26:15 +02:00
|
|
|
int m_mouse_indicator_triangle_size { 5 };
|
2021-09-05 12:22:27 +02:00
|
|
|
|
2021-09-11 13:40:55 -04:00
|
|
|
float m_pixel_grid_threshold { 15.0f };
|
|
|
|
|
2021-08-08 20:50:27 +02:00
|
|
|
Gfx::StandardCursor m_active_cursor { Gfx::StandardCursor::None };
|
|
|
|
|
2021-06-14 17:36:18 +02:00
|
|
|
Selection m_selection;
|
2020-05-12 22:22:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|