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