From 98a374bdb2586fcd389321c5b70c13550d721324 Mon Sep 17 00:00:00 2001 From: ope dada <85258317+OpeDada@users.noreply.github.com> Date: Mon, 10 Feb 2025 10:19:39 +0900 Subject: [PATCH] refactor(web): plugin playground presets - camera position (#1415) --- .../Plugins/presets/camera/cameraPosition.ts | 306 +++++++++++------- 1 file changed, 194 insertions(+), 112 deletions(-) diff --git a/web/src/beta/features/PluginPlayground/Plugins/presets/camera/cameraPosition.ts b/web/src/beta/features/PluginPlayground/Plugins/presets/camera/cameraPosition.ts index 07681c08d..74b00d6ae 100644 --- a/web/src/beta/features/PluginPlayground/Plugins/presets/camera/cameraPosition.ts +++ b/web/src/beta/features/PluginPlayground/Plugins/presets/camera/cameraPosition.ts @@ -12,11 +12,6 @@ extensions: type: widget name: Camera Position Widget description: Widget for controlling camera position - widgetLayout: - defaultLocation: - zone: outer - section: center - area: top `, disableEdit: true, disableDelete: true @@ -36,25 +31,33 @@ const widgetFile: FileType = { reearth.ui.show(\` ${PRESET_PLUGIN_COMMON_STYLE}
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - +
+
+ +
-
- +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+
-
+ + // Send message to apply camera position + parent.postMessage({ + type: 'applyCameraPosition', + position: cameraParams + }, '*'); + }); + + // Handle messages from extension + window.addEventListener('message', (e) => { + if (e.data.type === 'currentPosition') { + // Only update if not preserving manual input + if (!preserveManualInput) { + const position = e.data.position; + + // Update input fields + Object.keys(inputs).forEach(key => { + if (position[key] !== undefined) { + inputs[key].value = Number(position[key]).toFixed(4); + } else { + inputs[key].value = ''; + } + }); + + // Recheck inputs after automatic update + checkInputs(); + } else { + // Reset the flag after one update + preserveManualInput = false; + } + } else if (e.data.type === 'positionApplied') { + // Show status message for successful application + statusMessage.textContent = 'Camera position applied!'; + } + }); + \`); reearth.camera.on("move", (camera) => { @@ -218,21 +290,31 @@ reearth.extension.on('message', (msg) => { if (msg.type === 'applyCameraPosition') { const params = msg.position; - reearth.camera.flyTo( - params, - { - duration: 0, - easing: (t) => t * t - } + // Get current camera position before flying + const currentPosition = reearth.camera.position || {}; + + // Check if the new position is actually different + const isPositionChanged = Object.keys(params).some(key => + //Use a tolerance threshold to compare floating-point values + Math.abs((currentPosition[key] || 0) - (params[key] || 0)) > 0.0001 ); - // Send confirmation message - reearth.ui.postMessage({ - type: 'positionApplied' - }); + if (isPositionChanged) { + reearth.camera.flyTo( + params, + { + duration: 0, + easing: (t) => t * t + } + ); + + // Send confirmation message + reearth.ui.postMessage({ + type: 'positionApplied' + }); + } } -}); - ` +});` }; export const cameraPosition: PluginType = {