Show an error message if opening input devices fail. Move real vs. configuration...
[enjoyable.git] / JSActionAnalog.m
index 5aded63..ae347fa 100644 (file)
 
 #import "JSActionAnalog.h"
 
-@implementation JSActionAnalog
+@implementation JSActionAnalog {
+    float magnitude;
+}
 
 @synthesize offset, scale;
 
 - (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.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 + 1];
+        self.name = [[NSString alloc] initWithFormat: @"Axis %d", self.index];
     }
     return self;
 }
 
 - (id)findSubActionForValue:(IOHIDValueRef)value {
     int raw = IOHIDValueGetIntegerValue(value);
-    float parsed = [self getRealValue:raw];
-    
-    if (parsed < -DEAD_ZONE)
+    float mag = offset + scale * raw;
+    if (mag < -DEAD_ZONE)
         return self.children[0];
-    else if (parsed > DEAD_ZONE)
+    else if (mag > DEAD_ZONE)
         return self.children[1];
     else
         return nil;
 
 - (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];
+    magnitude = offset + scale * raw;
+    [self.children[0] setActive:magnitude < -DEAD_ZONE];
+    [self.children[1] setActive:magnitude > DEAD_ZONE];
 }
 
-- (float)getRealValue:(int)value {
-    return offset + scale * value;
+- (float)magnitude {
+    return magnitude;
 }
 
-
 @end