forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJOYHat.m
67 lines (54 loc) · 1.19 KB
/
JOYHat.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
#import "JOYHat.h"
#import "JOYElement.h"
#import <AppKit/AppKit.h>
@implementation JOYHat
{
JOYElement *_element;
double _state;
}
- (uint64_t)uniqueID
{
return _element.uniqueID | (uint64_t)self.combinedIndex << 32;
}
- (NSString *)description
{
if (self.isPressed) {
return [NSString stringWithFormat:@"<%@: %p (%llx); State: %f degrees>", self.className, self, self.uniqueID, self.angle];
}
return [NSString stringWithFormat:@"<%@: %p (%llx); State: released>", self.className, self, self.uniqueID];
}
- (instancetype)initWithElement:(JOYElement *)element
{
self = [super init];
if (!self) return self;
_element = element;
_state = -1;
return self;
}
- (bool)isPressed
{
return _state >= 0 && _state < 360;
}
- (double)angle
{
if (self.isPressed) return fmod((_state + 270), 360);
return -1;
}
- (unsigned)resolution
{
return _element.max - _element.min + 1;
}
- (bool)updateState
{
signed state = ([_element value] - _element.min) * 360.0 / self.resolution;
if (_state != state) {
_state = state;
return true;
}
return false;
}
- (NSString *)usageString
{
return @"Hat switch";
}
@end