Mobile gestures: item tap/drag/double-tap/long-press on original windows
Port the EterPythonLib CSlotWindow/CGridSlotWindow mouse handlers (select, drop, use, right click, tooltips, grid picking/CheckMoving) and CWindowManager IsDragging/right button, which were missing so item interaction did not work on desktop either. app.SetMouseHandler now renders the attached icon, app.IsPressed reads real key state, desktop double clicks reach the window manager. The touch router maps UI touches to the original mouse: tap = click at the touch-down point, drag = press-and-drag, double tap = double click, long press on an item slot = touch.SetLongPressEvent, which opens the ui_mobile_actions item sheet (use / split / quick slot via virtual Shift/Ctrl). New ava2.inventory_touch test drives uiinventory.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
908838c8f9
commit
6244b1d860
@@ -0,0 +1,146 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# 移动端长按物品的操作面板(docs/MOBILE-ARCHITECTURE.md,手势交互)。
|
||||
#
|
||||
# 触摸手势由原生 touch router 翻译成原版鼠标事件:点按 = 单击(拿起/放下物品),
|
||||
# 拖动 = 鼠标拖拽,双击 = 鼠标双击(使用/装备)。长按物品格时 router 调用
|
||||
# touch.SetLongPressEvent 注册的函数:指针停在格子上,原版 OverInItem 的物品提示保持显示,
|
||||
# 同时弹出本面板,把 PC 上靠修饰键完成的操作做成按钮,按钮调用的仍是原版槽位事件:
|
||||
# 使用/装备 -> slotWindow.OnUseSlot(原版右键/双击)
|
||||
# 拆分 -> 按住 Shift 调 OnSelectItemSlot(uiinventory.SelectItemSlot 的拆分分支)
|
||||
# 加入快捷栏 -> 按住 Ctrl 调 OnSelectItemSlot(交易/商店等窗口打开时是原版 Ctrl 放入)
|
||||
# 点面板外任意处关闭。丢弃、出售沿用拖拽(拖出窗口 / 拖到商店)。
|
||||
import ui
|
||||
import grp
|
||||
import app
|
||||
import wndMgr
|
||||
import touch
|
||||
import localeInfo
|
||||
|
||||
|
||||
def _Text(name, default):
|
||||
return getattr(localeInfo, name, default)
|
||||
|
||||
|
||||
class ItemActionSheet(ui.Window):
|
||||
WIDTH = 180
|
||||
ROW = 40
|
||||
PAD = 12
|
||||
# 物品提示框以手指为中心显示,面板放在它旁边
|
||||
TOOLTIP_HALF_WIDTH = 150
|
||||
|
||||
def __init__(self, interface=None):
|
||||
ui.Window.__init__(self, "TOP_MOST")
|
||||
self.SetWindowName("MobileItemActions")
|
||||
self.interface = interface
|
||||
self.SetPosition(0, 0)
|
||||
self.SetSize(wndMgr.GetScreenWidth(), wndMgr.GetScreenHeight())
|
||||
|
||||
self.panel = ui.Bar("TOP_MOST")
|
||||
self.panel.SetParent(self)
|
||||
self.panel.SetColor(grp.GenerateColor(0.08, 0.07, 0.06, 0.92))
|
||||
self.panel.Show()
|
||||
self.buttons = []
|
||||
|
||||
self.slotWindow = None
|
||||
self.slotNumber = 0
|
||||
self.Hide()
|
||||
|
||||
def __del__(self):
|
||||
ui.Window.__del__(self)
|
||||
|
||||
def Destroy(self):
|
||||
self.Close()
|
||||
self.buttons = []
|
||||
self.slotWindow = None
|
||||
self.interface = None
|
||||
|
||||
def __IsInventoryGrid(self, slotWindow):
|
||||
inventory = getattr(self.interface, "wndInventory", None) if self.interface else None
|
||||
return inventory is not None and slotWindow is getattr(inventory, "wndItem", None)
|
||||
|
||||
# touch.SetLongPressEvent: True = 已处理(手指松开不再点击)
|
||||
def OnLongPress(self, slotWindow, slotNumber, itemCount, x, y):
|
||||
actions = []
|
||||
if getattr(slotWindow, "eventUseSlot", None):
|
||||
actions.append((_Text("MOBILE_ITEM_USE", "Use"), self.__OnUse))
|
||||
if self.__IsInventoryGrid(slotWindow):
|
||||
if itemCount > 1:
|
||||
actions.append((_Text("MOBILE_ITEM_SPLIT", "Split"), self.__OnSplit))
|
||||
actions.append((_Text("MOBILE_ITEM_QUICKSLOT", "Quick slot"), self.__OnQuickSlot))
|
||||
# 没有可做的操作时也吃掉这次触摸:只看物品提示
|
||||
if not actions:
|
||||
return True
|
||||
actions.append((_Text("UI_CLOSE", "Close"), self.Close))
|
||||
|
||||
self.slotWindow = slotWindow
|
||||
self.slotNumber = slotNumber
|
||||
self.__Build(actions)
|
||||
self.__Place(x, y)
|
||||
self.Show()
|
||||
self.SetTop()
|
||||
return True
|
||||
|
||||
def __Build(self, actions):
|
||||
for button in self.buttons:
|
||||
button.Hide()
|
||||
self.buttons = []
|
||||
for i, (text, event) in enumerate(actions):
|
||||
button = ui.Button()
|
||||
button.SetParent(self.panel)
|
||||
button.SetPosition(self.PAD, self.PAD + i * self.ROW)
|
||||
button.SetUpVisual("d:/ymir work/ui/public/large_button_01.sub")
|
||||
button.SetOverVisual("d:/ymir work/ui/public/large_button_02.sub")
|
||||
button.SetDownVisual("d:/ymir work/ui/public/large_button_03.sub")
|
||||
button.SetText(text)
|
||||
button.SetEvent(ui.__mem_func__(event))
|
||||
button.Show()
|
||||
self.buttons.append(button)
|
||||
self.panel.SetSize(self.WIDTH, self.PAD * 2 + len(actions) * self.ROW - (self.ROW - 25))
|
||||
|
||||
def __Place(self, x, y):
|
||||
sw = wndMgr.GetScreenWidth()
|
||||
sh = wndMgr.GetScreenHeight()
|
||||
safe_l, safe_t, safe_r, safe_b = touch.GetSafeArea()
|
||||
w = self.WIDTH
|
||||
h = self.panel.GetHeight()
|
||||
if x - self.TOOLTIP_HALF_WIDTH - w >= safe_l:
|
||||
px = x - self.TOOLTIP_HALF_WIDTH - w
|
||||
else:
|
||||
px = min(x + self.TOOLTIP_HALF_WIDTH, sw - safe_r - w)
|
||||
py = max(safe_t, min(y - h / 2, sh - safe_b - h))
|
||||
self.panel.SetPosition(px, py)
|
||||
|
||||
def Close(self):
|
||||
self.Hide()
|
||||
self.slotWindow = None
|
||||
|
||||
# 面板外的点按(全屏透明底)关闭面板
|
||||
def OnMouseLeftButtonUp(self):
|
||||
self.Close()
|
||||
return True
|
||||
|
||||
def __Take(self):
|
||||
slotWindow, slotNumber = self.slotWindow, self.slotNumber
|
||||
self.Close()
|
||||
return slotWindow, slotNumber
|
||||
|
||||
def __OnUse(self):
|
||||
slotWindow, slotNumber = self.__Take()
|
||||
if slotWindow:
|
||||
slotWindow.OnUseSlot(slotNumber)
|
||||
|
||||
def __SelectWithKey(self, dik):
|
||||
slotWindow, slotNumber = self.__Take()
|
||||
if not slotWindow:
|
||||
return
|
||||
touch.SetVirtualKey(dik, 1)
|
||||
try:
|
||||
slotWindow.OnSelectItemSlot(slotNumber)
|
||||
finally:
|
||||
touch.SetVirtualKey(dik, 0)
|
||||
|
||||
def __OnSplit(self):
|
||||
self.__SelectWithKey(app.DIK_LSHIFT)
|
||||
|
||||
def __OnQuickSlot(self):
|
||||
self.__SelectWithKey(app.DIK_LCONTROL)
|
||||
@@ -8,6 +8,7 @@ import wndMgr
|
||||
import ui_mobile_joystick
|
||||
import ui_mobile_combat
|
||||
import ui_mobile_drawer
|
||||
import ui_mobile_actions
|
||||
import ui_mobile_widgets as widgets
|
||||
import touch
|
||||
import uiCharacter
|
||||
@@ -53,6 +54,9 @@ class MobileHUD(ui.Window):
|
||||
self.safe_area = touch.GetSafeArea()
|
||||
self.root = None
|
||||
self.__BuildUI(sw, sh)
|
||||
# 长按物品格:物品提示 + 操作面板(ui_mobile_actions)
|
||||
self.item_actions = ui_mobile_actions.ItemActionSheet(interface)
|
||||
touch.SetLongPressEvent(ui.__mem_func__(self.item_actions.OnLongPress))
|
||||
|
||||
def __del__(self):
|
||||
ui.Window.__del__(self)
|
||||
|
||||
Reference in New Issue
Block a user