X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=JSActionAnalog.m;h=e7d6eba8f6b2d08687e8e775f241fc30ffd97db0;hp=027bd5db95f8436a7f7040e9057e7f95e6e869ab;hb=e68c19b5923618b763543c74bf8dd6f85d4d323e;hpb=fad073260e61084c4962e172c58a0595261bd811 diff --git a/JSActionAnalog.m b/JSActionAnalog.m index 027bd5d..e7d6eba 100644 --- a/JSActionAnalog.m +++ b/JSActionAnalog.m @@ -5,53 +5,49 @@ // Created by Sam McCall on 5/05/09. // +// TODO: Dead zone should be configurable per-device. +#define DEAD_ZONE 0.3 + +#import "JSActionAnalog.h" + @implementation JSActionAnalog -- (id) initWithIndex: (int)newIndex { - if(self = [super init]) { - subActions = @[[[SubAction alloc] initWithIndex: 0 name: @"Low" base: self], - [[SubAction alloc] initWithIndex: 1 name: @"High" base: self], - [[SubAction alloc] initWithIndex: 2 name: @"Analog" base: self]]; - index = newIndex; - name = [[NSString alloc] initWithFormat: @"Axis %d", (index+1)]; - } - return self; -} +@synthesize offset, scale; --(id) findSubActionForValue: (IOHIDValueRef) value { - if ([subActions[2] active]) { - return subActions[2]; // TODO? +- (id)initWithIndex:(int)newIndex offset:(float)offset_ scale:(float)scale_ { + if ((self = [super init])) { + self.subActions = @[[[SubAction alloc] initWithIndex:0 name:@"Low" base:self], + [[SubAction alloc] initWithIndex:1 name:@"High" base:self]]; + self.index = newIndex; + self.offset = offset_; + self.scale = scale_; + self.name = [[NSString alloc] initWithFormat: @"Axis %d", self.index + 1]; } - - //Target* target = [[base->configsController currentConfig] getTargetForAction: [subActions objectAtIndex: 0]]; - - int raw = IOHIDValueGetIntegerValue(value); - double parsed = [self getRealValue: raw]; - - if(parsed < -0.3) // fixed?! - return subActions[0]; - else if(parsed > 0.3) - return subActions[1]; - return NULL; + return self; } --(void) notifyEvent: (IOHIDValueRef) value { - // Analog action is always active - [subActions[2] setActive: true]; +- (id)findSubActionForValue:(IOHIDValueRef)value { + int raw = IOHIDValueGetIntegerValue(value); + float parsed = [self getRealValue:raw]; - int raw = IOHIDValueGetIntegerValue(value); - double parsed = [self getRealValue: raw]; - - [subActions[0] setActive: (parsed < -0.3)]; - [subActions[1] setActive: (parsed > 0.3)]; + if (parsed < -DEAD_ZONE) + return self.subActions[0]; + else if (parsed > DEAD_ZONE) + return self.subActions[1]; + else + return nil; } --(double) getRealValue: (int)value { - double parsed = offset + scale * value; - return parsed; +- (void)notifyEvent:(IOHIDValueRef)value { + int raw = IOHIDValueGetIntegerValue(value); + float parsed = [self getRealValue:raw]; + [self.subActions[0] setActive:parsed < -DEAD_ZONE]; + [self.subActions[1] setActive:parsed > DEAD_ZONE]; } -@synthesize offset, scale; +- (float)getRealValue:(int)value { + return offset + scale * value; +} @end