This script ensures that a Journal menu element always shows a page index with an odd number (1, 3, 5, etc) - so that another page next to it can always show even numbers.
It also prevents shift left/right buttons showing if the two pages cannot be shifted further.
To use it:
- Copy the code below into a C# script named JournalSnapPage.cs
- Attach the Journal Snap Page component to a GameObject in the scene
- Configure the component's Inspector fields, setting the name of the Menu, the Journal page (left-hand-side), and the button that shifts the pages right (all are assumed to be in the same Menu)
JournalSnapPage.cs:
using UnityEngine;
using AC;
public class JournalSnapPage : MonoBehaviour
{
public string journalMenuName = "Journal";
public string journalElementName = "PageLeft";
public string shiftRightButtonName = "ShiftRight";
private void OnEnable ()
{
EventManager.OnMenuElementShift += OnMenuElementShift;
EventManager.OnMenuTurnOn += OnMenuTurnOn;
}
private void OnDisable ()
{
EventManager.OnMenuElementShift -= OnMenuElementShift;
EventManager.OnMenuTurnOn -= OnMenuTurnOn;
}
private void OnMenuElementShift (MenuElement _element, AC_ShiftInventory shiftType)
{
if (PlayerMenus.GetMenuWithName (journalMenuName).elements.Contains (_element))
{
MenuJournal journal = PlayerMenus.GetElementWithName (journalMenuName, journalElementName) as MenuJournal;
bool isEven = (journal.showPage%2 == 0);
if (isEven)
{
int newShowPage = journal.showPage + ((shiftType == AC_ShiftInventory.ShiftNext) ? 1 : -1);
if (newShowPage < 1)
{
newShowPage = 1;
}
if (newShowPage >= journal.pages.Count - 1)
{
newShowPage = journal.pages.Count - 1;
}
journal.showPage = newShowPage;
}
UpdateShiftRightButton ();
}
}
private void OnMenuTurnOn (Menu _menu, bool isInstant)
{
if (_menu.title == journalMenuName)
{
UpdateShiftRightButton ();
}
}
private void UpdateShiftRightButton ()
{
MenuJournal journal = PlayerMenus.GetElementWithName (journalMenuName, journalElementName) as MenuJournal;
MenuButton shiftRightButton = PlayerMenus.GetElementWithName (journalMenuName, shiftRightButtonName) as MenuButton;
int pageDiff = journal.pages.Count - journal.showPage;
shiftRightButton.IsVisible = (pageDiff > 1);
}
}