2025-03-11 12:48:26 -04:00
|
|
|
#import "AppConfig.h"
|
2021-07-07 08:37:03 -10:00
|
|
|
#import "PreferenceDefs.h"
|
2015-10-26 22:26:34 -04:00
|
|
|
#import "VirtualPadView.h"
|
|
|
|
#import "VirtualPadButton.h"
|
|
|
|
#import "VirtualPadStick.h"
|
|
|
|
#include "../VirtualPad.h"
|
|
|
|
|
2021-07-07 08:24:18 -10:00
|
|
|
@interface VirtualPadView ()
|
2021-07-07 08:25:35 -10:00
|
|
|
@property(strong, nonatomic) UISelectionFeedbackGenerator* selectionFeedback;
|
2021-07-07 08:24:18 -10:00
|
|
|
@end
|
|
|
|
|
2015-10-26 22:26:34 -04:00
|
|
|
@implementation VirtualPadView
|
|
|
|
|
2020-12-02 18:37:31 -05:00
|
|
|
- (id)initWithFrame:(CGRect)frame padHandler:(CPH_Generic*)padHandler
|
2015-10-26 22:26:34 -04:00
|
|
|
{
|
2020-12-02 18:37:31 -05:00
|
|
|
self = [super initWithFrame:frame];
|
2015-10-26 22:26:34 -04:00
|
|
|
if(self)
|
|
|
|
{
|
2015-10-31 19:20:10 -04:00
|
|
|
NSMutableDictionary<NSString*, UIImage*>* itemImages = [[NSMutableDictionary alloc] init];
|
2020-12-02 18:37:31 -05:00
|
|
|
[itemImages setObject:[UIImage imageNamed:@"select.png"] forKey:@"select"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"start.png"] forKey:@"start"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"up.png"] forKey:@"up"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"down.png"] forKey:@"down"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"left.png"] forKey:@"left"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"right.png"] forKey:@"right"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"triangle.png"] forKey:@"triangle"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"cross.png"] forKey:@"cross"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"circle.png"] forKey:@"circle"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"square.png"] forKey:@"square"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"lr.png"] forKey:@"lr"];
|
|
|
|
[itemImages setObject:[UIImage imageNamed:@"analogstick.png"] forKey:@"analogStick"];
|
2015-10-31 19:20:10 -04:00
|
|
|
self.itemImages = itemImages;
|
2021-07-07 08:25:35 -10:00
|
|
|
_selectionFeedback = [[UISelectionFeedbackGenerator alloc] init];
|
2015-10-26 22:26:34 -04:00
|
|
|
self.opaque = NO;
|
|
|
|
self.multipleTouchEnabled = YES;
|
|
|
|
_padHandler = padHandler;
|
|
|
|
[self rebuildPadItems];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:37:31 -05:00
|
|
|
- (void)rebuildPadItems
|
2015-10-26 22:26:34 -04:00
|
|
|
{
|
|
|
|
auto frame = self.frame;
|
|
|
|
auto padItems = CVirtualPad::GetItems(frame.size.width, frame.size.height);
|
|
|
|
NSMutableArray<VirtualPadItem*>* items = [[NSMutableArray alloc] init];
|
|
|
|
for(const auto& padItem : padItems)
|
|
|
|
{
|
|
|
|
auto itemWidth = padItem.x2 - padItem.x1;
|
|
|
|
auto itemHeight = padItem.y2 - padItem.y1;
|
|
|
|
VirtualPadItem* item = nil;
|
|
|
|
if(padItem.isAnalog)
|
|
|
|
{
|
|
|
|
auto stick = [[VirtualPadStick alloc] init];
|
|
|
|
stick.codeX = padItem.code0;
|
|
|
|
stick.codeY = padItem.code1;
|
|
|
|
item = stick;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto button = [[VirtualPadButton alloc] init];
|
|
|
|
button.code = padItem.code0;
|
2020-12-02 18:37:31 -05:00
|
|
|
button.caption = [NSString stringWithUTF8String:padItem.caption.c_str()];
|
2015-10-26 22:26:34 -04:00
|
|
|
item = button;
|
|
|
|
}
|
2020-12-02 18:37:31 -05:00
|
|
|
item.image = [self.itemImages objectForKey:[NSString stringWithUTF8String:padItem.imageName.c_str()]];
|
2015-10-26 22:26:34 -04:00
|
|
|
item.padHandler = _padHandler;
|
|
|
|
item.bounds = CGRectMake(padItem.x1, padItem.y1, itemWidth, itemHeight);
|
2020-12-02 18:37:31 -05:00
|
|
|
[items addObject:item];
|
2015-10-26 22:26:34 -04:00
|
|
|
}
|
|
|
|
self.items = items;
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:37:31 -05:00
|
|
|
- (void)drawRect:(CGRect)rect
|
2015-10-26 22:26:34 -04:00
|
|
|
{
|
2020-12-02 18:37:31 -05:00
|
|
|
[super drawRect:rect];
|
|
|
|
|
2015-10-26 22:26:34 -04:00
|
|
|
auto context = UIGraphicsGetCurrentContext();
|
2020-12-02 18:37:31 -05:00
|
|
|
|
2015-10-26 22:26:34 -04:00
|
|
|
for(VirtualPadItem* item in self.items)
|
|
|
|
{
|
2020-12-02 18:37:31 -05:00
|
|
|
[item draw:context];
|
2015-10-26 22:26:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:37:31 -05:00
|
|
|
- (void)touchesBegan:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event
|
2015-10-26 22:26:34 -04:00
|
|
|
{
|
|
|
|
for(UITouch* touch in touches)
|
|
|
|
{
|
2020-12-02 18:37:31 -05:00
|
|
|
auto touchPos = [touch locationInView:self];
|
2015-10-26 22:26:34 -04:00
|
|
|
for(VirtualPadItem* item in self.items)
|
|
|
|
{
|
|
|
|
if(CGRectContainsPoint(item.bounds, touchPos))
|
|
|
|
{
|
|
|
|
item.touch = touch;
|
2020-12-02 18:37:31 -05:00
|
|
|
[item onPointerDown:touchPos];
|
2021-07-07 08:38:06 -10:00
|
|
|
if(CAppConfig::GetInstance().GetPreferenceBoolean(PREFERENCE_UI_VIRTUALPAD_HAPTICFEEDBACK))
|
|
|
|
[self.selectionFeedback selectionChanged];
|
2015-10-26 22:26:34 -04:00
|
|
|
[self setNeedsDisplay];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:37:31 -05:00
|
|
|
- (void)touchesMoved:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event
|
2015-10-26 22:26:34 -04:00
|
|
|
{
|
|
|
|
for(UITouch* touch in touches)
|
|
|
|
{
|
2020-12-02 18:37:31 -05:00
|
|
|
auto touchPos = [touch locationInView:self];
|
2015-10-26 22:26:34 -04:00
|
|
|
for(VirtualPadItem* item in self.items)
|
|
|
|
{
|
|
|
|
if(item.touch == touch)
|
|
|
|
{
|
2020-12-02 18:37:31 -05:00
|
|
|
[item onPointerMove:touchPos];
|
2015-10-26 22:26:34 -04:00
|
|
|
[self setNeedsDisplay];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:37:31 -05:00
|
|
|
- (void)endOrCancelTouches:(NSSet<UITouch*>*)touches
|
2015-10-26 22:26:34 -04:00
|
|
|
{
|
|
|
|
for(UITouch* touch in touches)
|
|
|
|
{
|
|
|
|
for(VirtualPadItem* item in self.items)
|
|
|
|
{
|
|
|
|
if(item.touch == touch)
|
|
|
|
{
|
|
|
|
item.touch = nil;
|
|
|
|
[item onPointerUp];
|
|
|
|
[self setNeedsDisplay];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 18:37:31 -05:00
|
|
|
- (void)touchesEnded:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event
|
2015-10-26 22:26:34 -04:00
|
|
|
{
|
2020-12-02 18:37:31 -05:00
|
|
|
[self endOrCancelTouches:touches];
|
2015-10-26 22:26:34 -04:00
|
|
|
}
|
|
|
|
|
2020-12-02 18:37:31 -05:00
|
|
|
- (void)touchesCancelled:(NSSet<UITouch*>*)touches withEvent:(UIEvent*)event
|
2015-10-26 22:26:34 -04:00
|
|
|
{
|
2020-12-02 18:37:31 -05:00
|
|
|
[self endOrCancelTouches:touches];
|
2015-10-26 22:26:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|