Add camera zoom
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
## Scrolls the camera when the mouse cursor reaches the screen edge.
|
||||
## Attach to a Camera2D node. Requires an OrthographicCamera2D or ViewportContainer
|
||||
## to determine screen bounds at runtime.
|
||||
@tool
|
||||
extends Camera2D
|
||||
|
||||
## Speed at which the camera scrolls (pixels per second).
|
||||
@@ -18,6 +19,15 @@ extends Camera2D
|
||||
## Maximum bounds when clamping (in world space).
|
||||
@export var clamp_max: Vector2 = Vector2.ZERO
|
||||
|
||||
## Zoom sensitivity per mouse wheel tick.
|
||||
@export var zoom_sensitivity: float = 0.1
|
||||
|
||||
## Minimum zoom level.
|
||||
@export var zoom_min: float = 0.25
|
||||
|
||||
## Maximum zoom level.
|
||||
@export var zoom_max: float = 3.0
|
||||
|
||||
var _screen_rect: Rect2
|
||||
|
||||
|
||||
@@ -29,6 +39,10 @@ func _ready() -> void:
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if Engine.is_editor_hint():
|
||||
queue_redraw()
|
||||
return
|
||||
|
||||
if is_zero_approx(scroll_speed):
|
||||
return
|
||||
|
||||
@@ -58,11 +72,46 @@ func _get_scroll_direction(mouse_pos: Vector2) -> Vector2:
|
||||
return direction.normalized()
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
if not Engine.is_editor_hint():
|
||||
return
|
||||
if not clamp_enabled:
|
||||
return
|
||||
|
||||
var rect_size: Vector2 = clamp_max - clamp_min
|
||||
draw_rect(Rect2(clamp_min, rect_size), Color(1, 0.4, 0, 0.35))
|
||||
draw_rect(Rect2(clamp_min, rect_size), Color(1, 0.6, 0.2, 0.8), false, 2.0)
|
||||
|
||||
|
||||
func _clamp_position() -> void:
|
||||
position.x = clampf(position.x, clamp_min.x, clamp_max.x)
|
||||
position.y = clampf(position.y, clamp_min.y, clamp_max.y)
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if Engine.is_editor_hint():
|
||||
return
|
||||
|
||||
if event is InputEventMouseButton:
|
||||
var mouse_event: InputEventMouseButton = event
|
||||
if mouse_event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
||||
_zoom(zoom_sensitivity, mouse_event.position)
|
||||
elif mouse_event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
||||
_zoom(-zoom_sensitivity, mouse_event.position)
|
||||
|
||||
|
||||
func _zoom(change: float, mouse_pos: Vector2) -> void:
|
||||
var new_zoom_value: float = clampf(zoom.x + change, zoom_min, zoom_max)
|
||||
if is_equal_approx(new_zoom_value, zoom.x):
|
||||
return
|
||||
|
||||
var viewport_center: Vector2 = get_viewport_rect().size / 2.0
|
||||
var offset: Vector2 = mouse_pos - viewport_center
|
||||
var world_point: Vector2 = position + offset / zoom
|
||||
zoom = Vector2(new_zoom_value, new_zoom_value)
|
||||
position = world_point - offset / zoom
|
||||
|
||||
|
||||
func _update_screen_rect() -> void:
|
||||
var viewport: Viewport = get_viewport()
|
||||
if viewport != null:
|
||||
|
||||
Reference in New Issue
Block a user