UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

178 lines 8.48 kB
var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread) || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; import { GroupItemHandler } from "../ItemHandlers"; import { PointF, Matrix, RectangleF } from "@aurigma/design-atoms-model/Math"; import Environment from "@aurigma/design-atoms-model/Utils/Environment"; import { CoordinatesConvertUtils } from "../Utils/CoordinatesConvertUtils"; import { CoordinateSystem } from "../Viewer/CoordinateSystem"; var HitTestManager = /** @class */ (function () { function HitTestManager(_canvas) { this._canvas = _canvas; } /** @inheritDoc */ HitTestManager.prototype.getItemHandlersByHitTest = function (point, coordinateSystem) { var _this = this; if (coordinateSystem === void 0) { coordinateSystem = CoordinateSystem.page; } var items = []; var workspacePoint = this._convertToWorkspace(point, coordinateSystem); var isSelectableItemHandler = function (itemHandler) { return itemHandler.visible && !itemHandler.isLocked() && _this._productHandler.isInteractive(itemHandler.item) && !itemHandler.getPermissions().noShow && !itemHandler.isEmpty(); }; for (var i = this._canvas.layers.length - 1; i >= 0; i--) { var layer = this._canvas.layers.getItem(i); if (!this._productHandler.isInteractive(layer.container)) continue; if (layer.visible && !layer.locked) { for (var j = layer.itemHandlers.length - 1; j >= 0; j--) { var vo = layer.itemHandlers.getItem(j); if (isSelectableItemHandler(vo)) { var ht = vo.hitTest(workspacePoint); if (ht.body) { items.push(vo); if (vo instanceof GroupItemHandler && vo.item.groupItemPermissions.allowSelectNestedItems) items.push.apply(items, __spread(vo.getNestedItemHandlers(false, true).filter(function (gItemHandler) { return gItemHandler.hitTest(workspacePoint).body === true && isSelectableItemHandler(gItemHandler); }))); } } } } } return items; }; /** @inheritDoc */ HitTestManager.prototype.findItemByHitTest = function (point, coordinateSystem) { var _this = this; if (coordinateSystem === void 0) { coordinateSystem = CoordinateSystem.page; } var workspacePoint = CoordinatesConvertUtils.pageToWorkspacePoint(point, this._canvas.viewer); var itemHandlers = this.getItemHandlersByHitTest(workspacePoint, CoordinateSystem.workspace); var selectedHandler = itemHandlers.find(function (itemHandler) { return _this._canvas.isItemHandlerSelected(itemHandler); }); if (selectedHandler != null) return selectedHandler.item; return itemHandlers[0] != null ? itemHandlers[0].item : null; }; HitTestManager.prototype.getFirstHandlerByHitTest = function (workspacePoint) { return this.getItemHandlersByHitTest(workspacePoint, CoordinateSystem.workspace)[0]; }; HitTestManager.prototype.hitTestSelection = function (rect, workspacePoint, onlyWidthResizeEnabled, tolerance) { if (onlyWidthResizeEnabled === void 0) { onlyWidthResizeEnabled = false; } if (tolerance === void 0) { tolerance = null; } var transformedPoint = this._convertToProductCoord(workspacePoint); var result = {}; var m = new Matrix(); if (rect.angle !== 0) { m.rotate(-rect.angle); } m.translate(-rect.centerX, -rect.centerY); transformedPoint = m.transformPoint(transformedPoint, true); var w = rect.width, h = rect.height; var mul = this._canvas.mul; // check body var bodyHeight = tolerance != null ? Math.max(tolerance / mul, h) : h; var bodyWidth = tolerance != null ? Math.max(tolerance / mul, w) : w; result.body = new RectangleF(-bodyWidth / 2, -bodyHeight / 2, bodyWidth, bodyHeight).contains(transformedPoint); // check resize result.resize = false; result.resizeIndex = 0; var resizeGripSize = Environment.IsTouchDevice() ? 33 : this._canvas.resizeGripSize; var r = resizeGripSize / mul, r2 = r / 2; var resizeGrips = [ null, new RectangleF(-w / 2 - r2, -h / 2 - r2, r, r), new RectangleF(w / 2 - r2, -h / 2 - r2, r, r), new RectangleF(w / 2 - r2, h / 2 - r2, r, r), new RectangleF(-w / 2 - r2, h / 2 - r2, r, r), new RectangleF(-w / 2 - r2, 0 - r2, r, r), new RectangleF(0 - r2, -h / 2 - r2, r, r), new RectangleF(w / 2 - r2, 0 - r2, r, r), new RectangleF(0 - r2, h / 2 - r2, r, r) ]; var widthResizeGripsIndexes = new Array(5, 7); for (var i = 1, imax = resizeGrips.length; i < imax; i++) { if (resizeGrips[i].contains(transformedPoint) && !(onlyWidthResizeEnabled && !(widthResizeGripsIndexes.indexOf(i) !== -1))) { result.resize = true; result.resizeIndex = i; break; } } // check rotate result.rotate = false; var rotationCenter = HitTestManager.getDeltaFromSelectionCenterToRotateCenter(this._canvas.rotationGripSize, Math.abs(h), Math.abs(w), this._canvas.viewer.contentAngle, mul); r = (this._canvas.rotationGripSize / 2) / mul; var tx = transformedPoint.x - rotationCenter.x; var ty = transformedPoint.y - rotationCenter.y; if (tx * tx + ty * ty < r * r) { result.rotate = true; } return result; }; /** Get delta from selection center to rotate center. Without angle!*/ HitTestManager.getDeltaFromSelectionCenterToRotateCenter = function (rotationGripSize, selectionHeight, selectionWidth, contentAngle, mul) { if (mul === void 0) { mul = 1; } var cx = 0; var cy = 0; ; var r = (rotationGripSize / 2) / mul; if (contentAngle == 0) { cx = 0; cy = selectionHeight / 2 + (rotationGripSize) / mul + r; } else if (contentAngle == 90) { cx = selectionWidth / 2 + (rotationGripSize) / mul + r; cy = 0; } else if (contentAngle == 180) { cx = 0; cy = -(selectionHeight / 2 + (rotationGripSize) / mul + r); } else if (contentAngle == 270) { cx = -(selectionWidth / 2 + (rotationGripSize) / mul + r); cy = 0; } return new PointF(cx, cy); }; Object.defineProperty(HitTestManager.prototype, "_productHandler", { get: function () { return this._canvas.viewer.productHandler; }, enumerable: true, configurable: true }); HitTestManager.prototype._convertToProductCoord = function (point) { return CoordinatesConvertUtils.workspaceToProduct(point, this._canvas.offset); }; HitTestManager.prototype._convertToWorkspace = function (point, coordinateSystem) { switch (coordinateSystem) { case CoordinateSystem.page: return CoordinatesConvertUtils.pageToWorkspacePoint(point, this._canvas.viewer); case CoordinateSystem.workspace: return point; default: throw new Error("Unexpected coordinateSystem " + coordinateSystem); } }; return HitTestManager; }()); export { HitTestManager }; //# sourceMappingURL=HitTestManager.js.map