Skip to content

Commit

Permalink
Merge pull request #593 from throckmorpheus/menu-fixes
Browse files Browse the repository at this point in the history
Resolves #589 (the top-level mods menu not responding to controller input), in addition to various little visual bugs with the menus.

Co-authored-by: Throckmorpheus <[email protected]>
  • Loading branch information
MegaPiggy and throckmorpheus authored Sep 26, 2024
2 parents 8796aab + 42b3093 commit 7555c05
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- name: pack
run: dotnet pack -o . src\OWML.ModHelper\OWML.ModHelper.csproj -p:NuspecFile=OWML.ModHelper.nuspec -p:NuspecProperties="version=${{ steps.version.outputs.prop }};"
- name: upload nuget
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: owml-nuget
path: OWML.*.nupkg
Expand All @@ -67,7 +67,7 @@ jobs:
mkdir Mods
7z a OWML.zip *
- name: upload zip
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: owml-zip
path: src\OWML.Launcher\bin\Debug\net48\OWML.zip
Expand Down
2 changes: 1 addition & 1 deletion src/OWML.Abstractions/GameObjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public TBehaviour CreateAndAdd<TBehaviour>(string name = null) =>
CreateAndAdd<TBehaviour>(typeof(TBehaviour), name);

public TBehaviour CreateAndAdd<TBehaviour>(Type type, string name = null) =>
(TBehaviour)(object)new GameObject(name).AddComponent(type);
(TBehaviour)(object)new GameObject(name ?? type.Name).AddComponent(type);
}
}
4 changes: 3 additions & 1 deletion src/OWML.Common/Interfaces/Menus/IOWMLPopupInputMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace OWML.Common.Interfaces.Menus
{
public interface IOWMLPopupInputMenu
{
public event PopupInputMenu.InputPopupValidateCharEvent OnInputPopupValidateChar;
public delegate bool InputPopupValidateCharEvent(string input, int charIndex, char addedChar);

public event InputPopupValidateCharEvent OnInputPopupValidateChar;

public void EnableMenu(bool value);

Expand Down
4 changes: 2 additions & 2 deletions src/OWML.ModHelper.Menus/CustomInputs/OWMLPopupInputMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class OWMLPopupInputMenu : PopupMenu, IOWMLPopupInputMenu
protected bool _virtualKeyboardOpen;

public event PopupInputMenu.InputPopupTextChangedEvent OnInputPopupTextChanged;
public event PopupInputMenu.InputPopupValidateCharEvent OnInputPopupValidateChar;
public event IOWMLPopupInputMenu.InputPopupValidateCharEvent OnInputPopupValidateChar;

public override void Awake()
{
Expand Down Expand Up @@ -158,7 +158,7 @@ private char OnValidateInput(string input, int charIndex, char addedChar)
Delegate[] invocationList = this.OnInputPopupValidateChar.GetInvocationList();
for (int i = 0; i < invocationList.Length; i++)
{
bool flag2 = (bool)invocationList[i].DynamicInvoke(new object[] { addedChar });
bool flag2 = (bool)invocationList[i].DynamicInvoke(new object[] { input, charIndex, addedChar });
flag = flag && flag2;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/OWML.ModHelper.Menus/ModInputMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private bool OnValidateNumber() =>
float.TryParse(_inputMenu.GetInputText(), out _);

private bool OnValidateCharNumber(char c) =>
"0123456789.".Contains("" + c);
"0123456789.-".Contains(c.ToString());

protected override void OnPopupConfirm()
{
Expand Down
6 changes: 6 additions & 0 deletions src/OWML.ModHelper.Menus/NewMenuSystem/MenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ private void EditExistingMenus()
{
var text = item.GetComponent<UIStyleApplier>()._textItems[0];
text.horizontalOverflow = HorizontalWrapMode.Wrap;

// Give a little bit of margin to account for the dividing lines
// (as otherwise it can look like the text is on top of them when in mouse mode)
float margin = 0.03f;
text.rectTransform.anchorMin = new Vector2(margin, 0.0f);
text.rectTransform.anchorMax = new Vector2(1f - margin, 1.0f);
}
}

Expand Down
107 changes: 86 additions & 21 deletions src/OWML.ModHelper.Menus/NewMenuSystem/OptionsMenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public OptionsMenuManager(IModConsole console, IModUnityEvents unityEvents, IPop
newMenu.transform.localScale = Vector3.one;
newMenu.transform.localPosition = Vector3.zero;
newMenu.transform.localRotation = Quaternion.identity;
newMenu.name = $"Menu-{name}";
var rt = newMenu.GetComponent<RectTransform>();
var ert = existingMenu.GetComponent<RectTransform>();
rt.anchorMin = ert.anchorMin;
Expand Down Expand Up @@ -84,6 +85,7 @@ public OptionsMenuManager(IModConsole console, IModUnityEvents unityEvents, IPop
newSubMenu.transform.localScale = Vector3.one;
newSubMenu.transform.localPosition = Vector3.zero;
newSubMenu.transform.localRotation = Quaternion.identity;
newSubMenu.name = $"Menu-{name}";
var rectTransform = newSubMenu.GetComponent<RectTransform>();
rectTransform.anchorMin = existingTabbedSubMenu.GetComponent<RectTransform>().anchorMin;
rectTransform.anchorMax = existingTabbedSubMenu.GetComponent<RectTransform>().anchorMax;
Expand Down Expand Up @@ -169,7 +171,7 @@ public void RemoveTab(Menu tab)
newSubMenuTabButton.transform.localScale = Vector3.one;
newSubMenuTabButton.transform.localPosition = Vector3.zero;
newSubMenuTabButton.transform.localRotation = Quaternion.identity;
newSubMenuTabButton.transform.SetSiblingIndex(newSubMenuTabButton.transform.parent.childCount - 2);
newSubMenuTabButton.transform.SetSiblingIndex(newSubMenuTabButton.transform.parent.childCount - 3);
newSubMenuTabButton.name = $"Button-{name}Tab";
Object.Destroy(newSubMenuTabButton.GetComponentInChildren<LocalizedText>());
newSubMenuTabButton.GetComponentInChildren<Text>().text = name;
Expand All @@ -190,6 +192,9 @@ public void RemoveTab(Menu tab)
rt.offsetMax = ert.offsetMax;
rt.sizeDelta = ert.sizeDelta;

var viewportRect = newSubMenu.GetComponentInChildren<ScrollRectFixedSizeHandle>().GetComponent<RectTransform>();
viewportRect.anchoredPosition3D = Vector3.zero;

if (menu._selectOnActivate == null)
{
menu._selectOnActivate = newSubMenuTabButton.GetComponent<Button>();
Expand Down Expand Up @@ -462,37 +467,96 @@ public GameObject AddSeparator(Menu menu, bool dots)

public SubmitAction CreateButton(Menu menu, string buttonLabel, string tooltip, MenuSide side)
{
var rootObj = new GameObject($"UIElement-{buttonLabel}");
rootObj.transform.parent = GetParentForAddedElements(menu);
rootObj.transform.localScale = Vector3.one;
rootObj.transform.localRotation = Quaternion.identity;
rootObj.transform.localPosition = Vector3.zero;

var layoutElement = rootObj.AddComponent<LayoutElement>();
layoutElement.minHeight = 70;
layoutElement.flexibleWidth = 1;

var existingHorizLayout = Resources.FindObjectsOfTypeAll<Menu>()
.Single(x => x.name == "GraphicsMenu").transform
.Find("Scroll View")
.Find("Viewport")
.Find("Content")
.Find("UIElement-ResolutionSelect")
.Find("HorizontalLayoutGroup").gameObject;

var newHorizLayout = Object.Instantiate(existingHorizLayout);
newHorizLayout.name = "HorizontalLayoutGroup";
newHorizLayout.transform.parent = rootObj.transform;
newHorizLayout.transform.localPosition = Vector3.zero;
newHorizLayout.transform.localScale = Vector3.one;
newHorizLayout.transform.localRotation = Quaternion.identity;

var hrt = newHorizLayout.GetComponent<RectTransform>();
var ohrt = existingHorizLayout.GetComponent<RectTransform>();
//hrt.anchorMin = ohrt.anchorMin;
//hrt.anchorMax = ohrt.anchorMax;
hrt.offsetMin = ohrt.offsetMin;
hrt.offsetMax = ohrt.offsetMax;
hrt.anchoredPosition3D = ohrt.anchoredPosition3D;
hrt.sizeDelta = ohrt.sizeDelta;

hrt.anchorMax = new Vector2(0.5f, 1f);
switch (side)
{
case MenuSide.LEFT:
hrt.anchorMin = new Vector2(0f, 1f);
break;
case MenuSide.CENTER:
hrt.anchorMin = new Vector2(0.25f, 1f);
break;
case MenuSide.RIGHT:
hrt.anchorMin = new Vector2(0.5f, 1f);
break;
}

Object.Destroy(newHorizLayout.GetComponentInChildren<LocalizedText>());

var oldLabelComponent = newHorizLayout.transform
.Find("LabelBlock")
.Find("HorizontalLayoutGroup")
.Find("Label")
.GetComponent<Text>();
var labelFont = Resources.FindObjectsOfTypeAll<Font>().First(x => x.name == "Adobe - SerifGothicStd-ExtraBold");
var labelFontSize = 28;

Object.Destroy(newHorizLayout.transform.Find("LabelBlock").gameObject);

var controlBlock = newHorizLayout.transform.Find("ControlBlock");
Object.Destroy(controlBlock.Find("OptionSelectorBG").gameObject);
Object.Destroy(controlBlock.Find("HorizontalLayoutGroup").gameObject);

var existingButton = Resources.FindObjectsOfTypeAll<Button>().First(x => x.name == "UIElement-ButtonContinue");

var newButtonObj = Object.Instantiate(existingButton);
newButtonObj.transform.parent = GetParentForAddedElements(menu);
newButtonObj.transform.parent = controlBlock;
newButtonObj.transform.localPosition = Vector3.zero;
newButtonObj.transform.localScale = Vector3.one;
newButtonObj.name = $"UIElement-Button-{buttonLabel}";
newButtonObj.transform.localRotation = Quaternion.identity;

if (side == MenuSide.LEFT)
{
Object.Destroy(newButtonObj.transform.Find("ForegroundLayoutGroup/RightSpacer").gameObject);
newButtonObj.transform.Find("ForegroundLayoutGroup/LeftSpacer").SetAsFirstSibling();
}
else if (side == MenuSide.CENTER)
{
Object.Destroy(newButtonObj.transform.Find("ForegroundLayoutGroup/RightSpacer").gameObject);
Object.Destroy(newButtonObj.transform.Find("ForegroundLayoutGroup/LeftSpacer").gameObject);
}
else if (side == MenuSide.RIGHT)
{
Object.Destroy(newButtonObj.transform.Find("ForegroundLayoutGroup/LeftSpacer").gameObject);
newButtonObj.transform.Find("ForegroundLayoutGroup/RightSpacer").SetAsFirstSibling();
}
Object.Destroy(newButtonObj.transform.Find("ForegroundLayoutGroup/RightSpacer").gameObject);
Object.Destroy(newButtonObj.transform.Find("ForegroundLayoutGroup/LeftSpacer").gameObject);

Object.Destroy(newButtonObj.GetComponent<SubmitAction>());
var submitAction = newButtonObj.gameObject.AddComponent<SubmitAction>();

Object.Destroy(newButtonObj.gameObject.GetComponentInChildren<LocalizedText>());

newButtonObj.GetComponentInChildren<Text>().text = buttonLabel;
var menuOption = newButtonObj.gameObject.GetAddComponent<MenuOption>();
menuOption._tooltipTextType = UITextType.None;
menuOption._overrideTooltipText = tooltip;
menuOption._label = newButtonObj.GetComponentInChildren<Text>();
menuOption._label.text = buttonLabel;
menuOption._label.font = labelFont;
menuOption._label.fontSize = labelFontSize;

menu._menuOptions = menu._menuOptions.Add(menuOption);

if (menu._selectOnActivate == null)
{
Expand Down Expand Up @@ -523,6 +587,7 @@ public SubmitAction CreateButtonWithLabel(Menu menu, string label, string button
.Find("HorizontalLayoutGroup").gameObject;

var newHorizLayout = Object.Instantiate(existingHorizLayout);
newHorizLayout.name = "HorizontalLayoutGroup";
newHorizLayout.transform.parent = newButtonObj.transform;
newHorizLayout.transform.localPosition = Vector3.zero;
newHorizLayout.transform.localScale = Vector3.one;
Expand Down Expand Up @@ -641,10 +706,10 @@ public IOWMLTextEntryElement AddTextEntryInput(Menu menu, string label, string i

if (isNumeric)
{
textInputPopup.OnInputPopupValidateChar += c =>
textInputPopup.OnInputPopupValidateChar += (string input, int charIndex, char addedChar) =>
{
var text = textInputPopup.GetInputText() + c;
return Regex.IsMatch(text, @"^\d*[,.]?\d*$");
var text = input.Insert(charIndex, addedChar.ToString());
return Regex.IsMatch(text, @"^-?\d*[,.]?\d*$");
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/OWML.ModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static Container CreateContainer(IApplicationHelper appHelper, IGameObjec
throw new UnityException($"Can't load OWML manifest: {manifestPath}");
}

var unityEvents = goHelper.CreateAndAdd<IModUnityEvents, ModUnityEvents>();
var unityEvents = goHelper.CreateAndAdd<IModUnityEvents, ModUnityEvents>("UnityEvents");

return new Container()
.Add(appHelper)
Expand Down

0 comments on commit 7555c05

Please sign in to comment.