Index hat switch names. Remove JSAction index properties entirely, once the name...
[enjoyable.git] / JSActionAnalog.m
1 //
2 // JSActionAnalog.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 // TODO: Dead zone should be configurable per-device.
9 #define DEAD_ZONE 0.3
10
11 #import "JSActionAnalog.h"
12
13 @implementation JSActionAnalog {
14 float magnitude;
15 }
16
17 @synthesize offset, scale;
18
19 - (id)initWithIndex:(int)newIndex offset:(float)offset_ scale:(float)scale_ {
20 if ((self = [super init])) {
21 self.children = @[[[JSAction alloc] initWithName:@"Low" base:self],
22 [[JSAction alloc] initWithName:@"High" base:self]];
23 self.offset = offset_;
24 self.scale = scale_;
25 self.name = [[NSString alloc] initWithFormat: @"Axis %d", newIndex];
26 }
27 return self;
28 }
29
30 - (id)findSubActionForValue:(IOHIDValueRef)value {
31 int raw = IOHIDValueGetIntegerValue(value);
32 float mag = offset + scale * raw;
33 if (mag < -DEAD_ZONE)
34 return self.children[0];
35 else if (mag > DEAD_ZONE)
36 return self.children[1];
37 else
38 return nil;
39 }
40
41 - (void)notifyEvent:(IOHIDValueRef)value {
42 int raw = IOHIDValueGetIntegerValue(value);
43 magnitude = offset + scale * raw;
44 [self.children[0] setActive:magnitude < -DEAD_ZONE];
45 [self.children[1] setActive:magnitude > DEAD_ZONE];
46 }
47
48 - (float)magnitude {
49 return magnitude;
50 }
51
52 @end