Now PaintCanvasBackground and PaintVisualGrid will automatically size themselves to a parent PaintNode if they can.

This commit is contained in:
Relintai 2022-11-19 13:16:54 +01:00
parent 17842720a5
commit 9f8f6cb3b4
4 changed files with 54 additions and 2 deletions

View File

@ -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<PaintNode>(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);

View File

@ -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();

View File

@ -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<PaintNode>(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);

View File

@ -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();