Add touch input router for mobile (step 3)

Route each finger to UI, the attack button, a floating lower-left joystick or
the camera (drag rotate, pinch zoom, tap = world click). Touch mode is default
on Android/iOS and available on desktop via --touch / AVA2_TOUCH=1; it disables
SDL touch-to-mouse and drives app.ENABLE_MOBILE_HUD. The joystick feeds the
original camera-relative movement path and the attack button sets the same
attack-key state as player.SetAttackKeyState. New `touch` Python module; the
mobile HUD joystick and combat wheel become display-only.

Tests: ava2.touch_router, ava2.touch_game (GameWindow + mobile HUD).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 19:46:24 +09:00
co-authored by Claude Opus 5.5
parent 1dfdcba739
commit 5597842d90
18 changed files with 978 additions and 103 deletions
+24 -11
View File
@@ -5,18 +5,25 @@ import math
import player
import app
import wndMgr
import touch
class MobileCombatWheel(ui.Window):
"""
移动端核心战斗轮盘 (Combat Radial Controller)
- 位于屏幕右下角
- 超大普攻按钮 (长按/轻点)
- 超大普攻按钮 (长按/轻点):按下由原生触摸分流器判定(touch.SetAttackButton),
按住期间等同键盘攻击键 player.SetAttackKeyState,本按钮只做按下显示
- 环绕式扇形技能槽位 (带 CD 倒计时遮罩与等阶显示)
- 辅助功能按键 (自动锁定、上下马等)
"""
ATTACK_IDLE_COLOR = grp.GenerateColor(0.6, 0.15, 0.1, 0.45)
ATTACK_DOWN_COLOR = grp.GenerateColor(0.95, 0.35, 0.2, 0.75)
def __init__(self, origin_x=None, origin_y=None):
ui.Window.__init__(self)
self.SetWindowName("MobileCombatWheel")
# 轮盘本身不拦截触摸,否则会盖住攻击键热区和世界点击;技能槽按钮仍可点
self.AddFlag("not_pick")
sw = wndMgr.GetScreenWidth()
sh = wndMgr.GetScreenHeight()
@@ -32,14 +39,16 @@ class MobileCombatWheel(ui.Window):
self.SetSize(380, 380)
# 1. 普通攻击主键 (超大圆形热区)
self.attack_btn = ui.Button()
# 包内没有 taskbar/attack_button_0x.sub,先用半透明色块标出热区(与摇杆同风格)
self.attack_btn = ui.Bar()
self.attack_btn.SetParent(self)
self.attack_btn.SetPosition(260 - 55, 260 - 55)
self.attack_btn.SetUpVisual("d:/ymir work/ui/game/taskbar/attack_button_01.sub")
self.attack_btn.SetOverVisual("d:/ymir work/ui/game/taskbar/attack_button_02.sub")
self.attack_btn.SetDownVisual("d:/ymir work/ui/game/taskbar/attack_button_03.sub")
self.attack_btn.SetEvent(ui.__mem_func__(self.__OnAttackClick))
self.attack_btn.AddFlag("not_pick")
self.attack_btn.SetPosition(260 - 60, 260 - 60)
self.attack_btn.SetSize(120, 120)
self.attack_btn.SetColor(self.ATTACK_IDLE_COLOR)
self.attack_btn.Show()
self.attack_pressed = False
touch.SetAttackButton(origin_x, origin_y, 60)
# 2. 扇形技能槽位群
self.skills = []
@@ -71,15 +80,19 @@ class MobileCombatWheel(ui.Window):
def __del__(self):
ui.Window.__del__(self)
def __OnAttackClick(self):
player.SetAttackKeyState(True)
app.RegisterDelayedCall(0.15, lambda: player.SetAttackKeyState(False))
def __OnSkillCast(self, index):
# 释放技能 (对应 Metin2 技能槽 1~4)
player.ClickSkillSlot(index + 1)
def OnUpdate(self):
pressed = touch.IsAttackPressed()
if pressed != self.attack_pressed:
self.attack_pressed = pressed
if pressed:
self.attack_btn.SetColor(self.ATTACK_DOWN_COLOR)
else:
self.attack_btn.SetColor(self.ATTACK_IDLE_COLOR)
# 刷新技能冷却状态
for item in self.skills:
slot_idx = item['slot'] + 1
+31 -70
View File
@@ -1,101 +1,62 @@
# -*- coding: utf-8 -*-
import ui
import grp
import math
import net
import chr
import player
import app
import wndMgr
import touch
class MobileJoystick(ui.Window):
"""
移动端专用虚拟摇杆 (Virtual Joystick)
- 位于屏幕左下角
- 支持手指按下、滑动拖拽、松开自动回中
- 动态计算移动向量与航向角,同步触发移动网络包
移动端虚拟摇杆的显示层。
触摸由原生触摸分流器处理(src/app/input/touch_router.h):手指落在左下摇杆区时,
摇杆以落点为中心出现,方向经 player 模块的移动接口驱动主角,与键盘 WASD 走同一路径。
本窗口只读取 touch.GetJoystick() 绘制底盘和摇杆头,不接收点击。
"""
def __init__(self, base_x=160, base_y=None, radius=75, knob_radius=35):
def __init__(self, base_x=160, base_y=None, radius=None, knob_radius=35):
ui.Window.__init__(self)
self.SetWindowName("MobileJoystick")
self.AddFlag("not_pick")
sw = wndMgr.GetScreenWidth()
sh = wndMgr.GetScreenHeight()
if base_y is None:
base_y = sh - 180
self.base_x = base_x
self.base_y = base_y
if radius is None:
radius = int(touch.GetLayout()[1][4])
self.radius = radius
self.knob_radius = knob_radius
self.knob_x = base_x
self.knob_y = base_y
self.is_dragging = False
self.SetPosition(base_x - radius - 20, base_y - radius - 20)
self.SetSize((radius + 20) * 2, (radius + 20) * 2)
self.SetSize(radius * 2, radius * 2)
# 外圈底盘
self.base_bar = ui.Bar()
self.base_bar.SetParent(self)
self.base_bar.SetPosition(20, 20)
self.base_bar.AddFlag("not_pick")
self.base_bar.SetPosition(0, 0)
self.base_bar.SetSize(radius * 2, radius * 2)
self.base_bar.SetColor(grp.GenerateColor(0.1, 0.1, 0.15, 0.4))
self.base_bar.Show()
# 内部活动摇杆
# 摇杆头
self.knob = ui.Bar()
self.knob.SetParent(self)
offset_k = radius - knob_radius + 20
self.knob.SetPosition(offset_k, offset_k)
self.knob.AddFlag("not_pick")
self.knob.SetSize(knob_radius * 2, knob_radius * 2)
self.knob.SetColor(grp.GenerateColor(0.8, 0.8, 0.9, 0.6))
self.knob.Show()
# 未按下时停在默认位置,提示摇杆区
if base_y is None:
import wndMgr
base_y = wndMgr.GetScreenHeight() - 180
self.rest_x = base_x
self.rest_y = base_y
self.__Place(base_x, base_y, base_x, base_y)
def __del__(self):
ui.Window.__del__(self)
def OnMouseLeftButtonDown(self):
self.is_dragging = True
return True
def OnMouseLeftButtonUp(self):
self.is_dragging = False
# 松手回中
offset_k = self.radius - self.knob_radius + 20
self.knob.SetPosition(offset_k, offset_k)
return True
def __Place(self, base_x, base_y, knob_x, knob_y):
self.SetPosition(int(base_x) - self.radius, int(base_y) - self.radius)
self.knob.SetPosition(int(knob_x - base_x) + self.radius - self.knob_radius,
int(knob_y - base_y) + self.radius - self.knob_radius)
def OnUpdate(self):
if not self.is_dragging:
return
# 采样当前鼠标/触控位置
mx, my = wndMgr.GetMousePosition()
dx = mx - self.base_x
dy = my - self.base_y
dist = math.hypot(dx, dy)
if dist > self.radius:
dx = (dx / dist) * self.radius
dy = (dy / dist) * self.radius
# 更新摇杆视觉位置
offset_k = self.radius - self.knob_radius + 20
self.knob.SetPosition(offset_k + int(dx), offset_k + int(dy))
# 若达到死区阈值,计算朝向并向服务器发包同步
if dist > 15:
angle_rad = math.atan2(dy, dx)
yaw_deg = math.degrees(angle_rad)
# 获取主角当前坐标
main_vid = net.GetMainActorVID() if hasattr(net, 'GetMainActorVID') else 0
if main_vid != 0:
cur_x, cur_y, cur_z = chr.GetPixelPosition(main_vid) if hasattr(chr, 'GetPixelPosition') else (0, 0, 0)
# 计算步进
step = 250
target_x = cur_x + int(step * math.cos(angle_rad))
target_y = cur_y + int(step * math.sin(angle_rad))
if hasattr(net, 'SendCharacterMovePacket'):
net.SendCharacterMovePacket(1, 0, int(yaw_deg), int(target_x), int(target_y))
active, base_x, base_y, knob_x, knob_y = touch.GetJoystick()
if active:
self.__Place(base_x, base_y, knob_x, knob_y)
else:
self.__Place(self.rest_x, self.rest_y, self.rest_x, self.rest_y)