This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OverlayViewController.mm
executable file
·229 lines (181 loc) · 7.41 KB
/
OverlayViewController.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*==============================================================================
Copyright (c) 2010-2013 QUALCOMM Austria Research Center GmbH.
All Rights Reserved.
Qualcomm Confidential and Proprietary
==============================================================================*/
#import <AVFoundation/AVFoundation.h>
#import <QCAR/QCAR.h>
#import <QCAR/CameraDevice.h>
#import "OverlayViewController.h"
#import "OverlayView.h"
#import "QCARutils.h"
@interface OverlayViewController (PrivateMethods)
+ (void) determineCameraCapabilities:(struct tagCameraCapabilities *) pCapabilities;
@end
@implementation OverlayViewController
- (id) init
{
if ((self = [super init]) != nil)
{
selectedTarget = 0;
qUtils = [QCARutils getInstance];
}
return self;
}
- (void)dealloc {
[optionsOverlayView release];
[super dealloc];
}
- (void) loadView
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
optionsOverlayView = [[OverlayView alloc] initWithFrame: screenBounds];
self.view = optionsOverlayView;
// We're going to let the parent VC handle all interactions so disable any UI
// Further on, we'll also implement a touch pass-through
self.view.userInteractionEnabled = NO;
}
- (void) handleViewRotation:(UIInterfaceOrientation)interfaceOrientation
{
// adjust the size according to the rotation
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect overlayRect = screenRect;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
overlayRect.size.width = screenRect.size.height;
overlayRect.size.height = screenRect.size.width;
}
optionsOverlayView.frame = overlayRect;
}
// The user touched the screen - pass through
- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
{
// pass events down to parent VC
[super touchesBegan:touches withEvent:event];
}
- (void) populateActionSheet
{
// add flash control button if supported by the device
if (MENU_OPTION_WANTED == flashIx && YES == cameraCapabilities.torch)
{
// set button text according to the current mode (toggle)
BOOL flashMode = [qUtils cameraTorchOn];
NSString *text = YES == flashMode ? @"Flash off" : @"Flash on";
flashIx = [mainOptionsAS addButtonWithTitle:text];
}
// add focus control button if supported by the device
if (MENU_OPTION_WANTED == autofocusContIx && YES == cameraCapabilities.autofocusContinuous)
{
// set button text according to the current mode (toggle)
BOOL contAFMode = [qUtils cameraContinuousAFOn];
NSString *text = YES == contAFMode ? @"Auto focus off" : @"Auto focus on";
autofocusContIx = [mainOptionsAS addButtonWithTitle:text];
}
// add single-shot focus control button if supported by the device
if (MENU_OPTION_WANTED == autofocusSingleIx && YES == cameraCapabilities.autofocus)
{
autofocusSingleIx = [mainOptionsAS addButtonWithTitle:@"Focus"];
}
// add 'select target' if there is more than one target
if (MENU_OPTION_WANTED == selectTargetIx && qUtils.targetsList && [qUtils.targetsList count] > 1)
{
NSInteger targetToShow = (selectedTarget + 1) % [qUtils.targetsList count];
DataSetItem *targetEntry = [qUtils.targetsList objectAtIndex:targetToShow];
NSString *text = [NSString stringWithFormat:@"Switch to %@", targetEntry.name];
selectTargetIx = [mainOptionsAS addButtonWithTitle:text];
}
// The cancel button is added in showOverlay rather than here. This enables
// a subclass to call [super populateActionSheet either before or after adding
// its own items
}
// pop-up is invoked by parent VC
- (void) showOverlay
{
// Get the camera capabilities
[OverlayViewController determineCameraCapabilities:&cameraCapabilities];
// Show camera control action sheet
mainOptionsAS = [[[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil] autorelease];
// Specify standard menu options... a subclass can alter these settings
// in populateActionSheet
autofocusContIx = MENU_OPTION_WANTED;
autofocusSingleIx = MENU_OPTION_UNWANTED;
selectTargetIx = MENU_OPTION_WANTED;
flashIx = MENU_OPTION_UNWANTED;
// Call myself to populate the menu. May be overridden.
[self populateActionSheet];
// Only show the menu if we ended up with something in it
if ([mainOptionsAS numberOfButtons] > 0)
{
// Add a cancel button (this is only shown by iOS in the Phone idiom)
[mainOptionsAS setCancelButtonIndex:[mainOptionsAS addButtonWithTitle:@"Cancel"]];
self.view.userInteractionEnabled = YES;
[mainOptionsAS showInView:self.view];
}
}
// check to see if any content would be shown in showOverlay
+ (BOOL) doesOverlayHaveContent
{
int count = 0;
struct tagCameraCapabilities capabilities;
[OverlayViewController determineCameraCapabilities:&capabilities];
if (YES == capabilities.autofocusContinuous)
++count;
if ([QCARutils getInstance].targetsList && [[QCARutils getInstance].targetsList count] > 1)
++count;
return (count > 0);
}
// UIActionSheetDelegate event handlers
- (void) mainOptionClickedButtonAtIndex:(NSInteger)buttonIndex
{
if (flashIx == buttonIndex)
{
BOOL newFlashMode = ![qUtils cameraTorchOn];
[qUtils cameraSetTorchMode:newFlashMode];
}
else if (selectTargetIx == buttonIndex)
{
selectedTarget = (selectedTarget + 1) % [qUtils.targetsList count];
DataSetItem *targetEntry = [qUtils.targetsList objectAtIndex:selectedTarget];
[qUtils activateDataSet:targetEntry.dataSet];
}
else if (autofocusContIx == buttonIndex)
{
// toggle camera continuous autofocus mode
BOOL newContAFMode = ![qUtils cameraContinuousAFOn];
[qUtils cameraSetContinuousAFMode:newContAFMode];
}
else if (autofocusSingleIx == buttonIndex)
{
[qUtils cameraPerformAF];
}
self.view.userInteractionEnabled = NO;
}
- (void) actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self mainOptionClickedButtonAtIndex:buttonIndex];
}
+ (void) determineCameraCapabilities:(struct tagCameraCapabilities *) pCapabilities
{
// Determine whether the cameras support torch and autofocus
NSArray* cameraArray = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
qUtils.noOfCameras = [cameraArray count];
AVCaptureDevicePosition cameraPosition = AVCaptureDevicePositionBack;
if (QCAR::CameraDevice::CAMERA_FRONT == qUtils.activeCamera)
{
cameraPosition = AVCaptureDevicePositionFront;
}
for (AVCaptureDevice* camera in cameraArray)
{
if (cameraPosition == [camera position])
{
pCapabilities->autofocus = [camera isFocusModeSupported:AVCaptureFocusModeAutoFocus];
pCapabilities->autofocusContinuous = [camera isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus];
pCapabilities->torch = [camera isTorchModeSupported:AVCaptureTorchModeOn];
}
}
}
@end