2020-10-23 01:50:33 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-11-05 17:33:04 +11:00
|
|
|
|
using UnityExplorer.Console;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
using UnityExplorer.UI.Modules;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
using UnityExplorer.Config;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-11-05 17:33:04 +11:00
|
|
|
|
namespace UnityExplorer.UI
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
|
|
|
|
public class MainMenu
|
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public abstract class Page
|
|
|
|
|
{
|
|
|
|
|
public abstract string Name { get; }
|
|
|
|
|
|
|
|
|
|
public GameObject Content;
|
|
|
|
|
public Button RefNavbarButton { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool Enabled
|
|
|
|
|
{
|
|
|
|
|
get => Content?.activeSelf ?? false;
|
|
|
|
|
set => Content?.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public abstract void Init();
|
|
|
|
|
public abstract void Update();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 01:50:33 +11:00
|
|
|
|
public static MainMenu Instance { get; set; }
|
|
|
|
|
|
|
|
|
|
public PanelDragger Dragger { get; private set; }
|
2020-10-23 19:55:02 +11:00
|
|
|
|
|
2020-10-23 01:50:33 +11:00
|
|
|
|
public GameObject MainPanel { get; private set; }
|
2020-10-23 19:55:02 +11:00
|
|
|
|
public GameObject PageViewport { get; private set; }
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public readonly List<Page> Pages = new List<Page>();
|
|
|
|
|
private Page m_activePage;
|
2020-10-23 20:40:44 +11:00
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
// Navbar buttons
|
|
|
|
|
private Button m_lastNavButtonPressed;
|
2020-10-28 20:52:40 +11:00
|
|
|
|
private readonly Color m_navButtonNormal = new Color(0.3f, 0.3f, 0.3f, 1);
|
|
|
|
|
private readonly Color m_navButtonHighlight = new Color(0.3f, 0.6f, 0.3f);
|
|
|
|
|
private readonly Color m_navButtonSelected = new Color(0.2f, 0.5f, 0.2f, 1);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
|
|
|
|
public MainMenu()
|
|
|
|
|
{
|
|
|
|
|
if (Instance != null)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning("An instance of MainMenu already exists, cannot create another!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Instance = this;
|
|
|
|
|
|
2020-10-23 20:40:44 +11:00
|
|
|
|
Pages.Add(new HomePage());
|
|
|
|
|
Pages.Add(new SearchPage());
|
|
|
|
|
Pages.Add(new ConsolePage());
|
|
|
|
|
Pages.Add(new OptionsPage());
|
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
ConstructMenu();
|
2020-10-23 20:40:44 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
foreach (Page page in Pages)
|
2020-10-23 20:40:44 +11:00
|
|
|
|
{
|
|
|
|
|
page.Init();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
// hide menu until each page has init layout (bit of a hack)
|
|
|
|
|
initPos = MainPanel.transform.position;
|
|
|
|
|
MainPanel.transform.position = new Vector3(9999, 9999);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
internal Vector3 initPos;
|
|
|
|
|
internal bool pageLayoutInit;
|
|
|
|
|
internal int layoutInitIndex;
|
|
|
|
|
|
2020-10-23 01:50:33 +11:00
|
|
|
|
public void Update()
|
|
|
|
|
{
|
2020-11-11 20:16:43 +11:00
|
|
|
|
if (!pageLayoutInit)
|
|
|
|
|
{
|
|
|
|
|
if (layoutInitIndex < Pages.Count)
|
|
|
|
|
{
|
|
|
|
|
SetPage(Pages[layoutInitIndex]);
|
|
|
|
|
layoutInitIndex++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pageLayoutInit = true;
|
|
|
|
|
MainPanel.transform.position = initPos;
|
|
|
|
|
SetPage(Pages[0]);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 20:57:34 +11:00
|
|
|
|
m_activePage?.Update();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public void SetPage(Page page)
|
2020-10-23 19:55:02 +11:00
|
|
|
|
{
|
2020-10-23 20:40:44 +11:00
|
|
|
|
if (m_activePage == page || page == null)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-10-23 20:40:44 +11:00
|
|
|
|
return;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-10-24 00:41:55 +11:00
|
|
|
|
m_activePage?.Content?.SetActive(false);
|
2020-10-27 00:54:08 +11:00
|
|
|
|
if (m_activePage is ConsolePage)
|
|
|
|
|
{
|
2020-10-28 20:52:40 +11:00
|
|
|
|
AutoCompleter.m_mainObj?.SetActive(false);
|
2020-10-27 00:54:08 +11:00
|
|
|
|
}
|
2020-10-24 00:41:55 +11:00
|
|
|
|
|
2020-10-23 20:40:44 +11:00
|
|
|
|
m_activePage = page;
|
|
|
|
|
|
2020-10-24 00:41:55 +11:00
|
|
|
|
m_activePage.Content?.SetActive(true);
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Button button = page.RefNavbarButton;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
ColorBlock colors = button.colors;
|
2020-10-23 19:55:02 +11:00
|
|
|
|
colors.normalColor = m_navButtonSelected;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
//try { colors.selectedColor = m_navButtonSelected; } catch { }
|
2020-10-23 19:55:02 +11:00
|
|
|
|
button.colors = colors;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
if (m_lastNavButtonPressed && m_lastNavButtonPressed != button)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
ColorBlock oldColors = m_lastNavButtonPressed.colors;
|
2020-10-23 19:55:02 +11:00
|
|
|
|
oldColors.normalColor = m_navButtonNormal;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
//try { oldColors.selectedColor = m_navButtonNormal; } catch { }
|
2020-10-23 19:55:02 +11:00
|
|
|
|
m_lastNavButtonPressed.colors = oldColors;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 20:40:44 +11:00
|
|
|
|
m_lastNavButtonPressed = button;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
#region UI Construction
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
private void ConstructMenu()
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-10-23 19:55:02 +11:00
|
|
|
|
MainPanel = UIFactory.CreatePanel(UIManager.CanvasRoot, "MainMenu", out GameObject content);
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
RectTransform panelRect = MainPanel.GetComponent<RectTransform>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
panelRect.anchorMin = new Vector2(0.25f, 0.1f);
|
2020-11-03 20:59:13 +11:00
|
|
|
|
panelRect.anchorMax = new Vector2(0.78f, 0.95f);
|
2020-10-23 19:55:02 +11:00
|
|
|
|
|
2020-11-08 21:04:41 +11:00
|
|
|
|
MainPanel.AddComponent<Mask>();
|
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
ConstructTitleBar(content);
|
|
|
|
|
|
|
|
|
|
ConstructNavbar(content);
|
|
|
|
|
|
|
|
|
|
ConstructMainViewport(content);
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
|
|
|
|
new DebugConsole(content);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
private void ConstructTitleBar(GameObject content)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-10-23 19:55:02 +11:00
|
|
|
|
// Core title bar holder
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject titleBar = UIFactory.CreateHorizontalGroup(content);
|
2020-10-23 19:55:02 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
HorizontalLayoutGroup titleGroup = titleBar.GetComponent<HorizontalLayoutGroup>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
titleGroup.childControlHeight = true;
|
|
|
|
|
titleGroup.childControlWidth = true;
|
|
|
|
|
titleGroup.childForceExpandHeight = true;
|
|
|
|
|
titleGroup.childForceExpandWidth = true;
|
|
|
|
|
titleGroup.padding.left = 15;
|
|
|
|
|
titleGroup.padding.right = 3;
|
|
|
|
|
titleGroup.padding.top = 3;
|
|
|
|
|
titleGroup.padding.bottom = 3;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
LayoutElement titleLayout = titleBar.AddComponent<LayoutElement>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
titleLayout.minHeight = 35;
|
|
|
|
|
titleLayout.flexibleHeight = 0;
|
|
|
|
|
|
|
|
|
|
// Explorer label
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject textObj = UIFactory.CreateLabel(titleBar, TextAnchor.MiddleLeft);
|
2020-10-23 19:55:02 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Text text = textObj.GetComponent<Text>();
|
2020-11-03 20:59:13 +11:00
|
|
|
|
text.text = $"<b>UnityExplorer</b> <i>v{ExplorerCore.VERSION}</i>";
|
2020-10-23 19:55:02 +11:00
|
|
|
|
text.resizeTextForBestFit = true;
|
|
|
|
|
text.resizeTextMinSize = 12;
|
|
|
|
|
text.resizeTextMaxSize = 20;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
LayoutElement textLayout = textObj.AddComponent<LayoutElement>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
textLayout.flexibleWidth = 50;
|
|
|
|
|
|
|
|
|
|
// Add PanelDragger using the label object
|
|
|
|
|
|
|
|
|
|
Dragger = new PanelDragger(titleBar.GetComponent<RectTransform>(), MainPanel.GetComponent<RectTransform>());
|
|
|
|
|
|
|
|
|
|
// Hide button
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject hideBtnObj = UIFactory.CreateButton(titleBar);
|
2020-10-23 19:55:02 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Button hideBtn = hideBtnObj.GetComponent<Button>();
|
2020-10-27 00:54:08 +11:00
|
|
|
|
#if CPP
|
2020-10-24 00:41:55 +11:00
|
|
|
|
hideBtn.onClick.AddListener(new Action(() => { ExplorerCore.ShowMenu = false; }));
|
2020-10-27 00:54:08 +11:00
|
|
|
|
#else
|
|
|
|
|
hideBtn.onClick.AddListener(() => { ExplorerCore.ShowMenu = false; });
|
|
|
|
|
#endif
|
2020-10-28 06:39:26 +11:00
|
|
|
|
ColorBlock colorBlock = hideBtn.colors;
|
|
|
|
|
colorBlock.normalColor = new Color(65f / 255f, 23f / 255f, 23f / 255f);
|
|
|
|
|
colorBlock.pressedColor = new Color(35f / 255f, 10f / 255f, 10f / 255f);
|
|
|
|
|
colorBlock.highlightedColor = new Color(156f / 255f, 0f, 0f);
|
2020-10-23 19:55:02 +11:00
|
|
|
|
hideBtn.colors = colorBlock;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
LayoutElement btnLayout = hideBtnObj.AddComponent<LayoutElement>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
btnLayout.minWidth = 90;
|
|
|
|
|
btnLayout.flexibleWidth = 2;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Text hideText = hideBtnObj.GetComponentInChildren<Text>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
hideText.color = Color.white;
|
|
|
|
|
hideText.resizeTextForBestFit = true;
|
|
|
|
|
hideText.resizeTextMinSize = 8;
|
|
|
|
|
hideText.resizeTextMaxSize = 16;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
hideText.text = $"Hide ({ModConfig.Instance.Main_Menu_Toggle})";
|
|
|
|
|
|
|
|
|
|
ModConfig.OnConfigChanged += ModConfig_OnConfigChanged;
|
|
|
|
|
|
|
|
|
|
void ModConfig_OnConfigChanged()
|
|
|
|
|
{
|
|
|
|
|
hideText.text = $"Hide ({ModConfig.Instance.Main_Menu_Toggle})";
|
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
private void ConstructNavbar(GameObject content)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject navbarObj = UIFactory.CreateHorizontalGroup(content);
|
2020-10-23 19:55:02 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
HorizontalLayoutGroup navGroup = navbarObj.GetComponent<HorizontalLayoutGroup>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
navGroup.padding.left = 3;
|
|
|
|
|
navGroup.padding.right = 3;
|
|
|
|
|
navGroup.padding.top = 3;
|
|
|
|
|
navGroup.padding.bottom = 3;
|
|
|
|
|
navGroup.spacing = 5;
|
|
|
|
|
navGroup.childControlHeight = true;
|
|
|
|
|
navGroup.childControlWidth = true;
|
|
|
|
|
navGroup.childForceExpandHeight = true;
|
|
|
|
|
navGroup.childForceExpandWidth = true;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
LayoutElement navLayout = navbarObj.AddComponent<LayoutElement>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
navLayout.minHeight = 35;
|
|
|
|
|
navLayout.flexibleHeight = 0;
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
foreach (Page page in Pages)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject btnObj = UIFactory.CreateButton(navbarObj);
|
|
|
|
|
Button btn = btnObj.GetComponent<Button>();
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-10-23 20:40:44 +11:00
|
|
|
|
page.RefNavbarButton = btn;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-10-27 00:54:08 +11:00
|
|
|
|
#if CPP
|
2020-10-24 00:41:55 +11:00
|
|
|
|
btn.onClick.AddListener(new Action(() => { SetPage(page); }));
|
2020-10-27 00:54:08 +11:00
|
|
|
|
#else
|
|
|
|
|
btn.onClick.AddListener(() => { SetPage(page); });
|
|
|
|
|
#endif
|
2020-10-23 19:55:02 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Text text = btnObj.GetComponentInChildren<Text>();
|
2020-10-23 20:40:44 +11:00
|
|
|
|
text.text = page.Name;
|
2020-10-23 19:55:02 +11:00
|
|
|
|
|
|
|
|
|
// Set button colors
|
2020-10-28 06:39:26 +11:00
|
|
|
|
ColorBlock colorBlock = btn.colors;
|
2020-10-23 20:40:44 +11:00
|
|
|
|
colorBlock.normalColor = m_navButtonNormal;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
//try { colorBlock.selectedColor = colorBlock.normalColor; } catch { }
|
2020-10-23 19:55:02 +11:00
|
|
|
|
colorBlock.highlightedColor = m_navButtonHighlight;
|
|
|
|
|
colorBlock.pressedColor = m_navButtonSelected;
|
|
|
|
|
btn.colors = colorBlock;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
private void ConstructMainViewport(GameObject content)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject mainObj = UIFactory.CreateHorizontalGroup(content);
|
|
|
|
|
HorizontalLayoutGroup mainGroup = mainObj.GetComponent<HorizontalLayoutGroup>();
|
2020-10-23 19:55:02 +11:00
|
|
|
|
mainGroup.childControlHeight = true;
|
|
|
|
|
mainGroup.childControlWidth = true;
|
|
|
|
|
mainGroup.childForceExpandHeight = true;
|
|
|
|
|
mainGroup.childForceExpandWidth = true;
|
|
|
|
|
|
|
|
|
|
PageViewport = mainObj;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
#endregion
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
}
|