166 lines
3.8 KiB
GDScript
166 lines
3.8 KiB
GDScript
##TODO:清楚掉這裏和PhantomCamera相關的部分
|
||
@tool
|
||
@icon("res://addons/reedcamera/icon/camera_anchor_icon.svg")
|
||
class_name CameraAnchor extends Node2D
|
||
|
||
const _CONSTANTS = preload("res://addons/reedcamera/_data/CameraSystemConst.gd")
|
||
|
||
@export_group("Anchor Config")
|
||
##该priority不会直接修改Anchor的priority,只用作初始化
|
||
@export var priority: int = 0
|
||
##此Anchor是否有效
|
||
@export var enabled: bool = true
|
||
## =========================
|
||
## Blend Config
|
||
## =========================
|
||
@export_subgroup("Blending")
|
||
##是否要存在相機過渡時間
|
||
@export var use_blend: bool = true
|
||
##過度時間
|
||
@export var blend_time: float = 0.3
|
||
|
||
@export_group("Camera Config")
|
||
@export_subgroup("Sizing")
|
||
@export var zoom: Vector2 = Vector2.ONE:
|
||
set(value):
|
||
zoom = value
|
||
if Engine.is_editor_hint():
|
||
queue_redraw()
|
||
@export_subgroup("Limit")
|
||
@export var use_camera_limit: bool = false:
|
||
set(value):
|
||
use_camera_limit = value
|
||
if Engine.is_editor_hint():
|
||
queue_redraw()
|
||
|
||
@export var limit_top: int = -10000000:
|
||
set(value):
|
||
limit_top = value
|
||
if Engine.is_editor_hint():
|
||
queue_redraw()
|
||
@export var limit_bottom: int = 10000000:
|
||
set(value):
|
||
limit_bottom = value
|
||
if Engine.is_editor_hint():
|
||
queue_redraw()
|
||
@export var limit_left: int = -10000000:
|
||
set(value):
|
||
limit_left = value
|
||
if Engine.is_editor_hint():
|
||
queue_redraw()
|
||
@export var limit_right: int = 10000000:
|
||
set(value):
|
||
limit_right = value
|
||
if Engine.is_editor_hint():
|
||
queue_redraw()
|
||
|
||
## 编辑器预览面板设置
|
||
@export_group("Editor Preview")
|
||
@export var show_camera_preview: bool = true
|
||
@export var preview_color: Color = Color(0.2, 0.9, 0.4, 0.8)
|
||
@export var preview_line_width: float = 2.0
|
||
@export var show_limit_preview: bool = true
|
||
@export var limit_preview_color: Color = Color(0.9, 0.3, 0.3, 0.8)
|
||
@export var limit_preview_line_width: float = 2.0
|
||
|
||
|
||
#@export_subgroup("Follow")
|
||
#@export var follow_player: bool = false
|
||
|
||
|
||
|
||
var _camera_global : Node = null
|
||
|
||
|
||
|
||
var _priority: int :
|
||
set(value):
|
||
if _priority != value:
|
||
_priority = value
|
||
on_priority_change.emit(_priority, self)
|
||
|
||
signal on_priority_change(_priority:int, anchor: CameraAnchor)
|
||
|
||
func _ready() -> void:
|
||
if not Engine.is_editor_hint():
|
||
_runtime_ready()
|
||
|
||
func _enter_tree() -> void:
|
||
if not Engine.is_editor_hint():
|
||
pass
|
||
#CameraSystem.register_anchor(self)
|
||
|
||
func _exit_tree() -> void:
|
||
if not Engine.is_editor_hint():
|
||
pass
|
||
#CameraSystem.unregister_anchor(self)
|
||
|
||
func _draw() -> void:
|
||
if not Engine.is_editor_hint():
|
||
return
|
||
|
||
if show_camera_preview:
|
||
draw_rect(
|
||
_camera_frame_rect_draw(),
|
||
preview_color,
|
||
false,
|
||
preview_line_width
|
||
)
|
||
|
||
if show_limit_preview and use_camera_limit:
|
||
draw_rect(
|
||
_camera_limit_rect(),
|
||
limit_preview_color,
|
||
false,
|
||
limit_preview_line_width
|
||
)
|
||
|
||
func _camera_limit_rect() -> Rect2:
|
||
var pos := Vector2(limit_left, limit_top)
|
||
var size := Vector2(
|
||
limit_right - limit_left,
|
||
limit_bottom - limit_top
|
||
)
|
||
|
||
return Rect2(pos, size)
|
||
|
||
func _camera_frame_rect_draw() -> Rect2:
|
||
var cg = _get_camera_global()
|
||
if cg:
|
||
var screen_size: Vector2 = cg.get_screen_size()
|
||
|
||
var z := zoom
|
||
z.x = maxf(z.x, 0.001)
|
||
z.y = maxf(z.y, 0.001)
|
||
|
||
var size := Vector2(
|
||
screen_size.x / z.x,
|
||
screen_size.y / z.y
|
||
)
|
||
|
||
# PhantomCamera:以自身为中心
|
||
return Rect2(-size * 0.5, size)
|
||
|
||
print("沒有找到全局相機")
|
||
return Rect2(Vector2.ZERO,Vector2.ZERO)
|
||
|
||
func _runtime_ready() -> void:
|
||
_priority = priority
|
||
|
||
##内部懶加載全局相機管理器
|
||
func _get_camera_global() -> Object:
|
||
if _camera_global:
|
||
return _camera_global
|
||
|
||
if Engine.has_singleton(_CONSTANTS.CAMERA_SYSTEM_NAME):
|
||
_camera_global = Engine.get_singleton(_CONSTANTS.CAMERA_SYSTEM_NAME)
|
||
|
||
return _camera_global
|
||
|
||
func push_camera() -> void:
|
||
#CameraSystem.reset_all_camera_priority()
|
||
_priority = 1000
|
||
|
||
func pop_camera() -> void:
|
||
_priority = 0
|