5 * LocationWindowCamera usage example for Swift
6 * Demonstrates specific methods: getCamera, setCamera, addCameraListener, removeCameraListener,
7 * flyTo, moveTo, zoomFactor, minZoomFactor, maxZoomFactor
9class LocationWindowCameraExample {
10 private var locationWindow: NCLocationWindow?
11 private var cameraListener: CameraListenerImpl?
14 demonstrateLocationWindowCameraMethods()
18 * Demonstrate LocationWindowCamera methods
20 private func demonstrateLocationWindowCameraMethods() {
21 print("=== LocationWindowCamera Methods ===")
23 demonstrateCameraProperties()
24 demonstrateGetSetCamera()
25 demonstrateCameraListeners()
26 demonstrateFlyToMethod()
27 demonstrateMoveToMethod()
28 demonstrateZoomProperties()
29 demonstrateCameraScenarios()
33 * Demonstrate camera properties setup
35 private func demonstrateCameraProperties() {
36 print("--- Camera Properties Setup ---")
38 guard let locationWindow = locationWindow else {
39 print("LocationWindow not available yet")
43 // [swift_LocationWindow_minZoomFactor]
44 // Set minimum zoom factor
45 locationWindow.minZoomFactor = 10.0
46 print("Set minimum zoom factor to 10.0 pixels per meter")
47 // [swift_LocationWindow_minZoomFactor]
49 // [swift_LocationWindow_maxZoomFactor]
50 // Set maximum zoom factor
51 locationWindow.maxZoomFactor = 1000.0
52 print("Set maximum zoom factor to 1000.0 pixels per meter")
53 // [swift_LocationWindow_maxZoomFactor]
57 * Demonstrate getCamera and setCamera methods
59 private func demonstrateGetSetCamera() {
60 print("--- getCamera and setCamera Methods ---")
62 guard let locationWindow = locationWindow else {
63 print("LocationWindow not available yet")
67 // [swift_LocationWindow_getCamera]
68 // Get current camera position
69 let currentCamera = locationWindow.camera
70 print("Current camera position:")
71 print(" Point: (\(currentCamera.point.x), \(currentCamera.point.y))")
72 print(" Zoom: \(currentCamera.zoom)")
73 print(" Rotation: \(currentCamera.rotation)°")
74 // [swift_LocationWindow_getCamera]
76 // [swift_Camera_constructor]
77 // Create camera with constructor
78 let newPoint = NCPoint(x: 100.0, y: 200.0)
79 let newCamera = NCCamera(point: newPoint, zoom: 50.0, rotation: 0.0)
80 print("Created camera with point (\(newPoint.x), \(newPoint.y)), zoom 50.0, rotation 0°")
81 // [swift_Camera_constructor]
83 // [swift_Camera_access]
84 // Access camera properties
85 print("Camera properties:")
86 print(" Point: (\(newCamera.point.x), \(newCamera.point.y))")
87 print(" Zoom: \(newCamera.zoom)")
88 print(" Rotation: \(newCamera.rotation)°")
89 // [swift_Camera_access]
91 // [swift_LocationWindow_setCamera]
92 // Set camera position without animation
93 locationWindow.camera = newCamera
94 print("Set camera position to (\(newPoint.x), \(newPoint.y)) with zoom 50.0 and rotation 0°")
95 // [swift_LocationWindow_setCamera]
97 // [swift_LocationWindow_setCamera_2]
98 // Set camera position with rotation
99 let rotatedPoint = NCPoint(x: 150.0, y: 250.0)
100 let rotatedCamera = NCCamera(point: rotatedPoint, zoom: 75.0, rotation: 45.0)
101 locationWindow.camera = rotatedCamera
102 print("Set camera position to (\(rotatedPoint.x), \(rotatedPoint.y)) with zoom 75.0 and rotation 45°")
103 // [swift_LocationWindow_setCamera_2]
105 // Test different camera positions
107 NCCamera(point: NCPoint(x: 0.0, y: 0.0), zoom: 10.0, rotation: 0.0), // Overview camera
108 NCCamera(point: NCPoint(x: 50.0, y: 75.0), zoom: 500.0, rotation: 0.0), // Detail camera
109 NCCamera(point: NCPoint(x: 100.0, y: 150.0), zoom: 100.0, rotation: 30.0), // Angled camera
112 for (i, camera) in testCameras.enumerated() {
113 locationWindow.camera = camera
114 print("Test camera \(i): Point (\(camera.point.x), \(camera.point.y)), Zoom \(camera.zoom), Rotation \(camera.rotation)°")
119 * Demonstrate camera listener methods
121 private func demonstrateCameraListeners() {
122 print("--- Camera Listener Methods ---")
124 guard let locationWindow = locationWindow else {
125 print("LocationWindow not available yet")
129 // [swift_LocationWindow_addCameraListener]
130 // Add camera listener
131 cameraListener = CameraListenerImpl()
132 locationWindow.addCameraListener(cameraListener!)
133 print("Added camera listener")
134 // [swift_LocationWindow_addCameraListener]
136 // [swift_LocationWindow_removeCameraListener]
137 // Remove camera listener
138 locationWindow.removeCameraListener(cameraListener!)
140 print("Removed camera listener")
141 // [swift_LocationWindow_removeCameraListener]
143 // Test multiple listeners
145 CameraListenerImpl(),
146 CameraListenerImpl(),
147 CameraListenerImpl(),
150 for (i, listener) in listeners.enumerated() {
151 locationWindow.addCameraListener(listener)
152 print("Added camera listener \(i)")
155 for (i, listener) in listeners.enumerated() {
156 locationWindow.removeCameraListener(listener)
157 print("Removed camera listener \(i)")
162 * Demonstrate flyTo method
164 private func demonstrateFlyToMethod() {
165 print("--- flyTo Method ---")
167 guard let locationWindow = locationWindow else {
168 print("LocationWindow not available yet")
172 // [swift_LocationWindow_flyTo]
173 // Fly to position with smooth animation
174 let targetPoint = NCPoint(x: 150.0, y: 250.0)
175 let targetCamera = NCCamera(point: targetPoint, zoom: 75.0, rotation: 45.0)
176 let callback = CameraCallbackImpl()
177 locationWindow.flyTo(camera: targetCamera, duration: 2000, callback: callback)
178 print("Started fly to animation to point (\(targetPoint.x), \(targetPoint.y))")
179 // [swift_LocationWindow_flyTo]
181 // [swift_LocationWindow_flyTo_2]
182 // Fly to another position with different duration
183 let targetPoint2 = NCPoint(x: 300.0, y: 400.0)
184 let targetCamera2 = NCCamera(point: targetPoint2, zoom: 25.0, rotation: 180.0)
185 let callback2 = CameraCallbackImpl()
186 locationWindow.flyTo(camera: targetCamera2, duration: 3000, callback: callback2)
187 print("Started fly to animation to point (\(targetPoint2.x), \(targetPoint2.y)) with 3 second duration")
188 // [swift_LocationWindow_flyTo_2]
190 // Test fly to with different parameters
192 ["point": NCPoint(x: 50.0, y: 50.0), "zoom": 100.0, "rotation": 0.0, "duration": 1000],
193 ["point": NCPoint(x: 200.0, y: 300.0), "zoom": 200.0, "rotation": 90.0, "duration": 1500],
194 ["point": NCPoint(x: 400.0, y: 100.0), "zoom": 50.0, "rotation": 270.0, "duration": 2500],
197 for (i, test) in flyToTests.enumerated() {
198 let point = test["point"] as! NCPoint
199 let zoom = test["zoom"] as! Double
200 let rotation = test["rotation"] as! Double
201 let duration = test["duration"] as! Int
203 let camera = NCCamera(point: point, zoom: zoom, rotation: rotation)
204 let testCallback = CameraCallbackImpl()
205 locationWindow.flyTo(camera: camera, duration: duration, callback: testCallback)
206 print("Fly to test \(i): Point (\(point.x), \(point.y)), Zoom \(zoom), Rotation \(rotation)°, Duration \(duration)ms")
211 * Demonstrate moveTo method
213 private func demonstrateMoveToMethod() {
214 print("--- moveTo Method ---")
216 guard let locationWindow = locationWindow else {
217 print("LocationWindow not available yet")
221 // [swift_LocationWindow_moveTo]
222 // Move to position with linear animation
223 let targetPoint = NCPoint(x: 200.0, y: 300.0)
224 let targetCamera = NCCamera(point: targetPoint, zoom: 100.0, rotation: 90.0)
225 let callback = CameraCallbackImpl()
226 locationWindow.moveTo(camera: targetCamera, duration: 1500, animationType: .linear, callback: callback)
227 print("Started move to with linear animation")
228 // [swift_LocationWindow_moveTo]
230 // [swift_LocationWindow_moveTo_2]
231 // Move to position with cubic animation
232 let callback2 = CameraCallbackImpl()
233 locationWindow.moveTo(camera: targetCamera, duration: 1500, animationType: .cubic, callback: callback2)
234 print("Started move to with cubic animation")
235 // [swift_LocationWindow_moveTo_2]
237 // [swift_LocationWindow_moveTo_3]
238 // Move to position with sine animation
239 let callback3 = CameraCallbackImpl()
240 locationWindow.moveTo(camera: targetCamera, duration: 1500, animationType: .sine, callback: callback3)
241 print("Started move to with sine animation")
242 // [swift_LocationWindow_moveTo_3]
244 // [swift_LocationWindow_moveTo_4]
245 // Move to position without animation
246 let instantPoint = NCPoint(x: 300.0, y: 400.0)
247 let instantCamera = NCCamera(point: instantPoint, zoom: 25.0, rotation: 180.0)
248 let instantCallback = CameraCallbackImpl()
249 locationWindow.moveTo(camera: instantCamera, duration: 0, animationType: .none, callback: instantCallback)
250 print("Executed instant move to position (\(instantPoint.x), \(instantPoint.y))")
251 // [swift_LocationWindow_moveTo_4]
253 // Test move to with different animation types
254 // [swift_AnimationType_enum]
255 let animationTypes: [NCAnimationType] = [
262 // [swift_AnimationType_enum]
264 for (i, animationType) in animationTypes.enumerated() {
265 let testPoint = NCPoint(x: 100.0 + Double(i) * 50.0, y: 200.0 + Double(i) * 50.0)
266 let testCamera = NCCamera(point: testPoint, zoom: 50.0 + Double(i) * 10.0, rotation: Double(i) * 30.0)
267 let testCallback = CameraCallbackImpl()
268 locationWindow.moveTo(camera: testCamera, duration: 1000, animationType: animationType, callback: testCallback)
269 print("Move to test \(i): Animation \(animationType), Point (\(testPoint.x), \(testPoint.y))")
274 * Demonstrate zoom properties
276 private func demonstrateZoomProperties() {
277 print("--- Zoom Properties ---")
279 guard let locationWindow = locationWindow else {
280 print("LocationWindow not available yet")
284 // [swift_LocationWindow_getZoomFactor]
285 // Get current zoom factor
286 let currentZoom = locationWindow.zoomFactor
287 print("Current zoom factor: \(currentZoom) pixels per meter")
288 // [swift_LocationWindow_getZoomFactor]
290 // [swift_LocationWindow_setZoomFactor]
291 // Set new zoom factor
292 locationWindow.zoomFactor = 200.0
293 print("Set zoom factor to 200.0 pixels per meter")
294 // [swift_LocationWindow_setZoomFactor]
296 // [swift_LocationWindow_getMinZoomFactor]
297 // Get minimum zoom factor
298 let minZoom = locationWindow.minZoomFactor
299 print("Minimum zoom factor: \(minZoom) pixels per meter")
300 // [swift_LocationWindow_getMinZoomFactor]
302 // [swift_LocationWindow_getMaxZoomFactor]
303 // Get maximum zoom factor
304 let maxZoom = locationWindow.maxZoomFactor
305 print("Maximum zoom factor: \(maxZoom) pixels per meter")
306 // [swift_LocationWindow_getMaxZoomFactor]
308 // Test zoom factor changes
309 let zoomLevels: [Double] = [50.0, 100.0, 150.0, 300.0, 500.0, 800.0]
311 for zoom in zoomLevels {
312 locationWindow.zoomFactor = zoom
313 print("Set zoom factor to \(zoom) pixels per meter")
316 let actualZoom = locationWindow.zoomFactor
317 print("Actual zoom factor: \(actualZoom) pixels per meter")
322 * Demonstrate camera scenarios
324 private func demonstrateCameraScenarios() {
325 print("--- Camera Scenarios ---")
327 guard let locationWindow = locationWindow else {
328 print("LocationWindow not available yet")
332 // [swift_LocationWindow_camera_scenarios]
333 // Create cameras for different scenarios
334 let overviewCamera = NCCamera(point: NCPoint(x: 0.0, y: 0.0), zoom: 10.0, rotation: 0.0)
335 let detailCamera = NCCamera(point: NCPoint(x: 50.0, y: 75.0), zoom: 500.0, rotation: 0.0)
336 let angledCamera = NCCamera(point: NCPoint(x: 100.0, y: 150.0), zoom: 100.0, rotation: 30.0)
338 print("Created cameras for different scenarios:")
339 print(" Overview camera: zoom \(overviewCamera.zoom)")
340 print(" Detail camera: zoom \(detailCamera.zoom)")
341 print(" Angled camera: rotation \(angledCamera.rotation)°")
342 // [swift_LocationWindow_camera_scenarios]
344 // Test scenario transitions
345 let scenarios = [overviewCamera, detailCamera, angledCamera]
346 let scenarioNames = ["Overview", "Detail", "Angled"]
348 for (i, scenario) in scenarios.enumerated() {
349 let name = scenarioNames[i]
350 let scenarioCallback = CameraCallbackImpl()
351 locationWindow.moveTo(camera: scenario, duration: 2000, animationType: .cubic, callback: scenarioCallback)
352 print("Transitioning to \(name) scenario")
360 if let cameraListener = cameraListener, let locationWindow = locationWindow {
361 locationWindow.removeCameraListener(cameraListener)
362 self.cameraListener = nil
363 print("Camera listener removed")
369 * Camera listener implementation
371class CameraListenerImpl: NSObject, NCCameraListener {
372 // [swift_CameraListener_onCameraPositionChanged]
373 func onCameraPositionChanged(reason: NCCameraUpdateReason, finished: Bool) {
374 let reasonText = (reason == .gestures) ? "user gestures" : "application"
375 let statusText = finished ? "finished" : "in progress"
377 print("Camera position changed: \(reasonText), status: \(statusText)")
380 print("Camera movement completed")
383 // [swift_CameraListener_onCameraPositionChanged]
387 * Camera callback implementation
389class CameraCallbackImpl: NSObject, NCCameraCallback {
390 // [swift_CameraCallback_onMoveFinished]
391 func onMoveFinished(completed: Bool) {
393 print("Camera movement completed successfully")
395 print("Camera movement was cancelled")
398 // [swift_CameraCallback_onMoveFinished]
406 * Create camera for centered point with given zoom
408 static func createCenteredCamera(x: Double, y: Double, zoom: Double) -> NCCamera {
409 let point = NCPoint(x: x, y: y)
410 return NCCamera(point: point, zoom: zoom, rotation: 0.0)
414 * Create camera with rotation
416 static func createRotatedCamera(x: Double, y: Double, zoom: Double, rotation: Double) -> NCCamera {
417 let point = NCPoint(x: x, y: y)
418 return NCCamera(point: point, zoom: zoom, rotation: rotation)
422 * Validate camera position
424 static func isValidCamera(_ camera: NCCamera) -> Bool {
425 return camera.zoom > 0 &&
426 camera.rotation >= 0 && camera.rotation < 360
430 * Create camera for showing area
432 static func createAreaCamera(centerX: Double, centerY: Double, width: Double, height: Double) -> NCCamera {
433 // Calculate zoom based on area size
434 let zoom = 100.0 / max(width, height)
435 let point = NCPoint(x: centerX, y: centerY)
436 return NCCamera(point: point, zoom: zoom, rotation: 0.0)
440 * Create camera for showing path
442 static func createPathCamera(points: [NCPoint]) -> NCCamera {
444 let point = NCPoint(x: 0.0, y: 0.0)
445 return NCCamera(point: point, zoom: 100.0, rotation: 0.0)
449 let centerX = points.map { $0.x }.reduce(0, +) / Double(points.count)
450 let centerY = points.map { $0.y }.reduce(0, +) / Double(points.count)
452 // Calculate area size
453 let minX = points.map { $0.x }.min()!
454 let minY = points.map { $0.y }.min()!
455 let maxX = points.map { $0.x }.max()!
456 let maxY = points.map { $0.y }.max()!
458 let width = maxX - minX
459 let height = maxY - minY
461 return createAreaCamera(centerX: centerX, centerY: centerY, width: width, height: height)
465 * Create camera for showing multiple points
467 static func createMultiPointCamera(points: [NCPoint], padding: Double = 10.0) -> NCCamera {
469 let point = NCPoint(x: 0.0, y: 0.0)
470 return NCCamera(point: point, zoom: 100.0, rotation: 0.0)
474 let centerX = points.map { $0.x }.reduce(0, +) / Double(points.count)
475 let centerY = points.map { $0.y }.reduce(0, +) / Double(points.count)
477 // Calculate area size with padding
478 let minX = points.map { $0.x }.min()! - padding
479 let minY = points.map { $0.y }.min()! - padding
480 let maxX = points.map { $0.x }.max()! + padding
481 let maxY = points.map { $0.y }.max()! + padding
483 let width = maxX - minX
484 let height = maxY - minY
486 return createAreaCamera(centerX: centerX, centerY: centerY, width: width, height: height)
491 * Extensions for convenient camera operations
493extension NCLocationWindow {
497 func zoomTo(point: NCPoint, zoom: Double, duration: Int = 1000) {
498 let camera = NCCamera(point: point, zoom: zoom, rotation: 0.0)
499 let callback = CameraCallbackImpl()
500 moveTo(camera: camera, duration: duration, animationType: .cubic, callback: callback)
506 func panTo(point: NCPoint, duration: Int = 1000) {
507 let currentCamera = camera
508 let newCamera = NCCamera(point: point, zoom: currentCamera.zoom, rotation: currentCamera.rotation)
509 let callback = CameraCallbackImpl()
510 moveTo(camera: newCamera, duration: duration, animationType: .linear, callback: callback)
516 func rotateTo(rotation: Double, duration: Int = 1000) {
517 let currentCamera = camera
518 let newCamera = NCCamera(point: currentCamera.point, zoom: currentCamera.zoom, rotation: rotation)
519 let callback = CameraCallbackImpl()
520 moveTo(camera: newCamera, duration: duration, animationType: .sine, callback: callback)
524 * Fly to point with automatic zoom
526 func flyToPoint(point: NCPoint, duration: Int = 2000) {
527 let currentCamera = camera
528 let distance = sqrt(pow(point.x - currentCamera.point.x, 2) + pow(point.y - currentCamera.point.y, 2))
529 let newZoom = max(currentCamera.zoom * 0.8, min(currentCamera.zoom * 1.2, 100.0 / distance))
531 let targetCamera = NCCamera(point: point, zoom: newZoom, rotation: currentCamera.rotation)
532 let callback = CameraCallbackImpl()
533 flyTo(camera: targetCamera, duration: duration, callback: callback)
541 // Create example instance
542 let example = LocationWindowCameraExample()
544 // Assume we have NCLocationWindow
545 // let locationWindow: NCLocationWindow = getLocationWindow()
546 // example.locationWindow = locationWindow
548 // Demonstrate methods
549 print("=== LocationWindowCamera Examples ===")
554 print("Examples ready to use!")