From 60e08f19991a325bb85062c20f80649b3be00369 Mon Sep 17 00:00:00 2001 From: Federico Ciuffardi Date: Tue, 18 May 2021 17:44:21 -0300 Subject: [PATCH] Fix limits for zoom and move --- Demo.tscn | 5 +++++ GCC2D.gd | 42 ++++++++++++++++++++++++++++++++++++++---- project.godot | 12 ++++++------ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Demo.tscn b/Demo.tscn index d868bfd..a26bd89 100644 --- a/Demo.tscn +++ b/Demo.tscn @@ -9,3 +9,8 @@ texture = ExtResource( 1 ) [node name="GCC2D" parent="." instance=ExtResource( 2 )] +anchor_mode = 0 +limit_left = -1920 +limit_top = -1080 +limit_right = 1920 +limit_bottom = 1080 diff --git a/GCC2D.gd b/GCC2D.gd index 7cdbe70..962ee54 100644 --- a/GCC2D.gd +++ b/GCC2D.gd @@ -8,6 +8,39 @@ export(int,"disabled","pinch") var zoom_gesture = 1 export(int,"disabled","twist") var rotation_gesture = 1 export(int,"disabled","single_drag","multi_drag") var movement_gesture = 2 +func set_position(p): + var position_limit_right + var position_limit_left + var position_limit_top + var position_limit_bottom + var camera_size = get_camera_size()*zoom + if anchor_mode == ANCHOR_MODE_FIXED_TOP_LEFT: + position_limit_right = limit_right - camera_size.x + position_limit_left = limit_left + position_limit_top = limit_top + position_limit_bottom = limit_bottom - camera_size.y + elif anchor_mode == ANCHOR_MODE_DRAG_CENTER: + position_limit_right = limit_right - camera_size.x/2 + position_limit_left = limit_left + camera_size.x/2 + position_limit_top = limit_top + camera_size.y/2 + position_limit_bottom = limit_bottom - camera_size.y/2 + if(position_limit_right < position_limit_left or position_limit_bottom < position_limit_top): + return false + position = p + if(position.x > position_limit_right): + position.x = position_limit_right + if(position.x < position_limit_left): + position.x = position_limit_left + if(position.y < position_limit_top): + position.y = position_limit_top + if(position.y > position_limit_bottom): + position.y = position_limit_bottom + print(position) + print("horizontal limits: ", position_limit_left," , ", position_limit_right ) + print("vertical limits: ", position_limit_top," , ", position_limit_bottom ) + return true + + func _unhandled_input(e): if (e is InputEventMultiScreenDrag and movement_gesture == 2 or e is InputEventSingleScreenDrag and movement_gesture == 1): @@ -24,8 +57,8 @@ func camera2global(position): return camera_center + (from_camera_center_pos*zoom).rotated(rotation) func _move(event): - position -= (event.relative*zoom).rotated(rotation) - + set_position(position - (event.relative*zoom).rotated(rotation)) + func _zoom(event): var li = event.distance var lf = event.distance + event.relative @@ -43,13 +76,14 @@ func _zoom(event): zd = zf - zi var from_camera_center_pos = event.position - get_camera_center_offset() - position -= (from_camera_center_pos*zd).rotated(rotation) zoom = zf*Vector2.ONE + if(!set_position(position - (from_camera_center_pos*zd).rotated(rotation))): + zoom = zi*Vector2.ONE func _rotate(event): var fccp = (event.position - get_camera_center_offset()) # from_camera_center_pos = fccp var fccp_op_rot = -fccp.rotated(event.relative) - position -= ((fccp_op_rot + fccp)*zoom).rotated(rotation-event.relative) + set_position(position - ((fccp_op_rot + fccp)*zoom).rotated(rotation-event.relative)) rotation -= event.relative func get_camera_center_offset(): diff --git a/project.godot b/project.godot index e15486c..5540feb 100644 --- a/project.godot +++ b/project.godot @@ -9,32 +9,32 @@ config_version=4 _global_script_classes=[ { -"base": "Reference", +"base": "InputEventAction", "class": "InputEventMultiScreenDrag", "language": "GDScript", "path": "res://GDTIM-v1_1_0/CustomInputEvents/InputEventMultiScreenDrag.gd" }, { -"base": "Reference", +"base": "InputEventAction", "class": "InputEventScreenPinch", "language": "GDScript", "path": "res://GDTIM-v1_1_0/CustomInputEvents/InputEventScreenPinch.gd" }, { -"base": "Reference", +"base": "InputEventAction", "class": "InputEventScreenTwist", "language": "GDScript", "path": "res://GDTIM-v1_1_0/CustomInputEvents/InputEventScreenTwist.gd" }, { -"base": "Reference", +"base": "InputEventAction", "class": "InputEventSingleScreenDrag", "language": "GDScript", "path": "res://GDTIM-v1_1_0/CustomInputEvents/InputEventSingleScreenDrag.gd" }, { -"base": "Reference", +"base": "InputEventAction", "class": "InputEventSingleScreenTap", "language": "GDScript", "path": "res://GDTIM-v1_1_0/CustomInputEvents/InputEventSingleScreenTap.gd" }, { -"base": "Reference", +"base": "InputEventAction", "class": "InputEventSingleScreenTouch", "language": "GDScript", "path": "res://GDTIM-v1_1_0/CustomInputEvents/InputEventSingleScreenTouch.gd"