UNPKG

qwc2-lts

Version:
230 lines (229 loc) 13 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Copyright 2025 Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */ import React from 'react'; import PropTypes from 'prop-types'; import { TransformControls } from 'three/addons/controls/TransformControls'; import LocaleUtils from '../../../utils/LocaleUtils'; import Icon from '../../Icon'; import ButtonBar from '../../widgets/ButtonBar'; import ColorButton from '../../widgets/ColorButton'; import NumericInput3D from './NumericInput3D'; var EditTool3D = /*#__PURE__*/function (_React$Component) { function EditTool3D() { var _this; _classCallCheck(this, EditTool3D); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, EditTool3D, [].concat(args)); _defineProperty(_this, "state", { mode: 'translate', numericInput: false }); _defineProperty(_this, "selectShapeOnRelease", function (ev) { if (ev.button === 0 && !_this.transformControls.dragging) { var renderer = _this.props.sceneContext.scene.renderer; renderer.domElement.addEventListener("pointerup", _this.selectShape, { once: true }); renderer.domElement.addEventListener("pointermove", function () { renderer.domElement.removeEventListener("pointerup", _this.selectShape); }); } }); _defineProperty(_this, "selectShape", function (ev) { var rect = ev.target.getBoundingClientRect(); var x = (ev.clientX - rect.left) / rect.width * 2 - 1; var y = -((ev.clientY - rect.top) / rect.height) * 2 + 1; var intersection = _this.props.sceneContext.getSceneIntersection(x, y); if (intersection) { // Check if closest (first) intersected object is within the current draw group var object = intersection.object; var drawGroup = _this.props.sceneContext.getSceneObject(_this.props.drawGroupId); for (var parent = object.parent; parent; parent = parent.parent) { if (parent === drawGroup) { _this.props.objectPicked(object); return; } } _this.props.objectPicked(null); } }); _defineProperty(_this, "updateTransformHelper", function () { _this.transformControls.getHelper().updateMatrixWorld(); _this.props.sceneContext.scene.notifyChange(); }); _defineProperty(_this, "toolChanged", function () { var _this$props$selectedO, _this$props$selectedO2; (_this$props$selectedO = _this.props.selectedObject) === null || _this$props$selectedO === void 0 || (_this$props$selectedO2 = _this$props$selectedO.updateMatrixWorld) === null || _this$props$selectedO2 === void 0 || _this$props$selectedO2.call(_this$props$selectedO); _this.transformControls.getHelper().updateMatrixWorld(); _this.props.sceneContext.scene.notifyChange(); }); _defineProperty(_this, "onKeyDown", function (ev) { if (ev.key === "Escape") { _this.transformControls.reset(); } }); _defineProperty(_this, "toggleNumericInput", function () { _this.setState(function (state) { return { numericInput: !state.numericInput }; }); }); return _this; } _inherits(EditTool3D, _React$Component); return _createClass(EditTool3D, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; var camera = this.props.sceneContext.scene.view.camera; var renderer = this.props.sceneContext.scene.renderer; this.transformControls = new TransformControls(camera, renderer.domElement); this.props.sceneContext.scene.add(this.transformControls.getHelper()); this.transformControls.setMode(this.state.mode); this.transformControls.addEventListener('change', this.toolChanged); this.transformControls.addEventListener('mouseUp', this.toolChanged); this.transformControls.addEventListener('dragging-changed', function (event) { _this2.props.sceneContext.scene.view.controls.enabled = !event.value; }); renderer.domElement.addEventListener("pointerdown", this.selectShapeOnRelease); renderer.domElement.addEventListener('keydown', this.onKeyDown); if (this.props.selectedObject) { this.transformControls.attach(this.props.selectedObject); this.transformControls.getHelper().updateMatrixWorld(); this.props.colorChanged(this.props.selectedObject.material.color.toArray().map(function (c) { return c * 255; })); } this.props.sceneContext.scene.view.controls.addEventListener('change', this.updateTransformHelper); this.props.sceneContext.scene.notifyChange(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { if (this.props.selectedObject !== prevProps.selectedObject) { if (prevProps.selectedObject) { this.transformControls.detach(); } if (this.props.selectedObject) { this.transformControls.attach(this.props.selectedObject); this.props.colorChanged(this.props.selectedObject.material.color.toArray().map(function (c) { return c * 255; })); } this.transformControls.getHelper().updateMatrixWorld(); this.props.sceneContext.scene.notifyChange(); } if (this.state.mode !== prevState.mode) { this.transformControls.setMode(this.state.mode); this.transformControls.getHelper().updateMatrixWorld(); this.props.sceneContext.scene.notifyChange(); } if (this.props.color !== prevProps.color && this.props.selectedObject) { var _this$props$selectedO3; (_this$props$selectedO3 = this.props.selectedObject.material.color).setRGB.apply(_this$props$selectedO3, _toConsumableArray(this.props.color.map(function (c) { return c / 255; }))); this.props.sceneContext.scene.notifyChange(); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.transformControls.detach(); this.props.sceneContext.scene.remove(this.transformControls.getHelper()); this.transformControls.dispose(); var domElement = this.props.sceneContext.scene.renderer.domElement; this.props.sceneContext.scene.view.controls.removeEventListener('change', this.updateTransformHelper); domElement.removeEventListener("pointerdown", this.selectShapeOnRelease); domElement.removeEventListener('keydown', this.onKeyDown); this.props.sceneContext.scene.notifyChange(); } }, { key: "render", value: function render() { var _this3 = this; var editButtons = [{ key: "translate", label: LocaleUtils.tr("draw3d.translate") }, { key: "scale", label: LocaleUtils.tr("draw3d.scale") }, { key: "rotate", label: LocaleUtils.tr("draw3d.rotate") }]; var extraButtons = [{ key: "NumericInput", tooltip: LocaleUtils.tr("draw3d.numericinput"), icon: "numericinput" }]; return /*#__PURE__*/React.createElement("div", { className: "redlining-controlsbar" }, /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(Icon, { className: "redlining-control-icon", icon: "pen", size: "large" }), /*#__PURE__*/React.createElement(ColorButton, { alpha: false, color: this.props.color, onColorChanged: this.props.colorChanged })), /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(ButtonBar, { active: this.state.mode, buttons: editButtons, onClick: function onClick(mode) { return _this3.setState({ mode: mode }); } })), /*#__PURE__*/React.createElement("span", { className: "redlining-group" }, /*#__PURE__*/React.createElement(ButtonBar, { active: this.state.numericInput ? "NumericInput" : null, buttons: extraButtons, onClick: this.toggleNumericInput })), this.state.numericInput ? /*#__PURE__*/React.createElement(NumericInput3D, { sceneContext: this.props.sceneContext, selectedObject: this.props.selectedObject, toggleNumericInput: this.toggleNumericInput, transformControls: this.transformControls }) : null); } }]); }(React.Component); _defineProperty(EditTool3D, "propTypes", { color: PropTypes.array, colorChanged: PropTypes.func, drawGroupId: PropTypes.string, objectPicked: PropTypes.func, sceneContext: PropTypes.object, selectedObject: PropTypes.object }); export { EditTool3D as default };