Skip to content

Commit

Permalink
ported touch support to godot 4
Browse files Browse the repository at this point in the history
  • Loading branch information
m-GDEV committed Dec 26, 2024
1 parent c3cfed2 commit e83a691
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lorien/InfiniteCanvas/PanZoomCamera.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ var _zoom_active := false
var _current_zoom_level := 1.0
var _start_mouse_pos := Vector2(0.0, 0.0)

# added by musa (remove)
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 @@ -35,6 +43,49 @@ func do_center(screen_space_center_point: Vector2) -> void:
get_viewport().warp_mouse(screen_space_center)
_do_pan(delta)


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_viewport().set_input_as_handled()

if event is InputEventScreenDrag:
_touch_events[event.index] = event
# At least one fing 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() * median_point
median_point = get_global_transform().affine_inverse() * 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_viewport().set_input_as_handled()

func _input(event):
touch_event(event)

# -------------------------------------------------------------------------------------------------
func tool_event(event: InputEvent) -> void:
if _is_input_enabled:
Expand Down
1 change: 1 addition & 0 deletions lorien/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ canvas_pan_key={
[input_devices]

pen_tablet/driver.windows="wintab"
pointing/emulate_mouse_from_touch=false

[locale]

Expand Down

0 comments on commit e83a691

Please sign in to comment.