From 14d9101ed73977a026ea123cba3604a024e188b1 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Fri, 29 Mar 2024 19:00:39 -0400 Subject: [PATCH 1/5] add FlxReplay getDuration --- flixel/system/replay/FlxReplay.hx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/flixel/system/replay/FlxReplay.hx b/flixel/system/replay/FlxReplay.hx index 6771ba789e..410b980670 100644 --- a/flixel/system/replay/FlxReplay.hx +++ b/flixel/system/replay/FlxReplay.hx @@ -241,4 +241,16 @@ class FlxReplay FlxArrayUtil.setLength(_frames, _capacity); frameCount = 0; } + + /** + * Returns the last recorded frame index. **Note:** this is different from `frameCount`, which + * is the number of unique records, which doesn't count frames with no input + */ + public function getDuration() + { + if (_frames != null) + return _frames[_frames.length - 1].frame; + + return 0; + } } From ed38a2104f3b087c4936b28f69d263c0a8d68bec Mon Sep 17 00:00:00 2001 From: George FunBook Date: Wed, 3 Apr 2024 15:39:28 -0500 Subject: [PATCH 2/5] style --- flixel/input/keyboard/FlxKeyboard.hx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/flixel/input/keyboard/FlxKeyboard.hx b/flixel/input/keyboard/FlxKeyboard.hx index c70ddc1f07..a21a40fe73 100644 --- a/flixel/input/keyboard/FlxKeyboard.hx +++ b/flixel/input/keyboard/FlxKeyboard.hx @@ -168,16 +168,16 @@ class FlxKeyboard extends FlxKeyManager * @param Record Array of data about key states. */ @:allow(flixel.system.replay.FlxReplay) - function playback(Record:Array):Void + function playback(record:Array):Void { - var i:Int = 0; - var l:Int = Record.length; + var i = 0; + final len = record.length; - while (i < l) + while (i < len) { - var o = Record[i++]; - var o2 = getKey(o.code); - o2.current = o.value; + final keyRecord = record[i++]; + final key = getKey(keyRecord.code); + key.current = keyRecord.value; } } } From 80faec796a65e51122a703aef15d2f2fd3929065 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 9 May 2024 14:14:13 -0500 Subject: [PATCH 3/5] add 1 to frame duration --- flixel/system/replay/FlxReplay.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flixel/system/replay/FlxReplay.hx b/flixel/system/replay/FlxReplay.hx index 410b980670..510fcaf5e0 100644 --- a/flixel/system/replay/FlxReplay.hx +++ b/flixel/system/replay/FlxReplay.hx @@ -249,7 +249,7 @@ class FlxReplay public function getDuration() { if (_frames != null) - return _frames[_frames.length - 1].frame; + return _frames[_frames.length - 1].frame + 1; return 0; } From 3f6dce29b9e98a4f2167a751a1e3308d96cbc297 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 9 May 2024 14:14:19 -0500 Subject: [PATCH 4/5] add docs --- flixel/system/replay/FlxReplay.hx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/flixel/system/replay/FlxReplay.hx b/flixel/system/replay/FlxReplay.hx index 510fcaf5e0..013092f684 100644 --- a/flixel/system/replay/FlxReplay.hx +++ b/flixel/system/replay/FlxReplay.hx @@ -24,6 +24,7 @@ class FlxReplay /** * The number of frames in this recording. + * **Note:** This doesn't include empty records, unlike `getDuration()` */ public var frameCount:Int; @@ -243,13 +244,18 @@ class FlxReplay } /** - * Returns the last recorded frame index. **Note:** this is different from `frameCount`, which + * The duration of this replay, in frames. **Note:** this is different from `frameCount`, which * is the number of unique records, which doesn't count frames with no input + * + * @since 5.9.0 */ public function getDuration() { if (_frames != null) + { + // Add 1 to the last frame index, because they are zero-based return _frames[_frames.length - 1].frame + 1; + } return 0; } From f9fe38bdef62d06e27663a3198124928d74461c9 Mon Sep 17 00:00:00 2001 From: George FunBook Date: Thu, 9 May 2024 14:14:30 -0500 Subject: [PATCH 5/5] add unit test --- tests/unit/src/flixel/system/replay/FlxReplayTest.hx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/unit/src/flixel/system/replay/FlxReplayTest.hx b/tests/unit/src/flixel/system/replay/FlxReplayTest.hx index 85a6d564a5..094465c074 100644 --- a/tests/unit/src/flixel/system/replay/FlxReplayTest.hx +++ b/tests/unit/src/flixel/system/replay/FlxReplayTest.hx @@ -102,6 +102,15 @@ class FlxReplayTest extends FlxTest { return new FrameRecord().create(i, null, new MouseRecord(0, 0, mouseState, 0)); } + + @Test // #3135 + function testGetDuration() + { + var replay = new FlxReplay(); + replay.load("987654321\n299km0,0,2,0\n"); + // add 1 because frame indices are zero-based + Assert.areEqual(300, replay.getDuration()); + } } class ReplayState extends FlxState