finish zoom/scroll

This commit is contained in:
James 2023-12-04 18:04:59 +00:00
parent 4faeccff10
commit 033a1c21da

View File

@ -1,37 +1,30 @@
extends Camera2D extends Camera2D
var zoom_speed=Vector2(.2,.2) var last_mouse_position = Vector2.ZERO
var scrolling=false;
# Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
pass # Replace with function body. set_process_input(true)
func _input(event): func _input(event):
if event is InputEventMouseButton: if event is InputEventMouseButton:
if event.is_pressed(): if event.button_index == MOUSE_BUTTON_MIDDLE and event.pressed:
if event.button_index==MOUSE_BUTTON_WHEEL_UP: last_mouse_position = get_global_mouse_position()
zoom+=zoom_speed elif event.button_index == MOUSE_BUTTON_MIDDLE and not event.pressed:
if event.button_index==MOUSE_BUTTON_WHEEL_DOWN: last_mouse_position = Vector2.ZERO
zoom-=zoom_speed if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and event.pressed:
if event.button_index==MOUSE_BUTTON_MIDDLE: zoom_towards_mouse(Vector2(0.9, 0.9))
scrolling=true elif event.button_index == MOUSE_BUTTON_WHEEL_UP and event.pressed:
if event.is_released(): zoom_towards_mouse(Vector2(1.1, 1.1))
if event.button_index==MOUSE_BUTTON_MIDDLE:
scrolling=false elif event is InputEventMouseMotion:
if event is InputEventMouseMotion: if last_mouse_position != Vector2.ZERO:
if scrolling: var mouse_motion = event.relative
offset-=event.velocity*0.1 var drag_offset = mouse_motion / zoom
position -= drag_offset
# Called every frame. 'delta' is the elapsed time since the previous frame. last_mouse_position = get_global_mouse_position()
func _process(delta):
if Input.is_action_pressed('move_up'): func zoom_towards_mouse(zoom_factor):
offset.y -= 1 var previous_mouse_position := get_local_mouse_position()
if Input.is_action_pressed('move_down'): zoom *= zoom_factor
offset.y += 1 var diff = previous_mouse_position - get_local_mouse_position()
if Input.is_action_pressed('move_left'): offset += diff
offset.x -= 1
if Input.is_action_pressed('move_right'):
offset.x += 1
# if Input.is_action_just_pressed('take'):
# drop()