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.

212 lines 9.55 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { BaseRectangleItemHandler } from "./BaseRectangleItemHandler"; import { Graphics } from "../Graphics"; import { getSquareDistanceToSegment, PointF, RectangleF, Transform } from "@aurigma/design-atoms-model/Math"; import * as Exceptions from "@aurigma/design-atoms-model/Utils/Exceptions"; import { OutOfRangeException } from "@aurigma/design-atoms-model/Exception"; import Environment from "@aurigma/design-atoms-model/Utils/Environment"; var PolylineItemHandler = /** @class */ (function (_super) { __extends(PolylineItemHandler, _super); function PolylineItemHandler(item, textWhizz, colorPreviewService) { if (textWhizz === void 0) { textWhizz = null; } return _super.call(this, 0, 0, 0, 0, item, textWhizz, colorPreviewService) || this; } Object.defineProperty(PolylineItemHandler.prototype, "item", { get: function () { return this._getItem(); }, enumerable: true, configurable: true }); PolylineItemHandler.prototype.drawItemHandler = function (itemHandlerCtx) { if (itemHandlerCtx == null || this.item.width <= 0) { return; } if (!this._isReadyToDraw) { this.canvas.drawWaitClock(itemHandlerCtx, this.rectangle.center); return; } var points = this.getActualPoints(); var colorPreview = this._getItemColorPreviews().colorPreview; Graphics.drawPolyline(itemHandlerCtx, points, this.item.width, colorPreview === null || colorPreview === void 0 ? void 0 : colorPreview.toString(), this.item.opacity); }; PolylineItemHandler.prototype.hitTest = function (point, isSelected) { /// <summary>Determines whether the specified point belongs to this vector polyline.</summary> /// <param name="p" type="Aurigma.GraphicsMill.AjaxControls.VectorObjects.Math.PointF">The point to test.</param> /// <returns type="Boolean"><strong>true</strong> if the specified point belongs to this vector polyline; otherwise <strong>false</strong>.</returns> var result = _super.prototype.hitTest.call(this, point.clone()); // for tolerance in selection. var mul = this.canvas.zoom * Environment.screenDpi / 72; var rectFrame = this._getRectangle(); var isBelongRect = isSelected ? rectFrame.toRectangleF().contains(point) : false; var isBelongPoliLine = this._belongToLine(point, 2 / mul); result.body = isBelongRect || isBelongPoliLine; return result; }; PolylineItemHandler.prototype.getControlBounds = function () { var minx = 0; var miny = 0; var maxx = 0; var maxy = 0; var pts = this.item.sourcePoints; var len = pts.length; if (len > 0) { minx = pts[0].x; maxx = pts[0].x; miny = pts[0].y; maxy = pts[0].y; } for (var i = 1; i < len; i++) { minx = Math.min(pts[i].x, minx); maxx = Math.max(pts[i].x, maxx); miny = Math.min(pts[i].y, miny); maxy = Math.max(pts[i].y, maxy); } var width = maxx - minx; var height = maxy - miny; return new RectangleF(minx, miny, width, height); }; // p - test point. PolylineItemHandler.prototype._belongToLine = function (p, tolerance) { var pts = this.getActualPoints(); var res = false; var eps = this.item.width / 2 + tolerance; var squareEps = eps * eps; //check if point belongs to any of segments for (var i = 1; i < pts.length; i++) { var squareDist = getSquareDistanceToSegment(p, pts[i - 1], pts[i]); if (squareDist <= squareEps) { res = true; // is true, so doesn't need to continue break; } } return res; }; PolylineItemHandler.prototype.getActualPoints = function () { /// <summary>Gets an array points this polyline is constructed from.</summary> /// <value type="Array">An array of points.</value> var pts = []; //calc center point once and use it in loop var center = this.getControlCenter(); for (var i = 0; i < this.item.sourcePoints.length; i++) { pts.push(this._getActualPointFromControlPoint(this.item.sourcePoints[i], center)); } return pts; }; PolylineItemHandler.prototype._getActualPointFromControlPoint = function (point, center) { var p = new PointF(point.x, point.y); p.translate(-center.x, -center.y); var transform = this.item.transform; p.scale(transform.scaleX, transform.scaleY); p.rotate(transform.angle); p.translate(transform.translateX, transform.translateY); p.translate(center.x, center.y); return p; }; PolylineItemHandler.prototype.addPoint = function (point) { this.insertPoint(point, this.item.sourcePoints.length); }; PolylineItemHandler.prototype.insertPoint = function (point, index) { if (index < 0 || index > this.item.sourcePoints.length) throw new OutOfRangeException(Exceptions.insertPointOutOfRange); var actualPoints = this.getActualPoints(); actualPoints.splice(index, 0, point); //change original points to actual points and reset transform this.item.sourcePoints = actualPoints; this.item.transform = new Transform(); }; PolylineItemHandler.prototype.setPoint = function (point, index) { if (index < 0 || index >= this.item.sourcePoints.length) throw new OutOfRangeException(Exceptions.setPointOutOfRange); var actualPoints = this.getActualPoints(); actualPoints[index] = point; this.item.sourcePoints = actualPoints; this.item.transform = new Transform(); this.raiseChanged(); }; PolylineItemHandler.prototype.getPointsCount = function () { return this.item.sourcePoints.length; }; PolylineItemHandler.prototype.getPoint = function (index) { if (index < 0 || index >= this.item.sourcePoints.length) throw new OutOfRangeException(Exceptions.setPointOutOfRange); return this._getActualPointFromControlPoint(this.item.sourcePoints[index], this.getControlCenter()); }; PolylineItemHandler.prototype.removePoint = function (index) { if (index < 0 || index >= this.item.sourcePoints.length) throw new OutOfRangeException(Exceptions.removePointOutOfRange); var pts = this.item.sourcePoints; pts.splice(index, 1); this.item.sourcePoints = pts; }; Object.defineProperty(PolylineItemHandler.prototype, "_isReadyToDraw", { get: function () { return this._areColorPreviewsReady; }, enumerable: true, configurable: true }); Object.defineProperty(PolylineItemHandler.prototype, "_areColorPreviewsReady", { get: function () { var colorPreview = this._getItemColorPreviews().colorPreview; return this.item.color == null || colorPreview != null; }, enumerable: true, configurable: true }); PolylineItemHandler.prototype._getItemColorPreviews = function () { var colorPreview = this._colorPreviewService.getPreview(this.item.color); if (this.item.color != null && colorPreview == null) { this._colorPreviewService.subscribeToPreviewLoaded(this.item.color, this._onColorPreviewLoaded); } return { colorPreview: colorPreview }; }; PolylineItemHandler.prototype._onItemPropertyChanged = function (sender, propertyName) { switch (propertyName) { case "width": case "color": case "sourcePoints": break; } }; PolylineItemHandler.prototype._getBoundsMargin = function () { return this.item.width; }; // add point, and try to smooth // add pointd while interactive drawing PolylineItemHandler.prototype._addPoint = function (pt) { var c = this.canvas; var mul = -1; if (c) { mul = (Environment.screenDpi / 72) * c.zoom; } var d = 30; var pts = this.item.sourcePoints; if (pts.length > 0) { var dist = pts[pts.length - 1].distance(pt); if (mul === -1 || dist * mul > d) { pts.push(pt); } } else { pts.push(pt); } }; PolylineItemHandler.typeName = "PolylineItemHandler"; return PolylineItemHandler; }(BaseRectangleItemHandler)); export { PolylineItemHandler }; //# sourceMappingURL=PolylineItemHandler.js.map