tool extends Node2D var table = SummerDayTable.new() onready var Util = preload("res://addons/summer_day/utilities/math_helper.gd") 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 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) _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( corners[i], corners[wrapi(i + 1, 0, 4)], Color.maroon) for i in range(4): draw_circle(corners[i], 0.01, Color.maroon) for i in range(4): draw_line( corners[i], corners[i + 4], Color.yellow) for i in range(4): draw_line( corners[i + 4], corners[wrapi(i + 5, 4, 8)], Color.purple) for i in range(4): draw_circle(corners[i + 4], 0.01, Color.purple) draw_texture_rect(table.texture, Rect2( Vector2(450.0, 50.0), Vector2(256.0, 32.0) ), 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()