X-Git-Url: https://git.yukkurigames.com/?a=blobdiff_plain;f=JSActionAnalog.m;h=5aded63d3ad01e51dc8da6d926d2e4f8c26cd655;hb=724979785b445dcba8a9861c2531ae0308bdf40a;hp=e35199613cabfd2fe2e8d43319ad71b41137a0e8;hpb=61f8cdec21ab083b29c22aa11fda54d6005666ca;p=enjoyable.git diff --git a/JSActionAnalog.m b/JSActionAnalog.m index e351996..5aded63 100644 --- a/JSActionAnalog.m +++ b/JSActionAnalog.m @@ -5,57 +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 = [NSArray arrayWithObjects: - [[SubAction alloc] initWithIndex: 0 name: @"Low" base: self], - [[SubAction alloc] initWithIndex: 1 name: @"High" base: self], - [[SubAction alloc] initWithIndex: 2 name: @"Analog" base: self], - nil - ]; - [subActions retain]; - index = newIndex; - name = [[NSString alloc] initWithFormat: @"Axis %d", (index+1)]; - } - return self; -} +@synthesize offset, scale; --(id) findSubActionForValue: (IOHIDValueRef) value { - if ([[subActions objectAtIndex: 2] active]) { - return [subActions objectAtIndex: 2]; // TODO? +- (id)initWithIndex:(int)newIndex offset:(float)offset_ scale:(float)scale_ { + if ((self = [super init])) { + self.children = @[[[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 objectAtIndex: 0]; - else if(parsed > 0.3) - return [subActions objectAtIndex: 1]; - return NULL; + return self; } --(void) notifyEvent: (IOHIDValueRef) value { - // Analog action is always active - [[subActions objectAtIndex: 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 objectAtIndex: 0] setActive: (parsed < -0.3)]; - [[subActions objectAtIndex: 1] setActive: (parsed > 0.3)]; + if (parsed < -DEAD_ZONE) + return self.children[0]; + else if (parsed > DEAD_ZONE) + return self.children[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.children[0] setActive:parsed < -DEAD_ZONE]; + [self.children[1] setActive:parsed > DEAD_ZONE]; } -@synthesize offset, scale; +- (float)getRealValue:(int)value { + return offset + scale * value; +} @end