Files
AVA2/upstream/python-root/ui_mobile_joystick.py
T
shenleiandClaude Opus 5.5 5597842d90 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>
2026-10-12 19:46:24 +09:00

63 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
import ui
import grp
import touch
class MobileJoystick(ui.Window):
"""
移动端虚拟摇杆的显示层。
触摸由原生触摸分流器处理(src/app/input/touch_router.h):手指落在左下摇杆区时,
摇杆以落点为中心出现,方向经 player 模块的移动接口驱动主角,与键盘 WASD 走同一路径。
本窗口只读取 touch.GetJoystick() 绘制底盘和摇杆头,不接收点击。
"""
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")
if radius is None:
radius = int(touch.GetLayout()[1][4])
self.radius = radius
self.knob_radius = knob_radius
self.SetSize(radius * 2, radius * 2)
# 外圈底盘
self.base_bar = ui.Bar()
self.base_bar.SetParent(self)
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)
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 __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):
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)