Play-/Source/ui_ios/VirtualPadButton.mm

90 lines
2.4 KiB
Text
Raw Permalink Normal View History

2025-03-11 12:48:26 -04:00
#import "AppConfig.h"
#import "PreferenceDefs.h"
2015-10-26 22:26:34 -04:00
#import "VirtualPadButton.h"
2021-07-07 08:24:18 -10:00
@interface VirtualPadButton ()
2021-07-07 08:25:35 -10:00
@property(strong, nonatomic) UIImpactFeedbackGenerator* impactFeedback;
@property(strong, nonatomic) UISelectionFeedbackGenerator* selectionFeedback;
2021-07-07 08:24:18 -10:00
@end
2015-10-26 22:26:34 -04:00
@implementation VirtualPadButton
2021-07-07 08:25:35 -10:00
- (id)init
{
self = [super init];
if(self)
{
_impactFeedback = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
_selectionFeedback = [[UISelectionFeedbackGenerator alloc] init];
}
return self;
2021-07-07 08:24:18 -10:00
}
2020-12-02 18:37:31 -05:00
- (void)draw:(CGContextRef)context
2015-10-26 22:26:34 -04:00
{
2020-12-02 18:37:31 -05:00
[self.image drawInRect:self.bounds];
2015-10-26 22:26:34 -04:00
if(self.pressed)
{
CGContextSaveGState(context);
CGContextSetBlendMode(context, kCGBlendModePlusDarker);
2020-12-02 18:37:31 -05:00
[self.image drawInRect:self.bounds];
CGContextRestoreGState(context);
2015-10-26 22:26:34 -04:00
}
2020-12-02 18:37:31 -05:00
if([self.caption length] != 0)
2015-10-26 22:26:34 -04:00
{
2020-12-02 18:37:31 -05:00
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary* attributes =
2020-12-02 18:37:31 -05:00
@{
NSParagraphStyleAttributeName : paragraphStyle,
NSForegroundColorAttributeName : [UIColor whiteColor]
};
CGSize textSize = [self.caption sizeWithAttributes:attributes];
CGRect textRect = CGRectOffset(self.bounds, 0, (self.bounds.size.height - textSize.height) / 2);
2020-12-02 18:37:31 -05:00
[self.caption drawInRect:textRect withAttributes:attributes];
2015-10-26 22:26:34 -04:00
}
}
2020-12-02 18:37:31 -05:00
- (void)onPointerDown:(CGPoint)position
2015-10-26 22:26:34 -04:00
{
self.pressed = YES;
self.padHandler->SetButtonState(self.code, true);
2021-07-07 08:38:06 -10:00
if(CAppConfig::GetInstance().GetPreferenceBoolean(PREFERENCE_UI_VIRTUALPAD_HAPTICFEEDBACK))
{
switch(self.code)
{
case PS2::CControllerInfo::BUTTON::START:
case PS2::CControllerInfo::BUTTON::SELECT:
case PS2::CControllerInfo::BUTTON::SQUARE:
case PS2::CControllerInfo::BUTTON::TRIANGLE:
case PS2::CControllerInfo::BUTTON::CROSS:
case PS2::CControllerInfo::BUTTON::CIRCLE:
case PS2::CControllerInfo::BUTTON::L1:
case PS2::CControllerInfo::BUTTON::L2:
case PS2::CControllerInfo::BUTTON::L3:
case PS2::CControllerInfo::BUTTON::R1:
case PS2::CControllerInfo::BUTTON::R2:
case PS2::CControllerInfo::BUTTON::R3:
[self.impactFeedback impactOccurred];
break;
default:
[self.selectionFeedback selectionChanged];
break;
}
}
2020-12-02 18:37:31 -05:00
[super onPointerDown:position];
2015-10-26 22:26:34 -04:00
}
2020-12-02 18:37:31 -05:00
- (void)onPointerUp
2015-10-26 22:26:34 -04:00
{
self.pressed = NO;
self.padHandler->SetButtonState(self.code, false);
[super onPointerUp];
}
@end