[gd_scene load_steps=9 format=2]

[ext_resource path="res://material_maker/icons/icons.svg" type="Texture" id=1]
[ext_resource path="res://material_maker/panels/preview_2d/control_point.tscn" type="PackedScene" id=2]
[ext_resource path="res://material_maker/panels/preview_2d/preview_2d.tscn" type="PackedScene" id=3]
[ext_resource path="res://material_maker/panels/preview_2d/preview_2d_panel.gd" type="Script" id=4]

[sub_resource type="Shader" id=1]
code = "shader_type canvas_item;

uniform vec2 size = vec2(585.0, 453.0);
uniform bool show_tiling = false;
uniform float margin = 0.2;

vec4 preview_2d(vec2 uv) {
	return vec4(0.8);
}

void fragment() {
	float ms = max(size.x, size.y);
	float margin_factor = 1.0+max(0.0, margin);
	vec2 uv = 0.5+margin_factor*(UV-vec2(0.5))*ms/size.yx;
	float is = min(size.x, size.y)/margin_factor;
	vec4 image = preview_2d(fract(uv));
	float checkerboard = mod(floor(uv.x*32.0)+floor(uv.y*32.0), 2.0);
	vec3 image_with_background = mix(vec3(checkerboard), image.xyz, image.a);
	if (show_tiling) {
		vec2 m2 = min(fract(uv), 1.0-fract(uv));
		float lines_color = 0.5*(cos(5.0*TIME+100.0*(uv.x+uv.y))+1.0);
		COLOR = vec4(mix(image_with_background, vec3(lines_color), step(is*min(m2.x, m2.y), 1.0)), 1.0);
	} else {
		vec2 outside = step(abs(uv-vec2(0.5)), vec2(0.5));
		COLOR = vec4(mix(vec3(0.0), image_with_background, outside.x*outside.y), 1.0);
	}
}"

[sub_resource type="ShaderMaterial" id=2]
shader = SubResource( 1 )
shader_param/size = Vector2( 585, 492 )
shader_param/show_tiling = false
shader_param/margin = 0.5

[sub_resource type="AtlasTexture" id=3]
flags = 4
atlas = ExtResource( 1 )
region = Rect2( 64, 48, 32, 32 )

[sub_resource type="AtlasTexture" id=4]
flags = 4
atlas = ExtResource( 1 )
region = Rect2( 16, 64, 16, 16 )

[node name="Preview2D" instance=ExtResource( 3 )]
material = SubResource( 2 )
margin_right = -758.0
margin_bottom = -267.0
rect_min_size = Vector2( 200, 200 )
script = ExtResource( 4 )
shader = "uniform vec2 size;
uniform bool export = false;
uniform bool show_tiling = true;
uniform float margin = 0.5;

void fragment() {
	float ms = max(size.x, size.y);
	float margin_factor = 1.0+max(0.0, margin);
	vec2 uv = 0.5+margin_factor*(UV-vec2(0.5))*ms/size.yx;
	float is = min(size.x, size.y)/margin_factor;
	vec4 image = preview_2d(fract(uv));
	if (export) {
		COLOR = image;
	} else {
		float checkerboard = mod(floor(uv.x*32.0)+floor(uv.y*32.0), 2.0);
		vec3 image_with_background = mix(vec3(checkerboard), image.xyz, image.a);
		if (show_tiling) {
			vec2 m2 = min(fract(uv), 1.0-fract(uv));
			float lines_color = 0.5*(cos(5.0*TIME+100.0*(uv.x+uv.y))+1.0);
			COLOR = vec4(mix(image_with_background, vec3(lines_color), step(is*min(m2.x, m2.y), 1.0)), 1.0);
		} else {
			vec2 outside = step(abs(uv-vec2(0.5)), vec2(0.5));
			COLOR = vec4(mix(vec3(0.0), image_with_background, outside.x*outside.y), 1.0);
		}
	}
}
"

[node name="P1" parent="." index="0" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 1, 0.466667, 0, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 37.0
margin_bottom = 61.0
texture = SubResource( 3 )

[node name="P2" parent="." index="1" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 0, 0.505882, 1, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 37.0
margin_bottom = 61.0
texture = SubResource( 3 )

[node name="P3" parent="." index="2" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 0, 1, 0.380392, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 37.0
margin_bottom = 61.0
texture = SubResource( 4 )

[node name="Rect1" parent="." index="3" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 1, 0.631373, 0, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 21.0
margin_bottom = 45.0
texture = SubResource( 4 )
parent_control = "P1"
control_type = 1

[node name="Radius1" parent="." index="4" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 1, 0.631373, 0, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 21.0
margin_bottom = 45.0
mouse_default_cursor_shape = 10
texture = SubResource( 4 )
parent_control = "P1"
control_type = 2

[node name="Radius11" parent="." index="5" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 1, 0.631373, 0, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 21.0
margin_bottom = 45.0
mouse_default_cursor_shape = 10
texture = SubResource( 4 )
parent_control = "Radius1"
control_type = 2

[node name="Scale1" parent="." index="6" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 1, 0.631373, 0, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 21.0
margin_bottom = 45.0
mouse_default_cursor_shape = 10
texture = SubResource( 4 )
parent_control = "P1"
control_type = 3

[node name="Angle1" parent="." index="7" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 1, 0.631373, 0, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 21.0
margin_bottom = 45.0
mouse_default_cursor_shape = 10
texture = SubResource( 4 )
parent_control = "P1"
control_type = 2

[node name="Angle2" parent="." index="8" instance=ExtResource( 2 )]
visible = false
self_modulate = Color( 1, 0.631373, 0, 1 )
margin_left = 5.0
margin_top = 29.0
margin_right = 21.0
margin_bottom = 45.0
mouse_default_cursor_shape = 10
texture = SubResource( 4 )
parent_control = "P1"
control_type = 2

[node name="ContextMenu" type="PopupMenu" parent="." index="9"]
margin_right = 109.0
margin_bottom = 52.0
items = [ "Show tiling", null, 1, false, false, 0, 0, null, "", false ]

[node name="Export" type="PopupMenu" parent="ContextMenu" index="1"]
margin_right = 91.0
margin_bottom = 88.0

[node name="Reference" type="PopupMenu" parent="ContextMenu" index="2"]
margin_right = 91.0
margin_bottom = 88.0
[connection signal="gui_input" from="." to="." method="_on_gui_input"]
[connection signal="id_pressed" from="ContextMenu" to="." method="_on_ContextMenu_id_pressed"]
[connection signal="id_pressed" from="ContextMenu/Export" to="." method="_on_Export_id_pressed"]
[connection signal="id_pressed" from="ContextMenu/Reference" to="." method="_on_Reference_id_pressed"]
