Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch screen pans and zooms on canvas by using drag and pinch #227

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions lorien/InfiniteCanvas/PanZoomCamera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ var _zoom_active := false
var _current_zoom_level := 1.0
var _start_mouse_pos := Vector2(0.0, 0.0)

var _touch_events = {}
var _touch_last_drag_distance := 0.0
var _touch_last_drag_median := Vector2.ZERO
var _multidrag_valid = false

# -------------------------------------------------------------------------------------------------
func set_zoom_level(zoom_level: float) -> void:
_current_zoom_level = _to_nearest_zoom_step(zoom_level)
Expand All @@ -27,7 +32,55 @@ func do_center(screen_space_center_point: Vector2) -> void:
var delta := screen_space_center - screen_space_center_point
get_viewport().warp_mouse(screen_space_center)
_do_pan(delta)

# -------------------------------------------------------------------------------------------------
func _process(delta):
_touch_events
pass

# -------------------------------------------------------------------------------------------------
func touch_event(event):
# Keep track of the fingers on the screen
if event is InputEventScreenTouch:
if event.pressed:
_touch_events[event.index] = event
_multidrag_valid = false
else:
_touch_events.erase(event.index)
#_touch_last_drag_distance = 0
get_tree().set_input_as_handled()

if event is InputEventScreenDrag:
_touch_events[event.index] = event
# At least one finger to drag
if _touch_events.size() == 1:
_do_pan(event.relative)
if _touch_events.size() == 2:
var events = []
for key in _touch_events.keys():
events.append(_touch_events.get(key))

var median_point = Vector2.ZERO
for e in events:
median_point += e.position
median_point /= events.size()
if _multidrag_valid:
_do_pan(median_point - _touch_last_drag_median)
_touch_last_drag_median = median_point
median_point = get_canvas_transform().affine_inverse().xform(median_point)
median_point = get_global_transform().affine_inverse().xform(median_point)

var drag_distance = events[0].position.distance_to(events[1].position)
var delta = -(drag_distance - _touch_last_drag_distance) * _current_zoom_level / 800
if _multidrag_valid:
_zoom_canvas(_current_zoom_level + delta, median_point)
_touch_last_drag_distance = drag_distance
_multidrag_valid = true
get_tree().set_input_as_handled()

func _input(event):
touch_event(event)

# -------------------------------------------------------------------------------------------------
func tool_event(event: InputEvent) -> void:
if _is_input_enabled:
Expand Down
4 changes: 4 additions & 0 deletions lorien/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ canvas_pan_right={
]
}

[input_devices]

pointing/emulate_mouse_from_touch=false

[locale]

translation_remaps={
Expand Down