Small rework for the PaintCanvasBackground.

This commit is contained in:
Relintai 2022-11-14 18:27:03 +01:00
parent 334b4ae3f8
commit 1cf7b38074
3 changed files with 36 additions and 4 deletions

View File

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

View File

@ -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 <class T>
static Ref<Texture> make_icon(T p_src) {
Ref<ImageTexture> texture(memnew(ImageTexture));
Ref<Image> 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() {

View File

@ -31,6 +31,7 @@ SOFTWARE.
class ShaderMaterial;
class Shader;
class Image;
class PaintCanvasBackground : public TextureRect {
GDCLASS(PaintCanvasBackground, TextureRect);