145 lines
4.5 KiB
GDScript
145 lines
4.5 KiB
GDScript
# MallUI (첫 버전) —— 道具商城仓库窗(창고몰 / item-mall storage)。
|
||
#
|
||
# var mu := preload("res://ui/mall_ui.gd").new()
|
||
# add_child(mu)
|
||
# mu.setup(m2client, canvas_parent, proto) # proto 可空
|
||
#
|
||
# `mall_opened` → 显示;`mall_changed` → 刷新。列出商城道具 + [取出](放进背包第一个空格)。
|
||
extends Node
|
||
|
||
var client: Node
|
||
var proto: Node
|
||
var _root: Control
|
||
var _list: VBoxContainer
|
||
var _title: Label
|
||
var _status: Label
|
||
var _password_dialog: ConfirmationDialog
|
||
|
||
func setup(m2client: Node, parent: Node, proto_node: Node = null) -> void:
|
||
client = m2client
|
||
proto = proto_node
|
||
_build(parent)
|
||
if client.has_signal("mall_opened"):
|
||
client.mall_opened.connect(func(_s): refresh())
|
||
if client.has_signal("mall_changed"):
|
||
client.mall_changed.connect(refresh)
|
||
if client.has_signal("mall_password_required"):
|
||
client.mall_password_required.connect(_ask_password)
|
||
|
||
func is_open() -> bool:
|
||
return _root != null and _root.visible
|
||
|
||
func _name_of(vnum: int) -> String:
|
||
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 _first_free_inv() -> int:
|
||
var used := {}
|
||
for it in client.get_inventory():
|
||
used[int(it.get("cell", -1))] = true
|
||
for i in range(90):
|
||
if not used.has(i):
|
||
return i
|
||
return -1
|
||
|
||
func refresh() -> void:
|
||
if client == null or _root == null:
|
||
return
|
||
_root.visible = client.is_mall_open()
|
||
if not _root.visible:
|
||
return
|
||
_status.text = ""
|
||
_title.text = "道具商城仓库(%d 格)" % client.get_mall_size()
|
||
for c in _list.get_children():
|
||
c.queue_free()
|
||
var items: Array = client.get_mall_items()
|
||
if items.is_empty():
|
||
var e := Label.new()
|
||
e.text = "(空)"
|
||
_list.add_child(e)
|
||
return
|
||
for it in items:
|
||
_list.add_child(_row(it))
|
||
|
||
func _ask_password() -> void:
|
||
if is_instance_valid(_password_dialog):
|
||
_password_dialog.queue_free()
|
||
_password_dialog = ConfirmationDialog.new()
|
||
_password_dialog.title = "商城密码"
|
||
_password_dialog.dialog_text = "请输入 1~6 位密码"
|
||
_password_dialog.ok_button_text = "确认"
|
||
_password_dialog.cancel_button_text = "取消"
|
||
var edit := LineEdit.new()
|
||
edit.secret = true
|
||
edit.max_length = 6
|
||
edit.placeholder_text = "密码"
|
||
edit.custom_minimum_size = Vector2(220, 28)
|
||
_password_dialog.add_child(edit)
|
||
_password_dialog.confirmed.connect(func() -> void:
|
||
if not client.mall_password(edit.text) and is_instance_valid(_status):
|
||
_status.text = "密码格式无效"
|
||
_password_dialog.queue_free()
|
||
_password_dialog = null)
|
||
_password_dialog.canceled.connect(func() -> void:
|
||
_password_dialog.queue_free()
|
||
_password_dialog = null)
|
||
_root.get_parent().add_child(_password_dialog)
|
||
_password_dialog.popup_centered()
|
||
|
||
func _row(it: Dictionary) -> Control:
|
||
var row := HBoxContainer.new()
|
||
row.custom_minimum_size = Vector2(300, 0)
|
||
var nm := Label.new()
|
||
nm.text = "%s ×%d" % [_name_of(int(it.get("vnum", 0))), int(it.get("count", 1))]
|
||
nm.custom_minimum_size = Vector2(220, 0)
|
||
nm.add_theme_font_size_override("font_size", 12)
|
||
row.add_child(nm)
|
||
var out := Button.new()
|
||
out.text = "取出"
|
||
var cell := int(it.get("cell", 0))
|
||
out.pressed.connect(func() -> void:
|
||
var target := _first_free_inv()
|
||
if target < 0:
|
||
_status.text = "背包已满"
|
||
return
|
||
if not client.mall_checkout(cell, 1, target):
|
||
_status.text = "取出请求发送失败")
|
||
row.add_child(out)
|
||
return row
|
||
|
||
func _build(parent: Node) -> void:
|
||
_root = Panel.new()
|
||
_root.set_anchors_preset(Control.PRESET_CENTER)
|
||
_root.position = Vector2(140, -180)
|
||
_root.custom_minimum_size = Vector2(340, 360)
|
||
_root.size = Vector2(340, 360)
|
||
_root.visible = false
|
||
parent.add_child(_root)
|
||
var box := VBoxContainer.new()
|
||
box.position = Vector2(16, 14)
|
||
box.custom_minimum_size = Vector2(308, 0)
|
||
box.add_theme_constant_override("separation", 6)
|
||
_root.add_child(box)
|
||
_title = Label.new()
|
||
_title.text = "道具商城仓库"
|
||
_title.add_theme_font_size_override("font_size", 16)
|
||
box.add_child(_title)
|
||
_status = Label.new()
|
||
_status.add_theme_font_size_override("font_size", 11)
|
||
_status.add_theme_color_override("font_color", Color(1, 0.65, 0.55))
|
||
box.add_child(_status)
|
||
var sc := ScrollContainer.new()
|
||
sc.custom_minimum_size = Vector2(308, 280)
|
||
box.add_child(sc)
|
||
_list = VBoxContainer.new()
|
||
_list.add_theme_constant_override("separation", 4)
|
||
sc.add_child(_list)
|
||
var close := Button.new()
|
||
close.text = "关闭"
|
||
close.pressed.connect(func() -> void: _root.visible = false)
|
||
box.add_child(close)
|