starling-framework
Version:
A fast, productive library for 2D cross-platform development.
483 lines (468 loc) • 20.6 kB
JavaScript
// Class: starling.textures.Texture
var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this
$global.Object.defineProperty(exports, "__esModule", {value: true});
var __map_reserved = {};
// Imports
var $hxClasses = require("./../../hxClasses_stub").default;
var $hxEnums = require("./../../hxEnums_stub").default;
var $import = require("./../../import_stub").default;
function openfl_geom_Point() {return $import(require("openfl/geom/Point"));}
function starling_utils_MatrixUtil() {return require("./../../starling/utils/MatrixUtil");}
function starling_textures_TextureOptions() {return require("./../../starling/textures/TextureOptions");}
function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));}
function openfl_geom_Matrix() {return $import(require("openfl/geom/Matrix"));}
function openfl_display_Bitmap() {return $import(require("openfl/display/Bitmap"));}
function js_Boot() {return require("./../../js/Boot");}
function Std() {return require("./../../Std");}
function openfl_display_BitmapData() {return $import(require("openfl/display/BitmapData"));}
function openfl_utils_ByteArray() {return $import(require("openfl/utils/ByteArray"));}
function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");}
function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));}
function Type() {return require("./../../Type");}
function openfl_display3D_textures_Texture() {return $import(require("openfl/display3D/textures/Texture"));}
function starling_textures_ConcretePotTexture() {return require("./../../starling/textures/ConcretePotTexture");}
function openfl_display3D_textures_RectangleTexture() {return $import(require("openfl/display3D/textures/RectangleTexture"));}
function starling_textures_ConcreteRectangleTexture() {return require("./../../starling/textures/ConcreteRectangleTexture");}
function openfl_display3D_textures_VideoTexture() {return $import(require("openfl/display3D/textures/VideoTexture"));}
function starling_textures_ConcreteVideoTexture() {return require("./../../starling/textures/ConcreteVideoTexture");}
function starling_core_Starling() {return require("./../../starling/core/Starling");}
function starling_errors_MissingContextError() {return require("./../../starling/errors/MissingContextError");}
function starling_textures_AtfData() {return require("./../../starling/textures/AtfData");}
function Reflect() {return require("./../../Reflect");}
function starling_utils_SystemUtil() {return require("./../../starling/utils/SystemUtil");}
function starling_errors_NotSupportedError() {return require("./../../starling/errors/NotSupportedError");}
function starling_utils_MathUtil() {return require("./../../starling/utils/MathUtil");}
function starling_textures_SubTexture() {return require("./../../starling/textures/SubTexture");}
// Constructor
var Texture = function() {
}
// Meta
Texture.__name__ = "starling.textures.Texture";
Texture.__isInterface__ = false;
Texture.prototype = {
dispose: function() {
},
setupVertexPositions: function(vertexData,vertexID,attrName,bounds) {
if(attrName == null) {
attrName = "position";
}
if(vertexID == null) {
vertexID = 0;
}
var frame = this.get_frame();
var width = this.get_width();
var height = this.get_height();
if(frame != null) {
Texture.sRectangle.setTo(-frame.x,-frame.y,width,height);
} else {
Texture.sRectangle.setTo(0,0,width,height);
}
vertexData.setPoint(vertexID,attrName,Texture.sRectangle.get_left(),Texture.sRectangle.get_top());
vertexData.setPoint(vertexID + 1,attrName,Texture.sRectangle.get_right(),Texture.sRectangle.get_top());
vertexData.setPoint(vertexID + 2,attrName,Texture.sRectangle.get_left(),Texture.sRectangle.get_bottom());
vertexData.setPoint(vertexID + 3,attrName,Texture.sRectangle.get_right(),Texture.sRectangle.get_bottom());
if(bounds != null) {
var scaleX = bounds.width / this.get_frameWidth();
var scaleY = bounds.height / this.get_frameHeight();
if(scaleX != 1.0 || scaleY != 1.0 || bounds.x != 0 || bounds.y != 0) {
Texture.sMatrix.identity();
Texture.sMatrix.scale(scaleX,scaleY);
Texture.sMatrix.translate(bounds.x,bounds.y);
vertexData.transformPoints(attrName,Texture.sMatrix,vertexID,4);
}
}
},
setupTextureCoordinates: function(vertexData,vertexID,attrName) {
if(attrName == null) {
attrName = "texCoords";
}
if(vertexID == null) {
vertexID = 0;
}
this.setTexCoords(vertexData,vertexID,attrName,0.0,0.0);
this.setTexCoords(vertexData,vertexID + 1,attrName,1.0,0.0);
this.setTexCoords(vertexData,vertexID + 2,attrName,0.0,1.0);
this.setTexCoords(vertexData,vertexID + 3,attrName,1.0,1.0);
},
localToGlobal: function(u,v,out) {
if(out == null) {
out = new (openfl_geom_Point().default)();
}
if(this == this.get_root()) {
out.setTo(u,v);
} else {
(starling_utils_MatrixUtil().default).transformCoords(this.get_transformationMatrixToRoot(),u,v,out);
}
return out;
},
globalToLocal: function(u,v,out) {
if(out == null) {
out = new (openfl_geom_Point().default)();
}
if(this == this.get_root()) {
out.setTo(u,v);
} else {
Texture.sMatrix.identity();
Texture.sMatrix.copyFrom(this.get_transformationMatrixToRoot());
Texture.sMatrix.invert();
(starling_utils_MatrixUtil().default).transformCoords(Texture.sMatrix,u,v,out);
}
return out;
},
setTexCoords: function(vertexData,vertexID,attrName,u,v) {
this.localToGlobal(u,v,Texture.sPoint);
vertexData.setPoint(vertexID,attrName,Texture.sPoint.x,Texture.sPoint.y);
},
getTexCoords: function(vertexData,vertexID,attrName,out) {
if(attrName == null) {
attrName = "texCoords";
}
if(out == null) {
out = new (openfl_geom_Point().default)();
}
vertexData.getPoint(vertexID,attrName,out);
return this.globalToLocal(out.x,out.y,out);
},
get_frame: function() {
return null;
},
get_frameWidth: function() {
if(this.get_frame() != null) {
return this.get_frame().width;
} else {
return this.get_width();
}
},
get_frameHeight: function() {
if(this.get_frame() != null) {
return this.get_frame().height;
} else {
return this.get_height();
}
},
get_width: function() {
return 0;
},
get_height: function() {
return 0;
},
get_nativeWidth: function() {
return 0;
},
get_nativeHeight: function() {
return 0;
},
get_scale: function() {
return 1.0;
},
get_base: function() {
return null;
},
get_root: function() {
return null;
},
get_format: function() {
return "bgra";
},
get_mipMapping: function() {
return false;
},
get_premultipliedAlpha: function() {
return false;
},
get_transformationMatrix: function() {
return null;
},
get_transformationMatrixToRoot: function() {
return null;
}
};
Texture.prototype.__class__ = Texture.prototype.constructor = $hxClasses["starling.textures.Texture"] = Texture;
// Init
{
Object.defineProperties(Texture.prototype,{ frame : { get : function () { return this.get_frame (); }}, frameWidth : { get : function () { return this.get_frameWidth (); }}, frameHeight : { get : function () { return this.get_frameHeight (); }}, width : { get : function () { return this.get_width (); }}, height : { get : function () { return this.get_height (); }}, nativeWidth : { get : function () { return this.get_nativeWidth (); }}, nativeHeight : { get : function () { return this.get_nativeHeight (); }}, scale : { get : function () { return this.get_scale (); }}, base : { get : function () { return this.get_base (); }}, root : { get : function () { return this.get_root (); }}, format : { get : function () { return this.get_format (); }}, mipMapping : { get : function () { return this.get_mipMapping (); }}, premultipliedAlpha : { get : function () { return this.get_premultipliedAlpha (); }}, transformationMatrix : { get : function () { return this.get_transformationMatrix (); }}, transformationMatrixToRoot : { get : function () { return this.get_transformationMatrixToRoot (); }}});
Object.defineProperties(Texture,{ maxSize : { get : function () { return Texture.get_maxSize (); }}, asyncBitmapUploadEnabled : { get : function () { return Texture.get_asyncBitmapUploadEnabled (); }, set : function (v) { return Texture.set_asyncBitmapUploadEnabled (v); }}});
};
// Statics
Texture.fromData = function(data,options) {
var texture = null;
if(((data) instanceof (openfl_display_Bitmap().default))) {
data = ((js_Boot().default).__cast(data , (openfl_display_Bitmap().default))).get_bitmapData();
}
if(options == null) {
options = Texture.sDefaultOptions;
}
if((Std().default).is(data,$hxClasses["Class"])) {
return Texture.fromEmbeddedAsset(data,options.get_mipMapping(),options.get_optimizeForRenderToTexture(),options.get_scale(),options.get_format(),options.get_forcePotTexture());
} else if(((data) instanceof (openfl_display_BitmapData().default))) {
return Texture.fromBitmapData(data,options.get_mipMapping(),options.get_optimizeForRenderToTexture(),options.get_scale(),options.get_format(),options.get_forcePotTexture(),options.get_onReady());
} else if(((data) instanceof (openfl_utils_ByteArray().default))) {
return Texture.fromAtfData(data,options.get_scale(),options.get_mipMapping(),options.get_onReady());
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Unsupported 'data' type: " + (Type().default).getClassName(data)));
}
}
Texture.fromTextureBase = function(base,width,height,options) {
if(options == null) {
options = Texture.sDefaultOptions;
}
if(((base) instanceof (openfl_display3D_textures_Texture().default))) {
return new (starling_textures_ConcretePotTexture().default)((js_Boot().default).__cast(base , (openfl_display3D_textures_Texture().default)),options.get_format(),width,height,options.get_mipMapping(),options.get_premultipliedAlpha(),options.get_optimizeForRenderToTexture(),options.get_scale());
} else if(((base) instanceof (openfl_display3D_textures_RectangleTexture().default))) {
return new (starling_textures_ConcreteRectangleTexture().default)((js_Boot().default).__cast(base , (openfl_display3D_textures_RectangleTexture().default)),options.get_format(),width,height,options.get_premultipliedAlpha(),options.get_optimizeForRenderToTexture(),options.get_scale());
} else if(((base) instanceof (openfl_display3D_textures_VideoTexture().default))) {
return new (starling_textures_ConcreteVideoTexture().default)((js_Boot().default).__cast(base , (openfl_display3D_textures_VideoTexture().default)),options.get_scale());
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Unsupported 'base' type: " + (Type().default).getClassName((Type().default).getClass(base))));
}
}
Texture.fromEmbeddedAsset = function(assetClass,mipMapping,optimizeForRenderToTexture,scale,format,forcePotTexture) {
if(forcePotTexture == null) {
forcePotTexture = false;
}
if(format == null) {
format = "bgra";
}
if(scale == null) {
scale = 1;
}
if(optimizeForRenderToTexture == null) {
optimizeForRenderToTexture = false;
}
if(mipMapping == null) {
mipMapping = true;
}
var texture;
var asset = (Type().default).createEmptyInstance(assetClass);
if(((asset) instanceof (openfl_display_Bitmap().default))) {
texture = Texture.fromBitmap(asset,mipMapping,optimizeForRenderToTexture,scale,format,forcePotTexture);
texture.get_root().set_onRestore(function(textureRoot) {
textureRoot.uploadBitmap((Type().default).createInstance(assetClass,[]));
});
} else if(((asset) instanceof (openfl_display_BitmapData().default))) {
texture = Texture.fromBitmapData(asset,mipMapping,optimizeForRenderToTexture,scale,format,forcePotTexture);
texture.get_root().set_onRestore(function(textureRoot1) {
texture.get_root().uploadBitmapData((Type().default).createInstance(assetClass,[]));
});
} else if(((asset) instanceof (openfl_utils_ByteArray().default))) {
texture = Texture.fromAtfData(asset,scale,mipMapping,null);
texture.get_root().set_onRestore(function(textureRoot2) {
textureRoot2.uploadAtfData((Type().default).createInstance(assetClass,[]));
});
} else {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid asset type: " + (Type().default).getClassName(asset)));
}
asset = null;
return texture;
}
Texture.fromBitmap = function(bitmap,generateMipMaps,optimizeForRenderToTexture,scale,format,forcePotTexture,async) {
if(forcePotTexture == null) {
forcePotTexture = false;
}
if(format == null) {
format = "bgra";
}
if(scale == null) {
scale = 1;
}
if(optimizeForRenderToTexture == null) {
optimizeForRenderToTexture = false;
}
if(generateMipMaps == null) {
generateMipMaps = true;
}
return Texture.fromBitmapData(bitmap.get_bitmapData(),generateMipMaps,optimizeForRenderToTexture,scale,format,forcePotTexture,async);
}
Texture.fromBitmapData = function(data,generateMipMaps,optimizeForRenderToTexture,scale,format,forcePotTexture,async) {
if(forcePotTexture == null) {
forcePotTexture = false;
}
if(format == null) {
format = "bgra";
}
if(scale == null) {
scale = 1;
}
if(optimizeForRenderToTexture == null) {
optimizeForRenderToTexture = false;
}
if(generateMipMaps == null) {
generateMipMaps = true;
}
var texture = Texture.empty(data.width / scale,data.height / scale,true,generateMipMaps,optimizeForRenderToTexture,scale,format,forcePotTexture);
texture.get_root().uploadBitmapData(data,async);
texture.get_root().set_onRestore(function(textureRoot) {
textureRoot.uploadBitmapData(data);
});
return texture;
}
Texture.fromAtfData = function(data,scale,useMipMaps,async,premultipliedAlpha) {
if(premultipliedAlpha == null) {
premultipliedAlpha = false;
}
if(useMipMaps == null) {
useMipMaps = true;
}
if(scale == null) {
scale = 1;
}
var context = (starling_core_Starling().default).get_current().get_context();
if(context == null) {
throw new (js__$Boot_HaxeError().default)(new (starling_errors_MissingContextError().default)());
}
var atfData = new (starling_textures_AtfData().default)(data);
var nativeTexture = context.createTexture(atfData.get_width(),atfData.get_height(),atfData.get_format(),false);
var concreteTexture = new (starling_textures_ConcretePotTexture().default)(nativeTexture,atfData.get_format(),atfData.get_width(),atfData.get_height(),useMipMaps && atfData.get_numTextures() > 1,premultipliedAlpha,false,scale);
concreteTexture.uploadAtfData(data,0,async);
concreteTexture.set_onRestore(function(textureRoot) {
textureRoot.uploadAtfData(data,0);
});
return concreteTexture;
}
Texture.fromNetStream = function(stream,scale,onComplete) {
if(scale == null) {
scale = 1;
}
if(stream.client == stream && !(Reflect().default).hasField(stream,"onMetaData")) {
stream.client = { onMetaData : function(md) {
}};
}
return Texture.fromVideoAttachment("NetStream",stream,scale,onComplete);
}
Texture.fromVideoAttachment = function(type,attachment,scale,onComplete) {
if(!(starling_utils_SystemUtil().default).get_supportsVideoTexture()) {
throw new (js__$Boot_HaxeError().default)(new (starling_errors_NotSupportedError().default)("Video Textures are not supported on this platform"));
}
var context = (starling_core_Starling().default).get_current().get_context();
if(context == null) {
throw new (js__$Boot_HaxeError().default)(new (starling_errors_MissingContextError().default)());
}
var base = context.createVideoTexture();
var texture = new (starling_textures_ConcreteVideoTexture().default)(base,scale);
texture.attachVideo(type,attachment,onComplete);
texture.set_onRestore(function(textureRoot) {
textureRoot.attachVideo(type,attachment);
});
return texture;
}
Texture.fromColor = function(width,height,color,alpha,optimizeForRenderToTexture,scale,format,forcePotTexture) {
if(forcePotTexture == null) {
forcePotTexture = false;
}
if(format == null) {
format = "bgra";
}
if(scale == null) {
scale = -1;
}
if(optimizeForRenderToTexture == null) {
optimizeForRenderToTexture = false;
}
if(alpha == null) {
alpha = 1.0;
}
if(color == null) {
color = 16777215;
}
var texture = Texture.empty(width,height,true,false,optimizeForRenderToTexture,scale,format,forcePotTexture);
texture.get_root().clear(color,alpha);
texture.get_root().set_onRestore(function(textureRoot) {
textureRoot.clear(color,alpha);
});
return texture;
}
Texture.empty = function(width,height,premultipliedAlpha,mipMapping,optimizeForRenderToTexture,scale,format,forcePotTexture) {
if(forcePotTexture == null) {
forcePotTexture = false;
}
if(format == null) {
format = "bgra";
}
if(scale == null) {
scale = -1;
}
if(optimizeForRenderToTexture == null) {
optimizeForRenderToTexture = false;
}
if(mipMapping == null) {
mipMapping = false;
}
if(premultipliedAlpha == null) {
premultipliedAlpha = true;
}
if(scale <= 0) {
scale = (starling_core_Starling().default).get_current().get_contentScaleFactor();
}
var actualWidth;
var actualHeight;
var nativeTexture;
var concreteTexture;
var context = (starling_core_Starling().default).get_current().get_context();
if(context == null) {
throw new (js__$Boot_HaxeError().default)(new (starling_errors_MissingContextError().default)());
}
var origWidth = width * scale;
var origHeight = height * scale;
var useRectTexture = !forcePotTexture && !mipMapping && (starling_core_Starling().default).get_current().get_profile() != "baselineConstrained" && format.indexOf("compressed") == -1;
if(useRectTexture) {
actualWidth = Math.ceil(origWidth - 0.000000001);
actualHeight = Math.ceil(origHeight - 0.000000001);
nativeTexture = context.createRectangleTexture(actualWidth,actualHeight,format,optimizeForRenderToTexture);
concreteTexture = new (starling_textures_ConcreteRectangleTexture().default)((js_Boot().default).__cast(nativeTexture , (openfl_display3D_textures_RectangleTexture().default)),format,actualWidth,actualHeight,premultipliedAlpha,optimizeForRenderToTexture,scale);
} else {
actualWidth = (starling_utils_MathUtil().default).getNextPowerOfTwo(origWidth);
actualHeight = (starling_utils_MathUtil().default).getNextPowerOfTwo(origHeight);
nativeTexture = context.createTexture(actualWidth,actualHeight,format,optimizeForRenderToTexture);
concreteTexture = new (starling_textures_ConcretePotTexture().default)((js_Boot().default).__cast(nativeTexture , (openfl_display3D_textures_Texture().default)),format,actualWidth,actualHeight,mipMapping,premultipliedAlpha,optimizeForRenderToTexture,scale);
}
concreteTexture.set_onRestore(function(textureRoot) {
textureRoot.clear();
});
if(actualWidth - origWidth < 0.001 && actualHeight - origHeight < 0.001) {
return concreteTexture;
} else {
return new (starling_textures_SubTexture().default)(concreteTexture,new (openfl_geom_Rectangle().default)(0,0,width,height),true);
}
}
Texture.fromTexture = function(texture,region,frame,rotated,scaleModifier) {
if(scaleModifier == null) {
scaleModifier = 1.0;
}
if(rotated == null) {
rotated = false;
}
return new (starling_textures_SubTexture().default)(texture,region,false,frame,rotated,scaleModifier);
}
Texture.get_maxSize = function() {
return Texture.getMaxSize();
}
Texture.getMaxSize = function(textureFormat) {
if(textureFormat == null) {
textureFormat = "bgra";
}
var target = (starling_core_Starling().default).get_current();
var context = target.get_context();
var profile = target != null ? target.get_profile() : "baseline";
var isCompressed = textureFormat == "compressed" || textureFormat == "compressedAlpha";
if(profile == "baseline" || profile == "baselineConstrained") {
return 2048;
} else if(!isCompressed && context != null && (Reflect().default).hasField(context,"supports8kTexture")) {
return 8192;
} else {
return 4096;
}
}
Texture.get_asyncBitmapUploadEnabled = function() {
return (starling_textures_ConcreteRectangleTexture().default).get_asyncUploadEnabled();
}
Texture.set_asyncBitmapUploadEnabled = function(value) {
(starling_textures_ConcreteRectangleTexture().default).set_asyncUploadEnabled(value);
(starling_textures_ConcretePotTexture().default).set_asyncUploadEnabled(value);
return value;
}
Texture.sDefaultOptions = new (starling_textures_TextureOptions().default)()
Texture.sRectangle = new (openfl_geom_Rectangle().default)()
Texture.sMatrix = new (openfl_geom_Matrix().default)()
Texture.sPoint = new (openfl_geom_Point().default)()
// Export
exports.default = Texture;