Skip to content

Commit

Permalink
fix: call aladin methods that calls to wasm in the positionChanged ca…
Browse files Browse the repository at this point in the history
…llback
  • Loading branch information
bmatthieu3 committed May 6, 2024
1 parent 5e8d0ac commit 2981e63
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
6 changes: 3 additions & 3 deletions examples/al-zoomchanged.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 10 additions & 7 deletions src/core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ pub struct App {

ack_send: async_channel::Sender<ImageParams>,
ack_recv: async_channel::Receiver<ImageParams>,

// callbacks
callback_position_changed: js_sys::Function,
//callback_position_changed: js_sys::Function,
}

use cgmath::{Vector2, Vector3};
Expand All @@ -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<Self, JsValue> {
let gl = gl.clone();
//let exec = Rc::new(RefCell::new(TaskExecutor::new()));
Expand Down Expand Up @@ -261,8 +260,7 @@ impl App {
fits_recv,
ack_send,
ack_recv,

callback_position_changed,
//callback_position_changed,
})
}

Expand Down Expand Up @@ -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<bool, JsValue> {
Expand All @@ -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,
Expand All @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions src/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/js/Aladin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) {
Expand Down
11 changes: 7 additions & 4 deletions src/js/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2981e63

Please sign in to comment.