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
+82 -1
View File
@@ -27,6 +27,9 @@ var _offered_cells := {}
var _self_grid: GridContainer
var _exchange_cells: Dictionary = {}
var item_mouse: Node
var _mobile_mode := false
var _mobile_inventory_scroll: ScrollContainer
var _mobile_inventory_list: VBoxContainer
const EXCHANGE_SLOT_COUNT := 12
const INVENTORY_WINDOW := 1
@@ -49,6 +52,15 @@ func setup(m2client: Node, parent: Node, proto_node: Node = null) -> void:
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
if _root:
_apply_mobile_layout()
_refresh_mobile_inventory()
func close() -> void:
if item_mouse and item_mouse.has_method("unregister_owner"):
item_mouse.unregister_owner(self)
@@ -87,6 +99,7 @@ func offer(inv_window: int, inv_cell: int) -> void:
_offered_cells[key] = true
_next_display = (_next_display + 1) % 12
_status.text = ""
_refresh_mobile_inventory()
else:
_status.text = "交易请求发送失败"
@@ -135,6 +148,56 @@ func refresh() -> void:
_accept_btn.modulate = Color(0.5, 1, 0.5) if me else Color(1, 1, 1)
_root.get_node("PeerAccept").text = "对方: 已接受" if peer else "对方: 未接受"
_status.text = ""
_refresh_mobile_inventory()
func _apply_mobile_layout() -> void:
if _root == null:
return
if _mobile_mode:
# The inventory candidate list is part of the same hosted surface, so it
# scales and receives the same safe-area treatment as the trade window.
_root.size = Vector2(688, 400)
_root.position = Vector2(-344, -200)
if _mobile_inventory_scroll:
_mobile_inventory_scroll.position = Vector2(452, 54)
_mobile_inventory_scroll.size = Vector2(224, 280)
_mobile_inventory_scroll.visible = true
else:
_root.size = Vector2(440, 360)
_root.position = Vector2(-220, -180)
if _mobile_inventory_scroll:
_mobile_inventory_scroll.position = Vector2.ZERO
_mobile_inventory_scroll.size = Vector2.ZERO
_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:
continue
if _offered_cells.has("%d:%d" % [INVENTORY_WINDOW, 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(214, 34)
button.tooltip_text = _tooltip_for(item)
var captured_cell := cell
button.pressed.connect(func() -> void: offer(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 _item_at(inv_window: int, inv_cell: int) -> Dictionary:
if client == null:
@@ -216,6 +279,7 @@ func _drop_to_slot(payload: Dictionary, display_pos: int) -> bool:
_status.text = "交易请求发送失败"
return false
_offered_cells[key] = true
_refresh_mobile_inventory()
return true
func _fill(box: VBoxContainer, rows: Array) -> void:
@@ -256,6 +320,11 @@ func _on_accept() -> void:
else:
_status.text = "接受请求发送失败"
func _on_cancel() -> void:
if client and client.has_method("exchange_cancel"):
client.exchange_cancel()
close()
func _capture_anchor() -> void:
if _anchor_valid or client == null or not client.has_method("get_main_vid") \
or not client.has_method("get_entity"):
@@ -369,6 +438,9 @@ func _build(parent: Node) -> void:
_gold_input = LineEdit.new()
_gold_input.placeholder_text = "金币"
_gold_input.custom_minimum_size = Vector2(90, 0)
# Godot will open the native numeric keyboard on Android/iOS while the
# validation below remains authoritative for desktop and hardware keyboards.
_gold_input.virtual_keyboard_type = LineEdit.KEYBOARD_TYPE_NUMBER
bottom.add_child(_gold_input)
var put := Button.new()
put.text = "放金币"
@@ -380,5 +452,14 @@ func _build(parent: Node) -> void:
bottom.add_child(_accept_btn)
var cancel := Button.new()
cancel.text = "取消"
cancel.pressed.connect(func() -> void: client.exchange_cancel())
cancel.pressed.connect(_on_cancel)
bottom.add_child(cancel)
_mobile_inventory_scroll = ScrollContainer.new()
_mobile_inventory_scroll.name = "MobileInventory"
_mobile_inventory_scroll.visible = false
_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()