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<RectAction>();
//ClassDB::register_class<PaintCanvasBackground>();
ClassDB::register_class<PaintCanvasBackground>();
ClassDB::register_class<PaintCanvasOutline>();
ClassDB::register_class<PaintColorGrid>();
ClassDB::register_class<PaintSelectionBox>();

View File

@ -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;
if (_material.is_valid()) {
_material->set_shader_param("pixel_size", _pixel_size);
}
}
PaintCanvasBackground::PaintCanvasBackground() {
@ -47,9 +49,14 @@ PaintCanvasBackground::PaintCanvasBackground() {
set_expand(true);
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->set_code(background_shader_shader_code);
@ -58,11 +65,15 @@ PaintCanvasBackground::PaintCanvasBackground() {
_material->set_shader_param("pixel_size", _pixel_size);
set_material(_material);
}
PaintCanvasBackground::~PaintCanvasBackground() {
_material.unref();
set_texture(PaintIcons::make_icon_grid_png());
break;
case NOTIFICATION_EXIT_TREE:
set_material(Ref<Material>());
_shader.unref();
_material.unref();
break;
}
}
void PaintCanvasBackground::_bind_methods() {

View File

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