awv3
Version:
⚡ AWV3 embedded CAD
138 lines (122 loc) • 5.08 kB
JavaScript
import Object3 from '../../three/object3';
import { MaterialSelector } from './materialselector';
import { ObjectSelector } from './objectselector';
/**
* @class Selector activates/deactivates selection for selection-elements in the UI.
* Creates a new interaction using the type-filter provided with the selection-element.
* Forwards events to the current selector (material- or object-selector). Provides
* functions to access selected ids and elements as well as to check if a certain
* element is already selected. Selected elements can be materials or objects.
* Activation/deactivation is triggered by observing the state of state.globals.activeSelection.
*/
export class Selector {
constructor(session, options) {
this.options = {
...options
};
this.session = session;
// Wait until the sessions pool has been added into the view (happens in <App/>)
this.session.pool.viewFound().then(view => this.view = view);
// the current selector
this.currentSelector = undefined;
this.objectSelector = new ObjectSelector(session);
this.materialSelector = new MaterialSelector(session);
// store the element for activation/deactivation purposes
this.element = undefined;
// observe when a selection-element is activated
this.session.observe(state => state.globals.activeSelection, id => {
if (id !== undefined) {
let element = this.session.elements[id]; //session stores all elements in hashmap
// Deactivate previous element/selector
if (this.element && this.element !== element) {
this.deactivate();
}
// activate new element
if (element.types && element.types.indexOf('Object') > -1) {
this.currentSelector = this.objectSelector;
} else {
this.currentSelector = this.materialSelector;
}
this.activate(element);
} else {
this.deactivate();
}
});
}
/**
* Deactivates the selection.
*/
deactivate() {
this.session.pool.scene.removeInteraction();
this.view.interaction.filter = undefined;
this.view.setCursor();
this.currentSelector.deactivate();
this.element = undefined;
}
/**
* Activates the selection for the given selection-element. Creates an interaction
* based on the element's type-filter.
*/
activate(element) {
this.element = element;
this.currentSelector.activate(element);
this.view.interaction.filter = [this.session.pool.scene];
let types = ['Scene'];
if (Array.isArray(element.types)) types = [...types, ...element.types];
else if (typeof element.types === 'function')
types = object => object.type === 'Scene' || element.types(object);
// Interaction on pool
this.session.pool.scene.createInteraction({ recursive: true, types }).on({
[Object3.Events.Interaction.Hovered]: event => {
this.currentSelector.hover(event);
},
[Object3.Events.Interaction.Unhovered]: event => {
this.currentSelector.unhover(event);
},
[Object3.Events.Interaction.Clicked]: event => {
this.currentSelector.clicked(event);
},
[Object3.Events.Interaction.Missed]: event => {
this.currentSelector.missed(event);
},
[Object3.Events.Lifecycle.Rendered]: () => {
this.currentSelector.rendered(event);
}
});
}
/**
* Check if element is already selected
* @return {bool} TRUE if selected, FALSE otherwise
*/
isSelected(element) {
return this.currentSelector ? this.currentSelector.isSelected(element) : false;
}
/**
* Returns the selected ids in an array
* @return {array} selectedIds - Array with the selected ids
*/
getSelectedIds() {
return this.currentSelector ? this.currentSelector.getSelectedIds() : [];
}
/**
* Returns the selected elements in an array
* @return {array} selectedElements - Array with the selected elements
*/
getSelectedElements() {
return this.currentSelector ? this.currentSelector.getSelectedElements() : [];
}
/**
* Removes the given elements from the current selection.
* @param {array} elements - elements to be removed from the selection.
*/
unselectElements(elements) {
this.currentSelector && this.currentSelector.unselectElements(elements);
}
/**
* Removes all elements from the current selection.
* @param {Boolean} keepLocals - If true, selected ids are kept within the selection-element
*/
removeAll(removeElements = true) {
this.currentSelector && this.currentSelector.removeAll(removeElements);
}
}