tool
extends Node2D

var image := Image.new()
var table := ImageTexture.new()

onready var Util = preload("res://Util.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()
	
	image.create(8, 1, false, Image.FORMAT_RGBAF)
	table.create_from_image(image, 0)
	#table.create(8, 1, Image.FORMAT_RGBAF, 0)
	#image = table.get_data()
	image.lock()
	for i in range(8):
		image.set_pixel(i, 0,
		Color(1.0, 0.0, 0.0))
	image.unlock()
	table.set_data(image)
	$"../Sprite".texture = table
	$"../CSGBox".material.set_shader_param("table", table)
	_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):
	#print(canvas_inv.xform(get_local_mouse_position()))
	#print(event.as_text())
	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(
			canvas_trans.xform(corners[i]),
			canvas_trans.xform(corners[wrapi(i + 1, 0, 4)]),
			Color.maroon)
	for i in range(4):
		draw_circle(canvas_trans.xform(corners[i]), 10.0, Color.maroon)
	for i in range(4):
		draw_line(
			canvas_trans.xform(corners[i]),
			canvas_trans.xform(corners[i + 4]),
			Color.yellow)
	for i in range(4):
		draw_line(
			canvas_trans.xform(corners[i + 4]),
			canvas_trans.xform(corners[wrapi(i + 5, 4, 8)]),
			Color.purple)
	for i in range(4):
		draw_circle(canvas_trans.xform(corners[i + 4]), 10.0, Color.purple)
		
	draw_texture_rect(table, Rect2(
		Vector2(750.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,
#	]
	
	image.lock()
	for i in range(4):
		image.set_pixel(i, 0,
		Color(front_matrix[i * 4], front_matrix[i * 4 + 1],
		front_matrix[i * 4 + 2], front_matrix[i * 4 + 3]))
	for i in range(4):
		image.set_pixel(i + 4, 0,
		Color(rear_matrix[i * 4], rear_matrix[i * 4 + 1],
		rear_matrix[i * 4 + 2], rear_matrix[i * 4 + 3]))
	image.unlock()
	table.set_data(image)
