starling-framework
Version:
A fast, productive library for 2D cross-platform development.
183 lines (167 loc) • 7.93 kB
JavaScript
// Class: starling.core.StatsDisplay
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_display_Sprite() {return require("./../../starling/display/Sprite");}
function starling_core_Starling() {return require("./../../starling/core/Starling");}
function js_Boot() {return require("./../../js/Boot");}
function starling_events_EnterFrameEvent() {return require("./../../starling/events/EnterFrameEvent");}
function openfl_system_System() {return $import(require("openfl/system/System"));}
function starling_utils_MathUtil() {return require("./../../starling/utils/MathUtil");}
function Std() {return require("./../../Std");}
function starling_text_TextField() {return require("./../../starling/text/TextField");}
function starling_display_Quad() {return require("./../../starling/display/Quad");}
function starling_styles_MeshStyle() {return require("./../../starling/styles/MeshStyle");}
// Constructor
var StatsDisplay = function() {
this.__showSkipped = false;
this.__skipCount = 0;
this.__drawCount = 0;
this.__gpuMemory = 0;
this.__memory = 0;
this.__fps = 0;
this.__totalTime = 0;
this.__frameCount = 0;
(starling_display_Sprite().default).call(this);
var fontName = "mini";
var fontSize = -1;
var fontColor = 16777215;
var width = 90;
var height = 45;
this.__labels = new (starling_text_TextField().default)(width,height);
this.__labels.get_format().setTo(fontName,fontSize,fontColor,"left","top");
this.__labels.set_batchable(true);
this.__labels.set_x(2);
this.__values = new (starling_text_TextField().default)(width - 1,height,"");
this.__values.get_format().setTo(fontName,fontSize,fontColor,"right","top");
this.__values.set_batchable(true);
this.__background = new (starling_display_Quad().default)(width,height,0);
this.updateLabels();
if(this.__background.get_style().get_type() != (starling_styles_MeshStyle().default)) {
this.__background.set_style(new (starling_styles_MeshStyle().default)());
}
if(this.__labels.get_style().get_type() != (starling_styles_MeshStyle().default)) {
this.__labels.set_style(new (starling_styles_MeshStyle().default)());
}
if(this.__values.get_style().get_type() != (starling_styles_MeshStyle().default)) {
this.__values.set_style(new (starling_styles_MeshStyle().default)());
}
this.addChild(this.__background);
this.addChild(this.__labels);
this.addChild(this.__values);
this.addEventListener("addedToStage",$bind(this,this.onAddedToStage));
this.addEventListener("removedFromStage",$bind(this,this.onRemovedFromStage));
}
// Meta
StatsDisplay.__name__ = "starling.core.StatsDisplay";
StatsDisplay.__isInterface__ = false;
StatsDisplay.__super__ = (starling_display_Sprite().default);
StatsDisplay.prototype = $extend((starling_display_Sprite().default).prototype, {
onAddedToStage: function(e) {
this.addEventListener("enterFrame",$bind(this,this.onEnterFrame));
(starling_core_Starling().default).get_current().addEventListener("skipFrame",$bind(this,this.onSkipFrame));
this.__totalTime = this.__frameCount = this.__skipCount = 0;
this.update();
},
onRemovedFromStage: function(e) {
this.removeEventListener("enterFrame",$bind(this,this.onEnterFrame));
(starling_core_Starling().default).get_current().removeEventListener("skipFrame",$bind(this,this.onSkipFrame));
},
onEnterFrame: function(e) {
var event = (js_Boot().default).__cast(e , (starling_events_EnterFrameEvent().default));
this.__totalTime += event.get_passedTime();
this.__frameCount++;
if(this.__totalTime > 0.5) {
this.update();
this.__frameCount = this.__skipCount = 0;
this.__totalTime = 0;
}
},
onSkipFrame: function() {
this.__skipCount += 1;
},
update: function() {
this.__background.set_color(this.__skipCount > this.__frameCount / 2 ? 16128 : 0);
this.__fps = this.__totalTime > 0 ? this.__frameCount / this.__totalTime : 0;
this.__memory = (openfl_system_System().default).get_totalMemory() * 9.5367431640625e-07;
this.__gpuMemory = this.get_supportsGpuMem() ? (starling_core_Starling().default).get_current().get_context().totalGPUMemory * 9.5367431640625e-07 : -1;
var skippedPerSec = this.__totalTime > 0 ? this.__skipCount / this.__totalTime : 0;
var skippedText = this.__showSkipped ? (starling_utils_MathUtil().default).toFixed(skippedPerSec,skippedPerSec < 100 ? 1 : 0) : "";
var fpsText = (starling_utils_MathUtil().default).toFixed(this.__fps,this.__fps < 100 ? 1 : 0);
var memText = (starling_utils_MathUtil().default).toFixed(this.__memory,this.__memory < 100 ? 1 : 0);
var gpuMemText = (starling_utils_MathUtil().default).toFixed(this.__gpuMemory,this.__gpuMemory < 100 ? 1 : 0);
var drwText = (Std().default).string(this.__totalTime > 0 ? this.__drawCount - 2 : this.__drawCount);
this.__values.set_text(fpsText + "\n" + (this.__showSkipped ? skippedText + "\n" : "") + memText + "\n" + (this.__gpuMemory >= 0 ? gpuMemText + "\n" : "") + drwText);
},
updateLabels: function() {
var labels = ["frames/sec:"];
if(this.__showSkipped) {
labels.splice(labels.length,0,"skipped/sec:");
}
labels.splice(labels.length,0,"std memory:");
if(this.get_supportsGpuMem()) {
labels.splice(labels.length,0,"gpu memory:");
}
labels.splice(labels.length,0,"draw calls:");
this.__labels.set_text(labels.join("\n"));
this.__background.set_height(this.__labels.get_textBounds().height + 4);
},
render: function(painter) {
painter.excludeFromCache(this);
painter.finishMeshBatch();
(starling_display_Sprite().default).prototype.render.call(this,painter);
},
getBounds: function(targetSpace,out) {
return this.__background.getBounds(targetSpace,out);
},
get_supportsGpuMem: function() {
return (starling_core_Starling().default).get_current().get_context().totalGPUMemory != 0;
},
get_drawCount: function() {
return this.__drawCount;
},
set_drawCount: function(value) {
return this.__drawCount = value;
},
get_fps: function() {
return this.__fps;
},
set_fps: function(value) {
return this.__fps = value;
},
get_memory: function() {
return this.__memory;
},
set_memory: function(value) {
return this.__memory = value;
},
get_gpuMemory: function() {
return this.__gpuMemory;
},
set_gpuMemory: function(value) {
return this.__gpuMemory = value;
},
get_showSkipped: function() {
return this.__showSkipped;
},
set_showSkipped: function(value) {
this.__showSkipped = value;
this.updateLabels();
this.update();
return this.__showSkipped;
}
});
StatsDisplay.prototype.__class__ = StatsDisplay.prototype.constructor = $hxClasses["starling.core.StatsDisplay"] = StatsDisplay;
// Init
Object.defineProperties(StatsDisplay.prototype,{ supportsGpuMem : { get : function () { return this.get_supportsGpuMem (); }}, drawCount : { get : function () { return this.get_drawCount (); }, set : function (v) { return this.set_drawCount (v); }}, fps : { get : function () { return this.get_fps (); }, set : function (v) { return this.set_fps (v); }}, memory : { get : function () { return this.get_memory (); }, set : function (v) { return this.set_memory (v); }}, gpuMemory : { get : function () { return this.get_gpuMemory (); }, set : function (v) { return this.set_gpuMemory (v); }}, showSkipped : { get : function () { return this.get_showSkipped (); }, set : function (v) { return this.set_showSkipped (v); }}});
// Statics
StatsDisplay.UPDATE_INTERVAL = 0.5
StatsDisplay.B_TO_MB = 9.5367431640625e-07
// Export
exports.default = StatsDisplay;