Skip to content

Commit

Permalink
add pause menu index/text methods
Browse files Browse the repository at this point in the history
  • Loading branch information
misternebula committed Feb 28, 2024
1 parent 707198e commit c9a01d4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
17 changes: 6 additions & 11 deletions src/OWML.Common/Interfaces/Menus/IPauseMenuManager.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

namespace OWML.Common
namespace OWML.Common
{
public interface IPauseMenuManager
{
public Menu MakePauseListMenu(string title);

public SubmitAction MakeSimpleButton(string name, Menu customMenu = null);
public SubmitAction MakeSimpleButton(string name, int index, bool fromTop, Menu customMenu = null);

public SubmitAction MakeMenuOpenButton(string name, Menu menuToOpen, int index, bool fromTop, Menu customMenu = null);

public GameObject MakeMenuOpenButton(string name, Menu menuToOpen, Menu customMenu = null);
public void SetButtonText(SubmitAction button, string text);

public void SetButtonIndex(SubmitAction button, int index, bool fromTop);
}
}
2 changes: 1 addition & 1 deletion src/OWML.ModHelper.Menus/NewMenuSystem/MenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void SaveConfig()
else
{
// Create button on pause screen that opens the mods menu
var modsButton = PauseMenuManager.MakeSimpleButton("MODS");
var modsButton = PauseMenuManager.MakeSimpleButton("MODS", 2, false);
modsButton.OnSubmitAction += () => OptionsMenuManager.OpenOptionsAtTab(modsMenuButton);
}

Expand Down
48 changes: 37 additions & 11 deletions src/OWML.ModHelper.Menus/NewMenuSystem/PauseMenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,61 @@ public Menu MakePauseListMenu(string title)
return newMenu.GetComponent<Menu>();
}

public SubmitAction MakeSimpleButton(string name, Menu customMenu = null)
public SubmitAction MakeSimpleButton(string name, int index, bool fromTop, Menu customMenu = null)
{
var button = CreateBase(name, customMenu);
var button = CreateBase(name, index, fromTop, customMenu);
button.SetActive(true);
return button.AddComponent<SubmitAction>();

var submitAction = button.AddComponent<SubmitAction>();

SetButtonText(submitAction, name);
SetButtonIndex(submitAction, index, fromTop);

return submitAction;
}

public GameObject MakeMenuOpenButton(string name, Menu menuToOpen, Menu customMenu = null)
public SubmitAction MakeMenuOpenButton(string name, Menu menuToOpen, int index, bool fromTop, Menu customMenu = null)
{
if (LoadManager.GetCurrentScene() != OWScene.SolarSystem && LoadManager.GetCurrentScene() != OWScene.EyeOfTheUniverse)
{
_console.WriteLine("Error - Cannot create pause button in this scene!", OWML.Common.MessageType.Error);
return null;
}

var menuRootObject = CreateBase(name, customMenu);
var menuRootObject = CreateBase(name, index, fromTop, customMenu);

var submitActionMenu = menuRootObject.AddComponent<SubmitActionMenu>();
submitActionMenu._menuToOpen = menuToOpen;

SetButtonText(submitActionMenu, name);
SetButtonIndex(submitActionMenu, index, fromTop);

menuRootObject.SetActive(true);
return menuRootObject;
return submitActionMenu;
}

private GameObject CreateBase(string name, Menu customMenu = null)
public void SetButtonText(SubmitAction button, string text)
{
var textComp = button.GetComponentInChildren<Text>();
textComp.text = text;
textComp.SetAllDirty();
}

public void SetButtonIndex(SubmitAction button, int index, bool fromTop)
{
if (index > button.transform.parent.childCount - 4)
{
throw new IndexOutOfRangeException("Can't set a button to have a higher index than being last!");
}

var indexToSetTo = fromTop
? index + 2
: button.transform.parent.childCount - 2 - index;

button.transform.SetSiblingIndex(indexToSetTo);
}

private GameObject CreateBase(string name, int index, bool fromTop, Menu customMenu = null)
{
if (_pauseMenuItemsTemplate == null)
{
Expand Down Expand Up @@ -138,10 +168,6 @@ private GameObject CreateBase(string name, Menu customMenu = null)
pauseButton.SetActive(false);
pauseButton.name = $"Button-{name}";

// Change text, and set mesh to dirty (maybe not needed?)
pauseButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = name;
pauseButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().SetAllDirty();

if (customMenu.GetSelectOnActivate() == null)
{
customMenu.SetSelectOnActivate(pauseButton.GetComponent<Button>());
Expand Down
8 changes: 4 additions & 4 deletions src/SampleMods/OWML.LoadCustomAssets/LoadCustomAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public override void SetupPauseMenu()
var pauseMenuManager = ModHelper.MenuHelper.PauseMenuManager;

var listMenu = pauseMenuManager.MakePauseListMenu("TEST");
var button = pauseMenuManager.MakeMenuOpenButton("TEST", listMenu);
var button = pauseMenuManager.MakeMenuOpenButton("TEST", listMenu, 1, true);

var button1 = pauseMenuManager.MakeSimpleButton("1", listMenu);
var button2 = pauseMenuManager.MakeSimpleButton("2", listMenu);
var button3 = pauseMenuManager.MakeSimpleButton("3", listMenu);
var button1 = pauseMenuManager.MakeSimpleButton("1", 0, true, listMenu);
var button2 = pauseMenuManager.MakeSimpleButton("2", 1, true, listMenu);
var button3 = pauseMenuManager.MakeSimpleButton("3", 2, true, listMenu);
}

public override void SetupOptionsMenu()
Expand Down

0 comments on commit c9a01d4

Please sign in to comment.