X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=JSActionAnalog.m;h=9c98f21593c20c0bde263ff9148def7c2aff8a89;hp=a1c57e8533c2668737b265a9cc1902471ed939e5;hb=f563321aec9e13b8479ab3b890a9179f095a8b17;hpb=530009447c5bbd360ac5023979cffc6d32a28df3 diff --git a/JSActionAnalog.m b/JSActionAnalog.m index a1c57e8..9c98f21 100644 --- a/JSActionAnalog.m +++ b/JSActionAnalog.m @@ -5,52 +5,49 @@ // Created by Sam McCall on 5/05/09. // -@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; +#define DEAD_ZONE 0.3 + +#import "JSActionAnalog.h" + +static float normalize(long p, long min, long max) { + return 2 * (p - min) / (float)(max - min) - 1; +} + +@implementation JSActionAnalog { + float magnitude; + long rawMin; + long rawMax; } --(id) findSubActionForValue: (IOHIDValueRef) value { - if ([[subActions objectAtIndex: 2] active]) { - return [subActions objectAtIndex: 2]; // TODO? +- (id)initWithIndex:(int)index rawMin:(long)rawMin_ rawMax:(long)rawMax_ { + if ((self = [super init])) { + self.name = [[NSString alloc] initWithFormat: @"Axis %d", index]; + self.children = @[[[JSAction alloc] initWithName:@"Low" base:self], + [[JSAction alloc] initWithName:@"High" base:self]]; + rawMax = rawMax_; + rawMin = rawMin_; } - - //Target* target = [[base->configsController currentConfig] getTargetForAction: [subActions objectAtIndex: 0]]; - - int raw = IOHIDValueGetIntegerValue(value); - double parsed = offset + scale * 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]; - - int raw = IOHIDValueGetIntegerValue(value); - double parsed = offset + scale * raw; - - [[subActions objectAtIndex: 0] setActive: (parsed < -0.3)]; - [[subActions objectAtIndex: 1] setActive: (parsed > 0.3)]; +- (id)findSubActionForValue:(IOHIDValueRef)value { + float mag = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax); + if (mag < -DEAD_ZONE) + return self.children[0]; + else if (mag > DEAD_ZONE) + return self.children[1]; + else + return nil; } -@synthesize offset, scale; +- (void)notifyEvent:(IOHIDValueRef)value { + magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax); + [self.children[0] setActive:magnitude < -DEAD_ZONE]; + [self.children[1] setActive:magnitude > DEAD_ZONE]; +} +- (float)magnitude { + return magnitude; +} @end