@turbox3d/design-engine
Version:
Large-scale design application engine library
199 lines • 7.89 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import { Command } from '@turbox3d/command-manager';
import EntityObject from '../entity-object';
import { Selection } from './domain';
export var ESelectMode;
(function (ESelectMode) {
/** 选择整体 */
ESelectMode["OVERALL"] = "overall";
/** 选择部件 */
ESelectMode["PART"] = "part";
/** 默认模式(无视层级,选中哪个部件就是哪个) */
ESelectMode["DEFAULT"] = "default";
})(ESelectMode || (ESelectMode = {}));
var EClickAction;
(function (EClickAction) {
EClickAction["CLICK"] = "click";
EClickAction["DOUBLE_CLICK"] = "double-click";
})(EClickAction || (EClickAction = {}));
export var SelectionCommand = /*#__PURE__*/function (_Command) {
function SelectionCommand() {
var _this;
_classCallCheck(this, SelectionCommand);
_this = _callSuper(this, SelectionCommand, arguments);
_this.selection = new Selection();
_this.modeMap = _defineProperty(_defineProperty(_defineProperty({}, ESelectMode.OVERALL, 1), ESelectMode.PART, 2), ESelectMode.DEFAULT, 1);
_this.onSelectHandler = function (models) {
//
};
_this.onUnselectHandler = function (models) {
//
};
return _this;
}
/** 获取被选中的 entities */
_inherits(SelectionCommand, _Command);
return _createClass(SelectionCommand, [{
key: "getSelectedEntities",
value: function getSelectedEntities() {
return this.selection.selectedEntities;
}
/** 切换选择模式 */
}, {
key: "switchSelectMode",
value: function switchSelectMode(selectMode) {
this.selection.switchSelectMode(selectMode);
this.selection.setLayerDepth(this.modeMap[this.selection.selectMode]);
}
/** 获取当前选择模式 */
}, {
key: "getSelectMode",
value: function getSelectMode() {
return this.selection.selectMode;
}
/**
* 设置事件的多选状态,每次事件行为都会根据此状态判断是否为多选行为
*
* 注意:每次选择事件之前都需要设置,因为事件完成后状态会被清除,这只在需要自定义多选按键/状态的情况下才会用到(默认情况下按 shift 会设置)
*/
}, {
key: "setMultiSelect",
value: function setMultiSelect(isMultiple) {
this.selection.setMultiSelect(isMultiple);
}
/** 选中指定模型 */
}, {
key: "select",
value: function select(models) {
var clearExisted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (clearExisted) {
this.selection.clearAllSelected(this.onUnselectHandler);
}
this.selection.select(models, this.onSelectHandler);
}
/** 取消选中指定模型 */
}, {
key: "unselect",
value: function unselect(models) {
this.selection.unselect(models, this.onUnselectHandler);
}
/** 清除已选中模型 */
}, {
key: "clearAllSelected",
value: function clearAllSelected() {
this.selection.setLayerDepth(this.modeMap[this.selection.selectMode]);
this.selection.clearAllSelected(this.onUnselectHandler);
}
/** 设置选中层级深度 */
}, {
key: "setLayerDepth",
value: function setLayerDepth(depth) {
this.selection.setLayerDepth(depth);
}
/** 获取选中层级深度 */
}, {
key: "getLayerDepth",
value: function getLayerDepth() {
return this.selection.layerDepth;
}
/** 设置可选中的 entity 类型 */
}, {
key: "setSelectEntityTypes",
value: function setSelectEntityTypes(types) {
this.selection.setSelectEntityTypes(types);
}
/** 获取可选中的 entity 类型 */
}, {
key: "getSelectEntityTypes",
value: function getSelectEntityTypes() {
return this.selection.selectEntityTypes;
}
}, {
key: "active",
value: function active(params) {
if (params) {
var selectMode = params.selectMode,
selectEntityTypes = params.selectEntityTypes,
hint = params.hint,
onSelectHandler = params.onSelectHandler,
onUnselectHandler = params.onUnselectHandler;
this.switchSelectMode(selectMode || ESelectMode.OVERALL);
this.setSelectEntityTypes(selectEntityTypes);
this.hint = hint;
onSelectHandler && (this.onSelectHandler = onSelectHandler);
onUnselectHandler && (this.onUnselectHandler = onUnselectHandler);
}
}
}, {
key: "onClick",
value: function onClick(viewEntity, event) {
if (event.event.shiftKey) {
this.setMultiSelect(true);
}
this.selectHandler(viewEntity, EClickAction.CLICK);
}
}, {
key: "onDBClick",
value: function onDBClick(viewEntity) {
this.selectHandler(viewEntity, EClickAction.DOUBLE_CLICK);
}
/** 选中实体通用逻辑 */
}, {
key: "selectHandler",
value: function selectHandler(viewEntity, action) {
var _a;
if (action === EClickAction.CLICK) {
if (!this.selection.selectEntityTypes || !this.selection.selectEntityTypes.includes(viewEntity.type)) {
this.selection.setLayerDepth(this.modeMap[this.selection.selectMode]);
}
}
var model = EntityObject.getEntityById(viewEntity.id);
var isMultiSelect = this.selection.isMultiSelectMode;
if (!isMultiSelect) {
this.selection.clearAllSelected(this.onUnselectHandler);
}
(_a = this.hint) === null || _a === void 0 ? void 0 : _a.unHint();
if (!model) {
this.selection.setLayerDepth(this.modeMap[this.selection.selectMode]);
this.setMultiSelect(false);
return;
}
if (this.selection.selectEntityTypes && !this.selection.selectEntityTypes.includes(viewEntity.type)) {
this.selection.setLayerDepth(this.modeMap[this.selection.selectMode]);
this.setMultiSelect(false);
return;
}
if (isMultiSelect && this.getSelectedEntities().includes(model)) {
this.unselect([model]);
this.setMultiSelect(false);
return;
}
var path = model.getParentPathChain();
var pathLength = path.length;
if (this.targetRootEntity !== path[0]) {
this.selection.setLayerDepth(this.modeMap[this.selection.selectMode]);
}
if (pathLength > 1) {
if (this.selection.selectMode === ESelectMode.DEFAULT) {
this.selection.select([model], this.onSelectHandler);
} else {
// eslint-disable-next-line no-nested-ternary
var index = pathLength > this.selection.layerDepth ? action === EClickAction.DOUBLE_CLICK ? this.selection.layerDepth : this.selection.layerDepth - 1 : this.selection.layerDepth + (pathLength - this.selection.layerDepth) - 1;
this.selection.select([path[index]], this.onSelectHandler);
}
} else {
this.selection.select([path[0]], this.onSelectHandler);
}
this.targetRootEntity = path[0];
this.setMultiSelect(false);
}
}]);
}(Command);
SelectionCommand.ESelectMode = ESelectMode;