-
Notifications
You must be signed in to change notification settings - Fork 0
/
HiLowSignalProcessorController.mm
135 lines (114 loc) · 3.46 KB
/
HiLowSignalProcessorController.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// -*- Mode: ObjC -*-
//
// Copyright (C) 2011, Brad Howes. All rights reserved.
//
#import <memory>
#import "HiLowSignalProcessor.h"
#import "HiLowSignalProcessorController.h"
#import "UserSettings.h"
@interface HiLowSignalProcessorController (Private)
+ (NSString**)levelNames;
+ (NSString**)keyNames;
@end
@implementation HiLowSignalProcessorController
enum GestureKind {
kAdjustMaxLowLevel,
kAdjustMinHighLevel,
kAdjustUnknown,
};
+ (NSString**)levelNames
{
static NSString* levelNames[] = {
[NSLocalizedString(@"Low Level", "Name of low level") retain],
[NSLocalizedString(@"High Level", "Name of high level") retain],
nil
};
return levelNames;
}
+ (NSString**)keyNames
{
static NSString* keyNames[] = {
kSettingsBitDetectorMaxLowLevelKey,
kSettingsBitDetectorMinHighLevelKey,
nil
};
return keyNames;
}
+ (id)createWithDetector:(HiLowSignalProcessor*)detector
{
return [[[HiLowSignalProcessorController alloc] initWithDetector:detector] autorelease];
}
- (id)initWithDetector:(HiLowSignalProcessor*)theDetector
{
if (self = [super init]) {
detector = theDetector;
}
return self;
}
- (void)dealloc
{
detector = nil;
[super dealloc];
}
- (void)drawOnSampleView:(GLfloat*)vertices
{
vertices[1] = detector.maxLowLevel;
vertices[3] = detector.maxLowLevel;
glLineWidth(2.0);
glColor4f(1., 1., 0., 1.0);
glDrawArrays(GL_LINES, 0, 2);
vertices[1] = detector.minHighLevel;
vertices[3] = detector.minHighLevel;
glColor4f(1., 0., 1., 1.0);
glDrawArrays(GL_LINES, 0, 2);
}
struct HitInfo
{
GestureKind kind;
CGFloat level;
CGFloat delta;
HitInfo(GestureKind k, CGFloat l, CGFloat y)
: kind(k), level(l), delta(fabs(l - y))
{}
bool operator<(const HitInfo& rhs) const { return delta < rhs.delta; }
};
- (Float32)distanceFromLevel:(Float32)value
{
HitInfo a(kAdjustMaxLowLevel, detector.maxLowLevel, value);
HitInfo b(kAdjustMinHighLevel, detector.minHighLevel, value);
if (b < a) std::swap(b, a);
return a.delta;
}
- (void)handlePanGesture:(UIPanGestureRecognizer*)recognizer viewPoint:(CGPoint)pos
{
if (recognizer.state == UIGestureRecognizerStateEnded) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithFloat:gestureLevel]
forKey:[HiLowSignalProcessorController keyNames][gestureKind]];
[[NSUserDefaults standardUserDefaults] synchronize];
gestureKind = kAdjustUnknown;
}
else {
if (recognizer.state == UIGestureRecognizerStateBegan) {
HitInfo a(kAdjustMaxLowLevel, detector.maxLowLevel, pos.y);
HitInfo b(kAdjustMinHighLevel, detector.minHighLevel, pos.y);
if (b < a) std::swap(b, a);
gestureKind = a.kind;
gestureStart = pos.y;
gestureLevel = a.level;
}
else {
gestureLevel += (pos.y - gestureStart);
gestureStart = pos.y;
if (gestureLevel > 1.0) gestureLevel = 1.0;
if (gestureLevel < -1.0) gestureLevel = -1.0;
if (gestureKind == kAdjustMaxLowLevel) {
detector.maxLowLevel = gestureLevel;
}
else {
detector.minHighLevel = gestureLevel;
}
}
[self showLevelOverlay:[HiLowSignalProcessorController levelNames][gestureKind] withValue:gestureLevel];
}
}
@end