Properly ported PaintCanvasBackground.

This commit is contained in:
Relintai 2022-04-18 15:32:55 +02:00
parent 71ac7bc4e3
commit 7abae2b54e
9 changed files with 236 additions and 16 deletions

View File

@ -29,7 +29,7 @@ module_env.add_source_files(env.modules_sources,"paint_canvas_layer.cpp")
module_env.add_source_files(env.modules_sources,"paint_canvas.cpp")
module_env.add_source_files(env.modules_sources,"paint_canvas_outline.cpp")
module_env.add_source_files(env.modules_sources,"paint_color_grid.cpp")
module_env.add_source_files(env.modules_sources,"paint_canvas_background.cpp")
module_env.add_source_files(env.modules_sources,"paint_layer_button.cpp")

View File

@ -0,0 +1,35 @@
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

@ -40,4 +40,38 @@ for x in pixmaps:
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

@ -65,3 +65,42 @@ static const unsigned char minidotta_invis_png[] = {
static const unsigned char unlock_layer_png[] = {
0x89, 0x50, 0x4e, 0x47, 0xd, 0xa, 0x1a, 0xa, 0x0, 0x0, 0x0, 0xd, 0x49, 0x48, 0x44, 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, 0xcf, 0x76, 0x3, 0xef, 0x35, 0x7d, 0x4, 0x8f, 0x7e, 0x99, 0x9e, 0x7b, 0x68, 0xcd, 0x17, 0x99, 0xa2, 0x33, 0x10, 0x43, 0x9f, 0x32, 0x9e, 0x0, 0x0, 0x0, 0x0, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};
// 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

@ -29,6 +29,7 @@ SOFTWARE.
#include "paint_visual_grid.h"
#include "scene/gui/control.h"
#include "scene/gui/texture_rect.h"
#include "paint_canvas_background.h"
#include "paint_canvas_layer.h"
@ -610,11 +611,11 @@ PaintCanvas::PaintCanvas() {
mouse_in_region = false;
mouse_on_top = false;
canvas_background_rect = memnew(TextureRect);
canvas_background_rect->set_texture(make_icon(grid_png));
canvas_background_rect->set_expand(true);
canvas_background_rect->set_stretch_mode(TextureRect::STRETCH_TILE);
add_child(canvas_background_rect);
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);
add_child(canvas_background);
canvas_layers = memnew(Control);
canvas_layers->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);

View File

@ -36,6 +36,7 @@ class Control;
class PaintCanvasOutline;
class PaintVisualGrid;
class PaintCanvasLayer;
class PaintCanvasBackground;
//class_name GECanvas
class PaintCanvas : public MarginContainer {
@ -116,7 +117,7 @@ public:
Ref<PaintCanvasLayer> preview_layer;
Ref<PaintCanvasLayer> tool_layer;
TextureRect *canvas_background_rect;
PaintCanvasBackground *canvas_background;
Control *canvas_layers;
TextureRect *preview_layer_rect;
TextureRect *tool_preview_layer_rect;

View File

@ -0,0 +1,58 @@
/*
Copyright (c) 2019 Flairieve
Copyright (c) 2020-2022 cobrapitz
Copyright (c) 2022 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "paint_canvas_background.h"
#include "scene/resources/material.h"
#include "scene/resources/shader.h"
#include "icons/paint_icons.h"
float PaintCanvasBackground::get_pixel_size() const {
return _pixel_size;
}
void PaintCanvasBackground::set_pixel_size(const float val) {
_pixel_size = val;
_material->set_shader_param("pixel_size", _pixel_size);
}
PaintCanvasBackground::PaintCanvasBackground() {
_pixel_size = 1;
_shader.instance();
_shader->set_code(background_shader_shader_code);
_material.instance();
_material->set_shader(_shader);
_material->set_shader_param("pixel_size", _pixel_size);
set_material(_material);
}
PaintCanvasBackground::~PaintCanvasBackground() {
}
void PaintCanvasBackground::_bind_methods() {
}

View File

@ -0,0 +1,54 @@
#ifndef PAINT_CANVAS_BACKGROUND_H
#define PAINT_CANVAS_BACKGROUND_H
/*
Copyright (c) 2019 Flairieve
Copyright (c) 2020-2022 cobrapitz
Copyright (c) 2022 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "scene/gui/texture_rect.h"
#include "core/reference.h"
class ShaderMaterial;
class Shader;
class PaintCanvasBackground : public TextureRect {
GDCLASS(PaintCanvasBackground, TextureRect);
public:
float get_pixel_size() const;
void set_pixel_size(const float val);
PaintCanvasBackground();
~PaintCanvasBackground();
protected:
static void _bind_methods();
float _pixel_size;
Ref<ShaderMaterial> _material;
Ref<Shader> _shader;
};
#endif

View File

@ -41,6 +41,7 @@ SOFTWARE.
#include "actions/rect_action.h"
#include "paint_canvas.h"
#include "paint_canvas_background.h"
#include "paint_canvas_layer.h"
#include "paint_color_grid.h"
#include "paint_layer_button.h"
@ -357,11 +358,7 @@ void PaintWindow::_handle_zoom(const Ref<InputEvent> &event) {
paint_canvas->set_pixel_size(px);
//Ref<CanvasItemMaterial> mat = paint_canvas->canvas_background_rect->get_material();
//if (mat.is_valid()) {
// mat->set_shader_param("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();
@ -383,7 +380,7 @@ void PaintWindow::_handle_zoom(const Ref<InputEvent> &event) {
paint_canvas->set_pixel_size(px);
//find_node("CanvasBackground").material.set_shader_param("pixel_size", 8 * pow(0.5, big_grid_pixels)/paint_canvas.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();
@ -1078,6 +1075,7 @@ void PaintWindow::_notification(int p_what) {
switch (p_what) {
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());
} break;
case NOTIFICATION_PROCESS: {
_process(get_process_delta_time());
@ -1100,8 +1098,8 @@ PaintWindow::PaintWindow() {
_total_added_layers = 0;
_middle_mouse_pressed = false;
_previous_tool = PaintWindow::PAINT;
brush_mode = PaintWindow::PAINT;
_previous_tool = PaintWindow::COLORPICKER;
brush_mode = PaintWindow::COLORPICKER;
selected_brush_prefab = BrushPrefabs::CIRCLE;
@ -1450,7 +1448,7 @@ PaintWindow::PaintWindow() {
_on_ChangeCanvasSizeDialog_confirmed();
_on_ChangeGridSizeDialog_confirmed();
//find_node("CanvasBackground").material.set_shader_param("pixel_size", 8 * pow(0.5, big_grid_pixels)/paint_canvas.pixel_size)
add_new_layer();