0032013: Samples - iOS sample compilation errors
[occt.git] / samples / ios / UIKitSample / UIKitSample / GLViewController.mm
1 // Copyright (c) 2017 OPEN CASCADE SAS
2 //
3 // This file is part of the examples of the Open CASCADE Technology software library.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
21
22 #import <Foundation/Foundation.h>
23
24 #import "GLViewController.h"
25 #import "GLView.h"
26
27 @implementation GLViewController
28
29 // =======================================================================
30 // function : init
31 // purpose  :
32 // =======================================================================
33 - (id) init
34 {
35   self = [super init];
36   
37   if (self) {
38     myOcctViewer = new OcctViewer();
39   }
40   
41   return self;
42 }
43
44 // =======================================================================
45 // function : Draw
46 // purpose  :
47 // =======================================================================
48 - (void) Draw
49 {
50 }
51
52 // =======================================================================
53 // function : Setup
54 // purpose  :
55 // =======================================================================
56 - (void) Setup {
57   if (!myOcctViewer->InitViewer(self.view)) {
58     NSLog(@"Failed to init viewer");
59   }
60   else {
61     [self importScrew:nullptr];
62   }
63 }
64
65 // =======================================================================
66 // function : loadView
67 // purpose  :
68 // =======================================================================
69 - (void) loadView
70 {
71   GLView* aGLView = [[GLView alloc] init];
72   aGLView->myController = self;
73   self.view = aGLView;
74 }
75
76 // =======================================================================
77 // function : touchesBegan
78 // purpose  :
79 // =======================================================================
80 - (void)touchesBegan:(NSSet *)theTouches withEvent:(UIEvent *)theEvent
81 {
82   [super touchesBegan:theTouches withEvent:theEvent];
83   
84   UITouch *aTouch = [theTouches anyObject];
85   if (aTouch != NULL) {
86     CGPoint aTouchPoint = [aTouch locationInView:self.view];
87     myOcctViewer->StartRotation((int)aTouchPoint.x, (int)aTouchPoint.y);
88   }
89 }
90
91 // =======================================================================
92 // function : touchesMoved
93 // purpose  :
94 // =======================================================================
95 - (void)touchesMoved:(NSSet *)theTouches withEvent:(UIEvent *)theEvent
96 {
97   [super touchesMoved:theTouches withEvent:theEvent];
98   
99   UITouch *aTouch = [theTouches anyObject];
100   if (aTouch != NULL) {
101     CGPoint aTouchPoint = [aTouch locationInView:self.view];
102     myOcctViewer->Rotation((int)aTouchPoint.x, (int)aTouchPoint.y);
103   }
104   
105   return;
106 }
107
108 // =======================================================================
109 // function : viewDidLoad
110 // purpose  :
111 // =======================================================================
112 -(void)viewDidLoad
113 {
114   // add zoom recognizer
115   UIPinchGestureRecognizer *aZoomRecognizer = [[UIPinchGestureRecognizer alloc]
116                                                initWithTarget:self
117                                                action:@selector(zoomHandler:)];
118   
119   [[self view] addGestureRecognizer:aZoomRecognizer];
120   
121   // add pan recognizer
122   UIPanGestureRecognizer *aPanRecognizer = [[UIPanGestureRecognizer alloc]
123                                             initWithTarget:self
124                                             action:@selector(panHandler:)];
125   
126   aPanRecognizer.maximumNumberOfTouches = 2;
127   aPanRecognizer.minimumNumberOfTouches = 2;
128   
129   [[self view] addGestureRecognizer:aPanRecognizer];
130   
131   UITapGestureRecognizer *aTapRecognizer = [[UITapGestureRecognizer alloc]
132                                             initWithTarget:self
133                                             action:@selector(tapHandler:)];
134   
135   [[self view] addGestureRecognizer:aTapRecognizer];
136   
137   
138   // add import buttons
139   UIBarButtonItem *importScrewBtn = [[UIBarButtonItem alloc]
140                                      initWithTitle:@"Sample 1"
141                                      style:UIBarButtonItemStylePlain
142                                      target:self
143                                      action:@selector(importScrew:)];
144   
145   UIBarButtonItem *importLinkrodsBtn = [[UIBarButtonItem alloc]
146                                         initWithTitle:@"Sample 2"
147                                         style:UIBarButtonItemStylePlain
148                                         target:self
149                                         action:@selector(importLinkrods:)];
150   
151   UIBarButtonItem *displayAboutDlgBtn = [[UIBarButtonItem alloc]
152                                          initWithTitle:@"About"
153                                          style:UIBarButtonItemStylePlain
154                                          target:self
155                                          action:@selector(displayAboutDlg:)];
156   
157   [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:importScrewBtn, importLinkrodsBtn, nil]];
158   [self.navigationItem setRightBarButtonItem: displayAboutDlgBtn];
159 }
160
161 // =======================================================================
162 // function : zoomHandler
163 // purpose  :
164 // =======================================================================
165 - (void)zoomHandler:(UIPinchGestureRecognizer *)pinchRecognizer
166 {
167   if ([pinchRecognizer numberOfTouches] > 1)
168   {
169     UIGestureRecognizerState aState = [pinchRecognizer state];
170     if (aState == UIGestureRecognizerStateBegan)
171     {
172       myFirstTouch[0] = [pinchRecognizer locationOfTouch:0 inView:self.view];
173       myFirstTouch[1] = [pinchRecognizer locationOfTouch:1 inView:self.view];
174     }
175     else if (aState == UIGestureRecognizerStateChanged) {
176       CGPoint aLastTouch[2] = {
177         [pinchRecognizer locationOfTouch:0 inView:self.view],
178         [pinchRecognizer locationOfTouch:1 inView:self.view]
179       };
180       
181       double aPinchCenterXStart = ( myFirstTouch[0].x + myFirstTouch[1].x ) / 2.0;
182       double aPinchCenterYStart = ( myFirstTouch[0].y + myFirstTouch[1].y ) / 2.0;
183       
184       double aStartDist = Sqrt( ( myFirstTouch[0].x - myFirstTouch[1].x ) * ( myFirstTouch[0].x - myFirstTouch[1].x ) +
185                               ( myFirstTouch[0].y - myFirstTouch[1].y ) * ( myFirstTouch[0].y - myFirstTouch[1].y ) );
186       double anEndDist = Sqrt( ( aLastTouch[0].x - aLastTouch[1].x ) * ( aLastTouch[0].x - aLastTouch[1].x ) +
187                             ( aLastTouch[0].y - aLastTouch[1].y ) * ( aLastTouch[0].y - aLastTouch[1].y ) );
188       
189       double aDeltaDist = anEndDist - aStartDist;
190       
191       myOcctViewer->Zoom(aPinchCenterXStart, aPinchCenterYStart, aDeltaDist);
192       
193       myFirstTouch[0] = aLastTouch[0];
194       myFirstTouch[1] = aLastTouch[1];
195     }
196   }
197 }
198
199 // =======================================================================
200 // function : panHandler
201 // purpose  :
202 // =======================================================================
203 - (void)panHandler:(UIPanGestureRecognizer *)panRecognizer
204 {
205   if ([panRecognizer numberOfTouches] > 1)
206   {
207     UIGestureRecognizerState aState = [panRecognizer state];
208     if (aState == UIGestureRecognizerStateBegan)
209     {
210       myFirstTouch[0] = [panRecognizer locationOfTouch:0 inView:self.view];
211       myFirstTouch[1] = [panRecognizer locationOfTouch:1 inView:self.view];
212     }
213     else if (aState == UIGestureRecognizerStateChanged) {
214       CGPoint aLastTouch[2] = {
215         [panRecognizer locationOfTouch:0 inView:self.view],
216         [panRecognizer locationOfTouch:1 inView:self.view]
217       };
218       
219       double aPinchCenterXStart = ( myFirstTouch[0].x + myFirstTouch[1].x ) / 2.0;
220       double aPinchCenterYStart = ( myFirstTouch[0].y + myFirstTouch[1].y ) / 2.0;
221       
222       double aPinchCenterXEnd = ( aLastTouch[0].x + aLastTouch[1].x ) / 2.0;
223       double aPinchCenterYEnd = ( aLastTouch[0].y + aLastTouch[1].y ) / 2.0;
224       
225       double aPinchCenterXDev = aPinchCenterXEnd - aPinchCenterXStart;
226       double aPinchCenterYDev = aPinchCenterYEnd - aPinchCenterYStart;
227       
228       myOcctViewer->Pan((int)aPinchCenterXDev, (int)-aPinchCenterYDev);
229     }
230   }
231 }
232
233 // =======================================================================
234 // function : tapHandler
235 // purpose  :
236 // =======================================================================
237 - (void)tapHandler:(UITapGestureRecognizer *)tapRecognizer
238 {
239   CGPoint aTapPoint = [tapRecognizer locationInView:self.view];
240   myOcctViewer->Select(aTapPoint.x, aTapPoint.y);
241 }
242
243 // =======================================================================
244 // function : importScrew
245 // purpose  :
246 // =======================================================================
247 - (void)importScrew:(UIBarButtonItem *)theSender
248 {
249   NSString* aNsPath = [[NSBundle mainBundle] pathForResource:@"screw"
250                                                       ofType:@"step"];
251   std::string aPath = std::string([aNsPath UTF8String]);
252   
253   myOcctViewer->ImportSTEP(aPath);
254   myOcctViewer->FitAll();
255 }
256
257 // =======================================================================
258 // function : importLinkrods
259 // purpose  :
260 // =======================================================================
261 - (void)importLinkrods:(UIBarButtonItem *)theSender
262 {
263   NSString* aNsPath = [[NSBundle mainBundle] pathForResource:@"linkrods"
264                                                       ofType:@"step"];
265   std::string aPath = std::string([aNsPath UTF8String]);
266   
267   myOcctViewer->ImportSTEP(aPath);
268   myOcctViewer->FitAll();
269 }
270
271 // =======================================================================
272 // function : displayAboutDlg
273 // purpose  :
274 // =======================================================================
275 - (void)displayAboutDlg:(UIBarButtonItem *)theSender
276 {
277   UIAlertController* anAbout = [UIAlertController alertControllerWithTitle:@"About"
278                                 message:@"UIKit based application for tutorial to Open CASCADE Technology.\n\n"
279                                       @"Copyright (c) 2017 OPEN CASCADE SAS"
280                                 preferredStyle:UIAlertControllerStyleAlert];
281   
282   UIAlertAction* aDefaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
283                                                          handler:^(UIAlertAction * action) {}];
284   
285   [anAbout addAction:aDefaultAction];
286   [self presentViewController:anAbout animated:YES completion:nil];
287 }
288
289 @end