Skip to content

Commit

Permalink
Fix HaxeFlixel#1729. Also, make VCR save/load format more space-effic…
Browse files Browse the repository at this point in the history
…ient while leaving loader backward-compatible.
  • Loading branch information
Gimmicky Apps committed Feb 19, 2016
1 parent 10195f4 commit 8fa6857
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
16 changes: 13 additions & 3 deletions flixel/input/mouse/FlxMouse.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import flixel.input.mouse.FlxMouseButton;
import flixel.system.FlxAssets;
import flixel.system.replay.MouseRecord;
import flixel.util.FlxDestroyUtil;
import flixel.input.FlxInput.FlxInputState;
#if FLX_NATIVE_CURSOR
import flash.ui.MouseCursor;
import flash.ui.MouseCursorData;
Expand Down Expand Up @@ -119,11 +120,12 @@ class FlxMouse extends FlxPointer implements IFlxInputManager
private var _visibleWhenFocusLost:Bool = true;

/**
* Helper variables for recording purposes.
* Helper variables for recording purposes. The last one is also used for a mouseup workaround on playback.
*/
private var _lastX:Int = 0;
private var _lastY:Int = 0;
private var _lastWheel:Int = 0;
private var _lastLeftCurrent:FlxInputState;

//Helper variable for cleaning up memory
private var _stage:Stage;
Expand Down Expand Up @@ -592,19 +594,27 @@ class FlxMouse extends FlxPointer implements IFlxInputManager
private function record():MouseRecord
{
if ((_lastX == _globalScreenX) && (_lastY == _globalScreenY)
&& (_leftButton.released) && (_lastWheel == wheel))
&& (_lastLeftCurrent == _leftButton.current) && (_lastWheel == wheel))
{
return null;
}

_lastX = _globalScreenX;
_lastY = _globalScreenY;
_lastLeftCurrent = _leftButton.current;
_lastWheel = wheel;
return new MouseRecord(_lastX, _lastY, _leftButton.current, _lastWheel);
}

private function playback(Record:MouseRecord):Void
{
_leftButton.current = Record.button;
// Manually dispatch a MOUSE_UP event so that, e.g., FlxButtons click correctly on playback.
if ( ( (_lastLeftCurrent == FlxInputState.PRESSED) || (_lastLeftCurrent == FlxInputState.JUST_PRESSED) ) // some clicks are fast enough to not pass through a frame where they are PRESSED
&& ( (Record.button == FlxInputState.RELEASED) || (Record.button == FlxInputState.JUST_RELEASED) ) ) // and JUST_RELEASED is swallowed by FlxButton and others, but not third-party code
{
_stage.dispatchEvent( new MouseEvent( MouseEvent.MOUSE_UP, true, false, Record.x, Record.y ) );
}
_lastLeftCurrent = _leftButton.current = Record.button;
wheel = Record.wheel;
_globalScreenX = Record.x;
_globalScreenY = Record.y;
Expand Down
31 changes: 12 additions & 19 deletions flixel/system/replay/FlxReplay.hx
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,14 @@ class FlxReplay
var line:String;
var i:Int = 1;
var l:Int = lines.length;
while (i < l)
while (i < l - 1) // last line is blank, traditionally
{
line = lines[i++];
if (line.length > 3)
_frames[frameCount++] = new FrameRecord().load(line);
if (frameCount >= _capacity)
{
_frames[frameCount++] = new FrameRecord().load(line);
if (frameCount >= _capacity)
{
_capacity *= 2;
FlxArrayUtil.setLength(_frames, _capacity);
}
_capacity *= 2;
FlxArrayUtil.setLength(_frames, _capacity);
}
}

Expand All @@ -125,20 +122,22 @@ class FlxReplay

/**
* Save the current recording data off to a String object.
* Basically goes through and calls FrameRecord.save() on each frame in the replay.
* Basically goes through and calls FrameRecord.save() on each frame in the replay, and outputs it only if anything happened that frame.
* return The gameplay recording in simple ASCII format.
*/
public function save():String
{
if (frameCount <= 0)
{
return null;
}
var output:String = seed+"\n";

var output:String = seed + "\n";
var curLine:String;
var i:Int = 0;
while (i < frameCount)
{
output += _frames[i++].save() + "\n";
curLine = _frames[i++].save();
if (curLine != null)
output += curLine + "\n";
}
return output;
}
Expand Down Expand Up @@ -196,24 +195,18 @@ class FlxReplay
return;
}
if (_frames[_marker].frame != frame++)
{
return;
}

var fr:FrameRecord = _frames[_marker++];

#if !FLX_NO_KEYBOARD
if (fr.keys != null)
{
FlxG.keys.playback(fr.keys);
}
#end

#if !FLX_NO_MOUSE
if (fr.mouse != null)
{
FlxG.mouse.playback(fr.mouse);
}
#end
}

Expand Down
3 changes: 3 additions & 0 deletions flixel/system/replay/FrameRecord.hx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class FrameRecord
public function save():String
{
var output:String = frame + "k";

if ((keys == null) && (mouse == null))
return null;

if (keys != null)
{
Expand Down

0 comments on commit 8fa6857

Please sign in to comment.