2019-10-19 16:26:04 +00:00
|
|
|
#import "JOYHat.h"
|
|
|
|
#import "JOYElement.h"
|
2021-07-29 19:43:55 +00:00
|
|
|
#import <AppKit/AppKit.h>
|
2019-10-19 16:26:04 +00:00
|
|
|
|
|
|
|
@implementation JOYHat
|
|
|
|
{
|
|
|
|
JOYElement *_element;
|
|
|
|
double _state;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (uint64_t)uniqueID
|
|
|
|
{
|
|
|
|
return _element.uniqueID;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
if (self.isPressed) {
|
|
|
|
return [NSString stringWithFormat:@"<%@: %p (%llu); State: %f degrees>", self.className, self, self.uniqueID, self.angle];
|
|
|
|
}
|
|
|
|
return [NSString stringWithFormat:@"<%@: %p (%llu); State: released>", self.className, self, self.uniqueID];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithElement:(JOYElement *)element
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (!self) return self;
|
|
|
|
|
|
|
|
_element = element;
|
2021-07-29 19:43:55 +00:00
|
|
|
_state = -1;
|
2019-10-19 16:26:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
unsigned state = ([_element value] - _element.min) * 360.0 / self.resolution;
|
|
|
|
if (_state != state) {
|
|
|
|
_state = state;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|