From b574d44dd711ddb550d91a68c9e628f0bddc2b1e Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 14 Nov 2022 23:08:37 +0100 Subject: [PATCH] Also register PaintCanvasBackground to the ClassDB, and fix crash on exit with it. --- modules/paint/register_types.cpp | 2 +- modules/paint/ui/paint_canvas_background.cpp | 41 +++++++++++++------- modules/paint/ui/paint_canvas_background.h | 2 + 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/modules/paint/register_types.cpp b/modules/paint/register_types.cpp index 90b2c0791..57296e100 100644 --- a/modules/paint/register_types.cpp +++ b/modules/paint/register_types.cpp @@ -65,7 +65,7 @@ void register_paint_types() { ClassDB::register_class(); ClassDB::register_class(); - //ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); diff --git a/modules/paint/ui/paint_canvas_background.cpp b/modules/paint/ui/paint_canvas_background.cpp index 753c75086..aab9aee0e 100644 --- a/modules/paint/ui/paint_canvas_background.cpp +++ b/modules/paint/ui/paint_canvas_background.cpp @@ -24,9 +24,9 @@ SOFTWARE. #include "paint_canvas_background.h" +#include "core/io/image.h" #include "scene/resources/material.h" #include "scene/resources/shader.h" -#include "core/io/image.h" #include "scene/resources/texture.h" #include "../shaders/shaders.h" @@ -39,7 +39,9 @@ float PaintCanvasBackground::get_pixel_size() const { void PaintCanvasBackground::set_pixel_size(const float val) { _pixel_size = val; - _material->set_shader_param("pixel_size", _pixel_size); + if (_material.is_valid()) { + _material->set_shader_param("pixel_size", _pixel_size); + } } PaintCanvasBackground::PaintCanvasBackground() { @@ -47,22 +49,31 @@ PaintCanvasBackground::PaintCanvasBackground() { set_expand(true); set_stretch_mode(TextureRect::STRETCH_TILE); - - set_texture(PaintIcons::make_icon_grid_png()); - - _shader.instance(); - _shader->set_code(background_shader_shader_code); - - _material.instance(); - _material->set_shader(_shader); - _material->set_shader_param("pixel_size", _pixel_size); - - set_material(_material); } PaintCanvasBackground::~PaintCanvasBackground() { - _material.unref(); - _shader.unref(); +} + +void PaintCanvasBackground::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: + _shader.instance(); + _shader->set_code(background_shader_shader_code); + + _material.instance(); + _material->set_shader(_shader); + _material->set_shader_param("pixel_size", _pixel_size); + + set_material(_material); + + set_texture(PaintIcons::make_icon_grid_png()); + break; + case NOTIFICATION_EXIT_TREE: + set_material(Ref()); + _shader.unref(); + _material.unref(); + break; + } } void PaintCanvasBackground::_bind_methods() { diff --git a/modules/paint/ui/paint_canvas_background.h b/modules/paint/ui/paint_canvas_background.h index 7627eff9b..d908b7612 100644 --- a/modules/paint/ui/paint_canvas_background.h +++ b/modules/paint/ui/paint_canvas_background.h @@ -44,6 +44,8 @@ public: ~PaintCanvasBackground(); protected: + void _notification(int p_what); + static void _bind_methods(); float _pixel_size;