mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-31 06:32:55 +00:00
add option to use new save on Host button
This commit is contained in:
parent
626a499a3b
commit
65b8e3bde1
@ -5,6 +5,7 @@ using QSB.Player.TransformSync;
|
||||
using QSB.SaveSync.Messages;
|
||||
using QSB.Utility;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
@ -30,6 +31,7 @@ internal class MenuManager : MonoBehaviour, IAddComponentOnStart
|
||||
private Button HostButton;
|
||||
private GameObject ConnectButton;
|
||||
private PopupInputMenu ConnectPopup;
|
||||
private ThreeChoicePopupMenu HostGameTypePopup;
|
||||
private Text _loadingText;
|
||||
private StringBuilder _nowLoadingSB;
|
||||
private const int _titleButtonIndex = 2;
|
||||
@ -46,9 +48,16 @@ internal class MenuManager : MonoBehaviour, IAddComponentOnStart
|
||||
|
||||
private bool _intentionalDisconnect;
|
||||
|
||||
private GameObject _threeChoicePopupBase;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Instance = this;
|
||||
|
||||
_threeChoicePopupBase = Instantiate(Resources.FindObjectsOfTypeAll<PopupMenu>().First(x => x.name == "TwoButton-Popup" && x.transform.parent.name == "PopupCanvas" && x.transform.parent.parent.name == "TitleMenu").gameObject);
|
||||
DontDestroyOnLoad(_threeChoicePopupBase);
|
||||
_threeChoicePopupBase.SetActive(false);
|
||||
|
||||
MakeTitleMenus();
|
||||
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
|
||||
QSBNetworkManager.singleton.OnClientConnected += OnConnected;
|
||||
@ -106,6 +115,60 @@ internal class MenuManager : MonoBehaviour, IAddComponentOnStart
|
||||
}
|
||||
}
|
||||
|
||||
public ThreeChoicePopupMenu CreateThreeChoicePopup(string message, string confirm1Text, string confirm2Text, string cancelText)
|
||||
{
|
||||
var newPopup = Instantiate(_threeChoicePopupBase);
|
||||
|
||||
switch (LoadManager.GetCurrentScene())
|
||||
{
|
||||
case OWScene.TitleScreen:
|
||||
newPopup.transform.parent = GameObject.Find("/TitleMenu/PopupCanvas").transform;
|
||||
break;
|
||||
case OWScene.SolarSystem:
|
||||
case OWScene.EyeOfTheUniverse:
|
||||
newPopup.transform.parent = GameObject.Find("/PauseMenu/PopupCanvas").transform;
|
||||
break;
|
||||
}
|
||||
|
||||
newPopup.transform.localPosition = Vector3.zero;
|
||||
newPopup.transform.localScale = Vector3.one;
|
||||
newPopup.GetComponentsInChildren<LocalizedText>().ToList().ForEach(x => Destroy(x));
|
||||
|
||||
var originalPopup = newPopup.GetComponent<PopupMenu>();
|
||||
|
||||
var ok1Button = originalPopup._confirmButton.gameObject;
|
||||
|
||||
var ok2Button = Instantiate(ok1Button, ok1Button.transform.parent);
|
||||
ok2Button.transform.SetSiblingIndex(1);
|
||||
|
||||
var popup = newPopup.AddComponent<ThreeChoicePopupMenu>();
|
||||
popup._labelText = originalPopup._labelText;
|
||||
popup._cancelAction = originalPopup._cancelAction;
|
||||
popup._ok1Action = originalPopup._okAction;
|
||||
popup._ok2Action = ok2Button.GetComponent<SubmitAction>();
|
||||
popup._cancelButton = originalPopup._cancelButton;
|
||||
popup._confirmButton1 = originalPopup._confirmButton;
|
||||
popup._confirmButton2 = ok2Button.GetComponent<ButtonWithHotkeyImageElement>();
|
||||
popup._rootCanvas = originalPopup._rootCanvas;
|
||||
popup._menuActivationRoot = originalPopup._menuActivationRoot;
|
||||
popup._startEnabled = originalPopup._startEnabled;
|
||||
popup._selectOnActivate = originalPopup._selectOnActivate;
|
||||
popup._selectableItemsRoot = originalPopup._selectableItemsRoot;
|
||||
popup._subMenus = originalPopup._subMenus;
|
||||
popup._menuOptions = originalPopup._menuOptions;
|
||||
popup.SetUpPopup(
|
||||
message,
|
||||
InputLibrary.menuConfirm,
|
||||
InputLibrary.confirm2,
|
||||
InputLibrary.cancel,
|
||||
new ScreenPrompt(confirm1Text),
|
||||
new ScreenPrompt(confirm2Text),
|
||||
new ScreenPrompt(cancelText),
|
||||
true,
|
||||
true);
|
||||
return popup;
|
||||
}
|
||||
|
||||
public void LoadGame(bool inEye)
|
||||
{
|
||||
var sceneToLoad = inEye ? OWScene.EyeOfTheUniverse : OWScene.SolarSystem;
|
||||
@ -187,6 +250,10 @@ internal class MenuManager : MonoBehaviour, IAddComponentOnStart
|
||||
TwoButtonInfoPopup = QSBCore.MenuApi.MakeTwoChoicePopup("", "", "");
|
||||
TwoButtonInfoPopup.OnPopupConfirm += () => OnCloseInfoPopup(true);
|
||||
TwoButtonInfoPopup.OnPopupCancel += () => OnCloseInfoPopup(false);
|
||||
|
||||
HostGameTypePopup = CreateThreeChoicePopup("Do you want to host an existing expedition, or host a new expedition?", "EXISTING SAVE", "NEW SAVE", "CANCEL");
|
||||
HostGameTypePopup.OnPopupConfirm1 += () => Host(false);
|
||||
HostGameTypePopup.OnPopupConfirm2 += () => Host(true);
|
||||
}
|
||||
|
||||
private static void SetButtonActive(Button button, bool active)
|
||||
@ -242,7 +309,7 @@ internal class MenuManager : MonoBehaviour, IAddComponentOnStart
|
||||
CreateCommonPopups();
|
||||
|
||||
HostButton = QSBCore.MenuApi.TitleScreen_MakeSimpleButton(HostString, _titleButtonIndex);
|
||||
HostButton.onClick.AddListener(Host);
|
||||
HostButton.onClick.AddListener(PreHost);
|
||||
|
||||
ConnectButton = QSBCore.MenuApi.TitleScreen_MakeMenuOpenButton(ConnectString, _titleButtonIndex + 1, ConnectPopup);
|
||||
|
||||
@ -288,8 +355,26 @@ internal class MenuManager : MonoBehaviour, IAddComponentOnStart
|
||||
LoadManager.LoadScene(OWScene.TitleScreen, LoadManager.FadeType.ToBlack, 2f);
|
||||
}
|
||||
|
||||
private void Host()
|
||||
private void PreHost()
|
||||
{
|
||||
var doesSaveExist = StandaloneProfileManager.SharedInstance.currentProfileGameSave.loopCount > 1;
|
||||
|
||||
if (!doesSaveExist)
|
||||
{
|
||||
Host(true);
|
||||
return;
|
||||
}
|
||||
|
||||
HostGameTypePopup.EnableMenu(true);
|
||||
}
|
||||
|
||||
private void Host(bool newSave)
|
||||
{
|
||||
if (newSave)
|
||||
{
|
||||
PlayerData.ResetGame();
|
||||
}
|
||||
|
||||
_intentionalDisconnect = false;
|
||||
|
||||
SetButtonActive(ConnectButton, false);
|
||||
|
338
QSB/Menus/ThreeChoicePopupMenu.cs
Normal file
338
QSB/Menus/ThreeChoicePopupMenu.cs
Normal file
@ -0,0 +1,338 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace QSB.Menus;
|
||||
|
||||
[RequireComponent(typeof(Canvas))]
|
||||
public class ThreeChoicePopupMenu : Menu
|
||||
{
|
||||
public Text _labelText;
|
||||
public SubmitAction _cancelAction;
|
||||
public SubmitAction _ok1Action;
|
||||
public SubmitAction _ok2Action;
|
||||
public ButtonWithHotkeyImageElement _cancelButton;
|
||||
public ButtonWithHotkeyImageElement _confirmButton1;
|
||||
public ButtonWithHotkeyImageElement _confirmButton2;
|
||||
public Canvas _rootCanvas;
|
||||
|
||||
protected Canvas _popupCanvas;
|
||||
protected GameObject _blocker;
|
||||
protected bool _closeMenuOnOk = true;
|
||||
protected IInputCommands _ok1Command;
|
||||
protected IInputCommands _ok2Command;
|
||||
protected IInputCommands _cancelCommand;
|
||||
protected bool _usingGamepad;
|
||||
|
||||
public event PopupConfirmEvent OnPopupConfirm1;
|
||||
public event PopupConfirmEvent OnPopupConfirm2;
|
||||
public event PopupValidateEvent OnPopupValidate;
|
||||
public event PopupCancelEvent OnPopupCancel;
|
||||
|
||||
public override Selectable GetSelectOnActivate()
|
||||
{
|
||||
_usingGamepad = OWInput.UsingGamepad();
|
||||
return _usingGamepad ? null : _selectOnActivate;
|
||||
}
|
||||
|
||||
public virtual void SetUpPopup(
|
||||
string message,
|
||||
IInputCommands ok1Command,
|
||||
IInputCommands ok2Command,
|
||||
IInputCommands cancelCommand,
|
||||
ScreenPrompt ok1Prompt,
|
||||
ScreenPrompt ok2Prompt,
|
||||
ScreenPrompt cancelPrompt,
|
||||
bool closeMenuOnOk = true,
|
||||
bool setCancelButtonActive = true)
|
||||
{
|
||||
_labelText.text = message;
|
||||
SetUpPopupCommands(ok1Command, ok2Command, cancelCommand, ok1Prompt, ok2Prompt, cancelPrompt);
|
||||
if (!(_cancelAction != null))
|
||||
{
|
||||
Debug.LogWarning("PopupMenu.SetUpPopup Cancel button not set!");
|
||||
return;
|
||||
}
|
||||
|
||||
_cancelAction.gameObject.SetActive(setCancelButtonActive);
|
||||
if (setCancelButtonActive)
|
||||
{
|
||||
_selectOnActivate = _cancelAction.GetRequiredComponent<Selectable>();
|
||||
return;
|
||||
}
|
||||
|
||||
_selectOnActivate = _ok1Action.GetRequiredComponent<Selectable>();
|
||||
}
|
||||
|
||||
public virtual void SetUpPopupCommands(
|
||||
IInputCommands ok1Command,
|
||||
IInputCommands ok2Command,
|
||||
IInputCommands cancelCommand,
|
||||
ScreenPrompt ok1Prompt,
|
||||
ScreenPrompt ok2Prompt,
|
||||
ScreenPrompt cancelPrompt)
|
||||
{
|
||||
_ok1Command = ok1Command;
|
||||
_ok2Command = ok2Command;
|
||||
_cancelCommand = cancelCommand;
|
||||
_confirmButton1.SetPrompt(ok1Prompt, InputMode.Menu);
|
||||
_confirmButton2.SetPrompt(ok2Prompt, InputMode.Menu);
|
||||
_cancelButton.SetPrompt(cancelPrompt, InputMode.Menu);
|
||||
}
|
||||
|
||||
public virtual void ResetPopup()
|
||||
{
|
||||
_labelText.text = "";
|
||||
_ok1Command = null;
|
||||
_ok2Command = null;
|
||||
_cancelCommand = null;
|
||||
_cancelButton.SetPrompt(null, InputMode.Menu);
|
||||
_confirmButton1.SetPrompt(null, InputMode.Menu);
|
||||
_confirmButton2.SetPrompt(null, InputMode.Menu);
|
||||
_selectOnActivate = null;
|
||||
}
|
||||
|
||||
public virtual void CloseMenuOnOk(bool value)
|
||||
{
|
||||
_closeMenuOnOk = value;
|
||||
}
|
||||
|
||||
public virtual bool EventsHaveListeners()
|
||||
{
|
||||
return OnPopupCancel != null || OnPopupConfirm1 != null || OnPopupConfirm2 != null;
|
||||
}
|
||||
|
||||
public override void InitializeMenu()
|
||||
{
|
||||
base.InitializeMenu();
|
||||
if (_cancelAction != null)
|
||||
{
|
||||
_cancelAction.OnSubmitAction += InvokeCancel;
|
||||
}
|
||||
|
||||
_ok1Action.OnSubmitAction += InvokeOk1;
|
||||
_ok2Action.OnSubmitAction += InvokeOk2;
|
||||
_popupCanvas = gameObject.GetAddComponent<Canvas>();
|
||||
_popupCanvas.overrideSorting = true;
|
||||
_popupCanvas.sortingOrder = 30000;
|
||||
gameObject.GetAddComponent<GraphicRaycaster>();
|
||||
gameObject.GetAddComponent<CanvasGroup>();
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
if (_cancelCommand != null && OWInput.IsNewlyPressed(_cancelCommand, InputMode.All))
|
||||
{
|
||||
InvokeCancel();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_ok1Command != null && OWInput.IsNewlyPressed(_ok1Command, InputMode.All))
|
||||
{
|
||||
InvokeOk1();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_ok2Command != null && OWInput.IsNewlyPressed(_ok2Command, InputMode.All))
|
||||
{
|
||||
InvokeOk2();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_usingGamepad != OWInput.UsingGamepad())
|
||||
{
|
||||
_usingGamepad = OWInput.UsingGamepad();
|
||||
if (_usingGamepad)
|
||||
{
|
||||
Locator.GetMenuInputModule().SelectOnNextUpdate(null);
|
||||
return;
|
||||
}
|
||||
|
||||
Locator.GetMenuInputModule().SelectOnNextUpdate(_selectOnActivate);
|
||||
}
|
||||
}
|
||||
|
||||
public override void EnableMenu(bool value)
|
||||
{
|
||||
if (value == _enabledMenu)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_enabledMenu = value;
|
||||
if (_enabledMenu && !_initialized)
|
||||
{
|
||||
InitializeMenu();
|
||||
}
|
||||
|
||||
if (!_addToMenuStackManager)
|
||||
{
|
||||
if (_enabledMenu)
|
||||
{
|
||||
Activate();
|
||||
if (_selectOnActivate != null)
|
||||
{
|
||||
var component = _selectOnActivate.GetComponent<SelectableAudioPlayer>();
|
||||
if (component != null)
|
||||
{
|
||||
component.SilenceNextSelectEvent();
|
||||
}
|
||||
|
||||
Locator.GetMenuInputModule().SelectOnNextUpdate(_selectOnActivate);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Deactivate(false);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (_enabledMenu)
|
||||
{
|
||||
MenuStackManager.SharedInstance.Push(this, true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (MenuStackManager.SharedInstance.Peek() == this)
|
||||
{
|
||||
MenuStackManager.SharedInstance.Pop(false);
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.LogError("Cannot disable Menu unless it is on the top the MenuLayerManager stack. Current menu on top: " + MenuStackManager.SharedInstance.Peek().gameObject.name);
|
||||
}
|
||||
|
||||
public override void Activate()
|
||||
{
|
||||
base.Activate();
|
||||
if (_rootCanvas != null)
|
||||
{
|
||||
_blocker = CreateBlocker(_rootCanvas);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Deactivate(bool keepPreviousMenuVisible = false)
|
||||
{
|
||||
if (_rootCanvas != null)
|
||||
{
|
||||
DestroyBlocker(_blocker);
|
||||
}
|
||||
|
||||
var component = _cancelAction.GetComponent<UIStyleApplier>();
|
||||
if (component != null)
|
||||
{
|
||||
component.ChangeState(UIElementState.NORMAL, true);
|
||||
}
|
||||
|
||||
component = _ok1Action.GetComponent<UIStyleApplier>();
|
||||
if (component != null)
|
||||
{
|
||||
component.ChangeState(UIElementState.NORMAL, true);
|
||||
}
|
||||
|
||||
component = _ok2Action.GetComponent<UIStyleApplier>();
|
||||
if (component != null)
|
||||
{
|
||||
component.ChangeState(UIElementState.NORMAL, true);
|
||||
}
|
||||
|
||||
base.Deactivate(keepPreviousMenuVisible);
|
||||
}
|
||||
|
||||
public override void OnCancelEvent(GameObject selectedObj, BaseEventData eventData)
|
||||
{
|
||||
base.OnCancelEvent(selectedObj, eventData);
|
||||
OnPopupCancel?.Invoke();
|
||||
}
|
||||
|
||||
protected virtual void InvokeCancel()
|
||||
{
|
||||
EnableMenu(false);
|
||||
OnPopupCancel?.Invoke();
|
||||
}
|
||||
|
||||
protected virtual bool Validate()
|
||||
{
|
||||
var flag = true;
|
||||
if (OnPopupValidate != null)
|
||||
{
|
||||
var invocationList = OnPopupValidate.GetInvocationList();
|
||||
for (var i = 0; i < invocationList.Length; i++)
|
||||
{
|
||||
var flag2 = (bool)invocationList[i].DynamicInvoke(Array.Empty<object>());
|
||||
flag = flag && flag2;
|
||||
}
|
||||
}
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
protected virtual void InvokeOk1()
|
||||
{
|
||||
if (!Validate())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_closeMenuOnOk)
|
||||
{
|
||||
EnableMenu(false);
|
||||
}
|
||||
|
||||
OnPopupConfirm1?.Invoke();
|
||||
}
|
||||
|
||||
protected virtual void InvokeOk2()
|
||||
{
|
||||
if (!Validate())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_closeMenuOnOk)
|
||||
{
|
||||
EnableMenu(false);
|
||||
}
|
||||
|
||||
OnPopupConfirm2?.Invoke();
|
||||
}
|
||||
|
||||
protected virtual GameObject CreateBlocker(Canvas rootCanvas)
|
||||
{
|
||||
var gameObject = new GameObject("Blocker");
|
||||
var rectTransform = gameObject.AddComponent<RectTransform>();
|
||||
rectTransform.SetParent(rootCanvas.transform, false);
|
||||
rectTransform.anchorMin = Vector3.zero;
|
||||
rectTransform.anchorMax = Vector3.one;
|
||||
rectTransform.sizeDelta = Vector2.zero;
|
||||
var canvas = gameObject.AddComponent<Canvas>();
|
||||
canvas.overrideSorting = true;
|
||||
canvas.sortingLayerID = _popupCanvas.sortingLayerID;
|
||||
canvas.sortingOrder = _popupCanvas.sortingOrder - 1;
|
||||
gameObject.AddComponent<GraphicRaycaster>();
|
||||
var image = gameObject.AddComponent<Image>();
|
||||
if (Locator.GetUIStyleManager() != null)
|
||||
{
|
||||
image.color = Locator.GetUIStyleManager().GetPopupBlockerColor();
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
image.color = Color.clear;
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
protected virtual void DestroyBlocker(GameObject blocker)
|
||||
{
|
||||
Destroy(blocker);
|
||||
}
|
||||
|
||||
public delegate void PopupConfirmEvent();
|
||||
|
||||
public delegate bool PopupValidateEvent();
|
||||
|
||||
public delegate void PopupCancelEvent();
|
||||
}
|
@ -33,7 +33,6 @@ internal class GameStateMessage : QSBMessage
|
||||
ReducedFrights = PlayerData.GetReducedFrights();
|
||||
}
|
||||
|
||||
|
||||
public override void Serialize(NetworkWriter writer)
|
||||
{
|
||||
base.Serialize(writer);
|
||||
|
Loading…
x
Reference in New Issue
Block a user