完善客户端功能并同步差距文档
This commit is contained in:
+123
-1
@@ -9,18 +9,31 @@
|
||||
# 取出/存入用第一个空位(简版:deposit 用 safe_pos = 下一个空格;checkout 用道具 cell)。
|
||||
extends Node
|
||||
|
||||
const ItemTooltip = preload("res://ui/item_tooltip.gd")
|
||||
|
||||
var client: Node
|
||||
var proto: Node
|
||||
var _tooltip_builder: RefCounted
|
||||
var _root: Control
|
||||
var _list: VBoxContainer
|
||||
var _gold: Label
|
||||
var _title: Label
|
||||
var _status: Label
|
||||
var _password_dialog: ConfirmationDialog
|
||||
var _grid: GridContainer
|
||||
var _cells: Dictionary = {}
|
||||
var _page_label: Label
|
||||
var _page := 0
|
||||
var item_mouse: Node
|
||||
|
||||
const SAFE_PAGE_SLOTS := 45
|
||||
const SAFE_WINDOW := 3
|
||||
|
||||
func setup(m2client: Node, parent: Node, proto_node: Node = null) -> void:
|
||||
client = m2client
|
||||
proto = proto_node
|
||||
_tooltip_builder = ItemTooltip.new()
|
||||
_tooltip_builder.setup(AssetRoot.path(), "en", proto)
|
||||
_build(parent)
|
||||
if client.has_signal("safebox_changed"):
|
||||
client.safebox_changed.connect(refresh)
|
||||
@@ -35,8 +48,13 @@ func is_open() -> bool:
|
||||
return _root != null and _root.visible
|
||||
|
||||
func close() -> void:
|
||||
if item_mouse and item_mouse.has_method("unregister_owner"):
|
||||
item_mouse.unregister_owner(self)
|
||||
if item_mouse.has_method("is_attached") and item_mouse.is_attached():
|
||||
item_mouse.cancel()
|
||||
if _root:
|
||||
_root.visible = false
|
||||
_page = 0
|
||||
if is_instance_valid(_password_dialog):
|
||||
_password_dialog.queue_free()
|
||||
_password_dialog = null
|
||||
@@ -69,6 +87,13 @@ func _name_of(vnum: int) -> String:
|
||||
return n
|
||||
return "#%d" % vnum
|
||||
|
||||
func _tooltip_for(item: Dictionary) -> String:
|
||||
var vnum := int(item.get("vnum", 0))
|
||||
if _tooltip_builder:
|
||||
var pd: Dictionary = proto.item(vnum) if proto and proto.has_method("item") else {}
|
||||
return _tooltip_builder.format(vnum, int(item.get("count", 1)), pd, item)
|
||||
return "%s\n#%d ×%d" % [_name_of(vnum), vnum, int(item.get("count", 1))]
|
||||
|
||||
func refresh() -> void:
|
||||
if client == null:
|
||||
return
|
||||
@@ -76,10 +101,14 @@ func refresh() -> void:
|
||||
if not _root.visible:
|
||||
return
|
||||
_status.text = ""
|
||||
_title.text = "仓库(%d 页)" % client.get_safebox_size()
|
||||
var pages := maxi(1, int(client.get_safebox_size()))
|
||||
_page = clampi(_page, 0, pages - 1)
|
||||
_title.text = "仓库(第 %d/%d 页)" % [_page + 1, pages]
|
||||
_page_label.text = "%d / %d" % [_page + 1, pages]
|
||||
_gold.text = "仓库金币: %d" % client.get_safebox_gold()
|
||||
for c in _list.get_children():
|
||||
c.queue_free()
|
||||
_render_grid()
|
||||
var items: Array = client.get_safebox_items()
|
||||
if items.is_empty():
|
||||
var e := Label.new()
|
||||
@@ -89,6 +118,64 @@ func refresh() -> void:
|
||||
for it in items:
|
||||
_list.add_child(_row(it))
|
||||
|
||||
func _render_grid() -> void:
|
||||
if _grid == null:
|
||||
return
|
||||
for pos in _cells:
|
||||
var cell: Button = _cells[pos]
|
||||
cell.text = ""
|
||||
cell.tooltip_text = ""
|
||||
cell.set_meta("vnum", 0)
|
||||
cell.set_meta("count", 0)
|
||||
var items: Array = client.get_safebox_items() if client and client.has_method("get_safebox_items") else []
|
||||
for it in items:
|
||||
var absolute_pos := int(it.get("cell", -1))
|
||||
var pos := absolute_pos - _page * SAFE_PAGE_SLOTS
|
||||
if not _cells.has(pos):
|
||||
continue
|
||||
var cell: Button = _cells[pos]
|
||||
var vnum := int(it.get("vnum", 0))
|
||||
var count := int(it.get("count", 1))
|
||||
cell.text = "%s%s" % [_name_of(vnum).substr(0, 7), (" ×%d" % count) if count > 1 else ""]
|
||||
cell.tooltip_text = _tooltip_for(it)
|
||||
cell.set_meta("vnum", vnum)
|
||||
cell.set_meta("count", count)
|
||||
|
||||
func _drop_to_slot(payload: Dictionary, local_pos: int) -> bool:
|
||||
if payload.is_empty() or not _cells.has(local_pos):
|
||||
return false
|
||||
var safe_pos := _page * SAFE_PAGE_SLOTS + local_pos
|
||||
var cell: Button = _cells[local_pos]
|
||||
if int(cell.get_meta("vnum", 0)) != 0:
|
||||
return false
|
||||
var source_window := int(payload.get("window", -1))
|
||||
var source_cell := int(payload.get("cell", -1))
|
||||
if source_cell < 0:
|
||||
return false
|
||||
if source_window in [1, 2] and client.has_method("safebox_checkin"):
|
||||
return client.safebox_checkin(safe_pos, source_window, source_cell)
|
||||
if source_window == SAFE_WINDOW and client.has_method("safebox_move"):
|
||||
return client.safebox_move(source_cell, safe_pos, 1)
|
||||
return false
|
||||
|
||||
func _on_grid_input(local_pos: int, event: InputEvent) -> void:
|
||||
if not item_mouse or not item_mouse.has_method("attach_item"):
|
||||
return
|
||||
if not (event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT
|
||||
and event.pressed):
|
||||
return
|
||||
var cell: Button = _cells[local_pos]
|
||||
var vnum := int(cell.get_meta("vnum", 0))
|
||||
if vnum == 0:
|
||||
return
|
||||
item_mouse.attach_item(SAFE_WINDOW, _page * SAFE_PAGE_SLOTS + local_pos, vnum,
|
||||
int(cell.get_meta("count", 1)), null, "safebox")
|
||||
|
||||
func _change_page(delta: int) -> void:
|
||||
var pages := maxi(1, int(client.get_safebox_size()))
|
||||
_page = clampi(_page + delta, 0, pages - 1)
|
||||
refresh()
|
||||
|
||||
func _ask_password(kind: String) -> void:
|
||||
if is_instance_valid(_password_dialog):
|
||||
_password_dialog.queue_free()
|
||||
@@ -164,6 +251,23 @@ func _build(parent: Node) -> void:
|
||||
_title.text = "仓库"
|
||||
_title.position = Vector2(12, 8)
|
||||
_root.add_child(_title)
|
||||
var prev := Button.new()
|
||||
prev.text = "‹"
|
||||
prev.position = Vector2(235, 6)
|
||||
prev.size = Vector2(28, 24)
|
||||
prev.pressed.connect(func(): _change_page(-1))
|
||||
_root.add_child(prev)
|
||||
_page_label = Label.new()
|
||||
_page_label.position = Vector2(266, 10)
|
||||
_page_label.size = Vector2(60, 18)
|
||||
_page_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
_root.add_child(_page_label)
|
||||
var next := Button.new()
|
||||
next.text = "›"
|
||||
next.position = Vector2(302, 6)
|
||||
next.size = Vector2(28, 24)
|
||||
next.pressed.connect(func(): _change_page(1))
|
||||
_root.add_child(next)
|
||||
_gold = Label.new()
|
||||
_gold.text = "仓库金币: 0"
|
||||
_gold.position = Vector2(12, 30)
|
||||
@@ -179,3 +283,21 @@ func _build(parent: Node) -> void:
|
||||
_list.position = Vector2(12, 54)
|
||||
_list.add_theme_constant_override("separation", 4)
|
||||
_root.add_child(_list)
|
||||
_list.visible = false
|
||||
_grid = GridContainer.new()
|
||||
_grid.columns = 5
|
||||
_grid.position = Vector2(12, 54)
|
||||
_grid.add_theme_constant_override("h_separation", 4)
|
||||
_grid.add_theme_constant_override("v_separation", 4)
|
||||
_root.add_child(_grid)
|
||||
for pos in SAFE_PAGE_SLOTS:
|
||||
var cell := Button.new()
|
||||
cell.custom_minimum_size = Vector2(58, 30)
|
||||
cell.add_theme_font_size_override("font_size", 9)
|
||||
cell.set_meta("safe_pos", pos)
|
||||
cell.gui_input.connect(func(e: InputEvent): _on_grid_input(pos, e))
|
||||
if item_mouse and item_mouse.has_method("register_target"):
|
||||
item_mouse.register_target(cell,
|
||||
func(payload: Dictionary): return _drop_to_slot(payload, pos), self)
|
||||
_grid.add_child(cell)
|
||||
_cells[pos] = cell
|
||||
|
||||
Reference in New Issue
Block a user