Funkin/source/ui/OptionsState.hx

281 lines
5.7 KiB
Haxe
Raw Normal View History

2021-03-13 21:11:56 -05:00
package ui;
import flixel.FlxSprite;
2021-03-24 16:44:58 -04:00
import flixel.FlxSubState;
import flixel.addons.transition.FlxTransitionableState;
2021-03-13 21:11:56 -05:00
import flixel.group.FlxGroup;
import flixel.util.FlxSignal;
import i18n.FireTongueHandler.t;
2021-03-13 21:11:56 -05:00
// typedef OptionsState = OptionsMenu_old;
// class OptionsState_new extends MusicBeatState
class OptionsState extends MusicBeatState
{
var pages = new Map<PageName, Page>();
2021-03-26 20:09:04 -04:00
var currentName:PageName = Options;
2021-03-13 21:11:56 -05:00
var currentPage(get, never):Page;
2021-03-24 16:44:58 -04:00
inline function get_currentPage()
return pages[currentName];
2021-03-13 21:11:56 -05:00
override function create()
{
var menuBG = new FlxSprite().loadGraphic(Paths.image('menuDesat'));
menuBG.color = 0xFFea71fd;
menuBG.setGraphicSize(Std.int(menuBG.width * 1.1));
menuBG.updateHitbox();
menuBG.screenCenter();
2021-03-16 10:56:08 -04:00
menuBG.scrollFactor.set(0, 0);
2021-03-13 21:11:56 -05:00
add(menuBG);
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
var options = addPage(Options, new OptionsMenu(false));
2021-03-26 20:09:04 -04:00
var preferences = addPage(Preferences, new PreferencesMenu());
2021-03-13 21:11:56 -05:00
var controls = addPage(Controls, new ControlsMenu());
2021-04-18 05:25:44 -04:00
// var colors = addPage(Colors, new ColorsMenu());
2021-03-24 16:44:58 -04:00
2021-03-24 19:19:08 -04:00
var mods = addPage(Mods, new ModMenu());
2021-03-13 21:11:56 -05:00
if (options.hasMultipleOptions())
{
options.onExit.add(exitToMainMenu);
controls.onExit.add(switchPage.bind(Options));
2021-04-18 05:25:44 -04:00
// colors.onExit.add(switchPage.bind(Options));
2021-03-26 20:09:04 -04:00
preferences.onExit.add(switchPage.bind(Options));
2021-03-24 19:19:08 -04:00
mods.onExit.add(switchPage.bind(Options));
2021-03-13 21:11:56 -05:00
}
else
{
// No need to show Options page
controls.onExit.add(exitToMainMenu);
setPage(Controls);
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
// disable for intro transition
currentPage.enabled = false;
super.create();
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function addPage<T:Page>(name:PageName, page:T)
{
page.onSwitch.add(switchPage);
pages[name] = page;
add(page);
page.exists = currentName == name;
return page;
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function setPage(name:PageName)
{
if (pages.exists(currentName))
currentPage.exists = false;
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
currentName = name;
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
if (pages.exists(currentName))
currentPage.exists = true;
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
override function finishTransIn()
{
super.finishTransIn();
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
currentPage.enabled = true;
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function switchPage(name:PageName)
{
2021-03-24 16:44:58 -04:00
// Todo animate?
2021-03-13 21:11:56 -05:00
setPage(name);
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function exitToMainMenu()
{
currentPage.enabled = false;
2021-03-24 16:44:58 -04:00
// Todo animate?
2021-03-13 21:11:56 -05:00
FlxG.switchState(new MainMenuState());
}
}
class Page extends FlxGroup
{
public var onSwitch(default, null) = new FlxTypedSignal<PageName->Void>();
public var onExit(default, null) = new FlxSignal();
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
public var enabled(default, set) = true;
2021-03-16 10:56:08 -04:00
public var canExit = true;
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
var controls(get, never):Controls;
2021-03-24 16:44:58 -04:00
inline function get_controls()
return PlayerSettings.player1.controls;
2021-03-13 21:11:56 -05:00
var subState:FlxSubState;
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
inline function switchPage(name:PageName)
{
onSwitch.dispatch(name);
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
inline function exit()
{
onExit.dispatch();
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
override function update(elapsed:Float)
{
super.update(elapsed);
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
if (enabled)
updateEnabled(elapsed);
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function updateEnabled(elapsed:Float)
{
2021-03-16 10:56:08 -04:00
if (canExit && controls.BACK)
2021-04-15 10:19:02 -04:00
{
FlxG.sound.play(Paths.sound('cancelMenu'));
2021-03-13 21:11:56 -05:00
exit();
2021-04-15 10:19:02 -04:00
}
2021-03-13 21:11:56 -05:00
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function set_enabled(value:Bool)
{
return this.enabled = value;
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function openPrompt(prompt:Prompt, onClose:Void->Void)
{
enabled = false;
2021-03-24 16:44:58 -04:00
prompt.closeCallback = function()
2021-03-13 21:11:56 -05:00
{
enabled = true;
if (onClose != null)
onClose();
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
FlxG.state.openSubState(prompt);
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
override function destroy()
{
super.destroy();
onSwitch.removeAll();
}
}
class OptionsMenu extends Page
{
var items:TextMenuList;
2021-03-24 16:44:58 -04:00
public function new(showDonate:Bool)
2021-03-13 21:11:56 -05:00
{
super();
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
add(items = new TextMenuList());
createItem(t("PREFERENCES"), function() switchPage(Preferences));
createItem(t("CONTROLS"), function() switchPage(Controls));
// createItem(t("COLORS"), function() switchPage(Colors));
createItem(t("MODS"), function() switchPage(Mods));
2021-03-24 18:41:54 -04:00
2021-03-13 21:11:56 -05:00
#if CAN_OPEN_LINKS
if (showDonate)
{
var hasPopupBlocker = #if web true #else false #end;
createItem(t("DONATE"), selectDonate, hasPopupBlocker);
2021-03-13 21:11:56 -05:00
}
#end
#if newgrounds
if (NGio.isLoggedIn)
createItem(t("LOGOUT"), selectLogout);
2021-03-13 21:11:56 -05:00
else
createItem(t("LOGIN"), selectLogin);
2021-03-13 21:11:56 -05:00
#end
createItem(t("EXIT"), exit);
2021-03-13 21:11:56 -05:00
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function createItem(name:String, callback:Void->Void, fireInstantly = false)
{
var item = items.createItem(0, 100 + items.length * 100, name, Bold, callback);
item.fireInstantly = fireInstantly;
item.screenCenter(X);
return item;
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
override function set_enabled(value:Bool)
{
items.enabled = value;
return super.set_enabled(value);
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
/**
2021-04-23 13:15:40 -04:00
* True if this page has multiple options, excluding the exit option.
2021-03-13 21:11:56 -05:00
* If false, there's no reason to ever show this page.
*/
public function hasMultipleOptions():Bool
{
return items.length > 2;
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
#if CAN_OPEN_LINKS
function selectDonate()
{
#if linux
Sys.command('/usr/bin/xdg-open', ["https://ninja-muffin24.itch.io/funkin", "&"]);
#else
FlxG.openURL('https://ninja-muffin24.itch.io/funkin');
#end
}
#end
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
#if newgrounds
function selectLogin()
{
openNgPrompt(NgPrompt.showLogin());
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function selectLogout()
{
openNgPrompt(NgPrompt.showLogout());
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
/**
* Calls openPrompt and redraws the login/logout button
* @param prompt
* @param onClose
*/
public function openNgPrompt(prompt:Prompt, ?onClose:Void->Void)
{
var onPromptClose = checkLoginStatus;
if (onClose != null)
{
2021-03-24 16:44:58 -04:00
onPromptClose = function()
2021-03-13 21:11:56 -05:00
{
checkLoginStatus();
onClose();
}
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
openPrompt(prompt, onPromptClose);
}
2021-03-24 16:44:58 -04:00
2021-03-13 21:11:56 -05:00
function checkLoginStatus()
{
2021-04-23 13:15:40 -04:00
// this shit don't work!! wtf!!!!
2021-03-13 21:11:56 -05:00
var prevLoggedIn = items.has("logout");
if (prevLoggedIn && !NGio.isLoggedIn)
items.resetItem("logout", "login", selectLogin);
2021-04-23 13:15:40 -04:00
else if (!prevLoggedIn && NGio.isLoggedIn)
items.resetItem("login", "logout", selectLogout);
2021-03-13 21:11:56 -05:00
}
#end
}
enum PageName
{
Options;
Controls;
2021-03-24 16:44:58 -04:00
Colors;
2021-03-24 18:41:54 -04:00
Mods;
2021-03-26 20:09:04 -04:00
Preferences;
2021-03-13 21:11:56 -05:00
}