Funkin/source/funkin/ui/freeplay/CapsuleText.hx

192 lines
5 KiB
Haxe
Raw Normal View History

package funkin.ui.freeplay;
2023-08-14 02:49:18 -04:00
import openfl.filters.BitmapFilterQuality;
import flixel.text.FlxText;
import flixel.group.FlxSpriteGroup;
import funkin.graphics.shaders.GaussianBlurShader;
import funkin.graphics.shaders.LeftMaskShader;
import flixel.math.FlxRect;
import flixel.tweens.FlxEase;
import flixel.util.FlxTimer;
import flixel.tweens.FlxTween;
import openfl.display.BlendMode;
2023-08-14 02:49:18 -04:00
class CapsuleText extends FlxSpriteGroup
{
public var blurredText:FlxText;
var whiteText:FlxText;
public var text(default, set):String;
var maskShaderSongName:LeftMaskShader = new LeftMaskShader();
public var clipWidth(default, set):Int = 255;
public var tooLong:Bool = false;
// 255, 27 normal
// 220, 27 favourited
2023-08-14 02:49:18 -04:00
public function new(x:Float, y:Float, songTitle:String, size:Float)
{
super(x, y);
blurredText = initText(songTitle, size);
blurredText.shader = new GaussianBlurShader(1);
whiteText = initText(songTitle, size);
// whiteText.shader = new GaussianBlurShader(0.3);
text = songTitle;
blurredText.color = 0xFF00ccff;
whiteText.color = 0xFFFFFFFF;
add(blurredText);
add(whiteText);
}
function initText(songTitle, size:Float):FlxText
{
var text:FlxText = new FlxText(0, 0, 0, songTitle, Std.int(size));
text.font = "5by7";
return text;
}
// ???? none
// 255, 27 normal
// 220, 27 favourited
function set_clipWidth(value:Int):Int
{
resetText();
if (whiteText.width > value)
{
tooLong = true;
blurredText.clipRect = new FlxRect(0, 0, value, blurredText.height);
whiteText.clipRect = new FlxRect(0, 0, value, whiteText.height);
}
else
{
tooLong = false;
blurredText.clipRect = null;
whiteText.clipRect = null;
}
return clipWidth = value;
}
2023-08-14 02:49:18 -04:00
function set_text(value:String):String
{
if (value == null) return value;
if (blurredText == null || whiteText == null)
{
trace('WARN: Capsule not initialized properly');
return text = value;
}
2023-08-14 02:49:18 -04:00
blurredText.text = value;
whiteText.text = value;
whiteText.textField.filters = [
new openfl.filters.GlowFilter(0x00ccff, 1, 5, 5, 210, BitmapFilterQuality.MEDIUM),
// new openfl.filters.BlurFilter(5, 5, BitmapFilterQuality.LOW)
];
return text = value;
2023-08-14 02:49:18 -04:00
}
var moveTimer:FlxTimer = new FlxTimer();
var moveTween:FlxTween;
public function initMove():Void
{
moveTimer.start(0.6, (timer) -> {
moveTextRight();
});
}
function moveTextRight():Void
{
var distToMove:Float = whiteText.width - clipWidth;
moveTween = FlxTween.tween(whiteText.offset, {x: distToMove}, 2,
{
onUpdate: function(_) {
whiteText.clipRect = new FlxRect(whiteText.offset.x, 0, clipWidth, whiteText.height);
blurredText.offset = whiteText.offset;
blurredText.clipRect = new FlxRect(whiteText.offset.x, 0, clipWidth, blurredText.height);
},
onComplete: function(_) {
moveTimer.start(0.3, (timer) -> {
moveTextLeft();
});
},
ease: FlxEase.sineInOut
});
}
function moveTextLeft():Void
{
moveTween = FlxTween.tween(whiteText.offset, {x: 0}, 2,
{
onUpdate: function(_) {
whiteText.clipRect = new FlxRect(whiteText.offset.x, 0, clipWidth, whiteText.height);
blurredText.offset = whiteText.offset;
blurredText.clipRect = new FlxRect(whiteText.offset.x, 0, clipWidth, blurredText.height);
},
onComplete: function(_) {
moveTimer.start(0.3, (timer) -> {
moveTextRight();
});
},
ease: FlxEase.sineInOut
});
}
public function resetText():Void
{
if (moveTween != null) moveTween.cancel();
if (moveTimer != null) moveTimer.cancel();
whiteText.offset.x = 0;
whiteText.clipRect = new FlxRect(whiteText.offset.x, 0, clipWidth, whiteText.height);
blurredText.clipRect = new FlxRect(whiteText.offset.x, 0, clipWidth, whiteText.height);
}
var flickerState:Bool = false;
var flickerTimer:FlxTimer;
public function flickerText():Void
{
resetText();
flickerTimer = new FlxTimer().start(1 / 24, flickerProgress, 19);
}
function flickerProgress(timer:FlxTimer):Void
{
if (flickerState == true)
{
whiteText.blend = BlendMode.ADD;
blurredText.blend = BlendMode.ADD;
blurredText.color = 0xFFFFFFFF;
whiteText.color = 0xFFFFFFFF;
whiteText.textField.filters = [
new openfl.filters.GlowFilter(0xFFFFFF, 1, 5, 5, 210, BitmapFilterQuality.MEDIUM),
// new openfl.filters.BlurFilter(5, 5, BitmapFilterQuality.LOW)
];
}
else
{
blurredText.color = 0xFF00aadd;
whiteText.color = 0xFFDDDDDD;
whiteText.textField.filters = [
new openfl.filters.GlowFilter(0xDDDDDD, 1, 5, 5, 210, BitmapFilterQuality.MEDIUM),
// new openfl.filters.BlurFilter(5, 5, BitmapFilterQuality.LOW)
];
}
flickerState = !flickerState;
}
override function update(elapsed:Float):Void
{
super.update(elapsed);
}
2023-08-14 02:49:18 -04:00
}