212 lines
4.6 KiB
Haxe
212 lines
4.6 KiB
Haxe
package;
|
|
|
|
import flixel.addons.display.FlxPieDial;
|
|
import flixel.group.FlxSpriteGroup;
|
|
import flixel.FlxSprite;
|
|
import flixel.util.FlxColor;
|
|
import flixel.math.FlxMath;
|
|
#if hxvlc
|
|
import hxvlc.flixel.FlxVideoSprite;
|
|
#end
|
|
|
|
class VideoSprite extends FlxSpriteGroup
|
|
{
|
|
#if VIDEOS_ALLOWED
|
|
public var finishCallback:Void->Void = null;
|
|
public var onSkip:Void->Void = null;
|
|
|
|
final _timeToSkip:Float = 1;
|
|
|
|
public var holdingTime:Float = 0;
|
|
public var videoSprite:FlxVideoSprite;
|
|
public var skipSprite:FlxPieDial;
|
|
public var cover:FlxSprite;
|
|
public var canSkip(default, set):Bool = false;
|
|
|
|
private var videoName:String;
|
|
|
|
public var waiting:Bool = false;
|
|
public var didPlay:Bool = false;
|
|
|
|
public var addCover:Bool = true;
|
|
|
|
private var controls(get, never):Controls;
|
|
|
|
inline function get_controls():Controls
|
|
return PlayerSettings.player1.controls;
|
|
|
|
public function new(videoName:String, isWaiting:Bool, canSkip:Bool = false, shouldLoop:Dynamic = false)
|
|
{
|
|
super();
|
|
|
|
this.videoName = videoName;
|
|
scrollFactor.set();
|
|
cameras = [FlxG.cameras.list[FlxG.cameras.list.length - 1]];
|
|
|
|
waiting = isWaiting;
|
|
if (!waiting || addCover) // for mid song videos, if not mid song, don't add the cover since it's not needed
|
|
{
|
|
cover = new FlxSprite().makeGraphic(1, 1, FlxColor.BLACK);
|
|
cover.scale.set(FlxG.width + 100, FlxG.height + 100);
|
|
cover.screenCenter();
|
|
cover.scrollFactor.set();
|
|
add(cover);
|
|
}
|
|
|
|
// initialize sprites
|
|
videoSprite = new FlxVideoSprite();
|
|
videoSprite.antialiasing = ClientPrefs.globalAntialiasing;
|
|
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();
|
|
}
|
|
|
|
final curState = FlxG.state;
|
|
|
|
if (PlayState.instance != null)
|
|
PlayState.instance.remove(this);
|
|
else if (curState != null)
|
|
curState.remove(this, true);
|
|
destroy();
|
|
alreadyDestroyed = true;
|
|
});
|
|
}
|
|
|
|
videoSprite.bitmap.onFormatSetup.add(function()
|
|
{
|
|
/*
|
|
#if hxvlc
|
|
var wd:Int = videoSprite.bitmap.formatWidth;
|
|
var hg:Int = videoSprite.bitmap.formatHeight;
|
|
trace('Video Resolution: ${wd}x${hg}');
|
|
videoSprite.scale.set(FlxG.width / wd, FlxG.height / hg);
|
|
#end
|
|
*/
|
|
videoSprite.setGraphicSize(FlxG.width);
|
|
videoSprite.updateHitbox();
|
|
videoSprite.screenCenter();
|
|
});
|
|
|
|
// start video and adjust resolution to screen size
|
|
videoSprite.load(videoName, shouldLoop ? ['input-repeat=65545'] : null);
|
|
}
|
|
|
|
var alreadyDestroyed:Bool = false;
|
|
|
|
override function destroy()
|
|
{
|
|
if (alreadyDestroyed)
|
|
{
|
|
super.destroy();
|
|
return;
|
|
}
|
|
|
|
trace('Video destroyed');
|
|
if (cover != null)
|
|
{
|
|
remove(cover);
|
|
cover.destroy();
|
|
}
|
|
|
|
if (finishCallback != null)
|
|
finishCallback();
|
|
onSkip = null;
|
|
|
|
final curState = FlxG.state;
|
|
|
|
if (PlayState.instance != null)
|
|
PlayState.instance.remove(this);
|
|
else if (curState != null)
|
|
curState.remove(this, true);
|
|
}
|
|
|
|
override function update(elapsed:Float)
|
|
{
|
|
if (canSkip)
|
|
{
|
|
if (controls.ACCEPT)
|
|
{
|
|
holdingTime = Math.max(0, Math.min(_timeToSkip, holdingTime + elapsed));
|
|
}
|
|
else if (holdingTime > 0)
|
|
{
|
|
holdingTime = Math.max(0, FlxMath.lerp(holdingTime, -0.1, FlxMath.bound(elapsed * 3, 0, 1)));
|
|
}
|
|
updateSkipAlpha();
|
|
|
|
if (holdingTime >= _timeToSkip)
|
|
{
|
|
if (onSkip != null)
|
|
onSkip();
|
|
finishCallback = null;
|
|
videoSprite.bitmap.onEndReached.dispatch();
|
|
PlayState.instance.remove(this);
|
|
trace('Skipped video');
|
|
super.destroy();
|
|
return;
|
|
}
|
|
}
|
|
super.update(elapsed);
|
|
}
|
|
|
|
function set_canSkip(newValue:Bool)
|
|
{
|
|
canSkip = newValue;
|
|
if (canSkip)
|
|
{
|
|
if (skipSprite == null)
|
|
{
|
|
skipSprite = new FlxPieDial(0, 0, 40, FlxColor.WHITE, 40, true, 24);
|
|
skipSprite.replaceColor(FlxColor.BLACK, FlxColor.TRANSPARENT);
|
|
skipSprite.x = FlxG.width - (skipSprite.width + 80);
|
|
skipSprite.y = FlxG.height - (skipSprite.height + 72);
|
|
skipSprite.amount = 0;
|
|
add(skipSprite);
|
|
}
|
|
}
|
|
else if (skipSprite != null)
|
|
{
|
|
remove(skipSprite);
|
|
skipSprite.destroy();
|
|
skipSprite = null;
|
|
}
|
|
return canSkip;
|
|
}
|
|
|
|
function updateSkipAlpha()
|
|
{
|
|
if (skipSprite == null)
|
|
return;
|
|
|
|
skipSprite.amount = Math.min(1, Math.max(0, (holdingTime / _timeToSkip) * 1.025));
|
|
skipSprite.alpha = FlxMath.remapToRange(skipSprite.amount, 0.025, 1, 0, 1);
|
|
}
|
|
|
|
public function resume()
|
|
{
|
|
if (videoSprite != null)
|
|
videoSprite.resume();
|
|
}
|
|
|
|
public function pause()
|
|
{
|
|
if (videoSprite != null)
|
|
videoSprite.pause();
|
|
}
|
|
#end
|
|
}
|