From 9f8f6cb3b44297021f269388a5b080e88499e30c Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 19 Nov 2022 13:16:54 +0100 Subject: [PATCH] Now PaintCanvasBackground and PaintVisualGrid will automatically size themselves to a parent PaintNode if they can. --- modules/paint/ui/paint_canvas_background.cpp | 25 ++++++++++++++++++++ modules/paint/ui/paint_canvas_background.h | 3 +++ modules/paint/ui/paint_visual_grid.cpp | 24 ++++++++++++++++++- modules/paint/ui/paint_visual_grid.h | 4 +++- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/modules/paint/ui/paint_canvas_background.cpp b/modules/paint/ui/paint_canvas_background.cpp index 71b074e7e..9f79e26cc 100644 --- a/modules/paint/ui/paint_canvas_background.cpp +++ b/modules/paint/ui/paint_canvas_background.cpp @@ -24,6 +24,8 @@ SOFTWARE. #include "paint_canvas_background.h" +#include "../nodes/paint_node.h" + int PaintCanvasBackground::get_grid_size() const { return _grid_size; } @@ -51,6 +53,22 @@ void PaintCanvasBackground::set_grid_white(const Color &val) { update(); } +PaintNode *PaintCanvasBackground::get_paint_node() { + Node *p = this; + + while (p) { + PaintNode *pn = Object::cast_to(p); + + if (pn) { + return pn; + } + + p = p->get_parent(); + } + + return NULL; +} + PaintCanvasBackground::PaintCanvasBackground() { _grid_size = 8; @@ -63,6 +81,13 @@ PaintCanvasBackground::~PaintCanvasBackground() { void PaintCanvasBackground::_notification(int p_what) { switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + PaintNode *pn = get_paint_node(); + + if (pn) { + set_size(pn->get_size()); + } + } break; case NOTIFICATION_DRAW: { ERR_FAIL_COND(_grid_size <= 0); diff --git a/modules/paint/ui/paint_canvas_background.h b/modules/paint/ui/paint_canvas_background.h index 6787dcf33..1c2ce2a5b 100644 --- a/modules/paint/ui/paint_canvas_background.h +++ b/modules/paint/ui/paint_canvas_background.h @@ -32,6 +32,7 @@ SOFTWARE. class ShaderMaterial; class Shader; class Image; +class PaintNode; class PaintCanvasBackground : public TextureRect { GDCLASS(PaintCanvasBackground, TextureRect); @@ -46,6 +47,8 @@ public: Color get_grid_white() const; void set_grid_white(const Color &val); + PaintNode *get_paint_node(); + PaintCanvasBackground(); ~PaintCanvasBackground(); diff --git a/modules/paint/ui/paint_visual_grid.cpp b/modules/paint/ui/paint_visual_grid.cpp index d0afea79d..87a66ac10 100644 --- a/modules/paint/ui/paint_visual_grid.cpp +++ b/modules/paint/ui/paint_visual_grid.cpp @@ -24,6 +24,8 @@ SOFTWARE. #include "paint_visual_grid.h" +#include "../nodes/paint_node.h" + int PaintVisualGrid::get_grid_size() { return _grid_size; } @@ -40,11 +42,31 @@ void PaintVisualGrid::set_grid_color(const Color &val) { update(); } -void PaintVisualGrid::_draw() { +PaintNode *PaintVisualGrid::get_paint_node() { + Node *p = this; + + while (p) { + PaintNode *pn = Object::cast_to(p); + + if (pn) { + return pn; + } + + p = p->get_parent(); + } + + return NULL; } void PaintVisualGrid::_notification(int p_what) { switch (p_what) { + case NOTIFICATION_ENTER_TREE: { + PaintNode *pn = get_paint_node(); + + if (pn) { + set_size(pn->get_size()); + } + } break; case NOTIFICATION_DRAW: { ERR_FAIL_COND(_grid_size <= 0); diff --git a/modules/paint/ui/paint_visual_grid.h b/modules/paint/ui/paint_visual_grid.h index b273abc68..a583360c2 100644 --- a/modules/paint/ui/paint_visual_grid.h +++ b/modules/paint/ui/paint_visual_grid.h @@ -27,6 +27,8 @@ SOFTWARE. #include "scene/gui/control.h" +class PaintNode; + class PaintVisualGrid : public Control { GDCLASS(PaintVisualGrid, Control); @@ -37,7 +39,7 @@ public: Color get_grid_color(); void set_grid_color(const Color &val); - void _draw(); + PaintNode *get_paint_node(); PaintVisualGrid(); ~PaintVisualGrid();