forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJOYButton.m
102 lines (89 loc) · 2.44 KB
/
JOYButton.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#import "JOYButton.h"
#import "JOYElement.h"
@implementation JOYButton
{
JOYElement *_element;
bool _state;
}
+ (NSString *)usageToString: (JOYButtonUsage) usage
{
if (usage < JOYButtonUsageNonGenericMax) {
return (NSString *[]) {
@"None",
@"A",
@"B",
@"C",
@"X",
@"Y",
@"Z",
@"Start",
@"Select",
@"Home",
@"Misc",
@"Left Stick",
@"Right Stick",
@"L1",
@"L2",
@"L3",
@"R1",
@"R2",
@"R3",
@"D-Pad Left",
@"D-Pad Right",
@"D-Pad Up",
@"D-Pad Down",
}[usage];
}
if (usage >= JOYButtonUsageGeneric0) {
return [NSString stringWithFormat:@"Generic Button %d", usage - JOYButtonUsageGeneric0];
}
return [NSString stringWithFormat:@"Unknown Usage Button %d", usage];
}
- (NSString *)usageString
{
return [self.class usageToString:_usage];
}
- (uint64_t)uniqueID
{
return _element.uniqueID;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p, %@ (%llu); State: %s>", self.className, self, self.usageString, self.uniqueID, _state? "Presssed" : "Released"];
}
- (instancetype)initWithElement:(JOYElement *)element
{
self = [super init];
if (!self) return self;
_element = element;
if (element.usagePage == kHIDPage_Button) {
uint16_t usage = element.usage;
_usage = JOYButtonUsageGeneric0 + usage;
}
else if (element.usagePage == kHIDPage_GenericDesktop) {
switch (element.usage) {
case kHIDUsage_GD_DPadUp: _usage = JOYButtonUsageDPadUp; break;
case kHIDUsage_GD_DPadDown: _usage = JOYButtonUsageDPadDown; break;
case kHIDUsage_GD_DPadRight: _usage = JOYButtonUsageDPadRight; break;
case kHIDUsage_GD_DPadLeft: _usage = JOYButtonUsageDPadLeft; break;
case kHIDUsage_GD_Start: _usage = JOYButtonUsageStart; break;
case kHIDUsage_GD_Select: _usage = JOYButtonUsageSelect; break;
case kHIDUsage_GD_SystemMainMenu: _usage = JOYButtonUsageHome; break;
}
}
return self;
}
- (bool) isPressed
{
return _state;
}
- (bool)updateState
{
bool state = [_element value];
if (_state != state) {
_state = state;
return true;
}
return false;
}
@end