laya-coreui-es
Version:
laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改
393 lines (392 loc) • 17.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Graphics = void 0;
const GraphicsBounds_1 = require("./GraphicsBounds");
const SpriteConst_1 = require("./SpriteConst");
const AlphaCmd_1 = require("./cmd/AlphaCmd");
const ClipRectCmd_1 = require("./cmd/ClipRectCmd");
const Draw9GridTexture_1 = require("./cmd/Draw9GridTexture");
const DrawCircleCmd_1 = require("./cmd/DrawCircleCmd");
const DrawCurvesCmd_1 = require("./cmd/DrawCurvesCmd");
const DrawImageCmd_1 = require("./cmd/DrawImageCmd");
const DrawLineCmd_1 = require("./cmd/DrawLineCmd");
const DrawLinesCmd_1 = require("./cmd/DrawLinesCmd");
const DrawPathCmd_1 = require("./cmd/DrawPathCmd");
const DrawPieCmd_1 = require("./cmd/DrawPieCmd");
const DrawPolyCmd_1 = require("./cmd/DrawPolyCmd");
const DrawRectCmd_1 = require("./cmd/DrawRectCmd");
const DrawTextureCmd_1 = require("./cmd/DrawTextureCmd");
const DrawTexturesCmd_1 = require("./cmd/DrawTexturesCmd");
const DrawTrianglesCmd_1 = require("./cmd/DrawTrianglesCmd");
const FillTextCmd_1 = require("./cmd/FillTextCmd");
const FillTextureCmd_1 = require("./cmd/FillTextureCmd");
const RestoreCmd_1 = require("./cmd/RestoreCmd");
const RotateCmd_1 = require("./cmd/RotateCmd");
const SaveCmd_1 = require("./cmd/SaveCmd");
const ScaleCmd_1 = require("./cmd/ScaleCmd");
const TransformCmd_1 = require("./cmd/TransformCmd");
const TranslateCmd_1 = require("./cmd/TranslateCmd");
const Event_1 = require("../events/Event");
const Point_1 = require("../maths/Point");
const Render_1 = require("../renders/Render");
const Texture_1 = require("../resource/Texture");
const Utils_1 = require("../utils/Utils");
const VectorGraphManager_1 = require("../utils/VectorGraphManager");
const ILaya_1 = require("../../ILaya");
class Graphics {
constructor() {
this._sp = null;
this._one = null;
this._render = this._renderEmpty;
this._cmds = null;
this._vectorgraphArray = null;
this._graphicBounds = null;
this.autoDestroy = false;
this._createData();
}
_createData() {
}
_clearData() {
}
_destroyData() {
}
destroy() {
this.clear(true);
if (this._graphicBounds)
this._graphicBounds.destroy();
this._graphicBounds = null;
this._vectorgraphArray = null;
if (this._sp) {
this._sp._renderType = 0;
this._sp._setRenderType(0);
this._sp = null;
}
this._destroyData();
}
clear(recoverCmds = true) {
if (recoverCmds) {
var tCmd = this._one;
if (this._cmds) {
var i, len = this._cmds.length;
for (i = 0; i < len; i++) {
tCmd = this._cmds[i];
tCmd.recover();
}
this._cmds.length = 0;
}
else if (tCmd) {
tCmd.recover();
}
}
else {
this._cmds = null;
}
this._one = null;
this._render = this._renderEmpty;
this._clearData();
if (this._sp) {
this._sp._renderType &= ~SpriteConst_1.SpriteConst.GRAPHICS;
this._sp._setRenderType(this._sp._renderType);
}
this._repaint();
if (this._vectorgraphArray) {
for (i = 0, len = this._vectorgraphArray.length; i < len; i++) {
VectorGraphManager_1.VectorGraphManager.getInstance().deleteShape(this._vectorgraphArray[i]);
}
this._vectorgraphArray.length = 0;
}
}
_clearBoundsCache() {
if (this._graphicBounds)
this._graphicBounds.reset();
}
_initGraphicBounds() {
if (!this._graphicBounds) {
this._graphicBounds = GraphicsBounds_1.GraphicsBounds.create();
this._graphicBounds._graphics = this;
}
}
_repaint() {
this._clearBoundsCache();
this._sp && this._sp.repaint();
}
_isOnlyOne() {
return !this._cmds || this._cmds.length === 0;
}
get cmds() {
return this._cmds;
}
set cmds(value) {
if (this._sp) {
this._sp._renderType |= SpriteConst_1.SpriteConst.GRAPHICS;
this._sp._setRenderType(this._sp._renderType);
}
this._cmds = value;
this._render = this._renderAll;
this._repaint();
}
getBounds(realSize = false) {
this._initGraphicBounds();
return this._graphicBounds.getBounds(realSize);
}
getBoundPoints(realSize = false) {
this._initGraphicBounds();
return this._graphicBounds.getBoundPoints(realSize);
}
drawImage(texture, x = 0, y = 0, width = 0, height = 0) {
if (!texture)
return null;
if (!width)
width = texture.sourceWidth;
if (!height)
height = texture.sourceHeight;
if (texture.getIsReady()) {
var wRate = width / texture.sourceWidth;
var hRate = height / texture.sourceHeight;
width = texture.width * wRate;
height = texture.height * hRate;
if (width <= 0 || height <= 0)
return null;
x += texture.offsetX * wRate;
y += texture.offsetY * hRate;
}
if (this._sp) {
this._sp._renderType |= SpriteConst_1.SpriteConst.GRAPHICS;
this._sp._setRenderType(this._sp._renderType);
}
var args = DrawImageCmd_1.DrawImageCmd.create.call(this, texture, x, y, width, height);
if (this._one == null) {
this._one = args;
this._render = this._renderOneImg;
}
else {
this._saveToCmd(null, args);
}
this._repaint();
return args;
}
drawTexture(texture, x = 0, y = 0, width = 0, height = 0, matrix = null, alpha = 1, color = null, blendMode = null, uv) {
if (!texture || alpha < 0.01)
return null;
if (!texture.getIsReady())
return null;
if (!width)
width = texture.sourceWidth;
if (!height)
height = texture.sourceHeight;
if (texture.getIsReady()) {
var wRate = width / texture.sourceWidth;
var hRate = height / texture.sourceHeight;
width = texture.width * wRate;
height = texture.height * hRate;
if (width <= 0 || height <= 0)
return null;
x += texture.offsetX * wRate;
y += texture.offsetY * hRate;
}
if (this._sp) {
this._sp._renderType |= SpriteConst_1.SpriteConst.GRAPHICS;
this._sp._setRenderType(this._sp._renderType);
}
var args = DrawTextureCmd_1.DrawTextureCmd.create.call(this, texture, x, y, width, height, matrix, alpha, color, blendMode, uv);
this._repaint();
return this._saveToCmd(null, args);
}
drawTextures(texture, pos) {
if (!texture)
return null;
return this._saveToCmd(Render_1.Render._context.drawTextures, DrawTexturesCmd_1.DrawTexturesCmd.create.call(this, texture, pos));
}
drawTriangles(texture, x, y, vertices, uvs, indices, matrix = null, alpha = 1, color = null, blendMode = null, colorNum = 0xffffffff) {
return this._saveToCmd(Render_1.Render._context.drawTriangles, DrawTrianglesCmd_1.DrawTrianglesCmd.create.call(this, texture, x, y, vertices, uvs, indices, matrix, alpha, color, blendMode, colorNum));
}
fillTexture(texture, x, y, width = 0, height = 0, type = "repeat", offset = null) {
if (texture && texture.getIsReady())
return this._saveToCmd(Render_1.Render._context._fillTexture, FillTextureCmd_1.FillTextureCmd.create.call(this, texture, x, y, width, height, type, offset || Point_1.Point.EMPTY, {}));
else
return null;
}
_saveToCmd(fun, args) {
if (this._sp) {
this._sp._renderType |= SpriteConst_1.SpriteConst.GRAPHICS;
this._sp._setRenderType(this._sp._renderType);
}
if (this._one == null) {
this._one = args;
this._render = this._renderOne;
}
else {
this._render = this._renderAll;
(this._cmds || (this._cmds = [])).length === 0 && this._cmds.push(this._one);
this._cmds.push(args);
}
this._repaint();
return args;
}
clipRect(x, y, width, height) {
return this._saveToCmd(Render_1.Render._context.clipRect, ClipRectCmd_1.ClipRectCmd.create.call(this, x, y, width, height));
}
fillText(text, x, y, font, color, textAlign) {
return this._saveToCmd(Render_1.Render._context.fillText, FillTextCmd_1.FillTextCmd.create.call(this, text, null, x, y, font || ILaya_1.ILaya.Text.defaultFontStr(), color, textAlign, 0, ""));
}
fillBorderText(text, x, y, font, fillColor, textAlign, lineWidth, borderColor) {
return this._saveToCmd(Render_1.Render._context.fillText, FillTextCmd_1.FillTextCmd.create.call(this, text, null, x, y, font || ILaya_1.ILaya.Text.defaultFontStr(), fillColor, textAlign, lineWidth, borderColor));
}
fillWords(words, x, y, font, color) {
return this._saveToCmd(Render_1.Render._context.fillText, FillTextCmd_1.FillTextCmd.create.call(this, null, words, x, y, font || ILaya_1.ILaya.Text.defaultFontStr(), color, '', 0, null));
}
fillBorderWords(words, x, y, font, fillColor, borderColor, lineWidth) {
return this._saveToCmd(Render_1.Render._context.fillText, FillTextCmd_1.FillTextCmd.create.call(this, null, words, x, y, font || ILaya_1.ILaya.Text.defaultFontStr(), fillColor, "", lineWidth, borderColor));
}
strokeText(text, x, y, font, color, lineWidth, textAlign) {
return this._saveToCmd(Render_1.Render._context.fillText, FillTextCmd_1.FillTextCmd.create.call(this, text, null, x, y, font || ILaya_1.ILaya.Text.defaultFontStr(), null, textAlign, lineWidth, color));
}
alpha(alpha) {
return this._saveToCmd(Render_1.Render._context.alpha, AlphaCmd_1.AlphaCmd.create.call(this, alpha));
}
transform(matrix, pivotX = 0, pivotY = 0) {
return this._saveToCmd(Render_1.Render._context._transform, TransformCmd_1.TransformCmd.create.call(this, matrix, pivotX, pivotY));
}
rotate(angle, pivotX = 0, pivotY = 0) {
return this._saveToCmd(Render_1.Render._context._rotate, RotateCmd_1.RotateCmd.create.call(this, angle, pivotX, pivotY));
}
scale(scaleX, scaleY, pivotX = 0, pivotY = 0) {
return this._saveToCmd(Render_1.Render._context._scale, ScaleCmd_1.ScaleCmd.create.call(this, scaleX, scaleY, pivotX, pivotY));
}
translate(tx, ty) {
return this._saveToCmd(Render_1.Render._context.translate, TranslateCmd_1.TranslateCmd.create.call(this, tx, ty));
}
save() {
return this._saveToCmd(Render_1.Render._context._save, SaveCmd_1.SaveCmd.create.call(this));
}
restore() {
return this._saveToCmd(Render_1.Render._context.restore, RestoreCmd_1.RestoreCmd.create.call(this));
}
replaceText(text) {
this._repaint();
var cmds = this._cmds;
if (!cmds) {
if (this._one && this._isTextCmd(this._one)) {
this._one.text = text;
return true;
}
}
else {
for (var i = cmds.length - 1; i > -1; i--) {
if (this._isTextCmd(cmds[i])) {
cmds[i].text = text;
return true;
}
}
}
return false;
}
_isTextCmd(cmd) {
var cmdID = cmd.cmdID;
return cmdID == FillTextCmd_1.FillTextCmd.ID;
}
replaceTextColor(color) {
this._repaint();
var cmds = this._cmds;
if (!cmds) {
if (this._one && this._isTextCmd(this._one)) {
this._setTextCmdColor(this._one, color);
}
}
else {
for (var i = cmds.length - 1; i > -1; i--) {
if (this._isTextCmd(cmds[i])) {
this._setTextCmdColor(cmds[i], color);
}
}
}
}
_setTextCmdColor(cmdO, color) {
var cmdID = cmdO.cmdID;
switch (cmdID) {
case FillTextCmd_1.FillTextCmd.ID:
cmdO.color = color;
break;
}
}
loadImage(url, x = 0, y = 0, width = 0, height = 0, complete = null) {
var tex = ILaya_1.ILaya.Loader.getRes(url);
if (!tex) {
tex = new Texture_1.Texture();
tex.load(url);
ILaya_1.ILaya.Loader.cacheTexture(url, tex);
tex.once(Event_1.Event.READY, this, this.drawImage, [tex, x, y, width, height]);
}
else {
if (!tex.getIsReady()) {
tex.once(Event_1.Event.READY, this, this.drawImage, [tex, x, y, width, height]);
}
else
this.drawImage(tex, x, y, width, height);
}
if (complete != null) {
tex.getIsReady() ? complete.call(this._sp) : tex.on(Event_1.Event.READY, this._sp, complete);
}
}
_renderEmpty(sprite, context, x, y) {
}
_renderAll(sprite, context, x, y) {
var cmds = this._cmds;
for (var i = 0, n = cmds.length; i < n; i++) {
cmds[i].run(context, x, y);
}
}
_renderOne(sprite, context, x, y) {
context.sprite = sprite;
this._one.run(context, x, y);
}
_renderOneImg(sprite, context, x, y) {
context.sprite = sprite;
this._one.run(context, x, y);
}
drawLine(fromX, fromY, toX, toY, lineColor, lineWidth = 1) {
var offset = (lineWidth < 1 || lineWidth % 2 === 0) ? 0 : 0.5;
return this._saveToCmd(Render_1.Render._context._drawLine, DrawLineCmd_1.DrawLineCmd.create.call(this, fromX + offset, fromY + offset, toX + offset, toY + offset, lineColor, lineWidth, 0));
}
drawLines(x, y, points, lineColor, lineWidth = 1) {
if (!points || points.length < 4)
return null;
var offset = (lineWidth < 1 || lineWidth % 2 === 0) ? 0 : 0.5;
return this._saveToCmd(Render_1.Render._context._drawLines, DrawLinesCmd_1.DrawLinesCmd.create.call(this, x + offset, y + offset, points, lineColor, lineWidth, 0));
}
drawCurves(x, y, points, lineColor, lineWidth = 1) {
return this._saveToCmd(Render_1.Render._context.drawCurves, DrawCurvesCmd_1.DrawCurvesCmd.create.call(this, x, y, points, lineColor, lineWidth));
}
drawRect(x, y, width, height, fillColor, lineColor = null, lineWidth = 1) {
var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
var lineOffset = lineColor ? lineWidth : 0;
return this._saveToCmd(Render_1.Render._context.drawRect, DrawRectCmd_1.DrawRectCmd.create.call(this, x + offset, y + offset, width - lineOffset, height - lineOffset, fillColor, lineColor, lineWidth));
}
drawCircle(x, y, radius, fillColor, lineColor = null, lineWidth = 1) {
var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
return this._saveToCmd(Render_1.Render._context._drawCircle, DrawCircleCmd_1.DrawCircleCmd.create.call(this, x, y, radius - offset, fillColor, lineColor, lineWidth, 0));
}
drawPie(x, y, radius, startAngle, endAngle, fillColor, lineColor = null, lineWidth = 1) {
var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
var lineOffset = lineColor ? lineWidth : 0;
return this._saveToCmd(Render_1.Render._context._drawPie, DrawPieCmd_1.DrawPieCmd.create.call(this, x + offset, y + offset, radius - lineOffset, Utils_1.Utils.toRadian(startAngle), Utils_1.Utils.toRadian(endAngle), fillColor, lineColor, lineWidth, 0));
}
drawPoly(x, y, points, fillColor, lineColor = null, lineWidth = 1) {
var tIsConvexPolygon = false;
if (points.length > 6) {
tIsConvexPolygon = false;
}
else {
tIsConvexPolygon = true;
}
var offset = (lineWidth >= 1 && lineColor) ? (lineWidth % 2 === 0 ? 0 : 0.5) : 0;
return this._saveToCmd(Render_1.Render._context._drawPoly, DrawPolyCmd_1.DrawPolyCmd.create.call(this, x + offset, y + offset, points, fillColor, lineColor, lineWidth, tIsConvexPolygon, 0));
}
drawPath(x, y, paths, brush = null, pen = null) {
return this._saveToCmd(Render_1.Render._context._drawPath, DrawPathCmd_1.DrawPathCmd.create.call(this, x, y, paths, brush, pen));
}
draw9Grid(texture, x = 0, y = 0, width = 0, height = 0, sizeGrid) {
this._saveToCmd(null, Draw9GridTexture_1.Draw9GridTexture.create(texture, x, y, width, height, sizeGrid));
}
}
exports.Graphics = Graphics;