Re-import exported files.
[enjoyable.git] / JSActionAnalog.m
1 //
2 // JSActionAnalog.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 5/05/09.
6 //
7
8 #define DEAD_ZONE 0.3
9
10 #import "JSActionAnalog.h"
11
12 static float normalize(long p, long min, long max) {
13 return 2 * (p - min) / (float)(max - min) - 1;
14 }
15
16 @implementation JSActionAnalog {
17 float magnitude;
18 long rawMin;
19 long rawMax;
20 }
21
22 - (id)initWithIndex:(int)index rawMin:(long)rawMin_ rawMax:(long)rawMax_ {
23 if ((self = [super init])) {
24 self.name = [[NSString alloc] initWithFormat: @"Axis %d", index];
25 self.children = @[[[JSAction alloc] initWithName:@"Low" base:self],
26 [[JSAction alloc] initWithName:@"High" base:self]];
27 rawMax = rawMax_;
28 rawMin = rawMin_;
29 }
30 return self;
31 }
32
33 - (id)findSubActionForValue:(IOHIDValueRef)value {
34 float mag = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
35 if (mag < -DEAD_ZONE)
36 return self.children[0];
37 else if (mag > DEAD_ZONE)
38 return self.children[1];
39 else
40 return nil;
41 }
42
43 - (void)notifyEvent:(IOHIDValueRef)value {
44 magnitude = normalize(IOHIDValueGetIntegerValue(value), rawMin, rawMax);
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