@turbox3d/design-engine
Version:
Large-scale design application engine library
206 lines (205 loc) • 8.52 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SelectionCommand = exports.ESelectMode = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _commandManager = require("@turbox3d/command-manager");
var _entityObject = _interopRequireDefault(require("../entity-object"));
var _domain = require("./domain");
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(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; })(); }
var ESelectMode;
(function (ESelectMode) {
/** 选择整体 */
ESelectMode["OVERALL"] = "overall";
/** 选择部件 */
ESelectMode["PART"] = "part";
/** 默认模式(无视层级,选中哪个部件就是哪个) */
ESelectMode["DEFAULT"] = "default";
})(ESelectMode || (exports.ESelectMode = ESelectMode = {}));
var EClickAction;
(function (EClickAction) {
EClickAction["CLICK"] = "click";
EClickAction["DOUBLE_CLICK"] = "double-click";
})(EClickAction || (EClickAction = {}));
var SelectionCommand = exports.SelectionCommand = /*#__PURE__*/function (_Command) {
function SelectionCommand() {
var _this;
(0, _classCallCheck2["default"])(this, SelectionCommand);
_this = _callSuper(this, SelectionCommand, arguments);
_this.selection = new _domain.Selection();
_this.modeMap = (0, _defineProperty2["default"])((0, _defineProperty2["default"])((0, _defineProperty2["default"])({}, ESelectMode.OVERALL, 1), ESelectMode.PART, 2), ESelectMode.DEFAULT, 1);
_this.onSelectHandler = function (models) {
//
};
_this.onUnselectHandler = function (models) {
//
};
return _this;
}
/** 获取被选中的 entities */
(0, _inherits2["default"])(SelectionCommand, _Command);
return (0, _createClass2["default"])(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["default"].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);
}
}]);
}(_commandManager.Command);
SelectionCommand.ESelectMode = ESelectMode;