From 98e87a78d8e8cd3757a3a6f3a3510331cf7189bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 22 May 2024 12:11:51 +0200 Subject: [PATCH] Extract the iCade code to Controls.mm --- ios/Controls.h | 23 +++++++- ios/Controls.mm | 127 ++++++++++++++++++++++++++++++++++++++++++ ios/ViewController.mm | 127 ++---------------------------------------- 3 files changed, 154 insertions(+), 123 deletions(-) diff --git a/ios/Controls.h b/ios/Controls.h index 7b7b4ee67a..cf4112f9d2 100644 --- a/ios/Controls.h +++ b/ios/Controls.h @@ -1,6 +1,10 @@ #pragma once +#include + #import +#include "iCade/iCadeState.h" +#include "Common/Input/InputState.h" // Code extracted from ViewController.mm, in order to modularize // and share it between multiple view controllers. @@ -8,6 +12,7 @@ bool SetupController(GCController *controller); struct TouchTracker { +public: void Began(NSSet *touches, UIView *view); void Moved(NSSet *touches, UIView *view); void Ended(NSSet *touches, UIView *view); @@ -16,4 +21,20 @@ private: void SendTouchEvent(float x, float y, int code, int pointerId); int ToTouchID(UITouch *uiTouch, bool allowAllocate); UITouch *touches_[10]{}; -}; \ No newline at end of file +}; + +// Can probably get rid of this, but let's keep it for now. +struct ICadeTracker { +public: + void ButtonDown(iCadeState button); + void ButtonUp(iCadeState button); + void InitKeyMap(); +private: + bool simulateAnalog = false; + bool iCadeConnectNotified = false; + + std::map iCadeToKeyMap; + + double lastSelectPress = 0.0f; + double lastStartPress = 0.0f; +}; diff --git a/ios/Controls.mm b/ios/Controls.mm index bca399c1eb..28ff43bf86 100644 --- a/ios/Controls.mm +++ b/ios/Controls.mm @@ -1,9 +1,11 @@ #include "Controls.h" #include "Common/Log.h" +#include "Common/TimeUtil.h" #include "Common/Input/InputState.h" #include "Common/System/NativeApp.h" #include "Common/System/Display.h" +#include "Core/KeyMap.h" static void controllerButtonPressed(BOOL pressed, InputKeyCode keyCode) { KeyInput key; @@ -233,3 +235,128 @@ void TouchTracker::Cancelled(NSSet *touches, UIView *view) { } } } + +void ICadeTracker::InitKeyMap() { + iCadeToKeyMap[iCadeJoystickUp] = NKCODE_DPAD_UP; + iCadeToKeyMap[iCadeJoystickRight] = NKCODE_DPAD_RIGHT; + iCadeToKeyMap[iCadeJoystickDown] = NKCODE_DPAD_DOWN; + iCadeToKeyMap[iCadeJoystickLeft] = NKCODE_DPAD_LEFT; + iCadeToKeyMap[iCadeButtonA] = NKCODE_BUTTON_9; // Select + iCadeToKeyMap[iCadeButtonB] = NKCODE_BUTTON_7; // LTrigger + iCadeToKeyMap[iCadeButtonC] = NKCODE_BUTTON_10; // Start + iCadeToKeyMap[iCadeButtonD] = NKCODE_BUTTON_8; // RTrigger + iCadeToKeyMap[iCadeButtonE] = NKCODE_BUTTON_4; // Square + iCadeToKeyMap[iCadeButtonF] = NKCODE_BUTTON_2; // Cross + iCadeToKeyMap[iCadeButtonG] = NKCODE_BUTTON_1; // Triangle + iCadeToKeyMap[iCadeButtonH] = NKCODE_BUTTON_3; // Circle +} + +void ICadeTracker::ButtonDown(iCadeState button) { + if (simulateAnalog && + ((button == iCadeJoystickUp) || + (button == iCadeJoystickDown) || + (button == iCadeJoystickLeft) || + (button == iCadeJoystickRight))) { + AxisInput axis; + switch (button) { + case iCadeJoystickUp : + axis.axisId = JOYSTICK_AXIS_Y; + axis.value = -1.0f; + break; + + case iCadeJoystickDown : + axis.axisId = JOYSTICK_AXIS_Y; + axis.value = 1.0f; + break; + + case iCadeJoystickLeft : + axis.axisId = JOYSTICK_AXIS_X; + axis.value = -1.0f; + break; + + case iCadeJoystickRight : + axis.axisId = JOYSTICK_AXIS_X; + axis.value = 1.0f; + break; + + default: + break; + } + axis.deviceId = DEVICE_ID_PAD_0; + NativeAxis(&axis, 1); + } else { + KeyInput key; + key.flags = KEY_DOWN; + key.keyCode = iCadeToKeyMap[button]; + key.deviceId = DEVICE_ID_PAD_0; + NativeKey(key); + } +} + +void ICadeTracker::ButtonUp(iCadeState button) { + if (!iCadeConnectNotified) { + iCadeConnectNotified = true; + KeyMap::NotifyPadConnected(DEVICE_ID_PAD_0, "iCade"); + } + + if (button == iCadeButtonA) { + // Pressing Select twice within 1 second toggles the DPad between + // normal operation and simulating the Analog stick. + if ((lastSelectPress + 1.0f) > time_now_d()) + simulateAnalog = !simulateAnalog; + lastSelectPress = time_now_d(); + } + + if (button == iCadeButtonC) { + // Pressing Start twice within 1 second will take to the Emu menu + if ((lastStartPress + 1.0f) > time_now_d()) { + KeyInput key; + key.flags = KEY_DOWN; + key.keyCode = NKCODE_ESCAPE; + key.deviceId = DEVICE_ID_KEYBOARD; + NativeKey(key); + return; + } + lastStartPress = time_now_d(); + } + + if (simulateAnalog && + ((button == iCadeJoystickUp) || + (button == iCadeJoystickDown) || + (button == iCadeJoystickLeft) || + (button == iCadeJoystickRight))) { + AxisInput axis; + switch (button) { + case iCadeJoystickUp : + axis.axisId = JOYSTICK_AXIS_Y; + axis.value = 0.0f; + break; + + case iCadeJoystickDown : + axis.axisId = JOYSTICK_AXIS_Y; + axis.value = 0.0f; + break; + + case iCadeJoystickLeft : + axis.axisId = JOYSTICK_AXIS_X; + axis.value = 0.0f; + break; + + case iCadeJoystickRight : + axis.axisId = JOYSTICK_AXIS_X; + axis.value = 0.0f; + break; + + default: + break; + } + axis.deviceId = DEVICE_ID_PAD_0; + NativeAxis(&axis, 1); + } else { + KeyInput key; + key.flags = KEY_UP; + key.keyCode = iCadeToKeyMap[button]; + key.deviceId = DEVICE_ID_PAD_0; + NativeKey(key); + } +} \ No newline at end of file diff --git a/ios/ViewController.mm b/ios/ViewController.mm index 900046f9d1..5d66732ddd 100644 --- a/ios/ViewController.mm +++ b/ios/ViewController.mm @@ -88,13 +88,8 @@ private: GLRenderManager *renderManager_; }; -static double lastSelectPress = 0.0f; -static double lastStartPress = 0.0f; -static bool simulateAnalog = false; -static bool iCadeConnectNotified = false; static bool threadEnabled = true; static bool threadStopped = false; -static TouchTracker g_touchTracker; id sharedViewController; static GraphicsContext *graphicsContext; @@ -102,7 +97,8 @@ static CameraHelper *cameraHelper; static LocationHelper *locationHelper; @interface PPSSPPViewControllerGL () { - std::map iCadeToKeyMap; + ICadeTracker g_iCadeTracker; + TouchTracker g_touchTracker; } @property (nonatomic, strong) EAGLContext* context; @@ -118,18 +114,7 @@ static LocationHelper *locationHelper; self = [super init]; if (self) { sharedViewController = self; - iCadeToKeyMap[iCadeJoystickUp] = NKCODE_DPAD_UP; - iCadeToKeyMap[iCadeJoystickRight] = NKCODE_DPAD_RIGHT; - iCadeToKeyMap[iCadeJoystickDown] = NKCODE_DPAD_DOWN; - iCadeToKeyMap[iCadeJoystickLeft] = NKCODE_DPAD_LEFT; - iCadeToKeyMap[iCadeButtonA] = NKCODE_BUTTON_9; // Select - iCadeToKeyMap[iCadeButtonB] = NKCODE_BUTTON_7; // LTrigger - iCadeToKeyMap[iCadeButtonC] = NKCODE_BUTTON_10; // Start - iCadeToKeyMap[iCadeButtonD] = NKCODE_BUTTON_8; // RTrigger - iCadeToKeyMap[iCadeButtonE] = NKCODE_BUTTON_4; // Square - iCadeToKeyMap[iCadeButtonF] = NKCODE_BUTTON_2; // Cross - iCadeToKeyMap[iCadeButtonG] = NKCODE_BUTTON_1; // Triangle - iCadeToKeyMap[iCadeButtonH] = NKCODE_BUTTON_3; // Circle + g_iCadeTracker.InitKeyMap(); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil]; @@ -348,114 +333,12 @@ extern float g_safeInsetBottom; - (void)buttonDown:(iCadeState)button { - if (simulateAnalog && - ((button == iCadeJoystickUp) || - (button == iCadeJoystickDown) || - (button == iCadeJoystickLeft) || - (button == iCadeJoystickRight))) { - AxisInput axis; - switch (button) { - case iCadeJoystickUp : - axis.axisId = JOYSTICK_AXIS_Y; - axis.value = -1.0f; - break; - - case iCadeJoystickDown : - axis.axisId = JOYSTICK_AXIS_Y; - axis.value = 1.0f; - break; - - case iCadeJoystickLeft : - axis.axisId = JOYSTICK_AXIS_X; - axis.value = -1.0f; - break; - - case iCadeJoystickRight : - axis.axisId = JOYSTICK_AXIS_X; - axis.value = 1.0f; - break; - - default: - break; - } - axis.deviceId = DEVICE_ID_PAD_0; - NativeAxis(&axis, 1); - } else { - KeyInput key; - key.flags = KEY_DOWN; - key.keyCode = iCadeToKeyMap[button]; - key.deviceId = DEVICE_ID_PAD_0; - NativeKey(key); - } + g_iCadeTracker.ButtonDown(button); } - (void)buttonUp:(iCadeState)button { - if (!iCadeConnectNotified) { - iCadeConnectNotified = true; - KeyMap::NotifyPadConnected(DEVICE_ID_PAD_0, "iCade"); - } - - if (button == iCadeButtonA) { - // Pressing Select twice within 1 second toggles the DPad between - // normal operation and simulating the Analog stick. - if ((lastSelectPress + 1.0f) > time_now_d()) - simulateAnalog = !simulateAnalog; - lastSelectPress = time_now_d(); - } - - if (button == iCadeButtonC) { - // Pressing Start twice within 1 second will take to the Emu menu - if ((lastStartPress + 1.0f) > time_now_d()) { - KeyInput key; - key.flags = KEY_DOWN; - key.keyCode = NKCODE_ESCAPE; - key.deviceId = DEVICE_ID_KEYBOARD; - NativeKey(key); - return; - } - lastStartPress = time_now_d(); - } - - if (simulateAnalog && - ((button == iCadeJoystickUp) || - (button == iCadeJoystickDown) || - (button == iCadeJoystickLeft) || - (button == iCadeJoystickRight))) { - AxisInput axis; - switch (button) { - case iCadeJoystickUp : - axis.axisId = JOYSTICK_AXIS_Y; - axis.value = 0.0f; - break; - - case iCadeJoystickDown : - axis.axisId = JOYSTICK_AXIS_Y; - axis.value = 0.0f; - break; - - case iCadeJoystickLeft : - axis.axisId = JOYSTICK_AXIS_X; - axis.value = 0.0f; - break; - - case iCadeJoystickRight : - axis.axisId = JOYSTICK_AXIS_X; - axis.value = 0.0f; - break; - - default: - break; - } - axis.deviceId = DEVICE_ID_PAD_0; - NativeAxis(&axis, 1); - } else { - KeyInput key; - key.flags = KEY_UP; - key.keyCode = iCadeToKeyMap[button]; - key.deviceId = DEVICE_ID_PAD_0; - NativeKey(key); - } + g_iCadeTracker.ButtonUp(button); } - (void)controllerDidConnect:(NSNotification *)note