@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 var _cached_single_act: SingleAct = null var _cached_prop_id: int = -1 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) 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 # 创建 / 更新 cached _cached_single_act = SingleAct.new() _cached_single_act.state_id = state.state_id _cached_single_act.use_trans = false _cached_single_act.context = {} _cached_prop_id = int(info.prop_id) # 启用 CheckBox cb_use_trans.disabled = false cb_use_trans.button_pressed = _cached_single_act.use_trans # 直接用 info + cached 刷 UI _set_context(info) func _on_use_trans_toggled(pressed: bool) -> void: if _cached_single_act == null: cb_use_trans.button_pressed = false return # ✅ 只改 cached _cached_single_act.use_trans = pressed # 重新 resolve 一次 info(简单、直观) var state := _resolve_state_from_cached() if state == null: return var info := StateResolveUtils.resolve_state_display_info(state) _set_context(info) 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 _reset_cb_use_trans() ## 初始化use trans 的 check box func _reset_cb_use_trans() ->void: cb_use_trans.disabled = true 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) ## 初始化启用use trans func _enable_cb_use_trans() -> void: cb_use_trans.disabled = false cb_use_trans.button_pressed = _cached_single_act.use_trans ## 创建一个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 ## 设置文本 func _set_context(info) ->void: if _cached_single_act == null: return 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