From 1cf7b380745520dd92eb5defe1f6f5ca63c8510f Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 14 Nov 2022 18:27:03 +0100 Subject: [PATCH] Small rework for the PaintCanvasBackground. --- modules/paint/deprecated/paint_canvas.cpp | 4 --- modules/paint/ui/paint_canvas_background.cpp | 35 ++++++++++++++++++++ modules/paint/ui/paint_canvas_background.h | 1 + 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/modules/paint/deprecated/paint_canvas.cpp b/modules/paint/deprecated/paint_canvas.cpp index 1d3f78b86..ae1e1a09c 100644 --- a/modules/paint/deprecated/paint_canvas.cpp +++ b/modules/paint/deprecated/paint_canvas.cpp @@ -605,10 +605,6 @@ PaintCanvasOld::PaintCanvasOld() { mouse_on_top = false; canvas_background = memnew(PaintCanvasBackground); - canvas_background->set_texture(make_icon(grid_png)); - canvas_background->set_expand(true); - canvas_background->set_stretch_mode(TextureRect::STRETCH_TILE); - //canvas_background->set_draw_behind_parent(true); add_child(canvas_background); canvas_layers = memnew(Control); diff --git a/modules/paint/ui/paint_canvas_background.cpp b/modules/paint/ui/paint_canvas_background.cpp index 5d8083e6c..ef20ae3a2 100644 --- a/modules/paint/ui/paint_canvas_background.cpp +++ b/modules/paint/ui/paint_canvas_background.cpp @@ -26,9 +26,37 @@ SOFTWARE. #include "scene/resources/material.h" #include "scene/resources/shader.h" +#include "core/io/image.h" +#include "scene/resources/texture.h" #include "../shaders/shaders.h" +#include "../paint_icons/paint_icons.h" + +static float scale = 1; + +template +static Ref make_icon(T p_src) { + Ref texture(memnew(ImageTexture)); + Ref img = memnew(Image(p_src)); + if (scale > 1) { + Size2 orig_size = Size2(img->get_width(), img->get_height()); + + img->convert(Image::FORMAT_RGBA8); + img->expand_x2_hq2x(); + if (scale != 2.0) { + img->resize(orig_size.x * scale, orig_size.y * scale); + } + } else if (scale < 1) { + Size2 orig_size = Size2(img->get_width(), img->get_height()); + img->convert(Image::FORMAT_RGBA8); + img->resize(orig_size.x * scale, orig_size.y * scale); + } + texture->create_from_image(img, ImageTexture::FLAG_FILTER); + + return texture; +} + float PaintCanvasBackground::get_pixel_size() const { return _pixel_size; } @@ -41,6 +69,11 @@ void PaintCanvasBackground::set_pixel_size(const float val) { PaintCanvasBackground::PaintCanvasBackground() { _pixel_size = 1; + set_expand(true); + set_stretch_mode(TextureRect::STRETCH_TILE); + + set_texture(make_icon(grid_png)); + _shader.instance(); _shader->set_code(background_shader_shader_code); @@ -52,6 +85,8 @@ PaintCanvasBackground::PaintCanvasBackground() { } PaintCanvasBackground::~PaintCanvasBackground() { + _material.unref(); + _shader.unref(); } void PaintCanvasBackground::_bind_methods() { diff --git a/modules/paint/ui/paint_canvas_background.h b/modules/paint/ui/paint_canvas_background.h index 5d145fe44..7627eff9b 100644 --- a/modules/paint/ui/paint_canvas_background.h +++ b/modules/paint/ui/paint_canvas_background.h @@ -31,6 +31,7 @@ SOFTWARE. class ShaderMaterial; class Shader; +class Image; class PaintCanvasBackground : public TextureRect { GDCLASS(PaintCanvasBackground, TextureRect);