Finish extracting the touch tracker from the ViewController

This commit is contained in:
Henrik Rydgård 2024-05-22 11:59:26 +02:00
parent 1db11599b4
commit bf8d55c970
3 changed files with 78 additions and 59 deletions

View File

@ -6,4 +6,14 @@
// and share it between multiple view controllers.
bool SetupController(GCController *controller);
void SendTouchEvent(float x, float y, int code, int pointerId);
struct TouchTracker {
void Began(NSSet *touches, UIView *view);
void Moved(NSSet *touches, UIView *view);
void Ended(NSSet *touches, UIView *view);
void Cancelled(NSSet *touches, UIView *view);
private:
void SendTouchEvent(float x, float y, int code, int pointerId);
int ToTouchID(UITouch *uiTouch, bool allowAllocate);
UITouch *touches_[10]{};
};

View File

@ -148,7 +148,7 @@ bool SetupController(GCController *controller) {
return true;
}
void SendTouchEvent(float x, float y, int code, int pointerId) {
void TouchTracker::SendTouchEvent(float x, float y, int code, int pointerId) {
float scale = [UIScreen mainScreen].scale;
if ([[UIScreen mainScreen] respondsToSelector:@selector(nativeScale)]) {
scale = [UIScreen mainScreen].nativeScale;
@ -172,3 +172,64 @@ void SendTouchEvent(float x, float y, int code, int pointerId) {
NativeTouch(input);
}
int TouchTracker::ToTouchID(UITouch *uiTouch, bool allowAllocate) {
// Find the id for the touch.
for (int localId = 0; localId < (int)ARRAY_SIZE(touches_); ++localId) {
if (touches_[localId] == uiTouch) {
return localId;
}
}
// Allocate a new one, perhaps?
if (allowAllocate) {
for (int localId = 0; localId < (int)ARRAY_SIZE(touches_); ++localId) {
if (touches_[localId] == 0) {
touches_[localId] = uiTouch;
return localId;
}
}
// None were free. Ignore?
return 0;
}
return -1;
}
void TouchTracker::Began(NSSet *touches, UIView *view) {
for (UITouch* touch in touches) {
CGPoint point = [touch locationInView:view];
int touchId = ToTouchID(touch, true);
SendTouchEvent(point.x, point.y, 1, touchId);
}
}
void TouchTracker::Moved(NSSet *touches, UIView *view) {
for (UITouch* touch in touches) {
CGPoint point = [touch locationInView:view];
int touchId = ToTouchID(touch, true);
SendTouchEvent(point.x, point.y, 0, touchId);
}
}
void TouchTracker::Ended(NSSet *touches, UIView *view) {
for (UITouch* touch in touches) {
CGPoint point = [touch locationInView:view];
int touchId = ToTouchID(touch, false);
if (touchId >= 0) {
SendTouchEvent(point.x, point.y, 2, touchId);
touches_[touchId] = nullptr;
}
}
}
void TouchTracker::Cancelled(NSSet *touches, UIView *view) {
for (UITouch* touch in touches) {
CGPoint point = [touch locationInView:view];
int touchId = ToTouchID(touch, false);
if (touchId >= 0) {
SendTouchEvent(point.x, point.y, 2, touchId);
touches_[touchId] = nullptr;
}
}
}

View File

@ -94,7 +94,7 @@ static bool simulateAnalog = false;
static bool iCadeConnectNotified = false;
static bool threadEnabled = true;
static bool threadStopped = false;
static UITouch *g_touches[10];
static TouchTracker g_touchTracker;
id<PPSSPPViewController> sharedViewController;
static GraphicsContext *graphicsContext;
@ -118,8 +118,6 @@ static LocationHelper *locationHelper;
self = [super init];
if (self) {
sharedViewController = self;
memset(g_touches, 0, sizeof(g_touches));
iCadeToKeyMap[iCadeJoystickUp] = NKCODE_DPAD_UP;
iCadeToKeyMap[iCadeJoystickRight] = NKCODE_DPAD_RIGHT;
iCadeToKeyMap[iCadeJoystickDown] = NKCODE_DPAD_DOWN;
@ -323,74 +321,24 @@ extern float g_safeInsetBottom;
graphicsContext->ThreadFrame();
}
int ToTouchID(UITouch *uiTouch, bool allowAllocate) {
// Find the id for the touch.
for (int localId = 0; localId < (int)ARRAY_SIZE(g_touches); ++localId) {
if (g_touches[localId] == uiTouch) {
return localId;
}
}
// Allocate a new one, perhaps?
if (allowAllocate) {
for (int localId = 0; localId < (int)ARRAY_SIZE(g_touches); ++localId) {
if (g_touches[localId] == 0) {
g_touches[localId] = uiTouch;
return localId;
}
}
// None were free. Ignore?
return 0;
}
return -1;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView *view = self.view;
for (UITouch* touch in touches) {
CGPoint point = [touch locationInView:view];
int touchId = ToTouchID(touch, true);
SendTouchEvent(point.x, point.y, 1, touchId);
}
g_touchTracker.Began(touches, self.view);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView *view = self.view;
for (UITouch* touch in touches) {
CGPoint point = [touch locationInView:view];
int touchId = ToTouchID(touch, true);
SendTouchEvent(point.x, point.y, 0, touchId);
}
g_touchTracker.Moved(touches, self.view);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView *view = self.view;
for (UITouch* touch in touches) {
CGPoint point = [touch locationInView:view];
int touchId = ToTouchID(touch, false);
if (touchId >= 0) {
SendTouchEvent(point.x, point.y, 2, touchId);
g_touches[touchId] = nullptr;
}
}
g_touchTracker.Ended(touches, self.view);
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UIView *view = self.view;
for (UITouch* touch in touches) {
CGPoint point = [touch locationInView:view];
int touchId = ToTouchID(touch, false);
if (touchId >= 0) {
SendTouchEvent(point.x, point.y, 2, touchId);
g_touches[touchId] = nullptr;
}
}
g_touchTracker.Cancelled(touches, self.view);
}
- (void)bindDefaultFBO