godot-plateformer/addons/reedscene/act/StateDropSlot.gd

177 lines
4.3 KiB
GDScript3
Raw Normal View History

2026-01-03 23:21:04 +08:00
@tool
class_name StateDrapSlot
extends Control
@onready var drop_area: StateDropArea = %DropArea
@onready var state_label: Label = %StateLabel
@onready var context: RichTextLabel = %Context
@onready var cb_use_trans: CheckBox = %CB_UseTrans
2026-01-04 21:21:01 +08:00
@onready var index: Label = %Index
2026-01-03 23:21:04 +08:00
var _cached_single_act: SingleAct = null
var _cached_prop_id: int = -1
2026-01-04 21:21:01 +08:00
var _cached_info: Dictionary
2026-01-03 23:21:04 +08:00
const NORMAL_STATE_LABLE: String = "Drag state node to icon."
const NORMAL_CONTEXT: String = "Information will be show here."
const COLOR_IDLE: Color = Color.GRAY
const COLOR_CHECKED_1: Color = Color.GREEN
const COLOR_CHECKED_2: Color = Color.YELLOW
const COLOR_CHECKED_3: Color = Color.ORANGE_RED
func _ready() -> void:
_reset_to_default()
_reset_cb_use_trans()
if drop_area:
drop_area.node_path_dropped.connect(_on_node_path_dropped)
2026-01-04 21:21:01 +08:00
func init(index: int) -> void:
self.index.text = str(index)
2026-01-03 23:21:04 +08:00
func _on_node_path_dropped(path: NodePath) -> void:
var state := StateResolveUtils.get_state_node_from_path(path)
if state == null:
_clear_cached_act()
_reset_to_default()
return
var info := StateResolveUtils.resolve_state_display_info(state)
if info == null:
_clear_cached_act()
_reset_to_default()
return
2026-01-04 21:21:01 +08:00
##缓存一遍有效的info
_cached_info = info
2026-01-03 23:21:04 +08:00
# 创建 / 更新 cached
_cached_single_act = SingleAct.new()
_cached_single_act.state_id = state.state_id
2026-01-04 21:21:01 +08:00
_cached_single_act.use_trans = cb_use_trans.button_pressed
2026-01-03 23:21:04 +08:00
_cached_single_act.context = {}
_cached_prop_id = int(info.prop_id)
2026-01-04 21:21:01 +08:00
## 启用 CheckBox
#cb_use_trans.disabled = false
#cb_use_trans.button_pressed = _cached_single_act.use_trans
2026-01-03 23:21:04 +08:00
# 直接用 info + cached 刷 UI
2026-01-04 21:21:01 +08:00
_update_context(info)
2026-01-03 23:21:04 +08:00
func _on_use_trans_toggled(pressed: bool) -> void:
2026-01-04 21:21:01 +08:00
# 如果存在就改,不存在無事發生
if _cached_single_act:
_cached_single_act.use_trans = pressed
_update_context()
2026-01-03 23:21:04 +08:00
func _resolve_state_from_path(path: NodePath) -> ReedPropState:
var root := EditorInterface.get_edited_scene_root()
if root == null:
return null
var node := root.get_node_or_null(path)
if node is ReedPropState:
return node
return null
## 重置回默认
func _reset_to_default() ->void:
state_label.text = NORMAL_STATE_LABLE
state_label.modulate = COLOR_IDLE
context.text = NORMAL_CONTEXT
context.modulate = COLOR_IDLE
2026-01-04 21:21:01 +08:00
2026-01-03 23:21:04 +08:00
## 初始化use trans 的 check box
func _reset_cb_use_trans() ->void:
cb_use_trans.button_pressed = false
if not cb_use_trans.toggled.is_connected(_on_use_trans_toggled):
cb_use_trans.toggled.connect(_on_use_trans_toggled)
## 创建一个CachedAct
func _build_cached_act(state: ReedPropState, info: Dictionary) -> void:
if _cached_single_act == null:
_cached_single_act = SingleAct.new()
_cached_single_act.state_id = state.state_id
_cached_single_act.use_trans = false # 默认值,可后续切换
_cached_single_act.context = {} # 预留
# Prop ID 缓存
_cached_prop_id = int(info.prop_id)
## 清空Cached Act
func _clear_cached_act() -> void:
_cached_single_act = null
_cached_prop_id = -1
## 设置文本
2026-01-04 21:21:01 +08:00
func _update_context(info:Dictionary = {}) ->void:
## 如果没有传入有效的info且没有cached过act则直接返回
## TODO:这里后续应该有一个报错的Message
if info == {} :
if _cached_info == {}:
return
else:
info = _cached_info
2026-01-03 23:21:04 +08:00
context.clear()
# —— 配置颜色变量 —— #
var prop_color_str := COLOR_CHECKED_1.to_html(false)
var state_color_str := COLOR_CHECKED_1.to_html(false)
var id_color_str := COLOR_CHECKED_2.to_html(false)
var use_trans := _cached_single_act.use_trans
var use_mark := "\u2714" if use_trans else "\u2716"
var use_mark_color_str := (
COLOR_CHECKED_1 if use_trans else COLOR_CHECKED_3
).to_html(false)
var bbcode := ""
bbcode += "[table=1][cell]"
# Prop
bbcode += "[b][color=%s]%s[/color][/b] [color=%s](ID_%s)[/color]" % [
prop_color_str,
info.prop_name,
id_color_str,
info.prop_id
]
# 空白占位
bbcode += "[color=#00000000] [/color]"
# State
bbcode += "[b][color=%s]%s[/color][/b] [color=%s](ID_%s)[/color]" % [
state_color_str,
info.state_name,
id_color_str,
info.state_id
]
# 再空白
bbcode += "[color=#00000000] [/color]"
# Use Trans 显示
bbcode += "[color=%s]%s[/color] Use Trans" % [
use_mark_color_str,
use_mark
]
bbcode += "[/cell][/table]"
context.text = bbcode