Change KeyInputTextView from a TextView to a TextField. Simplifies the nib contents...
[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
15 @synthesize offset, scale;
16
17 - (id)initWithIndex:(int)newIndex offset:(float)offset_ scale:(float)scale_ {
18 if ((self = [super init])) {
19 self.subActions = @[[[SubAction alloc] initWithIndex:0 name:@"Low" base:self],
20 [[SubAction alloc] initWithIndex:1 name:@"High" base:self]];
21 self.index = newIndex;
22 self.offset = offset_;
23 self.scale = scale_;
24 self.name = [[NSString alloc] initWithFormat: @"Axis %d", self.index + 1];
25 }
26 return self;
27 }
28
29 - (id)findSubActionForValue:(IOHIDValueRef)value {
30 int raw = IOHIDValueGetIntegerValue(value);
31 float parsed = [self getRealValue:raw];
32
33 if (parsed < -DEAD_ZONE)
34 return self.subActions[0];
35 else if (parsed > DEAD_ZONE)
36 return self.subActions[1];
37 else
38 return nil;
39 }
40
41 - (void)notifyEvent:(IOHIDValueRef)value {
42 int raw = IOHIDValueGetIntegerValue(value);
43 float parsed = [self getRealValue:raw];
44 [self.subActions[0] setActive:parsed < -DEAD_ZONE];
45 [self.subActions[1] setActive:parsed > DEAD_ZONE];
46 }
47
48 - (float)getRealValue:(int)value {
49 return offset + scale * value;
50 }
51
52
53 @end