diff --git a/src/js/Aladin.js b/src/js/Aladin.js index 860dc6fd..4f313c22 100644 --- a/src/js/Aladin.js +++ b/src/js/Aladin.js @@ -2082,6 +2082,8 @@ export let Aladin = (function () { "select", // deprecated, use objectsSelected instead "objectsSelected", + "regionSelected", + "objectClicked", "objectHovered", "objectHoveredStop", @@ -2257,6 +2259,10 @@ aladin.on("layerChanged", (layer, layerName, state) => { this.fire("selectstart", { mode, callback }); }; + Aladin.prototype.selectRegion = async function (region, callback) { + this.view.regionSelection(region, callback); + } + Aladin.prototype.fire = function (what, params) { if (what === "selectstart") { const { mode, callback } = params; diff --git a/src/js/FiniteStateMachine/CircleSelect.js b/src/js/FiniteStateMachine/CircleSelect.js index a66e0119..65e1abb4 100644 --- a/src/js/FiniteStateMachine/CircleSelect.js +++ b/src/js/FiniteStateMachine/CircleSelect.js @@ -107,9 +107,30 @@ export class CircleSelect extends FSM { if (typeof callback === "function") { let objList = Selector.getObjects(s, view); + view.selectObjects(objList); callback(objList); } + + var region_callback = view.aladin.callbacksByEventName['regionSelected']; + if (typeof region_callback === "function") { + let startCooWorld = view.aladin.pix2world(this.startCoo.x, this.startCoo.y); + let radius = view.aladin.angularDist( + this.startCoo.x, + this.startCoo.y, + this.coo.x, + this.coo.y + ); + + region_callback({ + type: "circle", + startCoo: { + ra: startCooWorld[0], + dec: startCooWorld[1] + }, + radius: radius, + }); + } } // execute selection callback only @@ -134,6 +155,27 @@ export class CircleSelect extends FSM { view.aladin.removeStatusBarMessage('selector') }; + let api = (params) => { + let startCoo = params["startCoo"]; + let endCoo = params["endCoo"]; + + let startCooPix = view.aladin.world2pix(startCoo["ra"], startCoo["dec"]); + let endCooPix = view.aladin.world2pix(endCoo["ra"], endCoo["dec"]); + + + this.startCoo = { + x: startCooPix[0], + y: startCooPix[1], + }; + + this.dispatch('mouseup', { + coo: { + x: endCooPix[0], + y: endCooPix[1], + }, + }); + } + super({ state: 'off', transitions: { @@ -141,7 +183,8 @@ export class CircleSelect extends FSM { start, }, start: { - mousedown + mousedown, + api, }, mousedown: { mousemove @@ -162,6 +205,9 @@ export class CircleSelect extends FSM { }, mouseup: { off, + }, + api: { + mouseup } } }) diff --git a/src/js/FiniteStateMachine/PolySelect.js b/src/js/FiniteStateMachine/PolySelect.js index d5c4a52a..5f9537b7 100644 --- a/src/js/FiniteStateMachine/PolySelect.js +++ b/src/js/FiniteStateMachine/PolySelect.js @@ -203,8 +203,25 @@ export class PolySelect extends FSM { var callback = view.aladin.callbacksByEventName['objectsSelected'] || view.aladin.callbacksByEventName['select']; if (typeof callback === "function") { let objList = Selector.getObjects(s, view); + + view.selectObjects(objList); callback(objList); } + + var region_callback = view.aladin.callbacksByEventName['regionSelected']; + if (typeof region_callback === "function") { + + region_callback({ + type: "poly", + coos: this.coos.map((coo) => { + let cooWorld = view.aladin.pix2world(coo.x, coo.y); + return { + ra: cooWorld[0], + dec: cooWorld[1], + }; + }), + }); + } } this.coos = []; @@ -216,6 +233,17 @@ export class PolySelect extends FSM { this.dispatch('off'); }; + let api = (params) => { + this.coos = params["coos"].map((coo) => { + let cooPix = view.aladin.world2pix(coo["ra"], coo["dec"]); + return { + x: cooPix[0], + y: cooPix[1], + }; + }); + this.dispatch("finish"); + } + let fsm; if (Utils.hasTouchScreen()) { let mousedown = click; @@ -270,7 +298,8 @@ export class PolySelect extends FSM { start, }, start: { - click + click, + api, }, click: { //mouseout, @@ -295,6 +324,9 @@ export class PolySelect extends FSM { }, finish: { off + }, + api: { + finish } } } diff --git a/src/js/FiniteStateMachine/RectSelect.js b/src/js/FiniteStateMachine/RectSelect.js index 608b47af..bc1d81d2 100644 --- a/src/js/FiniteStateMachine/RectSelect.js +++ b/src/js/FiniteStateMachine/RectSelect.js @@ -116,6 +116,39 @@ export class RectSelect extends FSM { view.selectObjects(objList); callback(objList); } + + var region_callback = view.aladin.callbacksByEventName['regionSelected']; + if (typeof region_callback === "function") { + let coos = [ + [ + Math.min(this.startCoo.x, this.coo.x), + Math.min(this.startCoo.y, this.coo.y), + ], + [ + Math.max(this.startCoo.x, this.coo.x), + Math.min(this.startCoo.y, this.coo.y), + ], + [ + Math.max(this.startCoo.x, this.coo.x), + Math.max(this.startCoo.y, this.coo.y), + ], + [ + Math.min(this.startCoo.x, this.coo.x), + Math.max(this.startCoo.y, this.coo.y), + ], + ]; + + region_callback({ + type: "rect", + coos: coos.map((coo) => { + let cooWorld = view.aladin.pix2world(coo[0], coo[1]); + return { + ra: cooWorld[0], + dec: cooWorld[1], + }; + }), + }); + } } this.dispatch('off'); @@ -139,6 +172,26 @@ export class RectSelect extends FSM { } }; + let api = (params) => { + let startCoo = params["startCoo"]; + let endCoo = params["endCoo"]; + + let startCooPix = view.aladin.world2pix(startCoo["ra"], startCoo["dec"]); + let endCooPix = view.aladin.world2pix(endCoo["ra"], endCoo["dec"]); + + this.startCoo = { + x: startCooPix[0], + y: startCooPix[1], + } + + selector.dispatch("mouseup", { + coo: { + x: endCooPix[0], + y: endCooPix[1], + }, + }); + } + super({ state: 'off', transitions: { @@ -149,7 +202,8 @@ export class RectSelect extends FSM { mousedown, mouseup, mouseout, - off + off, + api, }, mousedown: { mousemove, @@ -173,6 +227,9 @@ export class RectSelect extends FSM { }, mouseup: { off, + }, + api: { + mouseup, } } }) diff --git a/src/js/View.js b/src/js/View.js index edcf1f5e..08fcfaa8 100644 --- a/src/js/View.js +++ b/src/js/View.js @@ -443,6 +443,11 @@ export let View = (function () { this.selector.start(mode, callback); } + View.prototype.regionSelection = function(region, callback) { + this.selector.start(region["selection_type"], callback); + this.selector.dispatch("api", region); + } + View.prototype.setMode = function (mode) { this.mode = mode;