Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video Sprite Fixes & Improvements #16138

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
video sprite improvements
  • Loading branch information
LarryFrosty committed Jan 11, 2025
commit 9be53b66ba951a5ce395dbe9c0db17d1b96bc974
39 changes: 14 additions & 25 deletions source/objects/VideoSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class VideoSprite extends FlxSpriteGroup {
private var videoName:String;

public var waiting:Bool = false;
public var didPlay:Bool = false;

public function new(videoName:String, isWaiting:Bool, canSkip:Bool = false, shouldLoop:Dynamic = false) {
public function new(videoName:String, isWaiting:Bool, canSkip:Bool = false, shouldLoop:Dynamic = false, autoPause = true) {
super();

this.videoName = videoName;
Expand All @@ -43,27 +42,12 @@ class VideoSprite extends FlxSpriteGroup {
// initialize sprites
videoSprite = new FlxVideoSprite();
videoSprite.antialiasing = ClientPrefs.data.antialiasing;
videoSprite.autoPause = autoPause;
add(videoSprite);
if(canSkip) this.canSkip = true;

// callbacks
if(!shouldLoop)
{
videoSprite.bitmap.onEndReached.add(function() {
if(alreadyDestroyed) return;

trace('Video destroyed');
if(cover != null)
{
remove(cover);
cover.destroy();
}

PlayState.instance.remove(this);
destroy();
alreadyDestroyed = true;
});
}
if(!shouldLoop) videoSprite.bitmap.onEndReached.add(destroy);

videoSprite.bitmap.onFormatSetup.add(function()
{
Expand All @@ -88,10 +72,7 @@ class VideoSprite extends FlxSpriteGroup {
override function destroy()
{
if(alreadyDestroyed)
{
super.destroy();
return;
}

trace('Video destroyed');
if(cover != null)
Expand All @@ -104,7 +85,16 @@ class VideoSprite extends FlxSpriteGroup {
finishCallback();
onSkip = null;

PlayState.instance.remove(this);
if(FlxG.state != null)
{
if(FlxG.state.members.contains(this))
FlxG.state.remove(this);

if(FlxG.state.subState != null && FlxG.state.subState.members.contains(this))
FlxG.state.subState.remove(this);
}
super.destroy();
alreadyDestroyed = true;
}

override function update(elapsed:Float)
Expand All @@ -126,9 +116,7 @@ class VideoSprite extends FlxSpriteGroup {
if(onSkip != null) onSkip();
finishCallback = null;
videoSprite.bitmap.onEndReached.dispatch();
PlayState.instance.remove(this);
trace('Skipped video');
super.destroy();
return;
}
}
Expand Down Expand Up @@ -167,6 +155,7 @@ class VideoSprite extends FlxSpriteGroup {
skipSprite.alpha = FlxMath.remapToRange(skipSprite.amount, 0.025, 1, 0, 1);
}

public function play() videoSprite?.play();
public function resume() videoSprite?.resume();
public function pause() videoSprite?.pause();
#end
Expand Down
4 changes: 2 additions & 2 deletions source/psychlua/FunkinLua.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ class FunkinLua {
}
return false;
});
Lua_helper.add_callback(lua, "startVideo", function(videoFile:String, ?canSkip:Bool = true) {
Lua_helper.add_callback(lua, "startVideo", function(videoFile:String, ?canSkip:Bool = true, ?forMidSong:Bool = false, ?shouldLoop:Bool = false, ?playOnLoad:Bool = true) {
#if VIDEOS_ALLOWED
if(FileSystem.exists(Paths.video(videoFile)))
{
Expand All @@ -1272,7 +1272,7 @@ class FunkinLua {
game.remove(game.videoCutscene);
game.videoCutscene.destroy();
}
game.videoCutscene = game.startVideo(videoFile, false, canSkip);
game.videoCutscene = game.startVideo(videoFile, forMidSong, canSkip, shouldLoop, playOnLoad);
return true;
}
else
Expand Down
43 changes: 32 additions & 11 deletions source/states/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,8 @@ class PlayState extends MusicBeatState
public function startVideo(name:String, forMidSong:Bool = false, canSkip:Bool = true, loop:Bool = false, playOnLoad:Bool = true)
{
#if VIDEOS_ALLOWED
inCutscene = true;
canPause = false;
inCutscene = !forMidSong;
canPause = forMidSong;

var foundFile:Bool = false;
var fileName:String = Paths.video(name);
Expand All @@ -844,23 +844,24 @@ class PlayState extends MusicBeatState
{
function onVideoEnd()
{
if (generatedMusic && PlayState.SONG.notes[Std.int(curStep / 16)] != null && !endingSong && !isCameraOnForcedPos)
if (isDead && generatedMusic && PlayState.SONG.notes[Std.int(curStep / 16)] != null && !endingSong && !isCameraOnForcedPos)
{
moveCameraSection();
FlxG.camera.snapToTarget();
}
videoCutscene = null;
canPause = false;
canPause = true;
inCutscene = false;
startAndEnd();
}
videoCutscene.finishCallback = onVideoEnd;
videoCutscene.onSkip = onVideoEnd;
}
add(videoCutscene);
if (GameOverSubstate.instance != null && isDead) GameOverSubstate.instance.add(videoCutscene);
else add(videoCutscene);

if (playOnLoad)
videoCutscene.videoSprite.play();
videoCutscene.play();
return videoCutscene;
}
#if (LUA_ALLOWED || HSCRIPT_ALLOWED)
Expand Down Expand Up @@ -1565,6 +1566,7 @@ class PlayState extends MusicBeatState
}
FlxTimer.globalManager.forEach(function(tmr:FlxTimer) if(!tmr.finished) tmr.active = false);
FlxTween.globalManager.forEach(function(twn:FlxTween) if(!twn.finished) twn.active = false);
if(videoCutscene != null) videoCutscene.pause();
}

super.openSubState(SubState);
Expand All @@ -1584,6 +1586,7 @@ class PlayState extends MusicBeatState
}
FlxTimer.globalManager.forEach(function(tmr:FlxTimer) if(!tmr.finished) tmr.active = true);
FlxTween.globalManager.forEach(function(twn:FlxTween) if(!twn.finished) twn.active = true);
if(videoCutscene != null) videoCutscene.resume();

paused = false;
callOnScripts('onResume');
Expand All @@ -1593,16 +1596,23 @@ class PlayState extends MusicBeatState

override public function onFocus():Void
{
if (health > 0 && !paused) resetRPC(Conductor.songPosition > 0.0);
if (!paused)
{
if (health > 0) resetRPC(Conductor.songPosition > 0.0);
if (videoCutscene != null) videoCutscene.resume();
}
super.onFocus();
}

override public function onFocusLost():Void
{
#if DISCORD_ALLOWED
if (health > 0 && !paused && autoUpdateRPC) DiscordClient.changePresence(detailsPausedText, SONG.song + " (" + storyDifficultyText + ")", iconP2.getCharacter());
#end

if (!paused)
{
#if DISCORD_ALLOWED
if (health > 0 && autoUpdateRPC) DiscordClient.changePresence(detailsPausedText, SONG.song + " (" + storyDifficultyText + ")", iconP2.getCharacter());
#end
if (videoCutscene != null) videoCutscene.pause();
}
super.onFocusLost();
}

Expand Down Expand Up @@ -1966,6 +1976,11 @@ class PlayState extends MusicBeatState
paused = true;
canResync = false;
canPause = false;
if(videoCutscene != null)
{
videoCutscene.destroy();
videoCutscene = null;
}

persistentUpdate = false;
persistentDraw = false;
Expand Down Expand Up @@ -3139,6 +3154,12 @@ class PlayState extends MusicBeatState
#end
stagesFunc(function(stage:BaseStage) stage.destroy());

if(videoCutscene != null)
{
videoCutscene.destroy();
videoCutscene = null;
}

FlxG.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
FlxG.stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyRelease);

Expand Down
Loading