SummerDay/addons/summer_day/tools/canvas/free_transform.gd
2022-01-03 23:53:28 +01:00

67 lines
1.7 KiB
GDScript

tool
extends SummerDayTool
class_name SummerDayFreeTransformCanvasTool
var touch_dict = {}
var initial_touches
var current_touches
var initial_transform: Transform2D
func _init():
priority = 1
func _handle_global_input(event: InputEvent):
pass
func _check_for_use(event: InputEvent):
if event is InputEventScreenTouch:
if event.pressed:
touch_dict[event.index] = event.position
else:
touch_dict.erase(event.index)
if touch_dict.size() == 2:
initial_transform = SummerDay.canvas_state.canvas_transform
var keys = touch_dict.keys()
initial_touches = [touch_dict[keys[0]], touch_dict[keys[1]]]
if event is InputEventScreenDrag:
touch_dict[event.index] = event.position
if touch_dict.size() == 2:
var keys = touch_dict.keys()
current_touches = [touch_dict[keys[0]], touch_dict[keys[1]]]
return true
return false
func _run(event: InputEvent):
var init_set = [
initial_touches[0],
initial_touches[1],
(initial_touches[1] - initial_touches[0]).tangent() + initial_touches[1],
(initial_touches[1] - initial_touches[0]).tangent() + initial_touches[0],
]
var curr_set = [
current_touches[0],
current_touches[1],
(current_touches[1] - current_touches[0]).tangent() + current_touches[1],
(current_touches[1] - current_touches[0]).tangent() + current_touches[0],
]
var basis = SummerDayMathHelper.basis_from_to_points(
init_set[0], init_set[1], init_set[2], init_set[3],
curr_set[0], curr_set[1], curr_set[2], curr_set[3]
)
var transform = Transform2D(
Vector2(basis.x.x, basis.x.y),
Vector2(basis.y.x, basis.y.y),
Vector2(basis.z.x, basis.z.y)
) * initial_transform
SummerDay.canvas_state.canvas_transform = transform