91 lines
2.6 KiB
GDScript
91 lines
2.6 KiB
GDScript
extends SummerDayTool
|
|
class_name SummerDayFreeTransformCanvasTool
|
|
|
|
|
|
var touch_dict = {}
|
|
var initial_touches
|
|
var current_touches
|
|
var initial_transform: Transform2D
|
|
|
|
|
|
func _init():
|
|
priority = 1
|
|
|
|
|
|
func _check_for_use(event: InputEvent, tool_context: SummerDayToolContext):
|
|
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 = tool_context.canvas_state.canvas_transform
|
|
|
|
var keys = touch_dict.keys()
|
|
initial_touches = [touch_dict[keys[0]], touch_dict[keys[1]]]
|
|
print(touch_dict)
|
|
|
|
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, tool_context: SummerDayToolContext):
|
|
# var jump := Transform2D.IDENTITY
|
|
#
|
|
# # Calculate scale.
|
|
# var initial_distance = initial_touches[0].distance_to(initial_touches[1])
|
|
# var current_distance = current_touches[0].distance_to(current_touches[1])
|
|
# var scale = current_distance / initial_distance
|
|
#
|
|
# # Calculate rotation.
|
|
# var initial_angle = initial_touches[0].angle_to_point(initial_touches[1])
|
|
# var current_angle = current_touches[0].angle_to_point(current_touches[1])
|
|
# var rotation = current_angle - initial_angle
|
|
#
|
|
# jump = jump.scaled(Vector2(scale, scale)).rotated(rotation)
|
|
#
|
|
# # Calculate origin.
|
|
# var transformed_initial_touch = jump.xform(initial_touches[0])
|
|
# var offset = current_touches[0] - transformed_initial_touch
|
|
#
|
|
# #print(jump)
|
|
# jump.origin = offset
|
|
# #jump = jump.translated(offset)
|
|
# #print(jump)
|
|
#
|
|
# var transform = jump * initial_transform
|
|
# #print(transform)
|
|
|
|
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]
|
|
)
|
|
|
|
#print(basis)
|
|
|
|
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
|
|
tool_context.canvas_state.canvas_transform = transform
|