SummerDay/Node2D.gd

144 lines
3.0 KiB
GDScript3
Raw Normal View History

2021-11-18 17:23:37 +01:00
tool
extends Node2D
var table = SummerDayTable.new()
2021-11-18 17:23:37 +01:00
onready var Util = preload("res://addons/summer_day/utilities/math_helper.gd")
2021-11-18 17:23:37 +01:00
var corners = [
Vector2(-0.4, -0.4),
Vector2(0.1, -0.4),
Vector2(0.1, 0.1),
Vector2(-0.4, 0.1),
Vector2(-0.1, -0.1),
Vector2(0.4, -0.1),
Vector2(0.4, 0.4),
Vector2(-0.1, 0.4),
]
var picked_point = -1
var rear_matrix
var front_matrix
2021-11-18 17:23:37 +01:00
var canvas_inv : Transform2D
var canvas_trans : Transform2D
# Called when the node enters the scene tree for the first time.
func _ready():
update_canvas_inv()
table.allocation_size = 2
table.create()
$"../Sprite".texture = table.texture
$"../CSGBox".material.set_shader_param("table", table.texture)
2021-11-18 17:23:37 +01:00
_update_matrix()
func update_canvas_inv():
while true:
var viewport_size = get_viewport_rect().size
canvas_inv = Transform2D(
Vector2(2.0 / viewport_size.x, 0.0),
Vector2(0.0, -2.0 / viewport_size.y),
Vector2(-1.0, 1.0)
)
canvas_trans = canvas_inv.affine_inverse()
update()
yield(get_viewport(), "size_changed")
func _input(event):
var mouse_position = get_viewport().get_mouse_position()
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
if event.pressed:
_pick_point(canvas_inv.xform(mouse_position))
else:
picked_point = -1
if event is InputEventMouseMotion:
if picked_point != -1:
corners[picked_point] = canvas_inv.xform(mouse_position)
_update_matrix()
update()
func _draw():
for i in range(4):
draw_line(
2021-12-12 17:04:17 +01:00
corners[i],
corners[wrapi(i + 1, 0, 4)],
2021-11-18 17:23:37 +01:00
Color.maroon)
for i in range(4):
2021-12-12 17:04:17 +01:00
draw_circle(corners[i], 0.01, Color.maroon)
2021-11-18 17:23:37 +01:00
for i in range(4):
draw_line(
2021-12-12 17:04:17 +01:00
corners[i],
corners[i + 4],
2021-11-18 17:23:37 +01:00
Color.yellow)
for i in range(4):
draw_line(
2021-12-12 17:04:17 +01:00
corners[i + 4],
corners[wrapi(i + 5, 4, 8)],
2021-11-18 17:23:37 +01:00
Color.purple)
for i in range(4):
2021-12-12 17:04:17 +01:00
draw_circle(corners[i + 4], 0.01, Color.purple)
2021-11-18 17:23:37 +01:00
draw_texture_rect(table.texture, Rect2(
2021-12-12 17:04:17 +01:00
Vector2(450.0, 50.0), Vector2(256.0, 32.0)
2021-11-18 17:23:37 +01:00
), false)
func _pick_point(mouse_pos):
var picked_point_so_far = -1
var nearest_distance = 20.0
for i in corners.size():
var distance = corners[i].distance_to(mouse_pos)
if distance < nearest_distance:
nearest_distance = distance
picked_point_so_far = i
picked_point = picked_point_so_far
func _update_matrix():
rear_matrix = Util.plane_3d_to_xy(
Vector3(-1.0, -1.0, 1.0),
Vector3(1.0, -1.0, 1.0),
Vector3(1.0, 1.0, 1.0),
Vector3(-1.0, 1.0, 1.0),
corners[0], corners[1], corners[2], corners[3]
)
front_matrix = Util.plane_3d_to_xy(
Vector3(-1.0, -1.0, -1.0),
Vector3(1.0, -1.0, -1.0),
Vector3(1.0, 1.0, -1.0),
Vector3(-1.0, 1.0, -1.0),
corners[4], corners[5], corners[6], corners[7]
)
# rear_matrix = [
# 0.5, 0.0, 0.0, 0.0,
# 0.0, 0.5, 0.0, 0.0,
# 0.0, 0.0, 0.5, 0.0,
# 0.0, 0.0, 0.0, 8.5,
# ]
#
# front_matrix = [
# 0.5, 0.0, 0.0, 0.0,
# 0.0, 0.5, 0.0, 20.0,
# 0.0, 0.0, 0.5, 0.0,
# 0.0, 0.0, 0.0, 8.5,
# ]
table.apply_matrix(front_matrix, 0, 0)
table.apply_matrix(rear_matrix, 0, 1)
table.flush()