Skip to content

Commit

Permalink
🎨 Improve target and fov control flow using new Lock object
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Apr 30, 2024
1 parent a6708df commit d90e730
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion js/imports.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import A from "https://esm.sh/aladin-lite@3.3.3-beta";
import A from "https://esm.sh/aladin-lite@3.4.0-beta";

export default A;
31 changes: 7 additions & 24 deletions js/models/event_handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MessageHandler from "./message_handler";
import Lock from "./lock";

export default class EventHandler {
/**
Expand Down Expand Up @@ -30,16 +31,12 @@ export default class EventHandler {
// is also necessary for the field of view.

/* Target control */
let targetJs = false;
let targetPy = false;
let targetLock = new Lock();

// Event triggered when the user moves the map in Aladin Lite
this.aladin.on("positionChanged", () => {
if (targetPy) {
targetPy = false;
return;
}
targetJs = true;
if (targetLock.unlock()) return;
targetLock.lock();
const raDec = this.aladin.getRaDec();
this.model.set("_target", `${raDec[0]} ${raDec[1]}`);
this.model.set("shared_target", `${raDec[0]} ${raDec[1]}`);
Expand All @@ -48,38 +45,24 @@ export default class EventHandler {

// Event triggered when the target is changed from the Python side using jslink
this.model.on("change:shared_target", () => {
if (targetJs) {
targetJs = false;
return;
}
targetPy = true;
const target = this.model.get("shared_target");
const [ra, dec] = target.split(" ");
this.aladin.gotoRaDec(ra, dec);
});

/* Field of View control */
let fovJs = false;
let fovPy = false;
let fovLock = new Lock();

this.aladin.on("zoomChanged", (fov) => {
if (fovPy) {
fovPy = false;
return;
}
fovJs = true;
if (fovLock.unlock()) return;
fovLock.lock();
// fov MUST be cast into float in order to be sent to the model
this.model.set("_fov", parseFloat(fov.toFixed(5)));
this.model.set("shared_fov", parseFloat(fov.toFixed(5)));
this.model.save_changes();
});

this.model.on("change:shared_fov", () => {
if (fovJs) {
fovJs = false;
return;
}
fovPy = true;
let fov = this.model.get("shared_fov");
this.aladin.setFoV(fov);
});
Expand Down
27 changes: 27 additions & 0 deletions js/models/lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default class Lock {
state = false;

/**
* Locks the object
* @returns {boolean} True if the object was locked, false otherwise
*/
unlock() {
if (this.state) {
this.state = false;
return true;
}
return false;
}

/**
* Unlocks the object
* @returns {boolean} True if the object was unlocked, false otherwise
*/
lock() {
if (!this.state) {
this.state = true;
return true;
}
return false;
}
}

0 comments on commit d90e730

Please sign in to comment.