From 45d8f98459bf927c52f262f85eb93c9d021d0d3f Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 21 Jun 2022 22:58:54 +0200 Subject: [PATCH] Fixed resizing the canvas. Also now the new menu will clear all layers. --- modules/paint/dialogs/paint_canvas_dialog.cpp | 1 + modules/paint/dialogs/paint_canvas_dialog.h | 2 ++ modules/paint/paint_canvas.cpp | 20 +++++++++++-------- modules/paint/paint_navbar.cpp | 2 ++ modules/paint/paint_window.cpp | 17 ++++++++++++++++ modules/paint/paint_window.h | 1 + 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/modules/paint/dialogs/paint_canvas_dialog.cpp b/modules/paint/dialogs/paint_canvas_dialog.cpp index 317eb1947..18f59f8be 100644 --- a/modules/paint/dialogs/paint_canvas_dialog.cpp +++ b/modules/paint/dialogs/paint_canvas_dialog.cpp @@ -57,6 +57,7 @@ void PaintCanvasDialog::_on_about_to_show() { PaintCanvasDialog::PaintCanvasDialog() { _x_prev_val = 0; _y_prev_val = 0; + is_new = false; set_resizable(true); set_title("Please Confirm..."); diff --git a/modules/paint/dialogs/paint_canvas_dialog.h b/modules/paint/dialogs/paint_canvas_dialog.h index d187b828a..0e1ff670e 100644 --- a/modules/paint/dialogs/paint_canvas_dialog.h +++ b/modules/paint/dialogs/paint_canvas_dialog.h @@ -45,6 +45,8 @@ public: PaintCanvasDialog(); ~PaintCanvasDialog(); + bool is_new; + protected: void _on_confirmed(); void _on_about_to_show(); diff --git a/modules/paint/paint_canvas.cpp b/modules/paint/paint_canvas.cpp index 7d4827ef3..228ab0ab6 100644 --- a/modules/paint/paint_canvas.cpp +++ b/modules/paint/paint_canvas.cpp @@ -233,17 +233,21 @@ Ref PaintCanvas::remove_layer(const String &layer_name) { del_layer->clear(); if (del_layer == active_layer) { - for (int i = 0; i < layers.size(); ++i) { - Ref layer = layers[i]; + if (layers.size() > 1) { + for (int i = 0; i < layers.size(); ++i) { + Ref layer = layers[i]; - ERR_CONTINUE(!layer.is_valid()); + ERR_CONTINUE(!layer.is_valid()); - if (layer == active_layer) { - continue; + if (layer == active_layer) { + continue; + } + + active_layer = layer; + break; } - - active_layer = layer; - break; + } else { + active_layer.unref(); } } diff --git a/modules/paint/paint_navbar.cpp b/modules/paint/paint_navbar.cpp index 72d0a87dc..78d93a8ba 100644 --- a/modules/paint/paint_navbar.cpp +++ b/modules/paint/paint_navbar.cpp @@ -41,6 +41,7 @@ SOFTWARE. void PaintNavbar::handle_menu_item_pressed(const int id) { switch (id) { case MENU_FILE_NEW: + paint_window->paint_canvas_dialog->is_new = true; paint_window->paint_canvas_dialog->popup_centered(); break; case MENU_FILE_SAVE: @@ -60,6 +61,7 @@ void PaintNavbar::handle_menu_item_pressed(const int id) { case MENU_EDIT_PASTE: break; case MENU_CANVAS_CHANGE_SIZE: + paint_window->paint_canvas_dialog->is_new = false; paint_window->paint_canvas_dialog->popup_centered(); break; case MENU_CANVAS_CROP_TO_CONTENT: diff --git a/modules/paint/paint_window.cpp b/modules/paint/paint_window.cpp index 001323f12..56b740f08 100644 --- a/modules/paint/paint_window.cpp +++ b/modules/paint/paint_window.cpp @@ -784,6 +784,11 @@ void PaintWindow::_on_ChangeGridSizeDialog_confirmed() { } void PaintWindow::_on_ChangeCanvasSizeDialog_confirmed() { + if (paint_canvas_dialog->is_new) { + delete_all_layers(); + add_new_layer(); + } + paint_canvas->resize(paint_canvas_dialog->get_size_x(), paint_canvas_dialog->get_size_y()); } @@ -925,6 +930,18 @@ void PaintWindow::duplicate_active_layer() { //print("added layer: ", new_layer.name, " (total:", layers_box_container.get_child_count(), ")") } +void PaintWindow::delete_all_layers() { + while (paint_canvas->layers.size() > 0) { + String layer_name = paint_canvas->layers[0]->name; + paint_canvas->remove_layer(layer_name); + layers_box_container->remove_child(_layer_button_ref[layer_name]); + _layer_button_ref[layer_name]->queue_delete(); + _layer_button_ref.erase(layer_name); + } + + _total_added_layers = 0; +} + void PaintWindow::move_up(Node *layer_btn) { int new_idx = MIN(layer_btn->get_index() + 1, layers_box_container->get_child_count()); //print("move_up: ", layer_btn.name, " from ", layer_btn.get_index(), " to ", new_idx) diff --git a/modules/paint/paint_window.h b/modules/paint/paint_window.h index 3573f3c26..21b9df91a 100644 --- a/modules/paint/paint_window.h +++ b/modules/paint/paint_window.h @@ -143,6 +143,7 @@ public: Ref add_new_layer(); void remove_active_layer(); void duplicate_active_layer(); + void delete_all_layers(); void move_up(Node *layer_btn); void move_down(Node *layer_btn);