feat: complete mobile UI implementation

This commit is contained in:
shenlei
2026-09-04 19:54:33 +09:00
parent 9a4a044da5
commit b19d5b5dd3
132 changed files with 6643 additions and 180 deletions
+123 -7
View File
@@ -25,9 +25,15 @@ var _cells: Dictionary = {}
var _page_label: Label
var _page := 0
var item_mouse: Node
var _mobile_mode := false
var _mobile_inventory_title: Label
var _mobile_inventory_scroll: ScrollContainer
var _mobile_inventory_list: VBoxContainer
var _mobile_deposit_pending := {}
const SAFE_PAGE_SLOTS := 45
const SAFE_WINDOW := 3
const INVENTORY_WINDOW := 1
func setup(m2client: Node, parent: Node, proto_node: Node = null) -> void:
client = m2client
@@ -47,6 +53,14 @@ func setup(m2client: Node, parent: Node, proto_node: Node = null) -> void:
func is_open() -> bool:
return _root != null and _root.visible
func set_mobile_mode(enabled: bool) -> void:
_mobile_mode = enabled
_apply_mobile_layout()
_refresh_mobile_inventory()
func get_mobile_window() -> Control:
return _root
func close() -> void:
if item_mouse and item_mouse.has_method("unregister_owner"):
item_mouse.unregister_owner(self)
@@ -55,6 +69,7 @@ func close() -> void:
if _root:
_root.visible = false
_page = 0
_mobile_deposit_pending.clear()
if is_instance_valid(_password_dialog):
_password_dialog.queue_free()
_password_dialog = null
@@ -62,12 +77,22 @@ func close() -> void:
func deposit(inv_window: int, inv_cell: int) -> void:
if not is_open() or inv_window < 0 or inv_cell < 0:
return
if inv_window == INVENTORY_WINDOW and _mobile_deposit_pending.has(inv_cell):
_status.text = "该物品的存入请求已发送"
return
var safe_pos := _next_free_slot()
if safe_pos < 0:
_status.text = "仓库已满"
return
if client.has_method("safebox_checkin") and not client.safebox_checkin(safe_pos, inv_window, inv_cell):
if not client.has_method("safebox_checkin"):
_status.text = "存入请求发送失败"
return
if not client.safebox_checkin(safe_pos, inv_window, inv_cell):
_status.text = "存入请求发送失败"
return
if _mobile_mode and inv_window == INVENTORY_WINDOW:
_mobile_deposit_pending[inv_cell] = true
_status.text = "已发送存入请求"
func _next_free_slot() -> int:
var used := {}
@@ -114,9 +139,10 @@ func refresh() -> void:
var e := Label.new()
e.text = "(空)"
_list.add_child(e)
return
for it in items:
_list.add_child(_row(it))
else:
for it in items:
_list.add_child(_row(it))
_refresh_mobile_inventory()
func _render_grid() -> void:
if _grid == null:
@@ -171,6 +197,71 @@ func _on_grid_input(local_pos: int, event: InputEvent) -> void:
item_mouse.attach_item(SAFE_WINDOW, _page * SAFE_PAGE_SLOTS + local_pos, vnum,
int(cell.get_meta("count", 1)), null, "safebox")
func _on_mobile_grid_tap(local_pos: int) -> void:
if not _mobile_mode or client == null or not _cells.has(local_pos):
return
var cell: Button = _cells[local_pos]
if int(cell.get_meta("vnum", 0)) == 0 or not client.has_method("safebox_checkout"):
return
var target := _first_free_inv()
if target < 0:
_status.text = "背包已满"
return
if not client.safebox_checkout(_page * SAFE_PAGE_SLOTS + local_pos, 1, target):
_status.text = "取出请求发送失败"
func _apply_mobile_layout() -> void:
if _root == null:
return
if _mobile_mode:
# The source-bag candidates stay inside the same hosted surface as the
# warehouse grid, so both obey one safe-area scale and touch boundary.
_root.size = Vector2(688, 400)
_root.position = Vector2(-344, -200)
_grid.position = Vector2(12, 54)
if _mobile_inventory_title:
_mobile_inventory_title.visible = true
_mobile_inventory_title.position = Vector2(350, 48)
if _mobile_inventory_scroll:
_mobile_inventory_scroll.visible = true
_mobile_inventory_scroll.position = Vector2(350, 76)
_mobile_inventory_scroll.size = Vector2(326, 292)
else:
_root.size = Vector2(340, 400)
_root.position = Vector2(-170, -200)
_grid.position = Vector2(12, 54)
if _mobile_inventory_title:
_mobile_inventory_title.visible = false
if _mobile_inventory_scroll:
_mobile_inventory_scroll.visible = false
func _refresh_mobile_inventory() -> void:
if not _mobile_mode or _mobile_inventory_list == null or not is_open():
return
for child in _mobile_inventory_list.get_children():
child.queue_free()
var inventory: Array = client.get_inventory() if client and client.has_method("get_inventory") else []
var shown := 0
for item in inventory:
var cell := int(item.get("cell", -1))
var vnum := int(item.get("vnum", 0))
if cell < 0 or vnum <= 0 or _mobile_deposit_pending.has(cell):
continue
var button := Button.new()
button.alignment = HORIZONTAL_ALIGNMENT_LEFT
button.text = "%s ×%d · 点按存入" % [_name_of(vnum), int(item.get("count", 1))]
button.custom_minimum_size = Vector2(316, 34)
button.tooltip_text = _tooltip_for(item)
var captured_cell := cell
button.pressed.connect(func() -> void: deposit(INVENTORY_WINDOW, captured_cell))
_mobile_inventory_list.add_child(button)
shown += 1
if shown == 0:
var empty := Label.new()
empty.text = "(没有可存入物品)"
empty.add_theme_font_size_override("font_size", 12)
_mobile_inventory_list.add_child(empty)
func _change_page(delta: int) -> void:
var pages := maxi(1, int(client.get_safebox_size()))
_page = clampi(_page + delta, 0, pages - 1)
@@ -188,7 +279,8 @@ func _ask_password(kind: String) -> void:
edit.secret = true
edit.max_length = 6
edit.placeholder_text = "密码"
edit.custom_minimum_size = Vector2(220, 28)
edit.virtual_keyboard_type = LineEdit.KEYBOARD_TYPE_NUMBER
edit.custom_minimum_size = Vector2(360, 44) if _mobile_mode else Vector2(220, 28)
_password_dialog.add_child(edit)
_password_dialog.confirmed.connect(func() -> void:
var sent: bool = client.safebox_password(edit.text) if kind == "safebox" else client.mall_password(edit.text)
@@ -199,8 +291,16 @@ func _ask_password(kind: String) -> void:
_password_dialog.canceled.connect(func() -> void:
_password_dialog.queue_free()
_password_dialog = null)
_root.get_parent().add_child(_password_dialog)
_password_dialog.popup_centered()
if _mobile_mode:
_password_dialog.set_meta("mobile_modal", true)
_password_dialog.set_meta("mobile_title", "商城密码" if kind == "mall" else "仓库密码")
_password_dialog.min_size = Vector2i(460, 230)
_password_dialog.size = Vector2i(460, 230)
_root.add_child(_password_dialog)
if _mobile_mode:
_password_dialog.popup_centered(Vector2i(460, 230))
else:
_password_dialog.popup_centered()
func _row(it: Dictionary) -> Control:
var row := HBoxContainer.new()
@@ -296,8 +396,24 @@ func _build(parent: Node) -> void:
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))
cell.pressed.connect(func(): _on_mobile_grid_tap(pos))
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
_mobile_inventory_title = Label.new()
_mobile_inventory_title.text = "背包 · 点按存入"
_mobile_inventory_title.add_theme_font_size_override("font_size", 12)
_mobile_inventory_title.add_theme_color_override("font_color", Color(0.95, 0.84, 0.58))
_mobile_inventory_title.visible = false
_root.add_child(_mobile_inventory_title)
_mobile_inventory_scroll = ScrollContainer.new()
_mobile_inventory_scroll.name = "MobileInventory"
_mobile_inventory_scroll.visible = false
_mobile_inventory_scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
_mobile_inventory_list = VBoxContainer.new()
_mobile_inventory_list.add_theme_constant_override("separation", 4)
_mobile_inventory_scroll.add_child(_mobile_inventory_list)
_root.add_child(_mobile_inventory_scroll)
_apply_mobile_layout()