@daign/2d-pipeline
Version:
Two dimensional graphics pipeline.
90 lines (89 loc) • 4.1 kB
JavaScript
;
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.PresentationNode = void 0;
var math_1 = require("@daign/math");
var genericNode_1 = require("./genericNode");
/**
* A concrete, renderable copy of a graphic node, belonging to a view.
*/
var PresentationNode = /** @class */ (function (_super) {
__extends(PresentationNode, _super);
/**
* Constructor.
* @param view - The view to which the node belongs.
* @param sourceNode - The source from which this node is copied.
*/
function PresentationNode(view, sourceNode) {
var _this = _super.call(this) || this;
_this.view = view;
_this.sourceNode = sourceNode;
/**
* The projection from view coordinates to the coordinates of the element.
*/
_this.projectViewToNode = new math_1.Matrix3().setIdentity();
/**
* The projection from the coordinates of the element to view coordinates.
*/
_this.projectNodeToView = new math_1.Matrix3().setIdentity();
/**
* The projection from the coordinates of the element to view coordinates, not including native
* transforms.
*/
_this.projectNodeToViewNonNative = new math_1.Matrix3().setIdentity();
_this.sourceNode.registerPresentationNode(_this);
// Recalculate projections when the transformation of the source node changes.
var callback = function () {
_this.updateProjectionMatrices();
};
_this.removeSourceNodeSubscription = _this.sourceNode.transformation.subscribeToChanges(callback);
return _this;
}
/**
* Update the projection matrices of the node and of its children.
*/
PresentationNode.prototype.updateProjectionMatrices = function () {
if (this.parent !== null) {
// The projection of the parent combined with the own transformation.
this.projectNodeToView.copy(this.sourceNode.transformation.transformMatrix);
this.projectNodeToView.transform(this.parent.projectNodeToView);
// The projection not including native transforms.
this.projectNodeToViewNonNative.copy(this.sourceNode.transformation.transformMatrixNonNative);
this.projectNodeToViewNonNative.transform(this.parent.projectNodeToViewNonNative);
}
else {
// When no parent exists the projection is equal to the own transformation.
this.projectNodeToView.copy(this.sourceNode.transformation.transformMatrix);
// The projection not including native transforms.
this.projectNodeToViewNonNative.copy(this.sourceNode.transformation.transformMatrixNonNative);
}
this.projectViewToNode.setToInverse(this.projectNodeToView);
this.children.forEach(function (child) {
child.updateProjectionMatrices();
});
};
/**
* Destroy this node and also remove the links from the source node.
*/
PresentationNode.prototype.destroyNode = function () {
_super.prototype.destroyNode.call(this);
this.removeSourceNodeSubscription();
this.sourceNode.removePresentationNode(this);
};
return PresentationNode;
}(genericNode_1.GenericNode));
exports.PresentationNode = PresentationNode;