File: JOYHat.m

package info (click to toggle)
sameboy 1.0.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,632 kB
  • sloc: ansic: 29,954; objc: 22,249; asm: 1,424; pascal: 1,373; makefile: 1,064; xml: 111
file content (67 lines) | stat: -rw-r--r-- 1,215 bytes parent folder | download | duplicates (2)
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