Show an error message if opening input devices fail. Move real vs. configuration...
[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.index = newIndex;
24 self.offset = offset_;
25 self.scale = scale_;
26 self.name = [[NSString alloc] initWithFormat: @"Axis %d", self.index];
27 }
28 return self;
29 }
30
31 - (id)findSubActionForValue:(IOHIDValueRef)value {
32 int raw = IOHIDValueGetIntegerValue(value);
33 float mag = offset + scale * raw;
34 if (mag < -DEAD_ZONE)
35 return self.children[0];
36 else if (mag > DEAD_ZONE)
37 return self.children[1];
38 else
39 return nil;
40 }
41
42 - (void)notifyEvent:(IOHIDValueRef)value {
43 int raw = IOHIDValueGetIntegerValue(value);
44 magnitude = offset + scale * raw;
45 [self.children[0] setActive:magnitude < -DEAD_ZONE];
46 [self.children[1] setActive:magnitude > DEAD_ZONE];
47 }
48
49 - (float)magnitude {
50 return magnitude;
51 }
52
53 @end