X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=JSActionAnalog.m;h=9c98f21593c20c0bde263ff9148def7c2aff8a89;hp=06552a967509f721cd0228fc6f977fd189c36ff1;hb=44a44209d4ce26fb30102014d7040975aea51f93;hpb=51d43664909060e85c943c4d63cc3cff307ceb1d diff --git a/JSActionAnalog.m b/JSActionAnalog.m index 06552a9..9c98f21 100644 --- a/JSActionAnalog.m +++ b/JSActionAnalog.m @@ -5,32 +5,33 @@ // Created by Sam McCall on 5/05/09. // -// TODO: Dead zone should be configurable per-device. #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; } -@synthesize offset, scale; - -- (id)initWithIndex:(int)newIndex offset:(float)offset_ scale:(float)scale_ { +- (id)initWithIndex:(int)index rawMin:(long)rawMin_ rawMax:(long)rawMax_ { 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]; + 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_; } return self; } - (id)findSubActionForValue:(IOHIDValueRef)value { - int raw = IOHIDValueGetIntegerValue(value); - float mag = offset + scale * raw; + float mag = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax); if (mag < -DEAD_ZONE) return self.children[0]; else if (mag > DEAD_ZONE) @@ -40,8 +41,7 @@ } - (void)notifyEvent:(IOHIDValueRef)value { - int raw = IOHIDValueGetIntegerValue(value); - magnitude = offset + scale * raw; + magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax); [self.children[0] setActive:magnitude < -DEAD_ZONE]; [self.children[1] setActive:magnitude > DEAD_ZONE]; }