UNPKG

@daign/2d-graphics

Version:

Two dimensional graphics library that implements the daign-2d-pipeline.

117 lines (116 loc) 5.27 kB
"use strict"; 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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ControlPoint = void 0; var _2d_pipeline_1 = require("@daign/2d-pipeline"); var basic_elements_1 = require("../basic-elements"); /** * Interactive control point for control objects. */ var ControlPoint = /** @class */ (function (_super) { __extends(ControlPoint, _super); /** * Constructor. * @param targetPoint - The point coordinates to display and modify. * @param targetTransformation - The transformation matrix of the control object. * @param application - The corresponding application. * @param controlObject - The corresponding control object. * @param controlIndex - The index of the point in the points array of the control object. * @param controlShape - The shape to display for a control point. Optional. */ function ControlPoint(targetPoint, targetTransformation, application, controlObject, controlIndex, controlShape) { var _this = _super.call(this) || this; _this.targetPoint = targetPoint; _this.targetTransformation = targetTransformation; _this.application = application; _this.controlObject = controlObject; _this.controlIndex = controlIndex; // The offset position that is applied to all elements of the control point. _this.offset = new _2d_pipeline_1.MatrixTransform(); _this.baseClass = 'controlPoint'; _this.points.initializeElements(1); _this.points.assignName('center', 0); _this.center = _this.targetPoint.clone().transform(_this.targetTransformation); _this.transformation.push(_this.offset); if (controlShape) { _this.appendChild(controlShape); } else if (controlShape === undefined) { // The default shape to display for a control point. // Center is (0,0) because the whole group is shifted into position. var defaultShape = new basic_elements_1.FixedRadiusCircle(); defaultShape.radius = 15; _this.appendChild(defaultShape); } return _this; // Else if controlShape is null then don't add a shape. } Object.defineProperty(ControlPoint.prototype, "center", { /** * Getter for the center position. */ get: function () { return this.points.getByName('center'); }, /** * Setter for the center position. */ set: function (position) { this.points.getByName('center').copy(position); this.calculateOffset(); }, enumerable: false, configurable: true }); /** * Calculate the offset transformation resulting from the center point position. */ ControlPoint.prototype.calculateOffset = function () { this.offset.matrix.setTranslation(this.center); }; /** * Save a snapshot copy of the current center position. */ ControlPoint.prototype.snap = function () { this.center.snap(); }; /** * Apply a translation delta to the last snapshot of the center. * @param delta - The translation delta. */ ControlPoint.prototype.drag = function (delta) { // Apply the translation to the last snapshot of the center. this.center.drag(delta); // TODO Get the correct presentation node that corresponds to the application's view. var presentationNode = this.controlObject.presentationNodes[0]; // Calculate the real world coordinates from the screen coordinates of the control point. var calculatedPosition = this.center.clone().transform(presentationNode.projectViewToNode); // Create copy of all points of the control object and set the calculated position. var updatedPoints = this.controlObject.getDeepCopyOfPoints(); updatedPoints[this.controlIndex].copy(calculatedPosition); // If a control modifier exists, then modify the points through it. if (this.controlObject.controlModifier) { updatedPoints = this.controlObject.controlModifier.modifyPoints(updatedPoints, this.controlIndex, this.controlObject); } // Copy all coordinates back to the control object. this.controlObject.writeUpdatesToPoints(updatedPoints); this.calculateOffset(); this.application.redraw(); }; return ControlPoint; }(basic_elements_1.Group)); exports.ControlPoint = ControlPoint;