From a6b5fb3771ec101e829649182c1b7a9641e7d2a6 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Sun, 12 May 2019 17:32:14 +0200 Subject: [PATCH] Console plugin --- Sources/arm/Console.hx | 140 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 Sources/arm/Console.hx diff --git a/Sources/arm/Console.hx b/Sources/arm/Console.hx new file mode 100644 index 00000000..eb7f9b90 --- /dev/null +++ b/Sources/arm/Console.hx @@ -0,0 +1,140 @@ +package arm; + +import kha.Scheduler; +import zui.Zui; +import zui.Id; + +class Console { + + static var first = true; + static var lastTime = 0.0; + static var frameTime = 0.0; + static var totalTime = 0.0; + static var frames = 0; + static var frameTimeAvg = 0.0; + static var graph:kha.Image = null; + static var graphA:kha.Image = null; + static var graphB:kha.Image = null; + static var lrow = [1/2, 1/2]; + static var haxeTrace:Dynamic->haxe.PosInfos->Void = null; + static var lastTraces:Array = ['']; + + static function consoleTrace(v:Dynamic, ?inf:haxe.PosInfos) { + lastTraces.unshift(Std.string(v)); + if (lastTraces.length > 10) lastTraces.pop(); + haxeTrace(v, inf); + } + + static function updateGraph() { + if (graph == null) { + graphA = kha.Image.createRenderTarget(280, 33); + graphB = kha.Image.createRenderTarget(280, 33); + graph = graphA; + } + else graph = graph == graphA ? graphB : graphA; + var graphPrev = graph == graphA ? graphB : graphA; + + graph.g2.begin(true, 0x00000000); + graph.g2.color = 0xffffffff; + graph.g2.drawImage(graphPrev, -3, 0); + + var avg = Math.round(frameTimeAvg * 1000); + var miss = avg > 16.7 ? (avg - 16.7) / 16.7 : 0.0; + graph.g2.color = kha.Color.fromFloats(miss, 1 - miss, 0, 1.0); + graph.g2.fillRect(280 - 3, 33 - avg, 3, avg); + + graph.g2.color = 0xff000000; + graph.g2.fillRect(280 - 3, 33 - 17, 3, 1); + + graph.g2.end(); + } + + public static function render(ui:Zui) { + + if (first) { + iron.App.notifyOnRender2D(tick); + first = false; + if (haxeTrace == null) { + haxeTrace = haxe.Log.trace; + haxe.Log.trace = consoleTrace; + } + } + + var avg = Math.round(frameTimeAvg * 10000) / 10; + var fpsAvg = avg > 0 ? Math.round(1000 / avg) : 0; + + if (ui.panel(Id.handle({selected: false}), 'Performance')) { + if (graph != null) ui.image(graph); + ui.indent(); + + ui.row(lrow); + ui.text('Frame'); + ui.text('$avg ms / $fpsAvg fps', Align.Right); + + ui.unindent(); + } + + // if (ui.panel(Id.handle({selected: false}), 'Render Targets')) { + // ui.indent(); + // #if (kha_opengl || kha_webgl) + // ui.imageInvertY = true; + // #end + // for (rt in iron.RenderPath.active.renderTargets) { + // ui.text(rt.raw.name); + // if (rt.image != null && !rt.is3D) { + // ui.image(rt.image); + // } + // } + // #if (kha_opengl || kha_webgl) + // ui.imageInvertY = false; + // #end + // ui.unindent(); + // } + + // if (ui.panel(Id.handle({selected: false}), 'Cached Materials')) { + // ui.indent(); + // for (c in iron.data.Data.cachedMaterials) { + // ui.text(c.name); + // } + // ui.unindent(); + // } + + // if (ui.panel(Id.handle({selected: false}), 'Cached Shaders')) { + // ui.indent(); + // for (c in iron.data.Data.cachedShaders) { + // ui.text(c.name); + // } + // ui.unindent(); + // } + + #if js + if (ui.panel(Id.handle({selected: false}), 'Script')) { + ui.indent(); + ui.row([8/10, 2/10]); + var t = ui.textInput(Id.handle()); + if (ui.button("Run")) { + try { trace("> " + t); js.Lib.eval(t); } + catch(e:Dynamic) { trace(e); } + } + for (t in lastTraces) ui.text(t); + ui.unindent(); + } + #end + } + + static function tick(g:kha.graphics2.Graphics) { + totalTime += frameTime; + frames++; + if (totalTime > 1.0) { + arm.ui.UITrait.inst.hwnd.redraws = 1; + frameTimeAvg = totalTime / frames; + totalTime = 0; + frames = 0; + g.end(); + updateGraph(); + g.begin(false); + } + frameTime = Scheduler.realTime() - lastTime; + lastTime = Scheduler.realTime(); + } +}