Also register PaintCanvasBackground to the ClassDB, and fix crash on exit with it.

This commit is contained in:
Relintai 2022-11-14 23:08:37 +01:00
parent 74a7615168
commit b574d44dd7
3 changed files with 29 additions and 16 deletions

View File

@ -65,7 +65,7 @@ void register_paint_types() {
ClassDB::register_class<RainbowAction>(); ClassDB::register_class<RainbowAction>();
ClassDB::register_class<RectAction>(); ClassDB::register_class<RectAction>();
//ClassDB::register_class<PaintCanvasBackground>(); ClassDB::register_class<PaintCanvasBackground>();
ClassDB::register_class<PaintCanvasOutline>(); ClassDB::register_class<PaintCanvasOutline>();
ClassDB::register_class<PaintColorGrid>(); ClassDB::register_class<PaintColorGrid>();
ClassDB::register_class<PaintSelectionBox>(); ClassDB::register_class<PaintSelectionBox>();

View File

@ -24,9 +24,9 @@ SOFTWARE.
#include "paint_canvas_background.h" #include "paint_canvas_background.h"
#include "core/io/image.h"
#include "scene/resources/material.h" #include "scene/resources/material.h"
#include "scene/resources/shader.h" #include "scene/resources/shader.h"
#include "core/io/image.h"
#include "scene/resources/texture.h" #include "scene/resources/texture.h"
#include "../shaders/shaders.h" #include "../shaders/shaders.h"
@ -39,17 +39,24 @@ float PaintCanvasBackground::get_pixel_size() const {
void PaintCanvasBackground::set_pixel_size(const float val) { void PaintCanvasBackground::set_pixel_size(const float val) {
_pixel_size = val; _pixel_size = val;
if (_material.is_valid()) {
_material->set_shader_param("pixel_size", _pixel_size); _material->set_shader_param("pixel_size", _pixel_size);
} }
}
PaintCanvasBackground::PaintCanvasBackground() { PaintCanvasBackground::PaintCanvasBackground() {
_pixel_size = 1; _pixel_size = 1;
set_expand(true); set_expand(true);
set_stretch_mode(TextureRect::STRETCH_TILE); set_stretch_mode(TextureRect::STRETCH_TILE);
}
set_texture(PaintIcons::make_icon_grid_png()); PaintCanvasBackground::~PaintCanvasBackground() {
}
void PaintCanvasBackground::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
_shader.instance(); _shader.instance();
_shader->set_code(background_shader_shader_code); _shader->set_code(background_shader_shader_code);
@ -58,11 +65,15 @@ PaintCanvasBackground::PaintCanvasBackground() {
_material->set_shader_param("pixel_size", _pixel_size); _material->set_shader_param("pixel_size", _pixel_size);
set_material(_material); set_material(_material);
}
PaintCanvasBackground::~PaintCanvasBackground() { set_texture(PaintIcons::make_icon_grid_png());
_material.unref(); break;
case NOTIFICATION_EXIT_TREE:
set_material(Ref<Material>());
_shader.unref(); _shader.unref();
_material.unref();
break;
}
} }
void PaintCanvasBackground::_bind_methods() { void PaintCanvasBackground::_bind_methods() {

View File

@ -44,6 +44,8 @@ public:
~PaintCanvasBackground(); ~PaintCanvasBackground();
protected: protected:
void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();
float _pixel_size; float _pixel_size;