62 lines
1.4 KiB
GDScript
62 lines
1.4 KiB
GDScript
@tool
|
|
extends PanelContainer
|
|
|
|
@onready var spawn_slot_root: VBoxContainer = %SpawnSlotRoot
|
|
@onready var add_button: Button = %AddButton
|
|
@onready var remove_button: Button = %RemoveButton
|
|
|
|
var _inited: bool = false
|
|
var _max_length: int = -1
|
|
|
|
## 默认的Slot
|
|
const SLOT: PackedScene = preload("res://addons/reedscene/act/StateDrapSlot.tscn")
|
|
|
|
func _ready() -> void:
|
|
if not Engine.is_editor_hint():
|
|
return
|
|
|
|
# 连接按钮
|
|
add_button.pressed.connect(_on_add_pressed)
|
|
remove_button.pressed.connect(_on_remove_pressed)
|
|
|
|
# 初始保证至少有一个 Slot
|
|
if spawn_slot_root.get_child_count() == 0:
|
|
_add_slot()
|
|
|
|
_update_remove_button_state()
|
|
|
|
func init(info:Dictionary) -> void:
|
|
_inited = true
|
|
if info.has("size"):
|
|
self._max_length = info.get("size") as int
|
|
|
|
func _on_add_pressed() -> void:
|
|
_add_slot()
|
|
|
|
func _on_remove_pressed() -> void:
|
|
_remove_slot()
|
|
|
|
func _add_slot() -> void:
|
|
if spawn_slot_root.get_child_count() >= _max_length:
|
|
return
|
|
|
|
var slot := SLOT.instantiate()
|
|
spawn_slot_root.add_child(slot)
|
|
slot.owner = get_tree().edited_scene_root # 编辑器可保存
|
|
slot.init(spawn_slot_root.get_children().size())
|
|
|
|
_update_remove_button_state()
|
|
|
|
func _remove_slot() -> void:
|
|
var count := spawn_slot_root.get_child_count()
|
|
if count <= 1:
|
|
return
|
|
|
|
var last := spawn_slot_root.get_child(count - 1)
|
|
last.queue_free()
|
|
|
|
_update_remove_button_state()
|
|
|
|
func _update_remove_button_state() -> void:
|
|
remove_button.disabled = spawn_slot_root.get_child_count() <= 1
|