Funkin/source/funkin/ui/OptionsState.hx

261 lines
5 KiB
Haxe
Raw Normal View History

package funkin.ui;
2021-03-13 21:11:56 -05:00
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;
2022-04-18 19:36:09 -04:00
import funkin.util.Constants;
import funkin.util.WindowUtil;
2021-03-13 21:11:56 -05:00
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
var options = addPage(Options, new OptionsMenu());
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-03-24 19:19:08 -04:00
2021-03-13 21:11:56 -05:00
if (options.hasMultipleOptions())
{
options.onExit.add(exitToMainMenu);
controls.onExit.add(switchPage.bind(Options));
2021-03-26 20:09:04 -04:00
preferences.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))
2022-04-18 19:36:09 -04:00
{
2021-03-13 21:11:56 -05:00
currentPage.exists = false;
2022-04-18 19:36:09 -04:00
currentPage.visible = 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))
2022-04-18 19:36:09 -04:00
{
2021-03-13 21:11:56 -05:00
currentPage.exists = true;
2022-04-18 19:36:09 -04:00
currentPage.visible = true;
}
2021-03-13 21:11:56 -05:00
}
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;
2022-04-18 19:36:09 -04:00
// TODO: Animate this transition?
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()
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());
2022-04-18 19:36:09 -04:00
createItem("PREFERENCES", function() switchPage(Preferences));
createItem("CONTROLS", function() switchPage(Controls));
2021-03-24 18:41:54 -04:00
2021-03-13 21:11:56 -05:00
#if newgrounds
if (NGio.isLoggedIn)
2022-04-18 19:36:09 -04:00
createItem("LOGOUT", selectLogout);
2021-03-13 21:11:56 -05:00
else
2022-04-18 19:36:09 -04:00
createItem("LOGIN", selectLogin);
2021-03-13 21:11:56 -05:00
#end
2022-04-18 19:36:09 -04:00
createItem("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)
{
2022-04-18 19:36:09 -04:00
var item = items.createItem(0, 100 + items.length * 100, name, BOLD, callback);
2021-03-13 21:11:56 -05:00
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 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
}