From 87c4248d48102dc2d306b665dd91b673547810aa Mon Sep 17 00:00:00 2001 From: shallowmallow Date: Tue, 13 Feb 2024 10:42:01 +0100 Subject: [PATCH 1/3] Calculating frames only when needed --- src/custom/ColorTable.hx | 16 ++++++++++++++++ src/custom/DemoGraph.hx | 14 ++++++++++++++ src/custom/MiniGraph.hx | 22 +++++++++++++++++++--- src/custom/Noise.hx | 23 ++++++++++++++++++++--- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/src/custom/ColorTable.hx b/src/custom/ColorTable.hx index 9d01e64..629493e 100644 --- a/src/custom/ColorTable.hx +++ b/src/custom/ColorTable.hx @@ -1,4 +1,5 @@ package custom; +import haxe.ui.events.UIEvent; import haxe.io.Bytes; import haxe.ui.Toolkit; import haxe.ui.components.Canvas; @@ -12,6 +13,20 @@ class ColorTable extends Canvas { super(); componentGraphics.setProperty("html5.graphics.method", "canvas"); } + + private var _showNextFrame = false; + + @:bind(this, UIEvent.SHOWN) + private function onShown(_) { + _showNextFrame = true; + if (pixels == null) pixels = Bytes.alloc(Std.int(this.width * this.height * 4)); + frame(); + } + + @:bind(this, UIEvent.HIDDEN) + private function onHidden(_) { + _showNextFrame = false; + } private override function onReady() { super.onReady(); @@ -20,6 +35,7 @@ class ColorTable extends Canvas { } private function frame() { + if (!_showNextFrame) return; drawColorTable(componentGraphics, Std.int(this.width), Std.int(this.height), 1); } diff --git a/src/custom/DemoGraph.hx b/src/custom/DemoGraph.hx index b22baa3..e30e6de 100644 --- a/src/custom/DemoGraph.hx +++ b/src/custom/DemoGraph.hx @@ -1,5 +1,6 @@ package custom; +import haxe.ui.events.UIEvent; import haxe.ui.Toolkit; import haxe.ui.components.Canvas; import haxe.ui.geom.Point; @@ -8,8 +9,20 @@ import haxe.ui.graphics.ComponentGraphics; class DemoGraph extends Canvas { public function new() { super(); + } + + private var _showNextFrame = false; + + @:bind(this, UIEvent.SHOWN) + private function onShown(_) { + _showNextFrame = true; frame(); } + + @:bind(this, UIEvent.HIDDEN) + private function onHidden(_) { + _showNextFrame = false; + } private static var offset1:Float = 0; private static var offset1Direction = 1; @@ -24,6 +37,7 @@ class DemoGraph extends Canvas { private var _pointCDirection = 1; private var _pointCOffset:Float = 0; private function frame() { + if (!_showNextFrame) return; componentGraphics.clear(); drawGrid(componentGraphics, 520, 420, 10); diff --git a/src/custom/MiniGraph.hx b/src/custom/MiniGraph.hx index eb66064..2ef5840 100644 --- a/src/custom/MiniGraph.hx +++ b/src/custom/MiniGraph.hx @@ -1,5 +1,6 @@ package custom; +import haxe.ui.events.UIEvent; import haxe.io.Bytes; import haxe.ui.Toolkit; import haxe.ui.components.Canvas; @@ -12,12 +13,26 @@ class MiniGraph extends Canvas { super(); componentGraphics.setProperty("html5.graphics.method", "canvas"); } + + private var _showNextFrame = false; + + @:bind(this, UIEvent.SHOWN) + private function onShown(_) { + _showNextFrame = true; + if (pixels == null) { + pixels = Bytes.alloc(Std.int(this.width * this.height * 4)); + populateDataPoints(); + } + frame(); + } + + @:bind(this, UIEvent.HIDDEN) + private function onHidden(_) { + _showNextFrame = false; + } private override function onReady() { super.onReady(); - pixels = Bytes.alloc(Std.int(this.width * this.height * 4)); - populateDataPoints(); - frame(); } private function populateDataPoints() { @@ -56,6 +71,7 @@ class MiniGraph extends Canvas { var skipFrame:Bool = false; // lets just slow it down in a hacky way bu skipping every other "frame" private function frame() { + if (!_showNextFrame) return; if (skipFrame == true) { skipFrame = false; Toolkit.callLater(frame); diff --git a/src/custom/Noise.hx b/src/custom/Noise.hx index 08dd563..f7b8bd7 100644 --- a/src/custom/Noise.hx +++ b/src/custom/Noise.hx @@ -1,5 +1,6 @@ package custom; +import haxe.ui.events.UIEvent; import haxe.io.Bytes; import haxe.ui.Toolkit; import haxe.ui.components.Canvas; @@ -15,16 +16,32 @@ class Noise extends Canvas { super(); componentGraphics.setProperty("html5.graphics.method", "canvas"); } + + private var _showNextFrame = false; + + @:bind(this, UIEvent.SHOWN) + private function onShown(_) { + _showNextFrame = true; + if (pixels == null) { + pixels = Bytes.alloc(Std.int(this.width * this.height * 4)); + } + frame(); + } + + @:bind(this, UIEvent.HIDDEN) + private function onHidden(_) { + _showNextFrame = false; + } private override function onReady() { super.onReady(); - pixels = Bytes.alloc(Std.int(this.width * this.height * 4)); - frame(); + } var skipFrame:Bool = false; // lets just slow it down in a hacky way bu skipping every other "frame" private function frame() { - if (skipFrame == true) { + if (!_showNextFrame) return; + if (skipFrame) { skipFrame = false; Toolkit.callLater(frame); return; From dcd1aafaa20e06b433a59f6fc73f31127a0566a5 Mon Sep 17 00:00:00 2001 From: shallowmallow Date: Tue, 13 Feb 2024 11:41:43 +0100 Subject: [PATCH 2/3] Removing now useless onReady --- src/custom/ColorTable.hx | 6 ------ src/custom/DemoGraph.hx | 4 ---- src/custom/MiniGraph.hx | 4 ---- src/custom/Noise.hx | 5 ----- 4 files changed, 19 deletions(-) diff --git a/src/custom/ColorTable.hx b/src/custom/ColorTable.hx index 629493e..bc27c2b 100644 --- a/src/custom/ColorTable.hx +++ b/src/custom/ColorTable.hx @@ -28,12 +28,6 @@ class ColorTable extends Canvas { _showNextFrame = false; } - private override function onReady() { - super.onReady(); - pixels = Bytes.alloc(Std.int(this.width * this.height * 4)); - frame(); - } - private function frame() { if (!_showNextFrame) return; drawColorTable(componentGraphics, Std.int(this.width), Std.int(this.height), 1); diff --git a/src/custom/DemoGraph.hx b/src/custom/DemoGraph.hx index e30e6de..86bf7f5 100644 --- a/src/custom/DemoGraph.hx +++ b/src/custom/DemoGraph.hx @@ -7,10 +7,6 @@ import haxe.ui.geom.Point; import haxe.ui.graphics.ComponentGraphics; class DemoGraph extends Canvas { - public function new() { - super(); - } - private var _showNextFrame = false; @:bind(this, UIEvent.SHOWN) diff --git a/src/custom/MiniGraph.hx b/src/custom/MiniGraph.hx index 2ef5840..929287f 100644 --- a/src/custom/MiniGraph.hx +++ b/src/custom/MiniGraph.hx @@ -31,10 +31,6 @@ class MiniGraph extends Canvas { _showNextFrame = false; } - private override function onReady() { - super.onReady(); - } - private function populateDataPoints() { var cx = Std.int(this.width); var missing = cx - _dataPoints.length; diff --git a/src/custom/Noise.hx b/src/custom/Noise.hx index f7b8bd7..af974fa 100644 --- a/src/custom/Noise.hx +++ b/src/custom/Noise.hx @@ -33,11 +33,6 @@ class Noise extends Canvas { _showNextFrame = false; } - private override function onReady() { - super.onReady(); - - } - var skipFrame:Bool = false; // lets just slow it down in a hacky way bu skipping every other "frame" private function frame() { if (!_showNextFrame) return; From aab3acc6697d4ada3d3db45fa80ffc9297eda55b Mon Sep 17 00:00:00 2001 From: shallowmallow Date: Tue, 13 Feb 2024 12:55:45 +0100 Subject: [PATCH 3/3] onShown is called twice, so do a check before showing the frame --- src/custom/ColorTable.hx | 1 + src/custom/DemoGraph.hx | 1 + src/custom/MiniGraph.hx | 1 + src/custom/Noise.hx | 1 + 4 files changed, 4 insertions(+) diff --git a/src/custom/ColorTable.hx b/src/custom/ColorTable.hx index bc27c2b..9230229 100644 --- a/src/custom/ColorTable.hx +++ b/src/custom/ColorTable.hx @@ -18,6 +18,7 @@ class ColorTable extends Canvas { @:bind(this, UIEvent.SHOWN) private function onShown(_) { + if (_showNextFrame) return; _showNextFrame = true; if (pixels == null) pixels = Bytes.alloc(Std.int(this.width * this.height * 4)); frame(); diff --git a/src/custom/DemoGraph.hx b/src/custom/DemoGraph.hx index 86bf7f5..f36b5d0 100644 --- a/src/custom/DemoGraph.hx +++ b/src/custom/DemoGraph.hx @@ -11,6 +11,7 @@ class DemoGraph extends Canvas { @:bind(this, UIEvent.SHOWN) private function onShown(_) { + if (_showNextFrame) return; _showNextFrame = true; frame(); } diff --git a/src/custom/MiniGraph.hx b/src/custom/MiniGraph.hx index 929287f..11dc9d6 100644 --- a/src/custom/MiniGraph.hx +++ b/src/custom/MiniGraph.hx @@ -18,6 +18,7 @@ class MiniGraph extends Canvas { @:bind(this, UIEvent.SHOWN) private function onShown(_) { + if (_showNextFrame) return; _showNextFrame = true; if (pixels == null) { pixels = Bytes.alloc(Std.int(this.width * this.height * 4)); diff --git a/src/custom/Noise.hx b/src/custom/Noise.hx index af974fa..8d3c4e0 100644 --- a/src/custom/Noise.hx +++ b/src/custom/Noise.hx @@ -21,6 +21,7 @@ class Noise extends Canvas { @:bind(this, UIEvent.SHOWN) private function onShown(_) { + if (_showNextFrame) return; _showNextFrame = true; if (pixels == null) { pixels = Bytes.alloc(Std.int(this.width * this.height * 4));