142 lines
4.5 KiB
GDScript
142 lines
4.5 KiB
GDScript
# RefineUI (M4) —— 精炼 / 强化对话框:`refine_ask` 来时弹。
|
||
#
|
||
# var ru := preload("res://ui/refine_ui.gd").new()
|
||
# add_child(ru)
|
||
# ru.setup(m2client, canvas_parent, proto) # proto 可空(出名字)
|
||
#
|
||
# `GC_REFINE_INFORMATION` → `M2Client.refine_ask({type,pos,src_vnum,result_vnum,cost,prob,materials})`。
|
||
# [精炼] → `M2Client.refine(pos, type)`;[取消] 关闭。
|
||
extends Node
|
||
|
||
var client: Node
|
||
var proto: Node
|
||
var _root: Control
|
||
var _text: RichTextLabel
|
||
var _cur := {}
|
||
var _mobile_mode := false
|
||
var _action_row: HBoxContainer
|
||
var _refine_button: Button
|
||
var _cancel_button: Button
|
||
|
||
func setup(m2client: Node, parent: Node, proto_node: Node = null) -> void:
|
||
client = m2client
|
||
proto = proto_node
|
||
_build(parent)
|
||
if client.has_signal("refine_ask"):
|
||
client.refine_ask.connect(_on_ask)
|
||
if client.has_signal("refine_result"):
|
||
client.refine_result.connect(_on_result)
|
||
|
||
func is_open() -> bool:
|
||
return _root != null and _root.visible
|
||
|
||
func get_mobile_window() -> Control:
|
||
return _root
|
||
|
||
func set_mobile_mode(enabled: bool) -> void:
|
||
_mobile_mode = enabled
|
||
_apply_mobile_layout()
|
||
|
||
func close() -> void:
|
||
if _root:
|
||
_root.visible = false
|
||
_cur = {}
|
||
|
||
func _name_of(vnum: int) -> String:
|
||
if vnum == 0:
|
||
return "-"
|
||
if proto and proto.has_method("item"):
|
||
var d: Dictionary = proto.item(vnum)
|
||
var n := String(d.get("locale_name", d.get("name", "")))
|
||
if n != "":
|
||
return n
|
||
return "#%d" % vnum
|
||
|
||
func _on_ask(info: Dictionary) -> void:
|
||
_cur = info
|
||
var lines := PackedStringArray()
|
||
lines.append("[b]%s[/b] → [b]%s[/b]" % [
|
||
_name_of(int(info.get("src_vnum", 0))), _name_of(int(info.get("result_vnum", 0)))])
|
||
lines.append("成功率 %d%% 费用 %d 金" % [int(info.get("prob", 0)), int(info.get("cost", 0))])
|
||
var mats: Array = info.get("materials", [])
|
||
if not mats.is_empty():
|
||
lines.append("材料:")
|
||
for m in mats:
|
||
lines.append(" %s ×%d" % [_name_of(int(m.get("vnum", 0))), int(m.get("count", 1))])
|
||
_text.text = "\n".join(lines)
|
||
_root.visible = true
|
||
|
||
func _do_refine() -> void:
|
||
if client and client.has_method("refine") and not _cur.is_empty():
|
||
client.refine(int(_cur.get("pos", 0)), int(_cur.get("type", 0)))
|
||
_root.visible = false
|
||
|
||
func _on_result(ok: bool) -> void:
|
||
_cur = {}
|
||
_text.text = "[b]精炼成功[/b]" if ok else "[b]精炼失败[/b]"
|
||
_root.visible = true
|
||
|
||
func _apply_mobile_layout() -> void:
|
||
if _root == null or _text == null or _action_row == null:
|
||
return
|
||
if _mobile_mode:
|
||
_root.size = Vector2(460, 300)
|
||
_root.position = Vector2(-230, -150)
|
||
_text.position = Vector2(18, 52)
|
||
_text.size = Vector2(424, 178)
|
||
_text.custom_minimum_size = Vector2(424, 178)
|
||
_action_row.position = Vector2(18, 242)
|
||
_refine_button.custom_minimum_size = Vector2(204, 44)
|
||
_cancel_button.custom_minimum_size = Vector2(204, 44)
|
||
else:
|
||
_root.size = Vector2(320, 260)
|
||
_root.position = Vector2(-160, -130)
|
||
_text.position = Vector2(14, 34)
|
||
_text.size = Vector2(292, 160)
|
||
_text.custom_minimum_size = Vector2(292, 160)
|
||
_action_row.position = Vector2(14, 208)
|
||
_refine_button.custom_minimum_size = Vector2(120, 30)
|
||
_cancel_button.custom_minimum_size = Vector2(120, 30)
|
||
|
||
func _build(parent: Node) -> void:
|
||
_root = Control.new()
|
||
_root.set_anchors_preset(Control.PRESET_CENTER)
|
||
_root.position = Vector2(-160, -130)
|
||
_root.size = Vector2(320, 260)
|
||
_root.visible = false
|
||
_root.set_meta("is_titlebar", true)
|
||
parent.add_child(_root)
|
||
var panel := Panel.new()
|
||
panel.set_anchors_preset(Control.PRESET_FULL_RECT)
|
||
var sb := StyleBoxFlat.new()
|
||
sb.bg_color = Color(0.09, 0.07, 0.05, 0.98)
|
||
sb.set_corner_radius_all(4)
|
||
panel.add_theme_stylebox_override("panel", sb)
|
||
_root.add_child(panel)
|
||
var title := Label.new()
|
||
title.text = "精炼"
|
||
title.position = Vector2(12, 8)
|
||
_root.add_child(title)
|
||
_text = RichTextLabel.new()
|
||
_text.bbcode_enabled = true
|
||
_text.position = Vector2(14, 34)
|
||
_text.custom_minimum_size = Vector2(292, 160)
|
||
_text.size = Vector2(292, 160)
|
||
_root.add_child(_text)
|
||
_action_row = HBoxContainer.new()
|
||
var row := _action_row
|
||
row.position = Vector2(14, 208)
|
||
row.add_theme_constant_override("separation", 12)
|
||
_root.add_child(row)
|
||
_refine_button = Button.new()
|
||
_refine_button.text = "精炼"
|
||
_refine_button.custom_minimum_size = Vector2(120, 30)
|
||
_refine_button.pressed.connect(_do_refine)
|
||
row.add_child(_refine_button)
|
||
_cancel_button = Button.new()
|
||
_cancel_button.text = "取消"
|
||
_cancel_button.custom_minimum_size = Vector2(120, 30)
|
||
_cancel_button.pressed.connect(func(): _root.visible = false)
|
||
row.add_child(_cancel_button)
|
||
_apply_mobile_layout()
|