starling-framework
Version:
A fast, productive library for 2D cross-platform development.
183 lines (167 loc) • 7.43 kB
JavaScript
// Class: starling.textures.ConcretePotTexture
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;
var $bind = require("./../../bind_stub").default;
var $extend = require("./../../extend_stub").default;
function starling_textures_ConcreteTexture() {return require("./../../starling/textures/ConcreteTexture");}
function starling_core_Starling() {return require("./../../starling/core/Starling");}
function Std() {return require("./../../Std");}
function openfl_display_BitmapData() {return $import(require("openfl/display/BitmapData"));}
function Reflect() {return require("./../../Reflect");}
function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");}
function openfl_errors_Error() {return $import(require("openfl/errors/Error"));}
function haxe_Timer() {return require("./../../haxe/Timer");}
function openfl_events_Event() {return $import(require("openfl/events/Event"));}
function openfl_geom_Matrix() {return $import(require("openfl/geom/Matrix"));}
function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));}
function openfl_geom_Point() {return $import(require("openfl/geom/Point"));}
function starling_utils_MathUtil() {return require("./../../starling/utils/MathUtil");}
function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));}
// Constructor
var ConcretePotTexture = function(base,format,width,height,mipMapping,premultipliedAlpha,optimizedForRenderTexture,scale) {
if(scale == null) {
scale = 1;
}
if(optimizedForRenderTexture == null) {
optimizedForRenderTexture = false;
}
(starling_textures_ConcreteTexture().default).call(this,base,format,width,height,mipMapping,premultipliedAlpha,optimizedForRenderTexture,scale);
if(width != (starling_utils_MathUtil().default).getNextPowerOfTwo(width)) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("width must be a power of two"));
}
if(height != (starling_utils_MathUtil().default).getNextPowerOfTwo(height)) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("height must be a power of two"));
}
}
// Meta
ConcretePotTexture.__name__ = "starling.textures.ConcretePotTexture";
ConcretePotTexture.__isInterface__ = false;
ConcretePotTexture.__super__ = (starling_textures_ConcreteTexture().default);
ConcretePotTexture.prototype = $extend((starling_textures_ConcreteTexture().default).prototype, {
dispose: function() {
this.get_base().removeEventListener("textureReady",$bind(this,this.onTextureReady));
(starling_textures_ConcreteTexture().default).prototype.dispose.call(this);
},
createBase: function() {
return (starling_core_Starling().default).get_current().get_context().createTexture((Std().default).int(this.get_nativeWidth()),(Std().default).int(this.get_nativeHeight()),this.get_format(),this.get_optimizedForRenderTexture());
},
uploadBitmapData: function(data,async) {
var buffer = null;
var isAsync = async != null;
if(isAsync) {
this._textureReadyCallback = async;
}
if(data.width != this.get_nativeWidth() || data.height != this.get_nativeHeight()) {
buffer = new (openfl_display_BitmapData().default)((Std().default).int(this.get_nativeWidth()),(Std().default).int(this.get_nativeHeight()),true,0);
buffer.copyPixels(data,data.rect,ConcretePotTexture.sOrigin);
data = buffer;
}
this.upload(data,0,isAsync);
if(this.get_mipMapping() && data.width > 1 && data.height > 1) {
var currentWidth = data.width >> 1;
var currentHeight = data.height >> 1;
var level = 1;
var canvas = new (openfl_display_BitmapData().default)(currentWidth,currentHeight,true,0);
var bounds = ConcretePotTexture.sRectangle;
var matrix = ConcretePotTexture.sMatrix;
matrix.setTo(0.5,0.0,0.0,0.5,0.0,0.0);
while(currentWidth >= 1 || currentHeight >= 1) {
bounds.setTo(0,0,currentWidth,currentHeight);
canvas.fillRect(bounds,0);
canvas.draw(data,matrix,null,null,null,true);
this.upload(canvas,level++,false);
matrix.scale(0.5,0.5);
currentWidth >>= 1;
currentHeight >>= 1;
}
canvas.dispose();
}
if(buffer != null) {
buffer.dispose();
}
this.setDataUploaded();
},
get_isPotTexture: function() {
return true;
},
uploadAtfData: function(data,offset,async) {
if(offset == null) {
offset = 0;
}
data.set_endian("bigEndian");
var isAsync = async != null;
if(isAsync) {
this._textureReadyCallback = async;
this.get_base().addEventListener("textureReady",$bind(this,this.onTextureReady));
}
this.get_potBase().uploadCompressedTextureFromByteArray(data,offset,isAsync);
this.setDataUploaded();
},
upload: function(source,mipLevel,isAsync) {
if(isAsync) {
this.uploadAsync(source,mipLevel);
this.get_base().addEventListener("textureReady",$bind(this,this.onTextureReady));
this.get_base().addEventListener("error",$bind(this,this.onTextureReady));
} else {
this.get_potBase().uploadFromBitmapData(source,mipLevel);
}
},
uploadAsync: function(source,mipLevel) {
var _gthis = this;
if(ConcretePotTexture.sAsyncUploadEnabled) {
var method = (Reflect().default).field(this.get_base(),"uploadFromBitmapDataAsync");
try {
(Reflect().default).callMethod(this.get_base(),method,[source,mipLevel]);
} catch( error ) {
var error1 = ((error) instanceof (js__$Boot_HaxeError().default)) ? error.val : error;
if(((error1) instanceof (openfl_errors_Error().default))) {
var error2 = error1;
if(error2.errorID == 3708 || error2.errorID == 1069) {
ConcretePotTexture.sAsyncUploadEnabled = false;
} else {
throw new (js__$Boot_HaxeError().default)(error2);
}
} else {
throw error;
}
}
}
if(!ConcretePotTexture.sAsyncUploadEnabled) {
(haxe_Timer().default).delay(function() {
_gthis.get_base().dispatchEvent(new (openfl_events_Event().default)("textureReady"));
},1);
this.get_potBase().uploadFromBitmapData(source);
}
},
onTextureReady: function(event) {
this.get_base().removeEventListener("textureReady",$bind(this,this.onTextureReady));
this.get_base().removeEventListener("error",$bind(this,this.onTextureReady));
if(this._textureReadyCallback != null) {
this._textureReadyCallback(this);
}
this._textureReadyCallback = null;
},
get_potBase: function() {
return this.get_base();
}
});
ConcretePotTexture.prototype.__class__ = ConcretePotTexture.prototype.constructor = $hxClasses["starling.textures.ConcretePotTexture"] = ConcretePotTexture;
// Init
// Statics
ConcretePotTexture.get_asyncUploadEnabled = function() {
return ConcretePotTexture.sAsyncUploadEnabled;
}
ConcretePotTexture.set_asyncUploadEnabled = function(value) {
return ConcretePotTexture.sAsyncUploadEnabled = value;
}
ConcretePotTexture.sMatrix = new (openfl_geom_Matrix().default)()
ConcretePotTexture.sRectangle = new (openfl_geom_Rectangle().default)()
ConcretePotTexture.sOrigin = new (openfl_geom_Point().default)()
ConcretePotTexture.sAsyncUploadEnabled = false
// Export
exports.default = ConcretePotTexture;