Extract the iCade code to Controls.mm

This commit is contained in:
Henrik Rydgård committed 2024-05-22 12:11:51 +02:00
1 parent bf8d55c970
commit 98e87a78d8
3 files changed
+154 -123

No files matched your search

+22 -1
View File
@@ -1,6 +1,10 @@
#pragma once
#include <map>
#import <GameController/GameController.h>
#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]{};
};
};
// 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<uint16_t, InputKeyCode> iCadeToKeyMap;
double lastSelectPress = 0.0f;
double lastStartPress = 0.0f;
};
+127
View File
@@ -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);
}
}
+5 -122
View File
@@ -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<PPSSPPViewController> sharedViewController;
static GraphicsContext *graphicsContext;
@@ -102,7 +97,8 @@ static CameraHelper *cameraHelper;
static LocationHelper *locationHelper;
@interface PPSSPPViewControllerGL () {
std::map<uint16_t, InputKeyCode> 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