From 2981e634d27cb969af13dbedb1ad36b35d1649d8 Mon Sep 17 00:00:00 2001 From: Matthieu Baumann Date: Mon, 6 May 2024 15:12:44 +0200 Subject: [PATCH] fix: call aladin methods that calls to wasm in the positionChanged callback --- examples/al-zoomchanged.html | 6 +++--- src/core/src/app.rs | 17 ++++++++++------- src/core/src/lib.rs | 16 +++++++++------- src/js/Aladin.js | 6 +++--- src/js/View.js | 11 +++++++---- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/examples/al-zoomchanged.html b/examples/al-zoomchanged.html index e219120ab..74b5744c9 100644 --- a/examples/al-zoomchanged.html +++ b/examples/al-zoomchanged.html @@ -14,12 +14,12 @@ aladin = A.aladin('#aladin-lite-div', {target: "30 0", fov: 360, fullScreen: true, cooFrame: 'galactic', showCooGridControl: true, showSimbadPointerControl: true, showCooGrid: true}); aladin.setProjection('AIT'); - aladin.on("zoomChanged", () => { console.log("zoomChanged") }) - aladin.on("positionChanged", ({ra, dec, dragging}) => { - console.log("positionChanged", ra, dec) + + aladin.on("positionChanged", ({ra, dec}) => { + }) aladin.gotoRaDec(0, 20); diff --git a/src/core/src/app.rs b/src/core/src/app.rs index efa4161b8..d12fb00ab 100644 --- a/src/core/src/app.rs +++ b/src/core/src/app.rs @@ -106,9 +106,8 @@ pub struct App { ack_send: async_channel::Sender, ack_recv: async_channel::Receiver, - // callbacks - callback_position_changed: js_sys::Function, + //callback_position_changed: js_sys::Function, } use cgmath::{Vector2, Vector3}; @@ -131,7 +130,7 @@ impl App { mut shaders: ShaderManager, resources: Resources, // Callbacks - callback_position_changed: js_sys::Function, + //callback_position_changed: js_sys::Function, ) -> Result { let gl = gl.clone(); //let exec = Rc::new(RefCell::new(TaskExecutor::new())); @@ -261,8 +260,7 @@ impl App { fits_recv, ack_send, ack_recv, - - callback_position_changed, + //callback_position_changed, }) } @@ -540,8 +538,12 @@ impl App { Ok(()) } - pub(crate) fn set_callback_position_changed(&mut self, callback: js_sys::Function) { + /*pub(crate) fn set_callback_position_changed(&mut self, callback: js_sys::Function) { self.callback_position_changed = callback; + }*/ + + pub(crate) fn is_inerting(&self) -> bool { + return self.inertia.is_some(); } pub(crate) fn update(&mut self, _dt: DeltaTime) -> Result { @@ -560,7 +562,7 @@ impl App { let cur_speed = inertia.get_cur_speed(); // Create the javascript object to pass to the callback - let args: js_sys::Object = js_sys::Object::new(); + /*let args: js_sys::Object = js_sys::Object::new(); let center = self.camera.get_center().lonlat(); js_sys::Reflect::set( &args, @@ -578,6 +580,7 @@ impl App { // Position has changed, we call the callback self.callback_position_changed .call1(&JsValue::null(), &args)?; + */ if cur_speed < thresh_speed { self.inertia = None; diff --git a/src/core/src/lib.rs b/src/core/src/lib.rs index 19f2bb6ca..b3a6ae0fe 100644 --- a/src/core/src/lib.rs +++ b/src/core/src/lib.rs @@ -166,13 +166,10 @@ impl WebClient { let shaders = ShaderManager::new(&gl, shaders).unwrap_abort(); // Event listeners callbacks - let callback_position_changed = js_sys::Function::new_no_args(""); + //let callback_position_changed = js_sys::Function::new_no_args(""); let app = App::new( - &gl, - aladin_div, - shaders, - resources, - callback_position_changed, + &gl, aladin_div, shaders, resources, + //callback_position_changed, )?; let dt = DeltaTime::zero(); @@ -182,9 +179,14 @@ impl WebClient { Ok(webclient) } - #[wasm_bindgen(js_name = setCallbackPositionChanged)] + /*#[wasm_bindgen(js_name = setCallbackPositionChanged)] pub fn set_callback_position_changed(&mut self, callback: js_sys::Function) { self.app.set_callback_position_changed(callback); + }*/ + + #[wasm_bindgen(js_name = isInerting)] + pub fn is_inerting(&self) -> bool { + return self.app.is_inerting(); } /// Update the view diff --git a/src/js/Aladin.js b/src/js/Aladin.js index c7943dea8..c9823afde 100644 --- a/src/js/Aladin.js +++ b/src/js/Aladin.js @@ -1773,7 +1773,7 @@ aladin.on('objectClicked', function(object, xyMouseCoords) { this.callbacksByEventName[what] = myFunction; - if (what === "positionChanged") { + /*if (what === "positionChanged") { // tell the backend about that callback // because it needs to be called when the inertia is done ALEvent.AL_USE_WASM.dispatchedTo(this.aladinDiv, {callback: (wasm) => { @@ -1782,9 +1782,9 @@ aladin.on('objectClicked', function(object, xyMouseCoords) { View.CALLBACKS_THROTTLE_TIME_MS, ); - wasm.setCallbackPositionChanged(myFunctionThrottled); + wasm.setCallbackPositionChanged(myFunctionThrottled.bind(this)); }}) - } + }*/ }; Aladin.prototype.addListener = function(alEventName, customFn) { diff --git a/src/js/View.js b/src/js/View.js index be1477de5..9b06005d0 100644 --- a/src/js/View.js +++ b/src/js/View.js @@ -124,7 +124,7 @@ export let View = (function () { () => { var posChangedFn = this.aladin.callbacksByEventName && this.aladin.callbacksByEventName['positionChanged']; if (typeof posChangedFn === 'function') { - var pos = this.aladin.pix2world(this.width / 2, this.height / 2); + var pos = this.aladin.pix2world(this.width / 2, this.height / 2, 'icrs'); if (pos !== undefined) { posChangedFn({ ra: pos[0], @@ -1246,9 +1246,12 @@ export let View = (function () { // Drawing code //try { this.moving = this.wasm.update(elapsedTime); - //} catch (e) { - // console.error(e) - //} + + // inertia run throttled position + if (this.moving && this.aladin.callbacksByEventName && this.aladin.callbacksByEventName['positionChanged'] && this.wasm.isInerting()) { + // run the trottled position + this.throttledPositionChanged(); + } ////// 2. Draw catalogues//////// const isViewRendering = this.wasm.isRendering();