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

[3.x] Implement OS agnostic InputEventGesture with Pan, Pinch, and Twist #37754

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from

Conversation

jeremyz
Copy link
Contributor

@jeremyz jeremyz commented Apr 10, 2020

this is shamelessy inspired by Federico-Ciuffardi's job on #36953

I have some ideas to implement thresholds to limit the amount of
emitted events but it will be implemented later.

this to be linked to #13139

@jeremyz
Copy link
Contributor Author

jeremyz commented Jun 15, 2020

[gd_scene load_steps=3 format=2]

[ext_resource path="res://icon.png" type="Texture" id=1]

[sub_resource type="GDScript" id=1]
script/source = "extends Node2D

const MAX_ZOOM = 0.5
const MIN_ZOOM = 0.1
onready var cam = $Camera

func _ready():
	cam.zoom.x = 0.2
	cam.zoom.y = 0.2

func _unhandled_input(e):
	if e is InputEventGesturePan:
	        _move(e)
	elif e is InputEventGestureTwist:
	        _rotate(e)
	elif e is InputEventGesturePinch:
	        _zoom(e)

func _move(event):
	cam.offset -= (event.get_delta() * cam.zoom).rotated(cam.rotation)

func _rotate(event):
	var fccp = (event.position - get_camera_center_offset())
	var fccp_op_rot =  - fccp.rotated(event.rotation)
	cam.offset -= ((fccp_op_rot + fccp) * cam.zoom).rotated(cam.rotation - event.rotation)
	cam.rotation -= event.rotation

func _zoom(event):
	var zf = clamp(cam.zoom.x * event.factor, MIN_ZOOM, MAX_ZOOM)
	var zd = zf - cam.zoom.x
	var from_camera_center_pos = event.position - get_camera_center_offset()
	cam.offset -= (from_camera_center_pos * zd).rotated(cam.rotation)
	cam.zoom = zf * Vector2.ONE

func camera2global(position):
	var camera_center = cam.global_position + cam.offset
	var from_camera_center_pos = position - get_camera_center_offset()
	return camera_center + (from_camera_center_pos * cam.zoom).rotated(cam.rotation)

func get_camera_center_offset():
	return get_viewport().get_visible_rect().size / 2
"

[node name="Node" type="Node2D"]
script = SubResource( 1 )

[node name="Sprite" type="Sprite" parent="."]
texture = ExtResource( 1 )

[node name="Camera" type="Camera2D" parent="."]
rotating = true
current = true

@jeremyz jeremyz requested a review from a team as a code owner March 12, 2021 12:26
Base automatically changed from 3.2 to 3.x March 16, 2021 11:11
@aaronfranke aaronfranke modified the milestones: 3.2, 3.3 Mar 16, 2021
@akien-mga akien-mga modified the milestones: 3.3, 3.4 Mar 26, 2021
@Calinou Calinou changed the title implement EventGesturePan,Pinch,Twist - OS agnostic implement EventGesturePan,Pinch,Twist - OS agnostic (3.x) Jul 16, 2021
@jeremyz jeremyz force-pushed the input-gesture branch 9 times, most recently from 099f122 to 268e65d Compare November 1, 2021 12:09
@Chaosus Chaosus modified the milestones: 3.4, 3.5 Nov 8, 2021
@akien-mga akien-mga changed the title implement EventGesturePan,Pinch,Twist - OS agnostic (3.x) [3.x] Implement OS agnostic InputEventGesture with Pan, Pinch, and Twist Nov 10, 2021
@akien-mga
Copy link
Member

For the record, I plan to focus on getting #39055 finalized and merged first, then we can update and merge this PR too for 3.5 ideally.

When we get to it though, we might need to change some things for 3.x specifically to avoid breaking compatibility (i.e. keeping the old names for the existing events and just update their implementation + add Twist).

@MuubNito
Copy link

For the record, I plan to focus on getting #39055 finalized and merged first, then we can update and merge this PR too for 3.5 ideally.

When we get to it though, we might need to change some things for 3.x specifically to avoid breaking compatibility (i.e. keeping the old names for the existing events and just update their implementation + add Twist).

Dude, im sorry but good luck getting this pr finished anytime...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants