awv3
Version:
⚡ AWV3 embedded CAD
248 lines (201 loc) • 7.76 kB
JavaScript
import * as THREE from 'three';
import Object3 from '../../../three/object3';
import Ccref from '../ccref';
import Snapper from '../snapper';
var BaseHandler =
/*#__PURE__*/
function () {
function BaseHandler(sketcher, name) {
var _this = this;
this.sketcher = sketcher;
this.sketch = sketcher.activeSketch;
this.name = name;
this.interactions = new Map();
this.additionalInteractionTypes = [];
this.destroyed = false; // bind this.on* to this and don't trigger event callbacks after destruction
var onKeyUp = this.onKeyUp,
onKeyDown = this.onKeyDown,
onMouseMove = this.onMouseMove;
this.onKeyUp = function (event) {
return _this.destroyed || onKeyUp.call(_this, event);
};
this.onKeyDown = function (event) {
return _this.destroyed || onKeyDown.call(_this, event);
};
if (this.onMouseMove) this.onMouseMove = function (event) {
return _this.destroyed || onMouseMove.call(_this, event);
};
}
var _proto = BaseHandler.prototype;
_proto.destroy = function destroy(options) {
this.destroyed = true;
this.removeInteractions(options);
};
_proto.filterObjectsWithInteraction = function filterObjectsWithInteraction(object) {
return false;
};
_proto.addInteraction = function addInteraction(object) {
if (!this.filterObjectsWithInteraction(object)) return;
var interactionListeners = {};
var _arr = Object.values(Object3.Events.Interaction);
for (var _i = 0; _i < _arr.length; _i++) {
var event = _arr[_i];
if (this[event]) interactionListeners[event] = this[event].bind(this, object);
}
this.interactions.set(object.id, interactionListeners);
object.graphics.createInteraction({
recursive: true,
first: false,
types: ['InfinitePlane', 'SketcherMesh'].concat(this.additionalInteractionTypes)
}).on(interactionListeners, {
sync: true
}); // prioritize sketch points in interaction if there is overlapping
if (object instanceof Ccref && object.isPoint()) object.graphics.interaction.priority = 1; //TODO: remove "object instanceof Ccref" after implementing global Ccref.
if (!(object instanceof Ccref)) object.graphics.interaction.priority = 1;
}; //May be overridden in descendants for customization interaction objects.
_proto.addInteractions = function addInteractions() {
this.addInteractionFor(this.sketch.descendants);
};
_proto.addInteractionFor = function addInteractionFor(objs) {
for (var _iterator = objs, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i2 >= _iterator.length) break;
_ref = _iterator[_i2++];
} else {
_i2 = _iterator.next();
if (_i2.done) break;
_ref = _i2.value;
}
var _object = _ref;
this.addInteraction(_object);
}
this.sketcher.view.input.on('keyup', this.onKeyUp);
this.sketcher.view.input.on('keydown', this.onKeyDown);
if (this.onMouseMove) this.sketcher.view.input.on('mousemove', this.onMouseMove);
};
_proto.removeInteraction = function removeInteraction(object) {
var interactionListeners = this.interactions.get(object.id);
this.interactions.delete(object.id);
if (object.graphics) {
object.graphics.removeListener(interactionListeners);
object.graphics.removeInteraction();
}
};
_proto.removeInteractions = function removeInteractions() {
var sketchObjs = [];
for (var _iterator2 = this.interactions.keys(), _isArray2 = Array.isArray(_iterator2), _i3 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i3 >= _iterator2.length) break;
_ref2 = _iterator2[_i3++];
} else {
_i3 = _iterator2.next();
if (_i3.done) break;
_ref2 = _i3.value;
}
var _id = _ref2;
sketchObjs.push(new Ccref(this.sketcher, _id));
}
this.removeInteractionFor(sketchObjs);
};
_proto.removeInteractionFor = function removeInteractionFor(objects) {
if (this.onMouseMove) this.sketcher.view.input.removeListener('mousemove', this.onMouseMove);
this.sketcher.view.input.removeListener('keydown', this.onKeyDown);
this.sketcher.view.input.removeListener('keyup', this.onKeyUp);
for (var _iterator3 = objects, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref3;
if (_isArray3) {
if (_i4 >= _iterator3.length) break;
_ref3 = _iterator3[_i4++];
} else {
_i4 = _iterator3.next();
if (_i4.done) break;
_ref3 = _i4.value;
}
var _obj = _ref3;
this.removeInteraction(_obj);
}
};
_proto.cancel = function cancel() {
// switch to drag handler by default
this.sketcher.namedElements.handlers.drag.value = true;
};
_proto.onKeyUp = function onKeyUp(event) {
switch (event.which) {
case 27:
// escape
this.cancel();
break;
case 46: // delete
case 8:
// backspace (for Mac)
this.sketcher.deleteSelected({
onlyIfConsoleIsEmpty: true
});
break;
}
};
_proto.onKeyDown = function onKeyDown(event) {};
_proto.createSnapper = function createSnapper() {
return new Snapper({
sketch: this.sketch,
gridStep: this.sketcher.gridStep,
pointRadius: this.sketcher.graphicScale
});
}; //note: can be called in (view.input.on + 'mousemove') callback
//in order to get current sketch coordinates of the mouse
_proto.getRecentMousePosition = function getRecentMousePosition() {
var mousemove = this.sketcher.view.input.recent.mousemove;
var point2 = new THREE.Vector2(mousemove.offsetX, mousemove.offsetY);
var line3 = this.sketcher.view.getViewLine3(point2);
var ray = new THREE.Ray(line3.start, line3.end.sub(line3.start).normalize());
ray.applyMatrix4(new THREE.Matrix4().getInverse(this.sketch.matrixWorld));
var plane = new THREE.Plane(new THREE.Vector3(0, 0, 1));
var result = new THREE.Vector3();
ray.intersectPlane(plane, result);
ray.direction.negate();
ray.intersectPlane(plane, result);
return result;
};
_proto.consoleComplete = function consoleComplete(cmd) {
var result = Object.keys(commands);
function getWeight(el) {
//Then bigger weight of el then far from beginning it will be placed.
if (cmd === el) return 0; //If el match to the cmd, el should be first in list.
else if (el.indexOf(cmd) === 0) return el.length - cmd.length;else if (el.indexOf(cmd) > 0) return el.indexOf(cmd) + el.length - cmd.length;else return 99999;
}
result.sort(function (a, b) {
var aWeight = getWeight(a);
var bWeight = getWeight(b);
return aWeight < bWeight ? -1 : aWeight === bWeight ? 0 : 1;
});
return result;
};
_proto.consoleExecute = function consoleExecute(cmd) {
// watch out for cmd === '__proto__' which could access the prototype
if (commands.hasOwnProperty(cmd)) {
this.sketcher.namedElements.handlers[commands[cmd]].value = true;
return true;
} else {
return false;
}
};
_proto.parseRestrictions = function parseRestrictions(cmd) {};
return BaseHandler;
}();
export { BaseHandler as default };
var commands = Object.freeze({
point: 'point',
pt: 'point',
line: 'line',
ln: 'line',
arccenter: 'arccenter',
ac: 'arccenter',
arcmiddle: 'arcmiddle',
am: 'arcmiddle',
arctangent: 'arctangent',
at: 'arctangent',
circle: 'circle',
cr: 'circle'
});