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 let animationTypes: [NCAnimationType] = [
262 for (i, animationType) in animationTypes.enumerated() {
263 let testPoint = NCPoint(x: 100.0 + Double(i) * 50.0, y: 200.0 + Double(i) * 50.0)
264 let testCamera = NCCamera(point: testPoint, zoom: 50.0 + Double(i) * 10.0, rotation: Double(i) * 30.0)
265 let testCallback = CameraCallbackImpl()
266 locationWindow.moveTo(camera: testCamera, duration: 1000, animationType: animationType, callback: testCallback)
267 print("Move to test \(i): Animation \(animationType), Point (\(testPoint.x), \(testPoint.y))")
272 * Demonstrate zoom properties
274 private func demonstrateZoomProperties() {
275 print("--- Zoom Properties ---")
277 guard let locationWindow = locationWindow else {
278 print("LocationWindow not available yet")
282 // [swift_LocationWindow_getZoomFactor]
283 // Get current zoom factor
284 let currentZoom = locationWindow.zoomFactor
285 print("Current zoom factor: \(currentZoom) pixels per meter")
286 // [swift_LocationWindow_getZoomFactor]
288 // [swift_LocationWindow_setZoomFactor]
289 // Set new zoom factor
290 locationWindow.zoomFactor = 200.0
291 print("Set zoom factor to 200.0 pixels per meter")
292 // [swift_LocationWindow_setZoomFactor]
294 // [swift_LocationWindow_getMinZoomFactor]
295 // Get minimum zoom factor
296 let minZoom = locationWindow.minZoomFactor
297 print("Minimum zoom factor: \(minZoom) pixels per meter")
298 // [swift_LocationWindow_getMinZoomFactor]
300 // [swift_LocationWindow_getMaxZoomFactor]
301 // Get maximum zoom factor
302 let maxZoom = locationWindow.maxZoomFactor
303 print("Maximum zoom factor: \(maxZoom) pixels per meter")
304 // [swift_LocationWindow_getMaxZoomFactor]
306 // Test zoom factor changes
307 let zoomLevels: [Double] = [50.0, 100.0, 150.0, 300.0, 500.0, 800.0]
309 for zoom in zoomLevels {
310 locationWindow.zoomFactor = zoom
311 print("Set zoom factor to \(zoom) pixels per meter")
314 let actualZoom = locationWindow.zoomFactor
315 print("Actual zoom factor: \(actualZoom) pixels per meter")
320 * Demonstrate camera scenarios
322 private func demonstrateCameraScenarios() {
323 print("--- Camera Scenarios ---")
325 guard let locationWindow = locationWindow else {
326 print("LocationWindow not available yet")
330 // [swift_LocationWindow_camera_scenarios]
331 // Create cameras for different scenarios
332 let overviewCamera = NCCamera(point: NCPoint(x: 0.0, y: 0.0), zoom: 10.0, rotation: 0.0)
333 let detailCamera = NCCamera(point: NCPoint(x: 50.0, y: 75.0), zoom: 500.0, rotation: 0.0)
334 let angledCamera = NCCamera(point: NCPoint(x: 100.0, y: 150.0), zoom: 100.0, rotation: 30.0)
336 print("Created cameras for different scenarios:")
337 print(" Overview camera: zoom \(overviewCamera.zoom)")
338 print(" Detail camera: zoom \(detailCamera.zoom)")
339 print(" Angled camera: rotation \(angledCamera.rotation)°")
340 // [swift_LocationWindow_camera_scenarios]
342 // Test scenario transitions
343 let scenarios = [overviewCamera, detailCamera, angledCamera]
344 let scenarioNames = ["Overview", "Detail", "Angled"]
346 for (i, scenario) in scenarios.enumerated() {
347 let name = scenarioNames[i]
348 let scenarioCallback = CameraCallbackImpl()
349 locationWindow.moveTo(camera: scenario, duration: 2000, animationType: .cubic, callback: scenarioCallback)
350 print("Transitioning to \(name) scenario")
358 if let cameraListener = cameraListener, let locationWindow = locationWindow {
359 locationWindow.removeCameraListener(cameraListener)
360 self.cameraListener = nil
361 print("Camera listener removed")
367 * Camera listener implementation
369class CameraListenerImpl: NSObject, NCCameraListener {
370 // [swift_CameraListener_onCameraPositionChanged]
371 func onCameraPositionChanged(reason: NCCameraUpdateReason, finished: Bool) {
372 let reasonText = (reason == .gestures) ? "user gestures" : "application"
373 let statusText = finished ? "finished" : "in progress"
375 print("Camera position changed: \(reasonText), status: \(statusText)")
378 print("Camera movement completed")
381 // [swift_CameraListener_onCameraPositionChanged]
385 * Camera callback implementation
387class CameraCallbackImpl: NSObject, NCCameraCallback {
388 func onMoveFinished(completed: Bool) {
390 print("Camera movement completed successfully")
392 print("Camera movement was cancelled")
402 * Create camera for centered point with given zoom
404 static func createCenteredCamera(x: Double, y: Double, zoom: Double) -> NCCamera {
405 let point = NCPoint(x: x, y: y)
406 return NCCamera(point: point, zoom: zoom, rotation: 0.0)
410 * Create camera with rotation
412 static func createRotatedCamera(x: Double, y: Double, zoom: Double, rotation: Double) -> NCCamera {
413 let point = NCPoint(x: x, y: y)
414 return NCCamera(point: point, zoom: zoom, rotation: rotation)
418 * Validate camera position
420 static func isValidCamera(_ camera: NCCamera) -> Bool {
421 return camera.zoom > 0 &&
422 camera.rotation >= 0 && camera.rotation < 360
426 * Create camera for showing area
428 static func createAreaCamera(centerX: Double, centerY: Double, width: Double, height: Double) -> NCCamera {
429 // Calculate zoom based on area size
430 let zoom = 100.0 / max(width, height)
431 let point = NCPoint(x: centerX, y: centerY)
432 return NCCamera(point: point, zoom: zoom, rotation: 0.0)
436 * Create camera for showing path
438 static func createPathCamera(points: [NCPoint]) -> NCCamera {
440 let point = NCPoint(x: 0.0, y: 0.0)
441 return NCCamera(point: point, zoom: 100.0, rotation: 0.0)
445 let centerX = points.map { $0.x }.reduce(0, +) / Double(points.count)
446 let centerY = points.map { $0.y }.reduce(0, +) / Double(points.count)
448 // Calculate area size
449 let minX = points.map { $0.x }.min()!
450 let minY = points.map { $0.y }.min()!
451 let maxX = points.map { $0.x }.max()!
452 let maxY = points.map { $0.y }.max()!
454 let width = maxX - minX
455 let height = maxY - minY
457 return createAreaCamera(centerX: centerX, centerY: centerY, width: width, height: height)
461 * Create camera for showing multiple points
463 static func createMultiPointCamera(points: [NCPoint], padding: Double = 10.0) -> NCCamera {
465 let point = NCPoint(x: 0.0, y: 0.0)
466 return NCCamera(point: point, zoom: 100.0, rotation: 0.0)
470 let centerX = points.map { $0.x }.reduce(0, +) / Double(points.count)
471 let centerY = points.map { $0.y }.reduce(0, +) / Double(points.count)
473 // Calculate area size with padding
474 let minX = points.map { $0.x }.min()! - padding
475 let minY = points.map { $0.y }.min()! - padding
476 let maxX = points.map { $0.x }.max()! + padding
477 let maxY = points.map { $0.y }.max()! + padding
479 let width = maxX - minX
480 let height = maxY - minY
482 return createAreaCamera(centerX: centerX, centerY: centerY, width: width, height: height)
487 * Extensions for convenient camera operations
489extension NCLocationWindow {
493 func zoomTo(point: NCPoint, zoom: Double, duration: Int = 1000) {
494 let camera = NCCamera(point: point, zoom: zoom, rotation: 0.0)
495 let callback = CameraCallbackImpl()
496 moveTo(camera: camera, duration: duration, animationType: .cubic, callback: callback)
502 func panTo(point: NCPoint, duration: Int = 1000) {
503 let currentCamera = camera
504 let newCamera = NCCamera(point: point, zoom: currentCamera.zoom, rotation: currentCamera.rotation)
505 let callback = CameraCallbackImpl()
506 moveTo(camera: newCamera, duration: duration, animationType: .linear, callback: callback)
512 func rotateTo(rotation: Double, duration: Int = 1000) {
513 let currentCamera = camera
514 let newCamera = NCCamera(point: currentCamera.point, zoom: currentCamera.zoom, rotation: rotation)
515 let callback = CameraCallbackImpl()
516 moveTo(camera: newCamera, duration: duration, animationType: .sine, callback: callback)
520 * Fly to point with automatic zoom
522 func flyToPoint(point: NCPoint, duration: Int = 2000) {
523 let currentCamera = camera
524 let distance = sqrt(pow(point.x - currentCamera.point.x, 2) + pow(point.y - currentCamera.point.y, 2))
525 let newZoom = max(currentCamera.zoom * 0.8, min(currentCamera.zoom * 1.2, 100.0 / distance))
527 let targetCamera = NCCamera(point: point, zoom: newZoom, rotation: currentCamera.rotation)
528 let callback = CameraCallbackImpl()
529 flyTo(camera: targetCamera, duration: duration, callback: callback)
537 // Create example instance
538 let example = LocationWindowCameraExample()
540 // Assume we have NCLocationWindow
541 // let locationWindow: NCLocationWindow = getLocationWindow()
542 // example.locationWindow = locationWindow
544 // Demonstrate methods
545 print("=== LocationWindowCamera Examples ===")
550 print("Examples ready to use!")