1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2025-12-10 22:17:05 +00:00
Funkin/source/funkin/ui/freeplay/CapsuleOptionsMenu.hx
KarimAkra 944141292d Move part of the code from the old repository
Co-Authored-By: sector-a <82838084+sector-a@users.noreply.github.com>
Co-Authored-By: mcagabe19 <egzozu.be.bas@gmail.com>
Co-Authored-By: Mihai Alexandru <77043862+majigsaw77@users.noreply.github.com>
Co-Authored-By: MoonDroid <81515012+moondroidcoder@users.noreply.github.com>
Co-Authored-By: luckydog7 <59097731+luckydog7@users.noreply.github.com>
2025-04-15 21:32:39 +03:00

190 lines
5.2 KiB
Haxe

package funkin.ui.freeplay;
import funkin.graphics.shaders.PureColor;
import funkin.input.Controls;
import flixel.group.FlxSpriteGroup;
import funkin.graphics.FunkinSprite;
import flixel.util.FlxColor;
import flixel.util.FlxTimer;
import flixel.text.FlxText;
import flixel.text.FlxText.FlxTextAlign;
#if mobile
import funkin.mobile.util.TouchUtil;
#end
@:nullSafety
class CapsuleOptionsMenu extends FlxSpriteGroup
{
var capsuleMenuBG:FunkinSprite;
var parent:FreeplayState;
var queueDestroy:Bool = false;
var instrumentalIds:Array<String> = [''];
var currentInstrumentalIndex:Int = 0;
var currentInstrumental:FlxText;
var busy:Bool = false;
var leftArrow:InstrumentalSelector;
var rightArrow:InstrumentalSelector;
public function new(parent:FreeplayState, x:Float = 0, y:Float = 0, instIds:Array<String>):Void
{
super(x, y);
this.parent = parent;
this.instrumentalIds = instIds;
capsuleMenuBG = FunkinSprite.createSparrow(0, 0, 'freeplay/instBox/instBox');
capsuleMenuBG.animation.addByPrefix('open', 'open0', 24, false);
capsuleMenuBG.animation.addByPrefix('idle', 'idle0', 24, true);
capsuleMenuBG.animation.addByPrefix('open', 'open0', 24, false);
currentInstrumental = new FlxText(0, 36, capsuleMenuBG.width, '');
currentInstrumental.setFormat('VCR OSD Mono', 40, FlxTextAlign.CENTER, true);
final PAD = 4;
leftArrow = new InstrumentalSelector(parent, PAD, 30, false, parent.getControls());
rightArrow = new InstrumentalSelector(parent, capsuleMenuBG.width - leftArrow.width - PAD, 30, true, parent.getControls());
var label:FlxText = new FlxText(0, 5, capsuleMenuBG.width, 'INSTRUMENTAL');
label.setFormat('VCR OSD Mono', 24, FlxTextAlign.CENTER, true);
add(capsuleMenuBG);
add(leftArrow);
add(rightArrow);
add(label);
add(currentInstrumental);
capsuleMenuBG.animation.finishCallback = function(_) {
capsuleMenuBG.animation.play('idle', true);
};
capsuleMenuBG.animation.play('open', true);
}
public override function update(elapsed:Float):Void
{
super.update(elapsed);
if (queueDestroy)
{
destroy();
return;
}
var changedInst = false;
if (!busy)
{
@:privateAccess
if (parent.controls.BACK #if mobile || TouchUtil.overlapsComplex(parent.backButton) && TouchUtil.justPressed #end)
{
close();
return;
}
if (parent.getControls().UI_LEFT_P #if mobile || TouchUtil.overlapsComplex(leftArrow) && TouchUtil.justPressed #end)
{
currentInstrumentalIndex = (currentInstrumentalIndex + 1) % instrumentalIds.length;
changedInst = true;
}
if (parent.getControls().UI_RIGHT_P #if mobile || TouchUtil.overlapsComplex(rightArrow) && TouchUtil.justPressed #end)
{
currentInstrumentalIndex = (currentInstrumentalIndex - 1 + instrumentalIds.length) % instrumentalIds.length;
changedInst = true;
}
if (parent.getControls().ACCEPT #if mobile || ((TouchUtil.overlapsComplex(currentInstrumental) && TouchUtil.justPressed)
&& !(TouchUtil.overlapsComplex(leftArrow) || TouchUtil.overlapsComplex(rightArrow))) #end)
{
busy = true;
onConfirm(instrumentalIds[currentInstrumentalIndex] ?? '');
}
}
if (!changedInst && currentInstrumental.text == '') changedInst = true;
if (changedInst)
{
currentInstrumental.text = instrumentalIds[currentInstrumentalIndex].toTitleCase() ?? '';
if (currentInstrumental.text == '') currentInstrumental.text = 'Default';
}
}
public function close():Void
{
// Play in reverse.
capsuleMenuBG.animation.play('open', true, true);
capsuleMenuBG.animation.finishCallback = function(_) {
parent.cleanupCapsuleOptionsMenu();
queueDestroy = true;
};
}
/**
* Override this with `capsuleOptionsMenu.onConfirm = myFunction;`
*/
public dynamic function onConfirm(targetInstId:String):Void
{
throw 'onConfirm not implemented!';
}
}
/**
* The difficulty selector arrows to the left and right of the difficulty.
*/
class InstrumentalSelector extends FunkinSprite
{
var controls:Controls;
var whiteShader:PureColor;
var parent:FreeplayState;
var baseScale:Float = 0.6;
public function new(parent:FreeplayState, x:Float, y:Float, flipped:Bool, controls:Controls)
{
super(x, y);
this.parent = parent;
this.controls = controls;
frames = Paths.getSparrowAtlas('freeplay/freeplaySelector');
animation.addByPrefix('shine', 'arrow pointer loop', 24);
animation.play('shine');
whiteShader = new PureColor(FlxColor.WHITE);
shader = whiteShader;
flipX = flipped;
scale.x = scale.y = 1 * baseScale;
updateHitbox();
}
override function update(elapsed:Float):Void
{
if (flipX && controls.UI_RIGHT_P) moveShitDown();
if (!flipX && controls.UI_LEFT_P) moveShitDown();
super.update(elapsed);
}
function moveShitDown():Void
{
offset.y -= 5;
whiteShader.colorSet = true;
scale.x = scale.y = 0.5 * baseScale;
new FlxTimer().start(2 / 24, function(tmr) {
scale.x = scale.y = 1 * baseScale;
whiteShader.colorSet = false;
updateHitbox();
});
}
}