From d3c039be7e1a4325cfc779d36d425e2198f38903 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 17 Apr 2022 18:14:06 +0200 Subject: [PATCH] Also port the logic of the selection box and visual grid. --- modules/paint/paint_canvas.cpp | 18 ++++++-- modules/paint/paint_selection_box.cpp | 35 ++++++++------- modules/paint/paint_selection_box.h | 7 +-- modules/paint/paint_visual_grid.cpp | 63 ++++++++++++++++----------- modules/paint/paint_visual_grid.h | 13 ------ 5 files changed, 76 insertions(+), 60 deletions(-) diff --git a/modules/paint/paint_canvas.cpp b/modules/paint/paint_canvas.cpp index d313e523e..024969ff5 100644 --- a/modules/paint/paint_canvas.cpp +++ b/modules/paint/paint_canvas.cpp @@ -545,8 +545,10 @@ void PaintCanvas::resize(int width, int height) { height = 1; } - set_canvas_width(width); - set_canvas_height(height); + Size2 s; + s.x = _canvas_height * _pixel_size; + s.y = _canvas_width * _pixel_size; + set_size(s); preview_layer->resize(width, height); tool_layer->resize(width, height); @@ -558,6 +560,8 @@ void PaintCanvas::resize(int width, int height) { layer->resize(width, height); } + + update(); } void PaintCanvas::_notification(int p_what) { @@ -573,6 +577,12 @@ void PaintCanvas::_notification(int p_what) { preview_layer = add_new_layer("Preview"); tool_layer = add_new_layer("Tool"); + //hack + _canvas_width = 0; + _canvas_height = 0; + + resize(64, 64); + set_process(true); } break; case NOTIFICATION_PROCESS: { @@ -588,8 +598,8 @@ PaintCanvas::PaintCanvas() { big_grid = nullptr; _pixel_size = 16; - _canvas_width = 48; - _canvas_height = 28; + _canvas_width = 64; + _canvas_height = 64; _grid_size = 16; _big_grid_size = 10; _can_draw = true; diff --git a/modules/paint/paint_selection_box.cpp b/modules/paint/paint_selection_box.cpp index a4db6ef54..451167744 100644 --- a/modules/paint/paint_selection_box.cpp +++ b/modules/paint/paint_selection_box.cpp @@ -30,30 +30,35 @@ void PaintSelectionBox::_process(float delta) { */ } void PaintSelectionBox::_draw() { - /* -if not rect_size == Vector2(): - draw_outline_box(rect_size, Color.gray, outline_size) - */ + if (get_size() != Vector2()) { + draw_outline_box(get_size(), Color(0.75, 0.75, 0.75), outline_size); + } } void PaintSelectionBox::draw_outline_box(const Vector2 &size, const Color &color, const int width) { - /* - #Top line - draw_line(Vector2(0 + 1, 0), Vector2(size.x, 0), color, width) - #Left line - draw_line(Vector2(0 + 1, 0), Vector2(0, size.y), color, width) - #Bottom line - draw_line(Vector2(0 + 1, size.y), Vector2(size.x, size.y), color, width) - #Right line - draw_line(Vector2(size.x, 0), Vector2(size.x, size.y), color, width) - */ + //Top line + draw_line(Vector2(0 + 1, 0), Vector2(size.x, 0), color, width); + //Left line + draw_line(Vector2(0 + 1, 0), Vector2(0, size.y), color, width); + //Bottom line + draw_line(Vector2(0 + 1, size.y), Vector2(size.x, size.y), color, width); + //Right line + draw_line(Vector2(size.x, 0), Vector2(size.x, size.y), color, width); } PaintSelectionBox::PaintSelectionBox() { - ////export var outline_size = 3 + outline_size = 3; } PaintSelectionBox::~PaintSelectionBox() { } +void PaintSelectionBox::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_DRAW: { + _draw(); + } break; + } +} + void PaintSelectionBox::_bind_methods() { } diff --git a/modules/paint/paint_selection_box.h b/modules/paint/paint_selection_box.h index 725616708..7080dcd7d 100644 --- a/modules/paint/paint_selection_box.h +++ b/modules/paint/paint_selection_box.h @@ -38,10 +38,11 @@ public: PaintSelectionBox(); ~PaintSelectionBox(); -protected: - static void _bind_methods(); + int outline_size; - //export var outline_size = 3 +protected: + void _notification(int p_what); + static void _bind_methods(); }; #endif diff --git a/modules/paint/paint_visual_grid.cpp b/modules/paint/paint_visual_grid.cpp index 3a992e04d..1f68c55fa 100644 --- a/modules/paint/paint_visual_grid.cpp +++ b/modules/paint/paint_visual_grid.cpp @@ -25,31 +25,32 @@ SOFTWARE. #include "paint_visual_grid.h" void PaintVisualGrid::_draw() { - /* - if size == 0: - size = 1 + if (size == 0) { + size = 1; + } - var temp_size = size + zoom + float temp_size = size + zoom; - var wrap_offset = Vector2(wrapf(offset.x, 0, temp_size), wrapf(offset.y, 0, temp_size)) + Vector2 wrap_offset = Vector2(Math::wrapf(offset.x, 0, temp_size), Math::wrapf(offset.y, 0, temp_size)); - var ceil_x = ceil(rect_size.x / temp_size) - var ceil_y = ceil(rect_size.y / temp_size) + Size2 rect_size = get_size(); - for i in ceil_y: - var start_x = Vector2(0, (i * temp_size) + wrap_offset.y) - var end_x = Vector2(rect_size.x, (i * temp_size) + wrap_offset.y) -# var end_x = Vector2(int(rect_size.x) + size - int(rect_size.x) % size, (i * temp_size) + wrap_offset.y) - draw_line(start_x, end_x, color, 1) + float ceil_x = Math::ceil(rect_size.x / temp_size); + float ceil_y = Math::ceil(rect_size.y / temp_size); - for i in ceil_x: - var start_y = Vector2((i * temp_size) + wrap_offset.x, 0) - var end_y = Vector2((i * temp_size) + (wrap_offset.x), rect_size.y) -# var end_y = Vector2((i * temp_size) + (wrap_offset.x), int(rect_size.y) + size - int(rect_size.y) % size) - draw_line(start_y, end_y, color, 1) + for (int i = 0; i < ceil_y; ++i) { + Point2 start_x = Vector2(0, (i * temp_size) + wrap_offset.y); + Point2 end_x = Vector2(rect_size.x, (i * temp_size) + wrap_offset.y); + //var end_x = Vector2(int(rect_size.x) + size - int(rect_size.x) % size, (i * temp_size) + wrap_offset.y); + draw_line(start_x, end_x, color, 1); + } - - */ + for (int i = 0; i < ceil_x; ++i) { + Point2 start_y = Vector2((i * temp_size) + wrap_offset.x, 0); + Point2 end_y = Vector2((i * temp_size) + (wrap_offset.x), rect_size.y); + //var end_y = Vector2((i * temp_size) + (wrap_offset.x), int(rect_size.y) + size - int(rect_size.y) % size); + draw_line(start_y, end_y, color, 1); + } } void PaintVisualGrid::_notification(int p_what) { @@ -62,15 +63,27 @@ void PaintVisualGrid::_notification(int p_what) { return update() */ + + switch (p_what) { + /* + case NOTIFICATION_PROCESS: { + if (!is_visible_in_tree()) { + return; + } + + update(); + } break; + */ + case NOTIFICATION_DRAW: { + _draw(); + } break; + } } PaintVisualGrid::PaintVisualGrid() { - /* - export var color = Color() - export var size:int = 16 - export var zoom = 0 - export var offset = Vector2(0, 0) - */ + color = Color(1, 1, 1); + size = 16; + zoom = 0; } PaintVisualGrid::~PaintVisualGrid() { diff --git a/modules/paint/paint_visual_grid.h b/modules/paint/paint_visual_grid.h index 926f3f2e6..5ea8ba260 100644 --- a/modules/paint/paint_visual_grid.h +++ b/modules/paint/paint_visual_grid.h @@ -27,12 +27,6 @@ SOFTWARE. #include "scene/gui/control.h" -#include "core/reference.h" - -class MeshDataResource; -class MeshDataInstance; -class Texture; - class PaintVisualGrid : public Control { GDCLASS(PaintVisualGrid, Control); @@ -51,13 +45,6 @@ protected: void _notification(int p_what); static void _bind_methods(); - /* - export var color = Color() - export var size:int = 16 - export var zoom = 0 - export var offset = Vector2(0, 0) - - */ }; #endif