"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 (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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SPainter = void 0; var lib_1 = require("@saga-web/base/lib"); var _1 = require("./"); var SCompositeType_1 = require("./enums/SCompositeType"); var SArrowStyleType_1 = require("./enums/SArrowStyleType"); var SShadow_1 = require("./SShadow"); var SPainter = (function (_super) { __extends(SPainter, _super); function SPainter(engine) { var _this = _super.call(this) || this; if (engine instanceof _1.SCanvasView) { _this.engine = new _1.SCanvasPaintEngine(engine.canvas); } else { _this.engine = engine; } _this.pen = new _1.SPen(); _this.brush = new _1.SBrush(); _this.font = new _1.SFont(); _this.composite = SCompositeType_1.SCompositeType.SourceOver; _this.shadow = new SShadow_1.SShadow(); return _this; } Object.defineProperty(SPainter.prototype, "pen", { get: function () { return this.engine.state.pen; }, set: function (value) { this.engine.state.pen = value; }, enumerable: false, configurable: true }); Object.defineProperty(SPainter.prototype, "brush", { get: function () { return this.engine.state.brush; }, set: function (value) { this.engine.state.brush = value; }, enumerable: false, configurable: true }); Object.defineProperty(SPainter.prototype, "font", { get: function () { return this.engine.state.font; }, set: function (value) { this.engine.state.font = value; this.engine.changeFont(value); }, enumerable: false, configurable: true }); Object.defineProperty(SPainter.prototype, "composite", { get: function () { return this.engine.state._composite; }, set: function (value) { this.engine.state._composite = value; }, enumerable: false, configurable: true }); Object.defineProperty(SPainter.prototype, "shadow", { get: function () { return this.engine.state.shadow; }, set: function (value) { this.engine.state.shadow = value; }, enumerable: false, configurable: true }); Object.defineProperty(SPainter.prototype, "worldTransform", { get: function () { return this.engine.state.matrix; }, enumerable: false, configurable: true }); SPainter.prototype.save = function () { this.engine.save(); }; SPainter.prototype.restore = function () { this.engine.restore(); }; SPainter.prototype.translate = function (x, y) { this.engine.translate(x, y); }; SPainter.prototype.scale = function (x, y) { this.engine.scale(x, y); }; SPainter.prototype.rotate = function (angle) { this.engine.rotate(angle); }; SPainter.prototype.transform = function (m11, m12, m21, m22, dx, dy) { this.engine.transform(m11, m12, m21, m22, dx, dy); }; SPainter.prototype.setTransform = function (m11, m12, m21, m22, dx, dy) { this.engine.setTransform(m11, m12, m21, m22, dx, dy); }; SPainter.prototype.resetTransform = function () { this.engine.resetTransform(); }; SPainter.prototype.setClip = function (path) { this.engine.setClip(path); }; SPainter.prototype.clearRect = function (x, y, w, h) { if (x instanceof _1.SRect) { this.engine.clearRect(x); } else if (x instanceof _1.SPoint && y instanceof _1.SPoint) { this.engine.clearRect(new _1.SRect(x, y)); } else if (x instanceof _1.SPoint && y instanceof _1.SSize) { this.engine.clearRect(new _1.SRect(x, y)); } else { this.engine.clearRect(new _1.SRect(x, y, w, h)); } }; SPainter.prototype.drawRect = function (x, y, w, h) { if (x instanceof _1.SRect) { this.engine.drawRect(x); } else if (x instanceof _1.SPoint && y instanceof _1.SPoint) { this.engine.drawRect(new _1.SRect(x, y)); } else if (x instanceof _1.SPoint && y instanceof _1.SSize) { this.engine.drawRect(new _1.SRect(x, y)); } else { this.engine.drawRect(new _1.SRect(x, y, w, h)); } }; SPainter.prototype.drawCircle = function (cx, cy, r) { this.engine.drawCircle(cx, cy, r); }; SPainter.prototype.drawEllipse = function (cx, cy, rx, ry) { this.engine.drawEllipse(cx, cy, rx, ry); }; SPainter.prototype.drawArc = function (x, y, width, height, startAngle, endAngle) { if (x instanceof _1.SRect) { this.engine.drawArc(x.x, x.y, x.width, x.height, y, width); } else { this.engine.drawArc(x, y, width, height, startAngle, endAngle); } }; SPainter.prototype.drawChord = function (x, y, width, height, startAngle, endAngle) { if (x instanceof _1.SRect) { this.engine.drawChord(x.x, x.y, x.width, x.height, y, width); } else { this.engine.drawChord(x, y, width, height, startAngle, endAngle); } }; SPainter.prototype.drawPie = function (x, y, width, height, startAngle, endAngle) { if (x instanceof _1.SRect) { this.engine.drawPie(x.x, x.y, x.width, x.height, y, width); } else { this.engine.drawPie(x, y, width, height, startAngle, endAngle); } }; SPainter.prototype.drawLine = function (x1, y1, x2, y2) { if (x1 instanceof _1.SLine) { this.engine.drawLine(x1); } else if (x1 instanceof _1.SPoint && y1 instanceof _1.SPoint) { this.engine.drawLine(new _1.SLine(x1, y1)); } else { this.engine.drawLine(new _1.SLine(x1, y1, x2, y2)); } }; SPainter.prototype.drawPolyline = function (points) { this.engine.drawPolyline(points); }; SPainter.prototype.drawPolygon = function (points) { this.engine.drawPolygon(points); }; SPainter.prototype.drawPath = function (path) { this.engine.drawPath(path); }; SPainter.prototype.drawText = function (text, x, y, maxWidth) { this.engine.drawText(text, x, y, maxWidth); }; SPainter.prototype.drawImage = function (img, x, y, width, height) { this.engine.drawImage(img, x, y, width, height); }; SPainter.prototype.toPx = function (p) { return p / this.engine.state.matrix.a; }; SPainter.prototype.textWidth = function (text) { return this.engine.textWidth(text); }; SPainter.prototype.drawArrowLine = function (x1, y1, x2, y2, st) { var line, style; if (x1 instanceof _1.SLine) { line = x1; style = y1; } else if (x1 instanceof _1.SPoint && y1 instanceof _1.SPoint) { line = new _1.SLine(x1, y1); style = x2; } else { line = new _1.SLine(x1, y1, x2, y2); style = st; } this.engine.drawLine(line); if (style) { if (style.begin) { if (style.begin == SArrowStyleType_1.SArrowStyleType.Basic) { this.drawBasicArrow(line, false); } else if (style.begin == SArrowStyleType_1.SArrowStyleType.Triangle) { this.drawTriangleArrow(line, false); } else if (style.begin == SArrowStyleType_1.SArrowStyleType.Diamond) { this.drawDiamondArrow(line, false); } else if (style.begin == SArrowStyleType_1.SArrowStyleType.Square) { this.drawSquareArrow(line, false); } else if (style.begin == SArrowStyleType_1.SArrowStyleType.Circle) { this.drawCircleArrow(line, false); } } if (style.end) { if (style.end == SArrowStyleType_1.SArrowStyleType.Basic) { this.drawBasicArrow(line, true); } else if (style.end == SArrowStyleType_1.SArrowStyleType.Triangle) { this.drawTriangleArrow(line, true); } else if (style.end == SArrowStyleType_1.SArrowStyleType.Diamond) { this.drawDiamondArrow(line, true); } else if (style.end == SArrowStyleType_1.SArrowStyleType.Square) { this.drawSquareArrow(line, true); } else if (style.end == SArrowStyleType_1.SArrowStyleType.Circle) { this.drawCircleArrow(line, true); } } } }; SPainter.prototype.drawBasicArrow = function (line, isEnd) { if (isEnd === void 0) { isEnd = true; } var d = 5; var x1 = d * Math.cos(Math.PI / 4); var y1 = d * Math.sin(Math.PI / 4); var fo = Math.atan(line.dy / line.dx); var ang = line.dx >= 0 ? fo : fo + Math.PI; if (isEnd) { this.save(); this.translate(line.x2, line.y2); this.rotate(ang); this.engine.drawPolyline([ new _1.SPoint(-x1, y1), new _1.SPoint(0, 0), new _1.SPoint(-x1, -y1) ]); this.restore(); } else { this.save(); this.translate(line.x1, line.y1); this.rotate(ang); this.engine.drawPolyline([ new _1.SPoint(x1, y1), new _1.SPoint(0, 0), new _1.SPoint(x1, -y1) ]); this.restore(); } }; SPainter.prototype.drawTriangleArrow = function (line, isEnd) { if (isEnd === void 0) { isEnd = true; } var d = 5; var x1 = d * Math.cos(Math.PI / 12); var y1 = d * Math.sin(Math.PI / 12); var fo = Math.atan(line.dy / line.dx); var ang = line.dx >= 0 ? fo : fo + Math.PI; if (isEnd) { this.save(); this.translate(line.x2, line.y2); this.rotate(ang); this.engine.drawPolygon([ new _1.SPoint(-x1, y1), new _1.SPoint(0, 0), new _1.SPoint(-x1, -y1) ]); this.restore(); } else { this.save(); this.translate(line.x1, line.y1); this.rotate(ang); this.engine.drawPolygon([ new _1.SPoint(x1, y1), new _1.SPoint(0, 0), new _1.SPoint(x1, -y1) ]); this.restore(); } }; SPainter.prototype.drawDiamondArrow = function (line, isEnd) { if (isEnd === void 0) { isEnd = true; } var d = 2; var x1 = d * Math.cos(Math.PI / 4); var y1 = d * Math.sin(Math.PI / 4); var fo = Math.atan(line.dy / line.dx); var ang = line.dx >= 0 ? fo : fo + Math.PI; if (isEnd) { this.save(); this.translate(line.x2, line.y2); this.rotate(ang); this.engine.drawPolygon([ new _1.SPoint(-x1, y1), new _1.SPoint(0, 0), new _1.SPoint(-x1, -y1), new _1.SPoint(-Math.sqrt(2) * d, 0) ]); this.restore(); } else { this.save(); this.translate(line.x1, line.y1); this.rotate(ang); this.engine.drawPolygon([ new _1.SPoint(x1, y1), new _1.SPoint(0, 0), new _1.SPoint(x1, -y1), new _1.SPoint(Math.sqrt(2) * d, 0) ]); this.restore(); } }; SPainter.prototype.drawSquareArrow = function (line, isEnd) { if (isEnd === void 0) { isEnd = true; } var d = 2; var fo = Math.atan(line.dy / line.dx); var ang = line.dx >= 0 ? fo : fo + Math.PI; if (isEnd) { this.save(); this.translate(line.x2, line.y2); this.rotate(ang); this.engine.drawPolygon([ new _1.SPoint(-d, d / 2), new _1.SPoint(0, d / 2), new _1.SPoint(0, -d / 2), new _1.SPoint(-d, -d / 2) ]); this.restore(); } else { this.save(); this.translate(line.x1, line.y1); this.rotate(ang); this.engine.drawPolygon([ new _1.SPoint(0, d / 2), new _1.SPoint(d, d / 2), new _1.SPoint(d, -d / 2), new _1.SPoint(0, -d / 2) ]); this.restore(); } }; SPainter.prototype.drawCircleArrow = function (line, isEnd) { if (isEnd === void 0) { isEnd = true; } var d = 2; var fo = Math.atan(line.dy / line.dx); var ang = line.dx >= 0 ? fo : fo + Math.PI; if (isEnd) { this.save(); this.translate(line.x2, line.y2); this.rotate(ang); this.engine.drawCircle(-d / 2, 0, d / 2); this.restore(); } else { this.save(); this.translate(line.x1, line.y1); this.rotate(ang); this.engine.drawCircle(d / 2, 0, d / 2); this.restore(); } }; return SPainter; }(lib_1.SObject)); exports.SPainter = SPainter;