Files
mtgodot-poc/project/ui/view_equipment_ui.gd

111 lines
3.7 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ViewEquipmentUI — GC_VIEW_EQUIP 的装备查看窗。
# 服务器推送某个角色的 11 个传统可见装备位后自动刷新并置顶。
extends Node
const WEAR_NAMES := ["身体", "头部", "鞋子", "护腕", "武器", "项链", "耳环", "盾牌", "饰品 1", "饰品 2", "箭矢"]
const ItemTooltip = preload("res://ui/item_tooltip.gd")
var client: Node
var ui: CanvasLayer
var proto: Node
var item_list: RefCounted
var _tooltip_builder: RefCounted
var _window: Control
func setup(m2client: Node, manager: CanvasLayer, proto_node: Node = null, il: RefCounted = null) -> void:
client = m2client
ui = manager
proto = proto_node
item_list = il
_tooltip_builder = ItemTooltip.new()
_tooltip_builder.setup(AssetRoot.path() if ClassDB.class_exists("AssetRoot") else "", "en", proto)
if client.has_signal("view_equipment"):
client.view_equipment.connect(show_for)
func show_for(vid: int) -> void:
if client == null or not client.has_method("get_view_equipment"):
return
var equipment: Array = client.get_view_equipment(vid)
if equipment.is_empty():
return
if is_instance_valid(_window):
ui.close(_window)
_window = _build_window(vid, equipment)
ui.open(_window)
func _build_window(vid: int, equipment: Array) -> Control:
var win := Panel.new()
win.name = "ViewEquipment"
win.position = Vector2(1280, 180)
win.size = Vector2(280, 390)
var bg := StyleBoxFlat.new()
bg.bg_color = Color(0.05, 0.06, 0.09, 0.94)
bg.border_color = Color(0.65, 0.5, 0.2, 0.9)
bg.set_border_width_all(1)
bg.set_corner_radius_all(5)
win.add_theme_stylebox_override("panel", bg)
var title := Label.new()
title.position = Vector2(12, 8)
title.size = Vector2(220, 26)
title.text = "%s 的装备" % _character_name(vid)
title.add_theme_font_size_override("font_size", 16)
win.add_child(title)
var close := Button.new()
close.text = "×"
close.position = Vector2(238, 4)
close.size = Vector2(34, 30)
close.pressed.connect(func():
if is_instance_valid(_window):
ui.close(_window)
_window = null)
win.add_child(close)
var list := VBoxContainer.new()
list.position = Vector2(12, 42)
list.size = Vector2(256, 336)
list.add_theme_constant_override("separation", 3)
win.add_child(list)
for d in equipment:
var wear := int(d.get("wear", 0))
var line := Label.new()
line.text = "%s: %s" % [_wear_name(wear), _item_name(int(d.get("vnum", 0)), int(d.get("count", 0)))]
line.tooltip_text = _item_tooltip(d)
line.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
list.add_child(line)
return win
func _character_name(vid: int) -> String:
if client.has_method("get_entity"):
var entity: Dictionary = client.get_entity(vid)
if not entity.is_empty() and not String(entity.get("name", "")).is_empty():
return String(entity["name"])
return "角色 #%d" % vid
func _wear_name(wear: int) -> String:
return WEAR_NAMES[wear] if wear >= 0 and wear < WEAR_NAMES.size() else "装备 %d" % wear
func _item_name(vnum: int, count: int) -> String:
if vnum == 0:
return "(空)"
var name := "物品 #%d" % vnum
if proto:
var pd: Dictionary = proto.item(vnum)
if not pd.is_empty():
name = String(pd.get("locale_name", pd.get("name", name)))
elif item_list and item_list.has(vnum):
name = item_list.type_of(vnum)
return "%s x%d" % [name, count] if count > 1 else name
func _item_tooltip(d: Dictionary) -> String:
var vnum := int(d.get("vnum", 0))
if _tooltip_builder and proto and proto.has_method("item"):
return _tooltip_builder.format(vnum, int(d.get("count", 1)), proto.item(vnum), d)
var attrs: Array = d.get("attrs", [])
if attrs.is_empty():
return ""
var lines := []
for attr in attrs:
lines.append("属性 %d: %+d" % [int(attr.get("type", 0)), int(attr.get("value", 0))])
return "\n".join(lines)