Reworked PaintCanvasBackground. Now it draws it's pattern using canvas draw commands.

This commit is contained in:
Relintai 2022-11-19 02:20:52 +01:00
parent 5a78997c83
commit 5c38af1d1a
7 changed files with 80 additions and 197 deletions

View File

@ -581,6 +581,7 @@ PaintCanvasOld::PaintCanvasOld() {
mouse_on_top = false;
canvas_background = memnew(PaintCanvasBackground);
canvas_background->set_grid_size(32);
add_child(canvas_background);
canvas_layers = memnew(Control);

View File

@ -499,7 +499,7 @@ void PaintWindow::_handle_zoom(const Ref<InputEvent> &event) {
paint_canvas->set_pixel_size(px);
paint_canvas->canvas_background->set_pixel_size(8 * pow(0.5, big_grid_pixels) / paint_canvas->get_pixel_size());
//paint_canvas->canvas_background->set_pixel_size(8 * pow(0.5, big_grid_pixels) / paint_canvas->get_pixel_size());
Point2 pos = paint_canvas->get_position();
Size2 size = paint_canvas->get_size();
@ -521,7 +521,7 @@ void PaintWindow::_handle_zoom(const Ref<InputEvent> &event) {
paint_canvas->set_pixel_size(px);
paint_canvas->canvas_background->set_pixel_size(8 * pow(0.5, big_grid_pixels) / paint_canvas->get_pixel_size());
//paint_canvas->canvas_background->set_pixel_size(8 * pow(0.5, big_grid_pixels) / paint_canvas->get_pixel_size());
Point2 pos = paint_canvas->get_position();
Size2 size = paint_canvas->get_size();
@ -1235,7 +1235,7 @@ void PaintWindow::_notification(int p_what) {
} break;
case NOTIFICATION_POSTINITIALIZE: {
connect("visibility_changed", this, "_on_Editor_visibility_changed");
paint_canvas->canvas_background->set_pixel_size(8 * pow(0.5, big_grid_pixels) / paint_canvas->get_pixel_size());
//paint_canvas->canvas_background->set_pixel_size(8 * pow(0.5, big_grid_pixels) / paint_canvas->get_pixel_size());
} break;
case NOTIFICATION_PROCESS: {
_process(get_process_delta_time());

View File

@ -1,35 +0,0 @@
shader_type canvas_item;
uniform float pixel_size : hint_range(0.01, 1.0);
void fragment() {
vec4 color = texture(TEXTURE, UV);
float light = 0.8;
float dark = 0.4;
float val = dark;
if ( int(UV.y * 8.0 * pixel_size) % 2 == 1 ) {
if ( int(UV.x * 8.0 * pixel_size) % 2 == 1 ) {
val = dark;
}
else {
val = light;
}
}
else {
if ( int(UV.x * 8.0 * pixel_size) % 2 == 1 ) {
val = light;
}
else {
val = dark;
}
}
color.rgb = vec3(val, val, val);
COLOR = color;
}

View File

@ -1,77 +0,0 @@
#!/usr/bin/env python
import glob
import os
enc = "utf-8"
# Change to the directory where the script is located,
# so that the script can be run from any location
os.chdir(os.path.dirname(os.path.realpath(__file__)))
# Generate include files
f = open("shaders.h", "wb")
f.write(b"// THIS FILE HAS BEEN AUTOGENERATED, DON'T EDIT!!\n")
# Generate png image block
f.write(b"\n// png image block\n")
pixmaps = glob.glob("*.png")
pixmaps.sort()
for x in pixmaps:
var_str = x[:-4] + "_png"
s = "\nstatic const unsigned char " + var_str + "[] = {\n\t"
f.write(s.encode(enc))
pngf = open(x, "rb")
b = pngf.read(1)
while len(b) == 1:
f.write(hex(ord(b)).encode(enc))
b = pngf.read(1)
if len(b) == 1:
f.write(b", ")
f.write(b"\n};\n")
pngf.close()
# Generate shaders block
f.write(b"\n// shaders block\n")
shaders = glob.glob("*.gsl")
shaders.sort()
for x in shaders:
var_str = x[:-4] + "_shader_code"
s = "\nstatic const char *" + var_str + " = \n"
f.write(s.encode(enc))
sf = open(x, "rb")
b = sf.readline()
while b != b"":
if b.endswith(b"\r\n"):
b = b[:-2]
if b.endswith(b"\n"):
b = b[:-1]
s = b' "' + b
f.write(s)
b = sf.readline()
if b != b"":
f.write(b'"\n')
f.write(b'";\n')
sf.close()
f.close()

View File

@ -1,42 +0,0 @@
// THIS FILE HAS BEEN AUTOGENERATED, DON'T EDIT!!
// png image block
// shaders block
static const char *background_shader_shader_code =
""
"shader_type canvas_item;"
""
"uniform float pixel_size : hint_range(0.01, 1.0);"
""
""
"void fragment() {"
" vec4 color = texture(TEXTURE, UV);"
" "
" float light = 0.8;"
" float dark = 0.4;"
" "
" float val = dark;"
" "
" if ( int(UV.y * 8.0 * pixel_size) % 2 == 1 ) {"
" if ( int(UV.x * 8.0 * pixel_size) % 2 == 1 ) {"
" val = dark;"
" }"
" else {"
" val = light;"
" }"
" }"
" else {"
" if ( int(UV.x * 8.0 * pixel_size) % 2 == 1 ) {"
" val = light;"
" }"
" else {"
" val = dark;"
" }"
" }"
" "
" color.rgb = vec3(val, val, val);"
" "
" COLOR = color;"
"}";

View File

@ -24,31 +24,38 @@ SOFTWARE.
#include "paint_canvas_background.h"
#include "core/io/image.h"
#include "scene/resources/material.h"
#include "scene/resources/shader.h"
#include "scene/resources/texture.h"
#include "../shaders/shaders.h"
#include "../paint_icons/icons.h"
float PaintCanvasBackground::get_pixel_size() const {
return _pixel_size;
int PaintCanvasBackground::get_grid_size() const {
return _grid_size;
}
void PaintCanvasBackground::set_pixel_size(const float val) {
_pixel_size = val;
void PaintCanvasBackground::set_grid_size(const int val) {
_grid_size = val;
if (_material.is_valid()) {
_material->set_shader_param("pixel_size", _pixel_size);
}
update();
}
Color PaintCanvasBackground::get_grid_black() const {
return _grid_black;
}
void PaintCanvasBackground::set_grid_black(const Color &val) {
_grid_black = val;
update();
}
Color PaintCanvasBackground::get_grid_white() const {
return _grid_white;
}
void PaintCanvasBackground::set_grid_white(const Color &val) {
_grid_white = val;
update();
}
PaintCanvasBackground::PaintCanvasBackground() {
_pixel_size = 1;
_grid_size = 8;
set_expand(true);
set_stretch_mode(TextureRect::STRETCH_TILE);
_grid_black = Color(0.6, 0.6, 0.6, 1);
_grid_white = Color(0.79, 0.84, 0.96, 1);
}
PaintCanvasBackground::~PaintCanvasBackground() {
@ -56,27 +63,50 @@ PaintCanvasBackground::~PaintCanvasBackground() {
void PaintCanvasBackground::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
_shader.instance();
_shader->set_code(background_shader_shader_code);
case NOTIFICATION_DRAW: {
Size2 s = get_size();
int size_x = s.x;
int size_y = s.y;
_material.instance();
_material->set_shader(_shader);
_material->set_shader_param("pixel_size", _pixel_size);
bool x_black = false;
for (int x = 0; x < size_x; x += _grid_size) {
int cx = size_x - x;
if (cx > _grid_size) {
cx = _grid_size;
}
set_material(_material);
bool y_black = !x_black;
for (int y = 0; y < size_y; y += _grid_size) {
int cy = size_y - y;
if (cy > _grid_size) {
cy = _grid_size;
}
set_texture(PaintIcons::make_icon_grid_png());
break;
case NOTIFICATION_EXIT_TREE:
set_material(Ref<Material>());
_shader.unref();
_material.unref();
break;
if (y_black) {
draw_rect(Rect2(x, y, cx, cy), _grid_black);
} else {
draw_rect(Rect2(x, y, cx, cy), _grid_white);
}
y_black = !y_black;
}
x_black = !x_black;
}
} break;
}
}
void PaintCanvasBackground::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_pixel_size"), &PaintCanvasBackground::get_pixel_size);
ClassDB::bind_method(D_METHOD("set_pixel_size"), &PaintCanvasBackground::set_pixel_size);
ClassDB::bind_method(D_METHOD("get_grid_size"), &PaintCanvasBackground::get_grid_size);
ClassDB::bind_method(D_METHOD("set_grid_size", "size"), &PaintCanvasBackground::set_grid_size);
ADD_PROPERTY(PropertyInfo(Variant::INT, "grid_size"), "set_grid_size", "get_grid_size");
ClassDB::bind_method(D_METHOD("get_grid_black"), &PaintCanvasBackground::get_grid_black);
ClassDB::bind_method(D_METHOD("set_grid_size", "size"), &PaintCanvasBackground::set_grid_size);
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "grid_black"), "set_grid_size", "get_grid_black");
ClassDB::bind_method(D_METHOD("get_grid_white"), &PaintCanvasBackground::get_grid_white);
ClassDB::bind_method(D_METHOD("set_grid_white", "size"), &PaintCanvasBackground::set_grid_white);
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "grid_white"), "set_grid_white", "get_grid_white");
}

View File

@ -37,8 +37,14 @@ class PaintCanvasBackground : public TextureRect {
GDCLASS(PaintCanvasBackground, TextureRect);
public:
float get_pixel_size() const;
void set_pixel_size(const float val);
int get_grid_size() const;
void set_grid_size(const int val);
Color get_grid_black() const;
void set_grid_black(const Color &val);
Color get_grid_white() const;
void set_grid_white(const Color &val);
PaintCanvasBackground();
~PaintCanvasBackground();
@ -48,10 +54,10 @@ protected:
static void _bind_methods();
float _pixel_size;
int _grid_size;
Ref<ShaderMaterial> _material;
Ref<Shader> _shader;
Color _grid_black;
Color _grid_white;
};
#endif