X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=JSActionAnalog.m;h=ae347fa9ad85774e7ac9ad41ba8ff109117df3d4;hp=293146421ba1477028ba6ec4d987a9ceb84a2ed1;hb=dd1f684886c2809133356bb9b335a35293e8849e;hpb=51ca12b552a9c17c4d4029b0340e193b273044a8 diff --git a/JSActionAnalog.m b/JSActionAnalog.m index 2931464..ae347fa 100644 --- a/JSActionAnalog.m +++ b/JSActionAnalog.m @@ -5,54 +5,49 @@ // Created by Sam McCall on 5/05/09. // -@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]]; - [subActions retain]; - index = newIndex; - name = [[NSString alloc] initWithFormat: @"Axis %d", (index+1)]; - } - return self; +// TODO: Dead zone should be configurable per-device. +#define DEAD_ZONE 0.3 + +#import "JSActionAnalog.h" + +@implementation JSActionAnalog { + float magnitude; } --(id) findSubActionForValue: (IOHIDValueRef) value { - if ([subActions[2] active]) { - return subActions[2]; // TODO? +@synthesize offset, scale; + +- (id)initWithIndex:(int)newIndex offset:(float)offset_ scale:(float)scale_ { + if ((self = [super init])) { + self.children = @[[[JSAction alloc] initWithName:@"Low" base:self], + [[JSAction alloc] initWithName:@"High" base:self]]; + self.index = newIndex; + self.offset = offset_; + self.scale = scale_; + self.name = [[NSString alloc] initWithFormat: @"Axis %d", self.index]; } - - //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]; - - int raw = IOHIDValueGetIntegerValue(value); - double parsed = [self getRealValue: raw]; - - [subActions[0] setActive: (parsed < -0.3)]; - [subActions[1] setActive: (parsed > 0.3)]; +- (id)findSubActionForValue:(IOHIDValueRef)value { + int raw = IOHIDValueGetIntegerValue(value); + float mag = offset + scale * raw; + if (mag < -DEAD_ZONE) + return self.children[0]; + else if (mag > 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); + magnitude = offset + scale * raw; + [self.children[0] setActive:magnitude < -DEAD_ZONE]; + [self.children[1] setActive:magnitude > DEAD_ZONE]; } -@synthesize offset, scale; - +- (float)magnitude { + return magnitude; +} @end