UNPKG

@daign/2d-graphics

Version:

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

51 lines (50 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ControlModifierChain = void 0; /** * Abstract class for a chain of modifiers that restrict or modify the outcome of drawing actions. */ var ControlModifierChain = /** @class */ (function () { /** * Constructor. */ function ControlModifierChain() { // Variable to temporarily disable the control modifier. this.enabled = true; // The array of control modifiers to apply one after the other. this.modifiers = []; /* Function that can cancel the execution of the modifier chain in certain cases. Replace this * function if needed and return false when execution should be canceled. */ this.selectorFunction = function () { return true; }; } /** * Method that applies the chain of modifiers to the point array. * @param updatedPoints - The array of updated points. * @param pointIndex - The index of the point that initiated the change. * @param controlObject - The corresponding control object. * @returns The modified array of points. */ ControlModifierChain.prototype.modifyPoints = function (updatedPoints, pointIndex, controlObject) { // Determine whether the modifier chain should be applied to the specified input. var doExecute = this.selectorFunction(updatedPoints, pointIndex, controlObject); // If execution is blocked than return the unmodified points. if (!doExecute || !this.enabled) { return updatedPoints; } // Array of modified points. Will be overwritten during each modifier execution. var modifiedPoints = updatedPoints; this.modifiers.forEach(function (modifier) { modifiedPoints = modifier.modifyPoints(modifiedPoints, pointIndex, controlObject); }); return modifiedPoints; }; /** * Add a modifier to the chain of modifiers. Can also be another ModifierChain. * @param modifier - The modifier to add. */ ControlModifierChain.prototype.addModifier = function (modifier) { this.modifiers.push(modifier); }; return ControlModifierChain; }()); exports.ControlModifierChain = ControlModifierChain;