mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-02-10 01:50:05 +01:00
Reworked PaintCanvasBackground. Now it draws it's pattern using canvas draw commands.
This commit is contained in:
parent
5a78997c83
commit
5c38af1d1a
@ -581,6 +581,7 @@ PaintCanvasOld::PaintCanvasOld() {
|
|||||||
mouse_on_top = false;
|
mouse_on_top = false;
|
||||||
|
|
||||||
canvas_background = memnew(PaintCanvasBackground);
|
canvas_background = memnew(PaintCanvasBackground);
|
||||||
|
canvas_background->set_grid_size(32);
|
||||||
add_child(canvas_background);
|
add_child(canvas_background);
|
||||||
|
|
||||||
canvas_layers = memnew(Control);
|
canvas_layers = memnew(Control);
|
||||||
|
@ -499,7 +499,7 @@ void PaintWindow::_handle_zoom(const Ref<InputEvent> &event) {
|
|||||||
|
|
||||||
paint_canvas->set_pixel_size(px);
|
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();
|
Point2 pos = paint_canvas->get_position();
|
||||||
Size2 size = paint_canvas->get_size();
|
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->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();
|
Point2 pos = paint_canvas->get_position();
|
||||||
Size2 size = paint_canvas->get_size();
|
Size2 size = paint_canvas->get_size();
|
||||||
@ -1235,7 +1235,7 @@ void PaintWindow::_notification(int p_what) {
|
|||||||
} break;
|
} break;
|
||||||
case NOTIFICATION_POSTINITIALIZE: {
|
case NOTIFICATION_POSTINITIALIZE: {
|
||||||
connect("visibility_changed", this, "_on_Editor_visibility_changed");
|
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;
|
} break;
|
||||||
case NOTIFICATION_PROCESS: {
|
case NOTIFICATION_PROCESS: {
|
||||||
_process(get_process_delta_time());
|
_process(get_process_delta_time());
|
||||||
|
@ -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;
|
|
||||||
}
|
|
@ -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()
|
|
||||||
|
|
@ -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;"
|
|
||||||
"}";
|
|
@ -24,31 +24,38 @@ SOFTWARE.
|
|||||||
|
|
||||||
#include "paint_canvas_background.h"
|
#include "paint_canvas_background.h"
|
||||||
|
|
||||||
#include "core/io/image.h"
|
int PaintCanvasBackground::get_grid_size() const {
|
||||||
#include "scene/resources/material.h"
|
return _grid_size;
|
||||||
#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;
|
|
||||||
}
|
}
|
||||||
void PaintCanvasBackground::set_pixel_size(const float val) {
|
void PaintCanvasBackground::set_grid_size(const int val) {
|
||||||
_pixel_size = val;
|
_grid_size = val;
|
||||||
|
|
||||||
if (_material.is_valid()) {
|
update();
|
||||||
_material->set_shader_param("pixel_size", _pixel_size);
|
}
|
||||||
}
|
|
||||||
|
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() {
|
PaintCanvasBackground::PaintCanvasBackground() {
|
||||||
_pixel_size = 1;
|
_grid_size = 8;
|
||||||
|
|
||||||
set_expand(true);
|
_grid_black = Color(0.6, 0.6, 0.6, 1);
|
||||||
set_stretch_mode(TextureRect::STRETCH_TILE);
|
_grid_white = Color(0.79, 0.84, 0.96, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
PaintCanvasBackground::~PaintCanvasBackground() {
|
PaintCanvasBackground::~PaintCanvasBackground() {
|
||||||
@ -56,27 +63,50 @@ PaintCanvasBackground::~PaintCanvasBackground() {
|
|||||||
|
|
||||||
void PaintCanvasBackground::_notification(int p_what) {
|
void PaintCanvasBackground::_notification(int p_what) {
|
||||||
switch (p_what) {
|
switch (p_what) {
|
||||||
case NOTIFICATION_ENTER_TREE:
|
case NOTIFICATION_DRAW: {
|
||||||
_shader.instance();
|
Size2 s = get_size();
|
||||||
_shader->set_code(background_shader_shader_code);
|
int size_x = s.x;
|
||||||
|
int size_y = s.y;
|
||||||
|
|
||||||
_material.instance();
|
bool x_black = false;
|
||||||
_material->set_shader(_shader);
|
for (int x = 0; x < size_x; x += _grid_size) {
|
||||||
_material->set_shader_param("pixel_size", _pixel_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());
|
if (y_black) {
|
||||||
break;
|
draw_rect(Rect2(x, y, cx, cy), _grid_black);
|
||||||
case NOTIFICATION_EXIT_TREE:
|
} else {
|
||||||
set_material(Ref<Material>());
|
draw_rect(Rect2(x, y, cx, cy), _grid_white);
|
||||||
_shader.unref();
|
}
|
||||||
_material.unref();
|
|
||||||
break;
|
y_black = !y_black;
|
||||||
|
}
|
||||||
|
|
||||||
|
x_black = !x_black;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintCanvasBackground::_bind_methods() {
|
void PaintCanvasBackground::_bind_methods() {
|
||||||
ClassDB::bind_method(D_METHOD("get_pixel_size"), &PaintCanvasBackground::get_pixel_size);
|
ClassDB::bind_method(D_METHOD("get_grid_size"), &PaintCanvasBackground::get_grid_size);
|
||||||
ClassDB::bind_method(D_METHOD("set_pixel_size"), &PaintCanvasBackground::set_pixel_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");
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,14 @@ class PaintCanvasBackground : public TextureRect {
|
|||||||
GDCLASS(PaintCanvasBackground, TextureRect);
|
GDCLASS(PaintCanvasBackground, TextureRect);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
float get_pixel_size() const;
|
int get_grid_size() const;
|
||||||
void set_pixel_size(const float val);
|
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();
|
||||||
~PaintCanvasBackground();
|
~PaintCanvasBackground();
|
||||||
@ -48,10 +54,10 @@ protected:
|
|||||||
|
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
float _pixel_size;
|
int _grid_size;
|
||||||
|
|
||||||
Ref<ShaderMaterial> _material;
|
Color _grid_black;
|
||||||
Ref<Shader> _shader;
|
Color _grid_white;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user