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:
co-authored by
Claude Opus 5.5
parent
1dfdcba739
commit
5597842d90
@@ -1,5 +1,6 @@
|
||||
#include "player_settings.h"
|
||||
#include "python_app.h"
|
||||
#include "app/input/touch_router.h"
|
||||
#include "server_clock.h"
|
||||
#include "generated_app_constants.h"
|
||||
#include "../upstream/ava2-client/UserInterface/Locale_inc.h"
|
||||
@@ -1018,11 +1019,8 @@ void initapp() {
|
||||
{nullptr, nullptr, 0, nullptr}
|
||||
};
|
||||
auto* module = Py_InitModule("app", methods);
|
||||
#ifdef __ANDROID__
|
||||
PyModule_AddIntConstant(module, "ENABLE_MOBILE_HUD", 1);
|
||||
#else
|
||||
PyModule_AddIntConstant(module, "ENABLE_MOBILE_HUD", 0);
|
||||
#endif
|
||||
// On for Android/iOS and for desktop --touch, which tests the mobile HUD with the mouse.
|
||||
PyModule_AddIntConstant(module, "ENABLE_MOBILE_HUD", input::IsTouchMode() ? 1 : 0);
|
||||
register_app_constants(module);
|
||||
|
||||
#ifdef ENABLE_MANAGER_BANK_SYSTEM
|
||||
|
||||
@@ -972,11 +972,15 @@ void UseGuildSkill(int skillSlot) {
|
||||
|
||||
static bool s_isAtkKey = false;
|
||||
static std::array<bool, 256> s_movementKeys{};
|
||||
static bool s_joystickActive = false;
|
||||
static float s_joystickX = 0.0f, s_joystickY = 0.0f;
|
||||
bool GetPlayerMovementYaw(float cameraYaw, float& movementYaw) {
|
||||
const int x = int(s_movementKeys[32] || s_movementKeys[205]) - int(s_movementKeys[30] || s_movementKeys[203]);
|
||||
const int z = int(s_movementKeys[31] || s_movementKeys[208]) - int(s_movementKeys[17] || s_movementKeys[200]);
|
||||
if (!x && !z) return false;
|
||||
movementYaw = std::atan2(float(x), float(z)) * 180.0f / 3.14159265f - cameraYaw;
|
||||
float x = float(int(s_movementKeys[32] || s_movementKeys[205]) - int(s_movementKeys[30] || s_movementKeys[203]));
|
||||
float z = float(int(s_movementKeys[31] || s_movementKeys[208]) - int(s_movementKeys[17] || s_movementKeys[200]));
|
||||
// Joystick screen-down is "back" like DIK_S, so it maps onto the same camera-relative axes.
|
||||
if (s_joystickActive) { x = s_joystickX; z = s_joystickY; }
|
||||
if (x == 0.0f && z == 0.0f) return false;
|
||||
movementYaw = std::atan2(x, z) * 180.0f / 3.14159265f - cameraYaw;
|
||||
movementYaw = std::fmod(movementYaw + 720.0f, 360.0f);
|
||||
return true;
|
||||
}
|
||||
@@ -2513,7 +2517,9 @@ static int s_mouseFunc[8] = {0};
|
||||
static int s_mouseState[8] = {0};
|
||||
bool IsPlayerMouseMoving() { return s_mouseState[0] == 1 && (s_mouseFunc[0] == 0 || s_mouseFunc[0] == 1); }
|
||||
bool IsPlayerCameraDragging() { return s_mouseState[1] == 1 && s_mouseFunc[1] == 2; }
|
||||
void CancelPlayerMovement() { s_movementKeys.fill(false); std::fill(std::begin(s_mouseState),std::end(s_mouseState),0); }
|
||||
void CancelPlayerMovement() { s_movementKeys.fill(false); s_joystickActive=false; std::fill(std::begin(s_mouseState),std::end(s_mouseState),0); }
|
||||
void SetPlayerJoystick(bool active, float x, float y) { s_joystickActive=active; s_joystickX=active?x:0.0f; s_joystickY=active?y:0.0f; }
|
||||
bool IsPlayerGameWindow(const void* handler) { return handler && handler==s_playerGameWindow; }
|
||||
static int s_mouseMiddleButtonState = 0;
|
||||
static bool s_playerVisible = true;
|
||||
static bool s_levelLimit = false;
|
||||
@@ -3182,16 +3188,18 @@ static PyObject* player_SendDragonSoulRefine(PyObject*, PyObject* args) {
|
||||
return Py_BuildNone();
|
||||
}
|
||||
|
||||
void SetPlayerAttackKey(bool pressed) {
|
||||
if (pressed && IsMainActorFishingMode()) {
|
||||
NEW_Fishing();
|
||||
return;
|
||||
}
|
||||
s_isAtkKey = pressed;
|
||||
}
|
||||
|
||||
static PyObject* player_SetAttackKeyState(PyObject*, PyObject* args) {
|
||||
int pressed = 0;
|
||||
if (!PyTuple_GetInteger(args, 0, &pressed)) return Py_BuildException();
|
||||
if (pressed) {
|
||||
if (IsMainActorFishingMode()) {
|
||||
NEW_Fishing();
|
||||
return Py_BuildNone();
|
||||
}
|
||||
}
|
||||
s_isAtkKey = (pressed != 0);
|
||||
SetPlayerAttackKey(pressed != 0);
|
||||
return Py_BuildNone();
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,12 @@ bool GetPlayerMovementYaw(float cameraYaw, float& movementYaw);
|
||||
bool IsPlayerMouseMoving();
|
||||
bool IsPlayerCameraDragging();
|
||||
void CancelPlayerMovement();
|
||||
// Touch joystick (screen axes, x right / y down, length 0..1); replaces the movement keys while active.
|
||||
void SetPlayerJoystick(bool active, float x, float y);
|
||||
// player.SetAttackKeyState for native input (touch attack button).
|
||||
void SetPlayerAttackKey(bool pressed);
|
||||
// True when handler is the window passed to player.SetGameWindow (game.GameWindow).
|
||||
bool IsPlayerGameWindow(const void* handler);
|
||||
void SetWeaponPower(uint32_t dwMinPower, uint32_t dwMaxPower, uint32_t dwMinMagicPower, uint32_t dwMaxMagicPower, uint32_t dwAddPower);
|
||||
void UpdateEquippedWeaponPower();
|
||||
bool HasEnoughArrow();
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "python_net.h"
|
||||
#include "python_app.h"
|
||||
#include "python_dbg.h"
|
||||
#include "python_touch.h"
|
||||
#include "python_system.h"
|
||||
#include "python_wndmgr.h"
|
||||
#include "python_gameplay_modules.h"
|
||||
@@ -113,6 +114,7 @@ PythonRuntime::PythonRuntime(const std::filesystem::path& stdlib,const std::file
|
||||
if(PyImport_AppendInittab("pack",initpack)!=0 ||
|
||||
PyImport_AppendInittab("app",initapp)!=0 ||
|
||||
PyImport_AppendInittab("dbg",initdbg)!=0 ||
|
||||
PyImport_AppendInittab("touch",inittouch)!=0 ||
|
||||
PyImport_AppendInittab("systemSetting",initsystemSetting)!=0 ||
|
||||
PyImport_AppendInittab("wndMgr",initwndMgr)!=0 ||
|
||||
PyImport_AppendInittab("item",initItem)!=0 ||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#include "python_touch.h"
|
||||
#include "app/input/touch_router.h"
|
||||
#include <Python.h>
|
||||
|
||||
namespace ava2 {
|
||||
namespace {
|
||||
|
||||
using input::GetTouchRouter;
|
||||
|
||||
PyObject* touch_is_enabled(PyObject*, PyObject*) {
|
||||
return PyInt_FromLong(input::IsTouchMode() ? 1 : 0);
|
||||
}
|
||||
|
||||
// (active, baseX, baseY, knobX, knobY) in UI pixels.
|
||||
PyObject* touch_get_joystick(PyObject*, PyObject*) {
|
||||
const auto joy = GetTouchRouter().GetJoystick();
|
||||
return Py_BuildValue("(iffff)", joy.active ? 1 : 0, joy.base_x, joy.base_y, joy.knob_x, joy.knob_y);
|
||||
}
|
||||
|
||||
PyObject* touch_is_attack_pressed(PyObject*, PyObject*) {
|
||||
return PyInt_FromLong(GetTouchRouter().IsAttackPressed() ? 1 : 0);
|
||||
}
|
||||
|
||||
// SetAttackButton(centerX, centerY, radius); radius 0 disables the button.
|
||||
PyObject* touch_set_attack_button(PyObject*, PyObject* args) {
|
||||
float x = 0, y = 0, r = 0;
|
||||
if (!PyArg_ParseTuple(args, "fff", &x, &y, &r)) return nullptr;
|
||||
auto layout = GetTouchRouter().GetLayout();
|
||||
layout.attack_x = x;
|
||||
layout.attack_y = y;
|
||||
layout.attack_r = r;
|
||||
GetTouchRouter().SetLayout(layout);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// SetJoystickArea(x, y, width, height[, radius])
|
||||
PyObject* touch_set_joystick_area(PyObject*, PyObject* args) {
|
||||
auto layout = GetTouchRouter().GetLayout();
|
||||
float x = 0, y = 0, w = 0, h = 0, r = layout.joy_radius;
|
||||
if (!PyArg_ParseTuple(args, "ffff|f", &x, &y, &w, &h, &r)) return nullptr;
|
||||
layout.joy_x = x;
|
||||
layout.joy_y = y;
|
||||
layout.joy_w = w;
|
||||
layout.joy_h = h;
|
||||
layout.joy_radius = r > 1.0f ? r : 1.0f;
|
||||
GetTouchRouter().SetLayout(layout);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// ((attackX, attackY, attackR), (joyX, joyY, joyW, joyH, joyRadius))
|
||||
PyObject* touch_get_layout(PyObject*, PyObject*) {
|
||||
const auto& l = GetTouchRouter().GetLayout();
|
||||
return Py_BuildValue("((fff)(fffff))", l.attack_x, l.attack_y, l.attack_r,
|
||||
l.joy_x, l.joy_y, l.joy_w, l.joy_h, l.joy_radius);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void inittouch() {
|
||||
static PyMethodDef methods[] = {
|
||||
{"IsEnabled", touch_is_enabled, METH_NOARGS, nullptr},
|
||||
{"GetJoystick", touch_get_joystick, METH_NOARGS, nullptr},
|
||||
{"IsAttackPressed", touch_is_attack_pressed, METH_NOARGS, nullptr},
|
||||
{"SetAttackButton", touch_set_attack_button, METH_VARARGS, nullptr},
|
||||
{"SetJoystickArea", touch_set_joystick_area, METH_VARARGS, nullptr},
|
||||
{"GetLayout", touch_get_layout, METH_NOARGS, nullptr},
|
||||
{nullptr, nullptr, 0, nullptr}
|
||||
};
|
||||
Py_InitModule("touch", methods);
|
||||
}
|
||||
|
||||
} // namespace ava2
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace ava2 {
|
||||
// `touch` module: lets the mobile HUD place the attack button / joystick area of the native
|
||||
// touch router (src/app/input/touch_router.h) and draw the joystick it drives.
|
||||
void inittouch();
|
||||
}
|
||||
Reference in New Issue
Block a user