Files
AVA2/upstream/python-root/ui_mobile_joystick.py
T
shenandshen 281418f351 Fix cross-platform rendering and optimize frame performance
Correct selection orientation, game-entry lifecycle, mounted NPCs, labels and skill panels. Add the native GPU scene path, resident geometry, bounded texture and Python caches, and collision broad phase.

Pin the independently maintained Metin2 SDK through a submodule and document reproducible setup. Include rendering regressions and macOS packaging improvements.

Validation: client Debug build and 12 targeted client tests passed; SDK build and all 14 SDK tests passed.
2026-10-11 17:55:53 -07:00

102 lines
3.3 KiB
Python

# -*- coding: utf-8 -*-
import ui
import grp
import math
import net
import chr
import player
import app
import wndMgr
class MobileJoystick(ui.Window):
"""
移动端专用虚拟摇杆 (Virtual Joystick)
- 位于屏幕左下角
- 支持手指按下、滑动拖拽、松开自动回中
- 动态计算移动向量与航向角,同步触发移动网络包
"""
def __init__(self, base_x=160, base_y=None, radius=75, knob_radius=35):
ui.Window.__init__(self)
self.SetWindowName("MobileJoystick")
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
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.base_bar = ui.Bar()
self.base_bar.SetParent(self)
self.base_bar.SetPosition(20, 20)
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.SetSize(knob_radius * 2, knob_radius * 2)
self.knob.SetColor(grp.GenerateColor(0.8, 0.8, 0.9, 0.6))
self.knob.Show()
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 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))