Funkin/source/funkin/ui/mainmenu/MainMenuState.hx

345 lines
8.7 KiB
Haxe
Raw Normal View History

package funkin.ui.mainmenu;
2020-10-31 21:11:14 -04:00
import funkin.graphics.FunkinSprite;
2024-03-16 22:20:22 -04:00
import flixel.addons.transition.FlxTransitionableState;
2023-06-15 00:29:54 -04:00
import funkin.ui.debug.DebugMenuSubState;
2020-10-31 21:11:14 -04:00
import flixel.FlxObject;
import flixel.FlxSprite;
2021-02-15 17:04:08 -05:00
import flixel.FlxState;
2021-02-12 01:20:20 -05:00
import flixel.addons.transition.FlxTransitionableState;
2020-10-31 21:11:14 -04:00
import flixel.effects.FlxFlicker;
import flixel.graphics.frames.FlxAtlasFrames;
import flixel.util.typeLimit.NextState;
2020-10-31 21:11:14 -04:00
import flixel.group.FlxGroup.FlxTypedGroup;
import flixel.input.touch.FlxTouch;
2020-11-06 21:17:27 -05:00
import flixel.text.FlxText;
2024-03-11 23:42:32 -04:00
import funkin.data.song.SongData.SongMusicData;
2020-10-31 21:11:14 -04:00
import flixel.tweens.FlxEase;
2024-02-13 01:38:11 -05:00
import funkin.graphics.FunkinCamera;
2024-03-11 23:42:32 -04:00
import funkin.audio.FunkinSound;
2020-10-31 21:11:14 -04:00
import flixel.tweens.FlxTween;
import funkin.ui.MusicBeatState;
2021-02-15 17:04:08 -05:00
import flixel.util.FlxTimer;
import funkin.ui.AtlasMenuList;
import funkin.ui.freeplay.FreeplayState;
import funkin.ui.MenuList;
2023-08-08 15:41:25 -04:00
import funkin.ui.title.TitleState;
2023-05-17 16:42:58 -04:00
import funkin.ui.story.StoryMenuState;
import funkin.ui.Prompt;
import funkin.util.WindowUtil;
2021-03-30 00:05:21 -04:00
#if discord_rpc
2021-03-23 00:05:46 -04:00
import Discord.DiscordClient;
#end
2021-02-19 23:10:16 -05:00
#if newgrounds
import funkin.ui.NgPrompt;
import io.newgrounds.NG;
2021-02-19 23:10:16 -05:00
#end
2020-10-31 21:11:14 -04:00
class MainMenuState extends MusicBeatState
{
var menuItems:MenuTypedList<AtlasMenuItem>;
var magenta:FlxSprite;
var camFollow:FlxObject;
override function create()
{
#if discord_rpc
// Updating Discord Rich Presence
DiscordClient.changePresence("In the Menus", null);
#end
transIn = FlxTransitionableState.defaultTransIn;
transOut = FlxTransitionableState.defaultTransOut;
playMenuMusic();
persistentUpdate = false;
persistentDraw = true;
var bg:FlxSprite = new FlxSprite(Paths.image('menuBG'));
bg.scrollFactor.x = 0;
bg.scrollFactor.y = 0.17;
bg.setGraphicSize(Std.int(bg.width * 1.2));
bg.updateHitbox();
bg.screenCenter();
add(bg);
camFollow = new FlxObject(0, 0, 1, 1);
add(camFollow);
magenta = new FlxSprite(Paths.image('menuBGMagenta'));
magenta.scrollFactor.x = bg.scrollFactor.x;
magenta.scrollFactor.y = bg.scrollFactor.y;
magenta.setGraphicSize(Std.int(bg.width));
magenta.updateHitbox();
magenta.x = bg.x;
magenta.y = bg.y;
magenta.visible = false;
// TODO: Why doesn't this line compile I'm going fucking feral
if (Preferences.flashingLights) add(magenta);
menuItems = new MenuTypedList<AtlasMenuItem>();
add(menuItems);
menuItems.onChange.add(onMenuItemChange);
2023-03-16 00:55:25 -04:00
menuItems.onAcceptPress.add(function(_) {
if (_.name == 'freeplay')
{
magenta.visible = true;
}
else
{
FlxFlicker.flicker(magenta, 1.1, 0.15, false, true);
}
});
menuItems.enabled = true; // can move on intro
createMenuItem('storymode', 'mainmenu/storymode', function() startExitState(() -> new StoryMenuState()));
2023-03-16 00:55:25 -04:00
createMenuItem('freeplay', 'mainmenu/freeplay', function() {
persistentDraw = true;
persistentUpdate = false;
// Freeplay has its own custom transition
2024-03-16 22:20:22 -04:00
FlxTransitionableState.skipNextTransIn = true;
FlxTransitionableState.skipNextTransOut = true;
openSubState(new FreeplayState());
});
#if CAN_OPEN_LINKS
// In order to prevent popup blockers from triggering,
// we need to open the link as an immediate result of a keypress event,
// so we can't wait for the flicker animation to complete.
var hasPopupBlocker = #if web true #else false #end;
createMenuItem('merch', 'mainmenu/merch', selectMerch, hasPopupBlocker);
#end
2023-03-16 00:55:25 -04:00
createMenuItem('options', 'mainmenu/options', function() {
startExitState(() -> new funkin.ui.options.OptionsState());
});
createMenuItem('credits', 'mainmenu/credits', function() {
2024-03-26 12:33:54 -04:00
startExitState(() -> new funkin.ui.credits.CreditsState());
});
// Reset position of menu items.
var spacing = 160;
var top = (FlxG.height - (spacing * (menuItems.length - 1))) / 2;
for (i in 0...menuItems.length)
{
var menuItem = menuItems.members[i];
menuItem.x = FlxG.width / 2;
menuItem.y = top + spacing * i;
2024-03-26 12:33:54 -04:00
menuItem.scrollFactor.x = 0.0;
// This one affects how much the menu items move when you scroll between them.
menuItem.scrollFactor.y = 0.4;
}
2023-03-16 00:55:25 -04:00
resetCamStuff();
subStateClosed.add(_ -> {
resetCamStuff();
});
subStateOpened.add(sub -> {
if (Type.getClass(sub) == FreeplayState)
{
new FlxTimer().start(0.5, _ -> {
magenta.visible = false;
});
}
});
// FlxG.camera.setScrollBounds(bg.x, bg.x + bg.width, bg.y, bg.y + bg.height * 1.2);
super.create();
// This has to come AFTER!
this.leftWatermarkText.text = Constants.VERSION;
// this.rightWatermarkText.text = "blablabla test";
// NG.core.calls.event.logEvent('swag').send();
}
2024-03-11 23:42:32 -04:00
function playMenuMusic():Void
{
2024-03-23 17:50:48 -04:00
FunkinSound.playMusic('freakyMenu',
{
overrideExisting: true,
restartTrack: false
});
2024-03-11 23:42:32 -04:00
}
2023-03-16 00:55:25 -04:00
function resetCamStuff()
{
FlxG.cameras.reset(new FunkinCamera('mainMenu'));
2023-03-16 00:55:25 -04:00
FlxG.camera.follow(camFollow, null, 0.06);
2024-04-09 15:10:58 -04:00
FlxG.camera.snapToTarget();
2023-03-16 00:55:25 -04:00
}
function createMenuItem(name:String, atlas:String, callback:Void->Void, fireInstantly:Bool = false):Void
{
var item = new AtlasMenuItem(name, Paths.getSparrowAtlas(atlas), callback);
item.fireInstantly = fireInstantly;
item.ID = menuItems.length;
item.scrollFactor.set();
// Set the offset of the item so the sprite is centered on the origin.
item.centered = true;
item.changeAnim('idle');
menuItems.addItem(name, item);
}
override function closeSubState()
{
magenta.visible = false;
super.closeSubState();
}
override function finishTransIn()
{
super.finishTransIn();
// menuItems.enabled = true;
// #if newgrounds
// if (NGio.savedSessionFailed)
// showSavedSessionFailed();
// #end
}
function onMenuItemChange(selected:MenuListItem)
{
camFollow.setPosition(selected.getGraphicMidpoint().x, selected.getGraphicMidpoint().y);
}
#if CAN_OPEN_LINKS
function selectDonate()
{
WindowUtil.openURL(Constants.URL_ITCH);
}
function selectMerch()
{
WindowUtil.openURL(Constants.URL_MERCH);
}
#end
#if newgrounds
function selectLogin()
{
openNgPrompt(NgPrompt.showLogin());
}
function selectLogout()
{
openNgPrompt(NgPrompt.showLogout());
}
function showSavedSessionFailed()
{
openNgPrompt(NgPrompt.showSavedSessionFailed());
}
/**
* Calls openPrompt and redraws the login/logout button
2023-06-08 16:30:45 -04:00
* @param prompt
* @param onClose
*/
public function openNgPrompt(prompt:Prompt, ?onClose:Void->Void)
{
var onPromptClose = checkLoginStatus;
if (onClose != null)
{
2023-03-16 00:55:25 -04:00
onPromptClose = function() {
checkLoginStatus();
onClose();
}
}
openPrompt(prompt, onPromptClose);
}
function checkLoginStatus()
{
var prevLoggedIn = menuItems.has("logout");
if (prevLoggedIn && !NGio.isLoggedIn) menuItems.resetItem("login", "logout", selectLogout);
else if (!prevLoggedIn && NGio.isLoggedIn) menuItems.resetItem("logout", "login", selectLogin);
}
#end
public function openPrompt(prompt:Prompt, onClose:Void->Void)
{
menuItems.enabled = false;
2023-03-16 00:55:25 -04:00
prompt.closeCallback = function() {
menuItems.enabled = true;
if (onClose != null) onClose();
}
openSubState(prompt);
}
function startExitState(state:NextState)
{
menuItems.enabled = false; // disable for exit
var duration = 0.4;
2023-03-16 00:55:25 -04:00
menuItems.forEach(function(item) {
if (menuItems.selectedIndex != item.ID)
{
FlxTween.tween(item, {alpha: 0}, duration, {ease: FlxEase.quadOut});
}
else
{
item.visible = false;
}
});
new FlxTimer().start(duration, function(_) FlxG.switchState(state));
}
override function update(elapsed:Float)
{
super.update(elapsed);
if (FlxG.onMobile)
{
var touch:FlxTouch = FlxG.touches.getFirst();
if (touch != null)
{
for (item in menuItems)
{
if (touch.overlaps(item))
{
if (menuItems.selectedIndex == item.ID && touch.justPressed) menuItems.accept();
else
menuItems.selectItem(item.ID);
}
}
}
}
// Open the debug menu, defaults to ` / ~
if (controls.DEBUG_MENU)
2023-06-15 00:29:54 -04:00
{
FlxG.state.openSubState(new DebugMenuSubState());
}
if (FlxG.sound.music.volume < 0.8)
{
FlxG.sound.music.volume += 0.5 * elapsed;
}
if (_exiting) menuItems.enabled = false;
if (controls.BACK && menuItems.enabled && !menuItems.busy)
{
2024-03-23 17:50:48 -04:00
FunkinSound.playOnce(Paths.sound('cancelMenu'));
FlxG.switchState(() -> new TitleState());
}
}
2020-10-31 21:11:14 -04:00
}