laya-coreui-es
Version:
laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改
1,544 lines (1,518 loc) • 1.06 MB
JavaScript
'use strict';
class ILaya {
static regClass(c) {
ILaya.__classMap[c.name] = c;
}
}
ILaya.Laya = null;
ILaya.Timer = null;
ILaya.Dragging = null;
ILaya.Sprite = null;
ILaya.TextRender = null;
ILaya.Loader = null;
ILaya.ShaderCompile = null;
ILaya.ClassUtils = null;
ILaya.SceneUtils = null;
ILaya.Context = null;
ILaya.Render = null;
ILaya.MouseManager = null;
ILaya.Text = null;
ILaya.Browser = null;
ILaya.Pool = null;
ILaya.Utils = null;
ILaya.Graphics = null;
ILaya.WorkerLoader = null;
ILaya.TextAtlas = null;
ILaya.timer = null;
ILaya.systemTimer = null;
ILaya.startTimer = null;
ILaya.updateTimer = null;
ILaya.lateTimer = null;
ILaya.physicsTimer = null;
ILaya.stage = null;
ILaya.loader = null;
ILaya.__classMap = {};
class Pool {
static getPoolBySign(sign) {
return Pool._poolDic[sign] || (Pool._poolDic[sign] = []);
}
static clearBySign(sign) {
if (Pool._poolDic[sign])
Pool._poolDic[sign].length = 0;
}
static recover(sign, item) {
if (item[Pool.POOLSIGN])
return;
item[Pool.POOLSIGN] = true;
Pool.getPoolBySign(sign).push(item);
}
static recoverByClass(instance) {
if (instance) {
var className = instance["__className"] || instance.constructor._$gid;
if (className)
Pool.recover(className, instance);
}
}
static _getClassSign(cla) {
var className = cla["__className"] || cla["_$gid"];
if (!className) {
cla["_$gid"] = className = Pool._CLSID + "";
Pool._CLSID++;
}
return className;
}
static createByClass(cls) {
return Pool.getItemByClass(Pool._getClassSign(cls), cls);
}
static getItemByClass(sign, cls) {
if (!Pool._poolDic[sign])
return new cls();
var pool = Pool.getPoolBySign(sign);
if (pool.length) {
var rst = pool.pop();
rst[Pool.POOLSIGN] = false;
}
else {
rst = new cls();
}
return rst;
}
static getItemByCreateFun(sign, createFun, caller = null) {
var pool = Pool.getPoolBySign(sign);
var rst = pool.length ? pool.pop() : createFun.call(caller);
rst[Pool.POOLSIGN] = false;
return rst;
}
static getItem(sign) {
var pool = Pool.getPoolBySign(sign);
var rst = pool.length ? pool.pop() : null;
if (rst) {
rst[Pool.POOLSIGN] = false;
}
return rst;
}
}
Pool._CLSID = 0;
Pool.POOLSIGN = "__InPool";
Pool._poolDic = {};
class AlphaCmd {
static create(alpha) {
var cmd = Pool.getItemByClass("AlphaCmd", AlphaCmd);
cmd.alpha = alpha;
return cmd;
}
recover() {
Pool.recover("AlphaCmd", this);
}
run(context, gx, gy) {
context.alpha(this.alpha);
}
get cmdID() {
return AlphaCmd.ID;
}
}
AlphaCmd.ID = "Alpha";
class DrawCircleCmd {
static create(x, y, radius, fillColor, lineColor, lineWidth, vid) {
var cmd = Pool.getItemByClass("DrawCircleCmd", DrawCircleCmd);
cmd.x = x;
cmd.y = y;
cmd.radius = radius;
cmd.fillColor = fillColor;
cmd.lineColor = lineColor;
cmd.lineWidth = lineWidth;
cmd.vid = vid;
return cmd;
}
recover() {
this.fillColor = null;
this.lineColor = null;
Pool.recover("DrawCircleCmd", this);
}
run(context, gx, gy) {
context._drawCircle(this.x + gx, this.y + gy, this.radius, this.fillColor, this.lineColor, this.lineWidth, this.vid);
}
get cmdID() {
return DrawCircleCmd.ID;
}
}
DrawCircleCmd.ID = "DrawCircle";
class DrawCurvesCmd {
static create(x, y, points, lineColor, lineWidth) {
var cmd = Pool.getItemByClass("DrawCurvesCmd", DrawCurvesCmd);
cmd.x = x;
cmd.y = y;
cmd.points = points;
cmd.lineColor = lineColor;
cmd.lineWidth = lineWidth;
return cmd;
}
recover() {
this.points = null;
this.lineColor = null;
Pool.recover("DrawCurvesCmd", this);
}
run(context, gx, gy) {
if (this.points)
context.drawCurves(this.x + gx, this.y + gy, this.points, this.lineColor, this.lineWidth);
}
get cmdID() {
return DrawCurvesCmd.ID;
}
}
DrawCurvesCmd.ID = "DrawCurves";
class DrawImageCmd {
static create(texture, x, y, width, height) {
var cmd = Pool.getItemByClass("DrawImageCmd", DrawImageCmd);
cmd.texture = texture;
texture._addReference();
cmd.x = x;
cmd.y = y;
cmd.width = width;
cmd.height = height;
return cmd;
}
recover() {
this.texture && this.texture._removeReference();
this.texture = null;
Pool.recover("DrawImageCmd", this);
}
run(context, gx, gy) {
if (this.texture)
context.drawTexture(this.texture, this.x + gx, this.y + gy, this.width, this.height);
}
get cmdID() {
return DrawImageCmd.ID;
}
}
DrawImageCmd.ID = "DrawImage";
class DrawLineCmd {
static create(fromX, fromY, toX, toY, lineColor, lineWidth, vid) {
var cmd = Pool.getItemByClass("DrawLineCmd", DrawLineCmd);
cmd.fromX = fromX;
cmd.fromY = fromY;
cmd.toX = toX;
cmd.toY = toY;
cmd.lineColor = lineColor;
cmd.lineWidth = lineWidth;
cmd.vid = vid;
return cmd;
}
recover() {
Pool.recover("DrawLineCmd", this);
}
run(context, gx, gy) {
context._drawLine(gx, gy, this.fromX, this.fromY, this.toX, this.toY, this.lineColor, this.lineWidth, this.vid);
}
get cmdID() {
return DrawLineCmd.ID;
}
}
DrawLineCmd.ID = "DrawLine";
class DrawLinesCmd {
static create(x, y, points, lineColor, lineWidth, vid) {
var cmd = Pool.getItemByClass("DrawLinesCmd", DrawLinesCmd);
cmd.x = x;
cmd.y = y;
cmd.points = points;
cmd.lineColor = lineColor;
cmd.lineWidth = lineWidth;
cmd.vid = vid;
return cmd;
}
recover() {
this.points = null;
this.lineColor = null;
Pool.recover("DrawLinesCmd", this);
}
run(context, gx, gy) {
this.points && context._drawLines(this.x + gx, this.y + gy, this.points, this.lineColor, this.lineWidth, this.vid);
}
get cmdID() {
return DrawLinesCmd.ID;
}
}
DrawLinesCmd.ID = "DrawLines";
class DrawPathCmd {
static create(x, y, paths, brush, pen) {
var cmd = Pool.getItemByClass("DrawPathCmd", DrawPathCmd);
cmd.x = x;
cmd.y = y;
cmd.paths = paths;
cmd.brush = brush;
cmd.pen = pen;
return cmd;
}
recover() {
this.paths = null;
this.brush = null;
this.pen = null;
Pool.recover("DrawPathCmd", this);
}
run(context, gx, gy) {
this.paths && context._drawPath(this.x + gx, this.y + gy, this.paths, this.brush, this.pen);
}
get cmdID() {
return DrawPathCmd.ID;
}
}
DrawPathCmd.ID = "DrawPath";
class DrawPieCmd {
static create(x, y, radius, startAngle, endAngle, fillColor, lineColor, lineWidth, vid) {
var cmd = Pool.getItemByClass("DrawPieCmd", DrawPieCmd);
cmd.x = x;
cmd.y = y;
cmd.radius = radius;
cmd._startAngle = startAngle;
cmd._endAngle = endAngle;
cmd.fillColor = fillColor;
cmd.lineColor = lineColor;
cmd.lineWidth = lineWidth;
cmd.vid = vid;
return cmd;
}
recover() {
this.fillColor = null;
this.lineColor = null;
Pool.recover("DrawPieCmd", this);
}
run(context, gx, gy) {
context._drawPie(this.x + gx, this.y + gy, this.radius, this._startAngle, this._endAngle, this.fillColor, this.lineColor, this.lineWidth, this.vid);
}
get cmdID() {
return DrawPieCmd.ID;
}
get startAngle() {
return this._startAngle * 180 / Math.PI;
}
set startAngle(value) {
this._startAngle = value * Math.PI / 180;
}
get endAngle() {
return this._endAngle * 180 / Math.PI;
}
set endAngle(value) {
this._endAngle = value * Math.PI / 180;
}
}
DrawPieCmd.ID = "DrawPie";
class DrawPolyCmd {
static create(x, y, points, fillColor, lineColor, lineWidth, isConvexPolygon, vid) {
var cmd = Pool.getItemByClass("DrawPolyCmd", DrawPolyCmd);
cmd.x = x;
cmd.y = y;
cmd.points = points;
cmd.fillColor = fillColor;
cmd.lineColor = lineColor;
cmd.lineWidth = lineWidth;
cmd.isConvexPolygon = isConvexPolygon;
cmd.vid = vid;
return cmd;
}
recover() {
this.points = null;
this.fillColor = null;
this.lineColor = null;
Pool.recover("DrawPolyCmd", this);
}
run(context, gx, gy) {
this.points && context._drawPoly(this.x + gx, this.y + gy, this.points, this.fillColor, this.lineColor, this.lineWidth, this.isConvexPolygon, this.vid);
}
get cmdID() {
return DrawPolyCmd.ID;
}
}
DrawPolyCmd.ID = "DrawPoly";
class DrawRectCmd {
static create(x, y, width, height, fillColor, lineColor, lineWidth) {
var cmd = Pool.getItemByClass("DrawRectCmd", DrawRectCmd);
cmd.x = x;
cmd.y = y;
cmd.width = width;
cmd.height = height;
cmd.fillColor = fillColor;
cmd.lineColor = lineColor;
cmd.lineWidth = lineWidth;
return cmd;
}
recover() {
this.fillColor = null;
this.lineColor = null;
Pool.recover("DrawRectCmd", this);
}
run(context, gx, gy) {
context.drawRect(this.x + gx, this.y + gy, this.width, this.height, this.fillColor, this.lineColor, this.lineWidth);
}
get cmdID() {
return DrawRectCmd.ID;
}
}
DrawRectCmd.ID = "DrawRect";
class Matrix {
constructor(a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0, nums = 0) {
this._bTransform = false;
if (Matrix._createFun != null) {
return Matrix._createFun(a, b, c, d, tx, ty, nums);
}
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.tx = tx;
this.ty = ty;
this._checkTransform();
}
identity() {
this.a = this.d = 1;
this.b = this.tx = this.ty = this.c = 0;
this._bTransform = false;
return this;
}
_checkTransform() {
return this._bTransform = (this.a !== 1 || this.b !== 0 || this.c !== 0 || this.d !== 1);
}
setTranslate(x, y) {
this.tx = x;
this.ty = y;
return this;
}
translate(x, y) {
this.tx += x;
this.ty += y;
return this;
}
scale(x, y) {
this.a *= x;
this.d *= y;
this.c *= x;
this.b *= y;
this.tx *= x;
this.ty *= y;
this._bTransform = true;
return this;
}
rotate(angle) {
var cos = Math.cos(angle);
var sin = Math.sin(angle);
var a1 = this.a;
var c1 = this.c;
var tx1 = this.tx;
this.a = a1 * cos - this.b * sin;
this.b = a1 * sin + this.b * cos;
this.c = c1 * cos - this.d * sin;
this.d = c1 * sin + this.d * cos;
this.tx = tx1 * cos - this.ty * sin;
this.ty = tx1 * sin + this.ty * cos;
this._bTransform = true;
return this;
}
skew(x, y) {
var tanX = Math.tan(x);
var tanY = Math.tan(y);
var a1 = this.a;
var b1 = this.b;
this.a += tanY * this.c;
this.b += tanY * this.d;
this.c += tanX * a1;
this.d += tanX * b1;
return this;
}
invertTransformPoint(out) {
var a1 = this.a;
var b1 = this.b;
var c1 = this.c;
var d1 = this.d;
var tx1 = this.tx;
var n = a1 * d1 - b1 * c1;
var a2 = d1 / n;
var b2 = -b1 / n;
var c2 = -c1 / n;
var d2 = a1 / n;
var tx2 = (c1 * this.ty - d1 * tx1) / n;
var ty2 = -(a1 * this.ty - b1 * tx1) / n;
return out.setTo(a2 * out.x + c2 * out.y + tx2, b2 * out.x + d2 * out.y + ty2);
}
transformPoint(out) {
return out.setTo(this.a * out.x + this.c * out.y + this.tx, this.b * out.x + this.d * out.y + this.ty);
}
transformPointN(out) {
return out.setTo(this.a * out.x + this.c * out.y, this.b * out.x + this.d * out.y);
}
getScaleX() {
return this.b === 0 ? this.a : Math.sqrt(this.a * this.a + this.b * this.b);
}
getScaleY() {
return this.c === 0 ? this.d : Math.sqrt(this.c * this.c + this.d * this.d);
}
invert() {
var a1 = this.a;
var b1 = this.b;
var c1 = this.c;
var d1 = this.d;
var tx1 = this.tx;
var n = a1 * d1 - b1 * c1;
this.a = d1 / n;
this.b = -b1 / n;
this.c = -c1 / n;
this.d = a1 / n;
this.tx = (c1 * this.ty - d1 * tx1) / n;
this.ty = -(a1 * this.ty - b1 * tx1) / n;
return this;
}
setTo(a, b, c, d, tx, ty) {
this.a = a, this.b = b, this.c = c, this.d = d, this.tx = tx, this.ty = ty;
return this;
}
concat(matrix) {
var a = this.a;
var c = this.c;
var tx = this.tx;
this.a = a * matrix.a + this.b * matrix.c;
this.b = a * matrix.b + this.b * matrix.d;
this.c = c * matrix.a + this.d * matrix.c;
this.d = c * matrix.b + this.d * matrix.d;
this.tx = tx * matrix.a + this.ty * matrix.c + matrix.tx;
this.ty = tx * matrix.b + this.ty * matrix.d + matrix.ty;
return this;
}
static mul(m1, m2, out) {
var aa = m1.a, ab = m1.b, ac = m1.c, ad = m1.d, atx = m1.tx, aty = m1.ty;
var ba = m2.a, bb = m2.b, bc = m2.c, bd = m2.d, btx = m2.tx, bty = m2.ty;
if (bb !== 0 || bc !== 0) {
out.a = aa * ba + ab * bc;
out.b = aa * bb + ab * bd;
out.c = ac * ba + ad * bc;
out.d = ac * bb + ad * bd;
out.tx = ba * atx + bc * aty + btx;
out.ty = bb * atx + bd * aty + bty;
}
else {
out.a = aa * ba;
out.b = ab * bd;
out.c = ac * ba;
out.d = ad * bd;
out.tx = ba * atx + btx;
out.ty = bd * aty + bty;
}
return out;
}
static mul16(m1, m2, out) {
var aa = m1.a, ab = m1.b, ac = m1.c, ad = m1.d, atx = m1.tx, aty = m1.ty;
var ba = m2.a, bb = m2.b, bc = m2.c, bd = m2.d, btx = m2.tx, bty = m2.ty;
if (bb !== 0 || bc !== 0) {
out[0] = aa * ba + ab * bc;
out[1] = aa * bb + ab * bd;
out[4] = ac * ba + ad * bc;
out[5] = ac * bb + ad * bd;
out[12] = ba * atx + bc * aty + btx;
out[13] = bb * atx + bd * aty + bty;
}
else {
out[0] = aa * ba;
out[1] = ab * bd;
out[4] = ac * ba;
out[5] = ad * bd;
out[12] = ba * atx + btx;
out[13] = bd * aty + bty;
}
return out;
}
scaleEx(x, y) {
var ba = this.a, bb = this.b, bc = this.c, bd = this.d;
if (bb !== 0 || bc !== 0) {
this.a = x * ba;
this.b = x * bb;
this.c = y * bc;
this.d = y * bd;
}
else {
this.a = x * ba;
this.b = 0 * bd;
this.c = 0 * ba;
this.d = y * bd;
}
this._bTransform = true;
}
rotateEx(angle) {
var cos = Math.cos(angle);
var sin = Math.sin(angle);
var ba = this.a, bb = this.b, bc = this.c, bd = this.d;
if (bb !== 0 || bc !== 0) {
this.a = cos * ba + sin * bc;
this.b = cos * bb + sin * bd;
this.c = -sin * ba + cos * bc;
this.d = -sin * bb + cos * bd;
}
else {
this.a = cos * ba;
this.b = sin * bd;
this.c = -sin * ba;
this.d = cos * bd;
}
this._bTransform = true;
}
clone() {
var dec = Matrix.create();
dec.a = this.a;
dec.b = this.b;
dec.c = this.c;
dec.d = this.d;
dec.tx = this.tx;
dec.ty = this.ty;
dec._bTransform = this._bTransform;
return dec;
}
copyTo(dec) {
dec.a = this.a;
dec.b = this.b;
dec.c = this.c;
dec.d = this.d;
dec.tx = this.tx;
dec.ty = this.ty;
dec._bTransform = this._bTransform;
return dec;
}
toString() {
return this.a + "," + this.b + "," + this.c + "," + this.d + "," + this.tx + "," + this.ty;
}
destroy() {
this.recover();
}
recover() {
Pool.recover("Matrix", this.identity());
}
static create() {
return Pool.getItemByClass("Matrix", Matrix);
}
}
Matrix.EMPTY = new Matrix();
Matrix.TEMP = new Matrix();
Matrix._createFun = null;
class Point {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
static create() {
return Pool.getItemByClass("Point", Point);
}
setTo(x, y) {
this.x = x;
this.y = y;
return this;
}
reset() {
this.x = this.y = 0;
return this;
}
recover() {
Pool.recover("Point", this.reset());
}
distance(x, y) {
return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));
}
toString() {
return this.x + "," + this.y;
}
normalize() {
var d = Math.sqrt(this.x * this.x + this.y * this.y);
if (d > 0) {
var id = 1.0 / d;
this.x *= id;
this.y *= id;
}
}
copy(point) {
return this.setTo(point.x, point.y);
}
}
Point.TEMP = new Point();
Point.EMPTY = new Point();
class Rectangle {
constructor(x = 0, y = 0, width = 0, height = 0) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
get right() {
return this.x + this.width;
}
get bottom() {
return this.y + this.height;
}
setTo(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
return this;
}
reset() {
this.x = this.y = this.width = this.height = 0;
return this;
}
recover() {
if (this == Rectangle.TEMP || this == Rectangle.EMPTY) {
console.log("recover Temp or Empty:", this);
return;
}
Pool.recover("Rectangle", this.reset());
}
static create() {
return Pool.getItemByClass("Rectangle", Rectangle);
}
copyFrom(source) {
this.x = source.x;
this.y = source.y;
this.width = source.width;
this.height = source.height;
return this;
}
contains(x, y) {
if (this.width <= 0 || this.height <= 0)
return false;
if (x >= this.x && x < this.right) {
if (y >= this.y && y < this.bottom) {
return true;
}
}
return false;
}
intersects(rect) {
return !(rect.x > (this.x + this.width) || (rect.x + rect.width) < this.x || rect.y > (this.y + this.height) || (rect.y + rect.height) < this.y);
}
intersection(rect, out = null) {
if (!this.intersects(rect))
return null;
out || (out = new Rectangle());
out.x = Math.max(this.x, rect.x);
out.y = Math.max(this.y, rect.y);
out.width = Math.min(this.right, rect.right) - out.x;
out.height = Math.min(this.bottom, rect.bottom) - out.y;
return out;
}
union(source, out = null) {
out || (out = new Rectangle());
this.clone(out);
if (source.width <= 0 || source.height <= 0)
return out;
out.addPoint(source.x, source.y);
out.addPoint(source.right, source.bottom);
return this;
}
clone(out = null) {
out || (out = new Rectangle());
out.x = this.x;
out.y = this.y;
out.width = this.width;
out.height = this.height;
return out;
}
toString() {
return this.x + "," + this.y + "," + this.width + "," + this.height;
}
equals(rect) {
if (!rect || rect.x !== this.x || rect.y !== this.y || rect.width !== this.width || rect.height !== this.height)
return false;
return true;
}
addPoint(x, y) {
this.x > x && (this.width += this.x - x, this.x = x);
this.y > y && (this.height += this.y - y, this.y = y);
if (this.width < x - this.x)
this.width = x - this.x;
if (this.height < y - this.y)
this.height = y - this.y;
return this;
}
_getBoundPoints() {
var rst = Rectangle._temB;
rst.length = 0;
if (this.width == 0 || this.height == 0)
return rst;
rst.push(this.x, this.y, this.x + this.width, this.y, this.x, this.y + this.height, this.x + this.width, this.y + this.height);
return rst;
}
static _getBoundPointS(x, y, width, height) {
var rst = Rectangle._temA;
rst.length = 0;
if (width == 0 || height == 0)
return rst;
rst.push(x, y, x + width, y, x, y + height, x + width, y + height);
return rst;
}
static _getWrapRec(pointList, rst = null) {
if (!pointList || pointList.length < 1)
return rst ? rst.setTo(0, 0, 0, 0) : Rectangle.TEMP.setTo(0, 0, 0, 0);
rst = rst ? rst : Rectangle.create();
var i, len = pointList.length, minX, maxX, minY, maxY, tPoint = Point.TEMP;
minX = minY = 99999;
maxX = maxY = -minX;
for (i = 0; i < len; i += 2) {
tPoint.x = pointList[i];
tPoint.y = pointList[i + 1];
minX = minX < tPoint.x ? minX : tPoint.x;
minY = minY < tPoint.y ? minY : tPoint.y;
maxX = maxX > tPoint.x ? maxX : tPoint.x;
maxY = maxY > tPoint.y ? maxY : tPoint.y;
}
return rst.setTo(minX, minY, maxX - minX, maxY - minY);
}
isEmpty() {
if (this.width <= 0 || this.height <= 0)
return true;
return false;
}
}
Rectangle.EMPTY = new Rectangle();
Rectangle.TEMP = new Rectangle();
Rectangle._temB = [];
Rectangle._temA = [];
class LayaGL {
}
LayaGL.ARRAY_BUFFER_TYPE_DATA = 0;
LayaGL.ARRAY_BUFFER_TYPE_CMD = 1;
LayaGL.ARRAY_BUFFER_REF_REFERENCE = 0;
LayaGL.ARRAY_BUFFER_REF_COPY = 1;
LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_ID = 0;
LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_DATA = 1;
var _sFactor = 1;
var _dFactor = 0;
class WebGLContext {
static __init__() {
var gl = LayaGL.instance;
WebGLContext._depthFunc = gl.LESS;
WebGLContext._blendEquation = gl.FUNC_ADD;
WebGLContext._blendEquationRGB = gl.FUNC_ADD;
WebGLContext._blendEquationAlpha = gl.FUNC_ADD;
_sFactor = gl.ONE;
_dFactor = gl.ZERO;
WebGLContext._sFactorAlpha = gl.ONE;
WebGLContext._dFactorAlpha = gl.ZERO;
WebGLContext._activedTextureID = gl.TEXTURE0;
var maxTexturenum = gl.getParameter(gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS);
WebGLContext._activeTextures = new Array(maxTexturenum);
WebGLContext._glTextureIDs = [gl.TEXTURE0, gl.TEXTURE1, gl.TEXTURE2, gl.TEXTURE3, gl.TEXTURE4, gl.TEXTURE5, gl.TEXTURE6, gl.TEXTURE7, gl.TEXTURE8, gl.TEXTURE9, gl.TEXTURE10, gl.TEXTURE11, gl.TEXTURE12, gl.TEXTURE13, gl.TEXTURE14, gl.TEXTURE15, gl.TEXTURE16, gl.TEXTURE17, gl.TEXTURE18, gl.TEXTURE19, gl.TEXTURE20, gl.TEXTURE21, gl.TEXTURE22, gl.TEXTURE23, gl.TEXTURE24, gl.TEXTURE25, gl.TEXTURE26, gl.TEXTURE27, gl.TEXTURE28, gl.TEXTURE29, gl.TEXTURE30, gl.TEXTURE31];
var maxVertexUniform = gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS);
var maxFragUniform = gl.getParameter(gl.MAX_FRAGMENT_UNIFORM_VECTORS);
WebGLContext._maxUniformFragmentVectors = Math.min(maxVertexUniform, maxFragUniform);
}
static useProgram(gl, program) {
if (WebGLContext._useProgram === program)
return false;
gl.useProgram(program);
WebGLContext._useProgram = program;
return true;
}
static setDepthTest(gl, value) {
value !== WebGLContext._depthTest && (WebGLContext._depthTest = value, value ? gl.enable(gl.DEPTH_TEST) : gl.disable(gl.DEPTH_TEST));
}
static setDepthMask(gl, value) {
value !== WebGLContext._depthMask && (WebGLContext._depthMask = value, gl.depthMask(value));
}
static setDepthFunc(gl, value) {
value !== WebGLContext._depthFunc && (WebGLContext._depthFunc = value, gl.depthFunc(value));
}
static setBlend(gl, value) {
value !== WebGLContext._blend && (WebGLContext._blend = value, value ? gl.enable(gl.BLEND) : gl.disable(gl.BLEND));
}
static setBlendEquation(gl, blendEquation) {
if (blendEquation !== WebGLContext._blendEquation) {
WebGLContext._blendEquation = blendEquation;
WebGLContext._blendEquationRGB = WebGLContext._blendEquationAlpha = null;
gl.blendEquation(blendEquation);
}
}
static setBlendEquationSeparate(gl, blendEquationRGB, blendEquationAlpha) {
if (blendEquationRGB !== WebGLContext._blendEquationRGB || blendEquationAlpha !== WebGLContext._blendEquationAlpha) {
WebGLContext._blendEquationRGB = blendEquationRGB;
WebGLContext._blendEquationAlpha = blendEquationAlpha;
WebGLContext._blendEquation = null;
gl.blendEquationSeparate(blendEquationRGB, blendEquationAlpha);
}
}
static setBlendFunc(gl, sFactor, dFactor, force = false) {
if (force || sFactor !== _sFactor || dFactor !== _dFactor) {
_sFactor = sFactor;
_dFactor = dFactor;
WebGLContext._sFactorRGB = null;
WebGLContext._dFactorRGB = null;
WebGLContext._sFactorAlpha = null;
WebGLContext._dFactorAlpha = null;
gl.blendFunc(sFactor, dFactor);
}
}
static setBlendFuncSeperate(gl, srcRGB, dstRGB, srcAlpha, dstAlpha) {
if (srcRGB !== WebGLContext._sFactorRGB || dstRGB !== WebGLContext._dFactorRGB || srcAlpha !== WebGLContext._sFactorAlpha || dstAlpha !== WebGLContext._dFactorAlpha) {
WebGLContext._sFactorRGB = srcRGB;
WebGLContext._dFactorRGB = dstRGB;
WebGLContext._sFactorAlpha = srcAlpha;
WebGLContext._dFactorAlpha = dstAlpha;
_sFactor = null;
_dFactor = null;
gl.blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
}
}
static setCullFace(gl, value) {
value !== WebGLContext._cullFace && (WebGLContext._cullFace = value, value ? gl.enable(gl.CULL_FACE) : gl.disable(gl.CULL_FACE));
}
static setFrontFace(gl, value) {
value !== WebGLContext._frontFace && (WebGLContext._frontFace = value, gl.frontFace(value));
}
static activeTexture(gl, textureID) {
if (WebGLContext._activedTextureID !== textureID) {
gl.activeTexture(textureID);
WebGLContext._activedTextureID = textureID;
}
}
static bindTexture(gl, target, texture) {
if (WebGLContext._activeTextures[WebGLContext._activedTextureID - gl.TEXTURE0] !== texture) {
gl.bindTexture(target, texture);
WebGLContext._activeTextures[WebGLContext._activedTextureID - gl.TEXTURE0] = texture;
}
}
static __init_native() {
if (!ILaya.Render.supportWebGLPlusRendering)
return;
var webGLContext = WebGLContext;
webGLContext.activeTexture = webGLContext.activeTextureForNative;
webGLContext.bindTexture = webGLContext.bindTextureForNative;
}
static useProgramForNative(gl, program) {
gl.useProgram(program);
return true;
}
static setDepthTestForNative(gl, value) {
if (value)
gl.enable(gl.DEPTH_TEST);
else
gl.disable(gl.DEPTH_TEST);
}
static setDepthMaskForNative(gl, value) {
gl.depthMask(value);
}
static setDepthFuncForNative(gl, value) {
gl.depthFunc(value);
}
static setBlendForNative(gl, value) {
if (value)
gl.enable(gl.BLEND);
else
gl.disable(gl.BLEND);
}
static setBlendFuncForNative(gl, sFactor, dFactor) {
gl.blendFunc(sFactor, dFactor);
}
static setCullFaceForNative(gl, value) {
if (value)
gl.enable(gl.CULL_FACE);
else
gl.disable(gl.CULL_FACE);
}
static setFrontFaceForNative(gl, value) {
gl.frontFace(value);
}
static activeTextureForNative(gl, textureID) {
gl.activeTexture(textureID);
}
static bindTextureForNative(gl, target, texture) {
gl.bindTexture(target, texture);
}
static bindVertexArrayForNative(gl, vertexArray) {
gl.bindVertexArray(vertexArray);
}
static getUniformMaxVector() {
return WebGLContext._maxUniformFragmentVectors;
}
}
WebGLContext._activeTextures = new Array(1);
WebGLContext._useProgram = null;
WebGLContext._depthTest = true;
WebGLContext._depthMask = true;
WebGLContext._blend = false;
WebGLContext._cullFace = false;
WebGLContext.mainContext = null;
class Handler {
constructor(caller = null, method = null, args = null, once = false) {
this.once = false;
this._id = 0;
this.setTo(caller, method, args, once);
}
setTo(caller, method, args, once = false) {
this._id = Handler._gid++;
this.caller = caller;
this.method = method;
this.args = args;
this.once = once;
return this;
}
run() {
if (this.method == null)
return null;
var id = this._id;
var result = this.method.apply(this.caller, this.args);
this._id === id && this.once && this.recover();
return result;
}
runWith(data) {
if (this.method == null)
return null;
var id = this._id;
if (data == null)
var result = this.method.apply(this.caller, this.args);
else if (!this.args && !data.unshift)
result = this.method.call(this.caller, data);
else if (this.args)
result = this.method.apply(this.caller, this.args.concat(data));
else
result = this.method.apply(this.caller, data);
this._id === id && this.once && this.recover();
return result;
}
clear() {
this.caller = null;
this.method = null;
this.args = null;
return this;
}
recover() {
if (this._id > 0) {
this._id = 0;
Handler._pool.push(this.clear());
}
}
static create(caller, method, args = null, once = true) {
if (Handler._pool.length)
return Handler._pool.pop().setTo(caller, method, args, once);
return new Handler(caller, method, args, once);
}
}
Handler._pool = [];
Handler._gid = 1;
class EventDispatcher {
hasListener(type) {
var listener = this._events && this._events[type];
return !!listener;
}
event(type, data = null) {
if (!this._events || !this._events[type])
return false;
var listeners = this._events[type];
if (listeners.run) {
if (listeners.once)
delete this._events[type];
data != null ? listeners.runWith(data) : listeners.run();
}
else {
for (var i = 0, n = listeners.length; i < n; i++) {
var listener = listeners[i];
if (listener) {
(data != null) ? listener.runWith(data) : listener.run();
}
if (!listener || listener.once) {
listeners.splice(i, 1);
i--;
n--;
}
}
if (listeners.length === 0 && this._events)
delete this._events[type];
}
return true;
}
on(type, caller, listener, args = null) {
return this._createListener(type, caller, listener, args, false);
}
once(type, caller, listener, args = null) {
return this._createListener(type, caller, listener, args, true);
}
_createListener(type, caller, listener, args, once, offBefore = true) {
offBefore && this.off(type, caller, listener, once);
var handler = EventHandler.create(caller || this, listener, args, once);
this._events || (this._events = {});
var events = this._events;
if (!events[type])
events[type] = handler;
else {
if (!events[type].run)
events[type].push(handler);
else
events[type] = [events[type], handler];
}
return this;
}
off(type, caller, listener, onceOnly = false) {
if (!this._events || !this._events[type])
return this;
var listeners = this._events[type];
if (listeners != null) {
if (listeners.run) {
if ((!caller || listeners.caller === caller) && (listener == null || listeners.method === listener) && (!onceOnly || listeners.once)) {
delete this._events[type];
listeners.recover();
}
}
else {
var count = 0;
for (var i = 0, n = listeners.length; i < n; i++) {
var item = listeners[i];
if (!item) {
count++;
continue;
}
if (item && (!caller || item.caller === caller) && (listener == null || item.method === listener) && (!onceOnly || item.once)) {
count++;
listeners[i] = null;
item.recover();
}
}
if (count === n)
delete this._events[type];
}
}
return this;
}
offAll(type = null) {
var events = this._events;
if (!events)
return this;
if (type) {
this._recoverHandlers(events[type]);
delete events[type];
}
else {
for (var name in events) {
this._recoverHandlers(events[name]);
}
this._events = null;
}
return this;
}
offAllCaller(caller) {
if (caller && this._events) {
for (var name in this._events) {
this.off(name, caller, null);
}
}
return this;
}
_recoverHandlers(arr) {
if (!arr)
return;
if (arr.run) {
arr.recover();
}
else {
for (var i = arr.length - 1; i > -1; i--) {
if (arr[i]) {
arr[i].recover();
arr[i] = null;
}
}
}
}
isMouseEvent(type) {
return EventDispatcher.MOUSE_EVENTS[type] || false;
}
}
EventDispatcher.MOUSE_EVENTS = { "rightmousedown": true, "rightmouseup": true, "rightclick": true, "mousedown": true, "mouseup": true, "mousemove": true, "mouseover": true, "mouseout": true, "click": true, "doubleclick": true };
class EventHandler extends Handler {
constructor(caller, method, args, once) {
super(caller, method, args, once);
}
recover() {
if (this._id > 0) {
this._id = 0;
EventHandler._pool.push(this.clear());
}
}
static create(caller, method, args = null, once = true) {
if (EventHandler._pool.length)
return EventHandler._pool.pop().setTo(caller, method, args, once);
return new EventHandler(caller, method, args, once);
}
}
EventHandler._pool = [];
class URL {
constructor(url) {
this._url = URL.formatURL(url);
this._path = URL.getPath(url);
}
get url() {
return this._url;
}
get path() {
return this._path;
}
static set basePath(value) {
URL._basePath = ILaya.Laya._getUrlPath();
URL._basePath = URL.formatURL(value);
}
static get basePath() {
return URL._basePath;
}
static formatURL(url) {
if (!url)
return "null path";
if (url.indexOf(":") > 0)
return url;
if (URL.exportSceneToJson)
url = URL.getAdptedFilePath(url);
if (URL.customFormat != null)
url = URL.customFormat(url);
if (url.indexOf(":") > 0)
return url;
var char1 = url.charAt(0);
if (char1 === ".") {
return URL._formatRelativePath(URL._basePath + url);
}
else if (char1 === '~') {
return URL.rootPath + url.substring(1);
}
else if (char1 === "d") {
if (url.indexOf("data:image") === 0)
return url;
}
else if (char1 === "/") {
return url;
}
return URL._basePath + url;
}
static _formatRelativePath(value) {
var parts = value.split("/");
for (var i = 0, len = parts.length; i < len; i++) {
if (parts[i] == '..') {
parts.splice(i - 1, 2);
i -= 2;
}
}
return parts.join('/');
}
static getPath(url) {
var ofs = url.lastIndexOf('/');
return ofs > 0 ? url.substr(0, ofs + 1) : "";
}
static getFileName(url) {
var ofs = url.lastIndexOf('/');
return ofs > 0 ? url.substr(ofs + 1) : url;
}
static getAdptedFilePath(url) {
if (!URL.exportSceneToJson || !url)
return url;
var i, len;
len = URL._adpteTypeList.length;
var tArr;
for (i = 0; i < len; i++) {
tArr = URL._adpteTypeList[i];
url = url.replace(tArr[0], tArr[1]);
}
return url;
}
}
URL.version = {};
URL.exportSceneToJson = false;
URL._basePath = "";
URL.rootPath = "";
URL.customFormat = function (url) {
var newUrl = URL.version[url];
if (!window.conch && newUrl)
url += "?v=" + newUrl;
return url;
};
URL._adpteTypeList = [[".scene3d", ".json"], [".scene", ".json"], [".taa", ".json"], [".prefab", ".json"]];
class Resource extends EventDispatcher {
static get cpuMemory() {
return Resource._cpuMemory;
}
static get gpuMemory() {
return Resource._gpuMemory;
}
static _addCPUMemory(size) {
Resource._cpuMemory += size;
}
static _addGPUMemory(size) {
Resource._gpuMemory += size;
}
static _addMemory(cpuSize, gpuSize) {
Resource._cpuMemory += cpuSize;
Resource._gpuMemory += gpuSize;
}
static getResourceByID(id) {
return Resource._idResourcesMap[id];
}
static getResourceByURL(url, index = 0) {
return Resource._urlResourcesMap[url][index];
}
static destroyUnusedResources() {
for (var k in Resource._idResourcesMap) {
var res = Resource._idResourcesMap[k];
if (!res.lock && res._referenceCount === 0)
res.destroy();
}
}
get id() {
return this._id;
}
get url() {
return this._url;
}
get cpuMemory() {
return this._cpuMemory;
}
get gpuMemory() {
return this._gpuMemory;
}
get destroyed() {
return this._destroyed;
}
get referenceCount() {
return this._referenceCount;
}
constructor() {
super();
this._id = 0;
this._url = null;
this._cpuMemory = 0;
this._gpuMemory = 0;
this._destroyed = false;
this._referenceCount = 0;
this.lock = false;
this.name = null;
this._id = ++Resource._uniqueIDCounter;
this._destroyed = false;
this._referenceCount = 0;
Resource._idResourcesMap[this.id] = this;
this.lock = false;
}
_setCPUMemory(value) {
var offsetValue = value - this._cpuMemory;
this._cpuMemory = value;
Resource._addCPUMemory(offsetValue);
}
_setGPUMemory(value) {
var offsetValue = value - this._gpuMemory;
this._gpuMemory = value;
Resource._addGPUMemory(offsetValue);
}
_setCreateURL(url) {
url = URL.formatURL(url);
if (this._url !== url) {
var resList;
if (this._url) {
resList = Resource._urlResourcesMap[this._url];
resList.splice(resList.indexOf(this), 1);
(resList.length === 0) && (delete Resource._urlResourcesMap[this._url]);
}
if (url) {
resList = Resource._urlResourcesMap[url];
(resList) || (Resource._urlResourcesMap[url] = resList = []);
resList.push(this);
}
this._url = url;
}
}
_addReference(count = 1) {
this._referenceCount += count;
}
_removeReference(count = 1) {
this._referenceCount -= count;
}
_clearReference() {
this._referenceCount = 0;
}
_recoverResource() {
}
_disposeResource() {
}
_activeResource() {
}
destroy() {
if (this._destroyed)
return;
this._destroyed = true;
this.lock = false;
this._disposeResource();
delete Resource._idResourcesMap[this.id];
var resList;
if (this._url) {
resList = Resource._urlResourcesMap[this._url];
if (resList) {
resList.splice(resList.indexOf(this), 1);
(resList.length === 0) && (delete Resource._urlResourcesMap[this._url]);
}
var resou = ILaya.Loader.loadedMap[this._url];
(resou == this) && (delete ILaya.Loader.loadedMap[this._url]);
}
}
}
Resource._uniqueIDCounter = 0;
Resource._idResourcesMap = {};
Resource._urlResourcesMap = {};
Resource._cpuMemory = 0;
Resource._gpuMemory = 0;
class Bitmap extends Resource {
get width() {
return this._width;
}
set width(width) {
this._width = width;
}
get height() {
return this._height;
}
set height(height) {
this._height = height;
}
constructor() {
super();
this._width = -1;
this._height = -1;
}
_getSource() {
throw "Bitmap: must override it.";
}
}
var FilterMode;
(function (FilterMode) {
FilterMode[FilterMode["Point"] = 0] = "Point";
FilterMode[FilterMode["Bilinear"] = 1] = "Bilinear";
FilterMode[FilterMode["Trilinear"] = 2] = "Trilinear";
})(FilterMode || (FilterMode = {}));
var TextureFormat;
(function (TextureFormat) {
TextureFormat[TextureFormat["R8G8B8"] = 0] = "R8G8B8";
TextureFormat[TextureFormat["R8G8B8A8"] = 1] = "R8G8B8A8";
TextureFormat[TextureFormat["R5G6B5"] = 16] = "R5G6B5";
TextureFormat[TextureFormat["Alpha8"] = 2] = "Alpha8";
TextureFormat[TextureFormat["DXT1"] = 3] = "DXT1";
TextureFormat[TextureFormat["DXT5"] = 4] = "DXT5";
TextureFormat[TextureFormat["ETC1RGB"] = 5] = "ETC1RGB";
TextureFormat[TextureFormat["ETC2RGB"] = 6] = "ETC2RGB";
TextureFormat[TextureFormat["ETC2RGBA"] = 7] = "ETC2RGBA";
TextureFormat[TextureFormat["ETC2RGB_Alpha8"] = 8] = "ETC2RGB_Alpha8";
TextureFormat[TextureFormat["ETC2SRGB"] = 28] = "ETC2SRGB";
TextureFormat[TextureFormat["PVRTCRGB_2BPPV"] = 9] = "PVRTCRGB_2BPPV";
TextureFormat[TextureFormat["PVRTCRGBA_2BPPV"] = 10] = "PVRTCRGBA_2BPPV";
TextureFormat[TextureFormat["PVRTCRGB_4BPPV"] = 11] = "PVRTCRGB_4BPPV";
TextureFormat[TextureFormat["PVRTCRGBA_4BPPV"] = 12] = "PVRTCRGBA_4BPPV";
TextureFormat[TextureFormat["R32G32B32A32"] = 15] = "R32G32B32A32";
TextureFormat[TextureFormat["R16G16B16A16"] = 17] = "R16G16B16A16";
TextureFormat[TextureFormat["ASTC4x4"] = 18] = "ASTC4x4";
TextureFormat[TextureFormat["ASTC4x4SRGB"] = 23] = "ASTC4x4SRGB";
TextureFormat[TextureFormat["ASTC6x6"] = 19] = "ASTC6x6";
TextureFormat[TextureFormat["ASTC6x6SRGB"] = 24] = "ASTC6x6SRGB";
TextureFormat[TextureFormat["ASTC8x8"] = 20] = "ASTC8x8";
TextureFormat[TextureFormat["ASTC8x8SRGB"] = 25] = "ASTC8x8SRGB";
TextureFormat[TextureFormat["ASTC10x10"] = 21] = "ASTC10x10";
TextureFormat[TextureFormat["ASTC10x10SRGB"] = 26] = "ASTC10x10SRGB";
TextureFormat[TextureFormat["ASTC12x12"] = 22] = "ASTC12x12";
TextureFormat[TextureFormat["ASTC12x12SRGB"] = 27] = "ASTC12x12SRGB";
TextureFormat[TextureFormat["KTXTEXTURE"] = -1] = "KTXTEXTURE";
TextureFormat[TextureFormat["PVRTEXTURE"] = -2] = "PVRTEXTURE";
})(TextureFormat || (TextureFormat = {}));
var WarpMode;
(function (WarpMode) {
WarpMode[WarpMode["Repeat"] = 0] = "Repeat";
WarpMode[WarpMode["Clamp"] = 1] = "Clamp";
})(WarpMode || (WarpMode = {}));
class BaseTexture extends Bitmap {
get mipmap() {
return this._mipmap;
}
get format() {
return this._format;
}
get wrapModeU() {
return this._wrapModeU;
}
set wrapModeU(value) {
if (this._wrapModeU !== value) {
this._wrapModeU = value;
(this._width !== -1) && (this._setWarpMode(LayaGL.instance.TEXTURE_WRAP_S, value));
}
}
get wrapModeV() {
return this._wrapModeV;
}
set wrapModeV(value) {
if (this._wrapModeV !== value) {
this._wrapModeV = value;
(this._height !== -1) && (this._setWarpMode(LayaGL.instance.TEXTURE_WRAP_T, value));
}
}
get filterMode() {
return this._filterMode;
}
set filterMode(value) {
if (value !== this._filterMode) {
this._filterMode = value;
((this._width !== -1) && (this._height !== -1)) && (this._setFilterMode(value));
}
}
get anisoLevel() {
return this._anisoLevel;
}
set anisoLevel(value) {
if (value !== this._anisoLevel) {
this._anisoLevel = Math.max(1, Math.min(16, value));
((this._width !== -1) && (this._height !== -1)) && (this._setAnisotropy(value));
}
}
get mipmapCount() {
return this._mipmapCount;
}
get defaulteTexture() {
throw "BaseTexture:must override it.";
}
constructor(format, mipMap) {
super();
this._wrapModeU = WarpMode.Repeat;
this._wrapModeV = WarpMode.Repeat;
this._filterMode = FilterMode.Bilinear;
this._readyed = false;
this._width = -1;
this._height = -1;
this._format = format;
this._mipmap = mipMap;
this._anisoLevel = 1;
this._glTexture = LayaGL.instance.createTexture();
}
_getFormatByteCount() {
switch (this._format) {
case TextureFormat.R8G8B8:
return 3;
case TextureFormat.R8G8B8A8:
return 4;
case TextureFormat.R5G6B5:
return 1;
case TextureFormat.Alpha8:
return 1;
case TextureFormat.R16G16B16A16:
return 2;
case TextureFormat.R32G32B32A32:
return 4;
default:
throw "Texture2D: unknown format.";
}
}
_isPot(size) {
return (size & (size - 1)) === 0;
}
_getGLFormat() {
var glFormat;
var gl = LayaGL.instance;
var gpu = LayaGL.layaGPUInstance;
switch (this._format) {
case TextureFormat.R8G8B8:
case TextureFormat.R5G6B5:
glFormat = gl.RGB;
break;
case TextureFormat.R8G8B8A8:
glFormat = gl.RGBA;
break;
case TextureFormat.Alpha8:
glFormat = gl.ALPHA;
break;
case TextureFormat.R32G32B32A32:
case Te