2021-08-23 12:02:12 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 flyinghead
|
|
|
|
Copyright (c) 2015 reicast. All rights reserved.
|
|
|
|
|
|
|
|
This file is part of Flycast.
|
|
|
|
|
|
|
|
Flycast is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Flycast is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Flycast. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-08-06 08:30:30 +00:00
|
|
|
//
|
|
|
|
// Created by Lounge Katt on 8/25/15.
|
|
|
|
//
|
|
|
|
#import "PadViewController.h"
|
2021-08-23 12:02:12 +00:00
|
|
|
#include "ios_gamepad.h"
|
2021-08-06 08:30:30 +00:00
|
|
|
|
2021-08-12 09:22:22 +00:00
|
|
|
@interface PadViewController () {
|
|
|
|
UITouch *joyTouch;
|
|
|
|
CGPoint joyBias;
|
2021-08-23 12:02:12 +00:00
|
|
|
std::shared_ptr<IOSVirtualGamepad> virtualGamepad;
|
2021-08-12 09:22:22 +00:00
|
|
|
}
|
2021-08-06 08:30:30 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation PadViewController
|
|
|
|
|
2021-08-23 12:02:12 +00:00
|
|
|
- (void)viewDidLoad
|
|
|
|
{
|
|
|
|
[super viewDidLoad];
|
|
|
|
virtualGamepad = std::make_shared<IOSVirtualGamepad>();
|
|
|
|
GamepadDevice::Register(virtualGamepad);
|
|
|
|
}
|
|
|
|
|
2021-08-06 08:30:30 +00:00
|
|
|
- (void)showController:(UIView *)parentView
|
|
|
|
{
|
|
|
|
[parentView addSubview:self.view];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)hideController
|
|
|
|
{
|
2021-08-23 12:02:12 +00:00
|
|
|
[self resetTouch];
|
2021-08-06 08:30:30 +00:00
|
|
|
[self.view removeFromSuperview];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isControllerVisible {
|
2021-08-10 15:04:36 +00:00
|
|
|
return self.view.window != nil;
|
2021-08-06 08:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)keycodeDown:(id)sender
|
|
|
|
{
|
2021-08-23 12:02:12 +00:00
|
|
|
virtualGamepad->gamepad_btn_input((u32)((UIButton *)sender).tag, true);
|
2021-08-06 08:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)keycodeUp:(id)sender
|
|
|
|
{
|
2021-08-23 12:02:12 +00:00
|
|
|
virtualGamepad->gamepad_btn_input((u32)((UIButton *)sender).tag, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resetTouch
|
|
|
|
{
|
|
|
|
joyTouch = nil;
|
|
|
|
self.joyXConstraint.constant = 0;
|
|
|
|
self.joyYConstraint.constant = 0;
|
|
|
|
virtualGamepad->gamepad_axis_input(IOS_AXIS_LX, 0);
|
|
|
|
virtualGamepad->gamepad_axis_input(IOS_AXIS_LY, 0);
|
2021-08-06 08:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 09:32:22 +00:00
|
|
|
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
|
|
|
|
{
|
2021-08-10 15:04:36 +00:00
|
|
|
if (joyTouch == nil) {
|
|
|
|
for (UITouch *touch in touches) {
|
|
|
|
CGPoint loc = [touch locationInView:[self joystickBackground]];
|
|
|
|
if ([self.joystickBackground pointInside:loc withEvent:event]) {
|
|
|
|
joyTouch = touch;
|
|
|
|
joyBias = loc;
|
2021-08-23 12:02:12 +00:00
|
|
|
virtualGamepad->gamepad_axis_input(IOS_AXIS_LX, 0);
|
|
|
|
virtualGamepad->gamepad_axis_input(IOS_AXIS_LY, 0);
|
2021-08-10 15:04:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-08-10 09:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 15:04:36 +00:00
|
|
|
[super touchesBegan:touches withEvent:event];
|
2021-08-10 09:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
|
|
|
|
{
|
2021-08-10 15:04:36 +00:00
|
|
|
if (joyTouch != nil) {
|
|
|
|
for (UITouch *touch in touches) {
|
|
|
|
if (touch == joyTouch) {
|
2021-08-23 12:02:12 +00:00
|
|
|
[self resetTouch];
|
2021-08-10 15:04:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-08-10 09:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 15:04:36 +00:00
|
|
|
[super touchesEnded:touches withEvent:event];
|
2021-08-10 09:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
|
|
|
|
{
|
2021-08-10 15:04:36 +00:00
|
|
|
if (joyTouch != nil) {
|
|
|
|
for (UITouch *touch in touches) {
|
|
|
|
if (touch == joyTouch) {
|
|
|
|
CGPoint pos = [touch locationInView:[self joystickBackground]];
|
|
|
|
pos.x -= joyBias.x;
|
|
|
|
pos.y -= joyBias.y;
|
|
|
|
pos.x = std::max<CGFloat>(std::min<CGFloat>(25.0, pos.x), -25.0);
|
|
|
|
pos.y = std::max<CGFloat>(std::min<CGFloat>(25.0, pos.y), -25.0);
|
|
|
|
self.joyXConstraint.constant = pos.x;
|
|
|
|
self.joyYConstraint.constant = pos.y;
|
2021-09-19 16:27:21 +00:00
|
|
|
virtualGamepad->gamepad_axis_input(IOS_AXIS_LX, (s8)std::round(pos.x * 32767.0 / 25.0));
|
|
|
|
virtualGamepad->gamepad_axis_input(IOS_AXIS_LY, (s8)std::round(pos.y * 32767.0 / 25.0));
|
2021-08-10 15:04:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-08-10 09:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 15:04:36 +00:00
|
|
|
[super touchesMoved:touches withEvent:event];
|
2021-08-10 09:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
|
|
|
|
{
|
2021-08-10 15:04:36 +00:00
|
|
|
if (joyTouch != nil) {
|
|
|
|
for (UITouch *touch in touches) {
|
|
|
|
if (touch == joyTouch) {
|
2021-08-23 12:02:12 +00:00
|
|
|
[self resetTouch];
|
2021-08-10 15:04:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-08-10 09:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-10 15:04:36 +00:00
|
|
|
[super touchesCancelled:touches withEvent:event];
|
2021-08-10 09:32:22 +00:00
|
|
|
}
|
2021-08-06 08:30:30 +00:00
|
|
|
@end
|