starling-framework
Version:
A fast, productive library for 2D cross-platform development.
237 lines (222 loc) • 8.52 kB
JavaScript
// Class: starling.textures.RenderTexture
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_SubTexture() {return require("./../../starling/textures/SubTexture");}
function starling_core_Starling() {return require("./../../starling/core/Starling");}
function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");}
function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));}
function openfl_errors_IllegalOperationError() {return $import(require("openfl/errors/IllegalOperationError"));}
function starling_textures_Texture() {return require("./../../starling/textures/Texture");}
function starling_display_Image() {return require("./../../starling/display/Image");}
// Constructor
var RenderTexture = function(width,height,persistent,scale,format,forcePotTexture) {
if(forcePotTexture == null) {
forcePotTexture = false;
}
if(format == null) {
format = "bgra";
}
if(scale == null) {
scale = -1;
}
if(persistent == null) {
persistent = true;
}
this._isPersistent = persistent;
this._activeTexture = (starling_textures_Texture().default).empty(width,height,true,false,true,scale,format,forcePotTexture);
this._activeTexture.get_root().set_onRestore(function(textureRoot) {
textureRoot.clear();
});
(starling_textures_SubTexture().default).call(this,this._activeTexture,new (openfl_geom_Rectangle().default)(0,0,width,height),true,null,false);
if(persistent && RenderTexture.get_useDoubleBuffering()) {
this._bufferTexture = (starling_textures_Texture().default).empty(width,height,true,false,true,scale,format,forcePotTexture);
this._bufferTexture.get_root().set_onRestore(function(textureRoot1) {
textureRoot1.clear();
});
this._helperImage = new (starling_display_Image().default)(this._bufferTexture);
this._helperImage.set_textureSmoothing("none");
}
}
// Meta
RenderTexture.__name__ = "starling.textures.RenderTexture";
RenderTexture.__isInterface__ = false;
RenderTexture.__super__ = (starling_textures_SubTexture().default);
RenderTexture.prototype = $extend((starling_textures_SubTexture().default).prototype, {
dispose: function() {
if(this._helperImage != null) {
this._helperImage.dispose();
}
if(this.get_parent() != this._bufferTexture && this._bufferTexture != null) {
this._bufferTexture.dispose();
}
if(this.get_parent() != this._activeTexture) {
this._activeTexture.dispose();
}
(starling_textures_SubTexture().default).prototype.dispose.call(this);
},
draw: function(object,matrix,alpha,antiAliasing,cameraPos) {
if(antiAliasing == null) {
antiAliasing = 0;
}
if(alpha == null) {
alpha = 1.0;
}
if(object == null) {
return;
}
if(this._drawing) {
this.__render(object,matrix,alpha);
} else {
this.__renderBundled($bind(this,this.__render),object,matrix,alpha,antiAliasing,cameraPos);
}
},
drawBundled: function(drawingBlock,antiAliasing,cameraPos) {
if(antiAliasing == null) {
antiAliasing = 0;
}
var renderBlockFunc = function(object,matrix,alpha) {
drawingBlock();
};
this.__renderBundled(renderBlockFunc,null,null,1.0,antiAliasing,cameraPos);
},
__render: function(object,matrix,alpha) {
if(alpha == null) {
alpha = 1.0;
}
var painter = (starling_core_Starling().default).get_current().get_painter();
var state = painter.get_state();
var wasCacheEnabled = painter.get_cacheEnabled();
var filter = object.get_filter();
var mask = object.get_mask();
painter.set_cacheEnabled(false);
painter.pushState();
state.set_alpha(object.get_alpha() * alpha);
state.setModelviewMatricesToIdentity();
state.set_blendMode(object.get_blendMode() == "auto" ? "normal" : object.get_blendMode());
if(matrix != null) {
state.transformModelviewMatrix(matrix);
} else {
state.transformModelviewMatrix(object.get_transformationMatrix());
}
if(mask != null) {
painter.drawMask(mask,object);
}
if(filter != null) {
filter.render(painter);
} else {
object.render(painter);
}
if(mask != null) {
painter.eraseMask(mask,object);
}
painter.popState();
painter.set_cacheEnabled(wasCacheEnabled);
},
__renderBundled: function(renderBlock,object,matrix,alpha,antiAliasing,cameraPos) {
if(antiAliasing == null) {
antiAliasing = 0;
}
if(alpha == null) {
alpha = 1.0;
}
var painter = (starling_core_Starling().default).get_current().get_painter();
var state = painter.get_state();
if(!(starling_core_Starling().default).get_current().get_contextValid()) {
return;
}
if(this.get_isDoubleBuffered()) {
var tmpTexture = this._activeTexture;
this._activeTexture = this._bufferTexture;
this._bufferTexture = tmpTexture;
this._helperImage.set_texture(this._bufferTexture);
}
painter.pushState();
var rootTexture = this._activeTexture.get_root();
state.setProjectionMatrix(0,0,rootTexture.get_width(),rootTexture.get_height(),this.get_width(),this.get_height(),cameraPos);
RenderTexture.sClipRect.setTo(0,0,this._activeTexture.get_width(),this._activeTexture.get_height());
state.set_clipRect(RenderTexture.sClipRect);
state.setRenderTarget(this._activeTexture,true,antiAliasing);
painter.prepareToDraw();
painter.get_context().setStencilActions("frontAndBack","always");
if(this.get_isDoubleBuffered() || !this.get_isPersistent() || !this._bufferReady) {
painter.clear();
}
if(this.get_isDoubleBuffered() && this._bufferReady) {
this._helperImage.render(painter);
} else {
this._bufferReady = true;
}
try {
this._drawing = true;
renderBlock(object,matrix,alpha);
} catch( e ) {
var e1 = ((e) instanceof (js__$Boot_HaxeError().default)) ? e.val : e;
}
this._drawing = false;
painter.popState();
},
clear: function(color,alpha) {
if(alpha == null) {
alpha = 0.0;
}
if(color == null) {
color = 0;
}
this._activeTexture.get_root().clear(color,alpha);
this._bufferReady = true;
},
get_isDoubleBuffered: function() {
return this._bufferTexture != null;
},
get_isPersistent: function() {
return this._isPersistent;
},
get_base: function() {
return this._activeTexture.get_base();
},
get_root: function() {
return this._activeTexture.get_root();
}
});
RenderTexture.prototype.__class__ = RenderTexture.prototype.constructor = $hxClasses["starling.textures.RenderTexture"] = RenderTexture;
// Init
{
Object.defineProperties(RenderTexture.prototype,{ isPersistent : { get : function () { return this.get_isPersistent (); }}});
Object.defineProperties(RenderTexture,{ useDoubleBuffering : { get : function () { return RenderTexture.get_useDoubleBuffering (); }, set : function (v) { return RenderTexture.set_useDoubleBuffering (v); }}});
};
// Statics
RenderTexture.get_useDoubleBuffering = function() {
if((starling_core_Starling().default).get_current() != null) {
var painter = (starling_core_Starling().default).get_current().get_painter();
var sharedData = painter.get_sharedData();
if(sharedData.exists("starling.textures.RenderTexture.useDoubleBuffering")) {
return sharedData.get("starling.textures.RenderTexture.useDoubleBuffering");
} else {
var profile = painter.get_profile() != null ? painter.get_profile() : "baseline";
var value = profile == "baseline" || profile == "baselineConstrained";
sharedData.set("starling.textures.RenderTexture.useDoubleBuffering",value);
return value;
}
} else {
return false;
}
}
RenderTexture.set_useDoubleBuffering = function(value) {
if((starling_core_Starling().default).get_current() == null) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_IllegalOperationError().default)("Starling not yet initialized"));
} else {
(starling_core_Starling().default).get_current().get_painter().get_sharedData().set("starling.textures.RenderTexture.useDoubleBuffering",value);
}
return value;
}
RenderTexture.USE_DOUBLE_BUFFERING_DATA_NAME = "starling.textures.RenderTexture.useDoubleBuffering"
RenderTexture.sClipRect = new (openfl_geom_Rectangle().default)()
// Export
exports.default = RenderTexture;