@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
51 lines (50 loc) • 1.42 kB
JavaScript
"use strict";
import { isBooleanTrue } from "../../../../../core/BooleanValue";
export class EntitySelectionHelper {
constructor(node) {
this.node = node;
this.selectedState = /* @__PURE__ */ new Map();
this._entitiesCount = 0;
this._selectedEntitiesCount = 0;
}
init(entities) {
this.selectedState.clear();
for (const entity of entities) {
this.selectedState.set(entity, false);
}
this._entitiesCount = entities.length;
this._selectedEntitiesCount = 0;
}
select(entity) {
const state = this.selectedState.get(entity);
if (state != null) {
if (state == false) {
this.selectedState.set(entity, true);
this._selectedEntitiesCount++;
}
}
}
entitiesToKeep() {
return this._entitiesForState(isBooleanTrue(this.node.pv.invert));
}
entitiesToDelete() {
return this._entitiesForState(!isBooleanTrue(this.node.pv.invert));
}
_entitiesForState(state) {
const requiredState = state ? true : false;
const arraySize = state ? this._selectedEntitiesCount : this._entitiesCount - this._selectedEntitiesCount;
if (arraySize == 0) {
return [];
} else {
const array = new Array(arraySize);
let i = 0;
this.selectedState.forEach((state2, entity) => {
if (state2 == requiredState) {
array[i] = entity;
i++;
}
});
return array;
}
}
}