Files
aladin-lite/src/js/Selector.js
T

175 lines
5.3 KiB
JavaScript

// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright 2013 - UDS/CNRS
// The Aladin Lite program is distributed under the terms
// of the GNU Lesser General Public License version 3
// or (at your option) any later version.
//
// This file is part of Aladin Lite.
//
// Aladin Lite is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Aladin Lite is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Aladin Lite. If not, see <https://www.gnu.org/licenses/>.
//
import { Color } from "./Color";
import { CircleSelect } from "./FiniteStateMachine/CircleSelect";
import { PolySelect } from "./FiniteStateMachine/PolySelect";
import { LineSelect } from "./FiniteStateMachine/LineSelect";
import { RectSelect } from "./FiniteStateMachine/RectSelect";
import { ALEvent } from "./events/ALEvent";
/******************************************************************************
* Aladin Lite project
*
* Class Selector
*
* A selector
*
* Author: Matthieu Baumann[CDS]
*
*****************************************************************************/
export class Selector {
// constructor
constructor(view, options) {
this.customColor = false;
this.color = options && options.color;
if (this.color) {
this.customColor = true;
}
this.lineWidth = (options && options.lineWidth) || 2;
this.select = null;
this.view = view;
this._addListeners(view.aladin)
};
_addListeners(aladin) {
let self = this;
ALEvent.RETICLE_CHANGED.listenedBy(aladin.aladinDiv, function (e) {
if (!self.customColor) {
let reticleColor = e.detail.color;
// take the color of the reticle
self.color = new Color(reticleColor).toHex();
}
})
}
start(mode, callback) {
this.view.aladin.addStatusBarMessage({
id: 'selector',
message: 'You entered the selection mode',
type: 'info'
})
let options = {
color: this.color,
lineWidth: this.lineWidth
};
if (mode === 'circle') {
this.select = new CircleSelect(options, this.view)
} else if (mode === 'rect') {
this.select = new RectSelect(options, this.view)
} else if (mode === 'poly') {
this.select = new PolySelect(options, this.view)
} else if (mode === 'line') {
this.select = new LineSelect(options, this.view)
}
this.dispatch('start', {callback})
}
cancel() {
this.select && this.dispatch('off')
}
dispatch(to, params) {
this.select.dispatch(to, params);
}
static getObjects(selection, view) {
if (!selection) {
return;
}
if (!selection.contains) {
// contains must be implemented for the region
return;
}
const bbox = selection.bbox();
var objList = [];
var cat, sources, s;
var objListPerCatalog = [];
if (view.catalogs) {
for (var k = 0; k < view.catalogs.length; k++) {
cat = view.catalogs[k];
if (!cat.isShowing) {
continue;
}
sources = cat.getSources();
for (var l = 0; l < sources.length; l++) {
s = sources[l];
if (!s.isShowing || !s.x || !s.y) {
continue;
}
// footprints
if (s.isFootprint() && s.tooSmallFootprint === false) {
if (s.footprint.intersectsBBox(bbox.x, bbox.y, bbox.w, bbox.h, view)) {
objListPerCatalog.push(s);
}
continue;
}
if (selection.contains(s)) {
objListPerCatalog.push(s);
}
}
if (objListPerCatalog.length > 0) {
objList.push(objListPerCatalog);
}
objListPerCatalog = [];
}
}
if (view.overlays) {
for (var k = 0; k < view.overlays.length; k++) {
let overlay = view.overlays[k];
if (!overlay.isShowing) {
continue;
}
var overlayItems = overlay.overlayItems;
for (var l = 0; l < overlayItems.length; l++) {
let o = overlayItems[l];
if (!o.isShowing) {
continue;
}
if (o.intersectsBBox(bbox.x, bbox.y, bbox.w, bbox.h, view)) {
objList.push([o]);
}
}
}
}
return objList;
}
}