using System; using System.Collections.Generic; using UnityExplorer.Core.CSharp; using UnityEngine; using UnityEngine.UI; using UnityExplorer.Core.Config; using UnityExplorer.Core.Unity; using UnityExplorer.UI.Main; using UnityExplorer.UI.Main.CSConsole; namespace UnityExplorer.UI.Main { public class MainMenu { public static MainMenu Instance { get; set; } public PanelDragger Dragger { get; private set; } public GameObject MainPanel { get; private set; } public GameObject PageViewport { get; private set; } public readonly List Pages = new List(); private BaseMenuPage m_activePage; // Navbar buttons private Button m_lastNavButtonPressed; 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); public MainMenu() { if (Instance != null) { ExplorerCore.LogWarning("An instance of MainMenu already exists, cannot create another!"); return; } Instance = this; Pages.Add(new HomePage()); Pages.Add(new SearchPage()); Pages.Add(new CSharpConsole()); Pages.Add(new OptionsPage()); ConstructMenu(); for (int i = 0; i < Pages.Count; i++) { var page = Pages[i]; if (!page.Init()) { // page init failed. Pages.RemoveAt(i); i--; if (page.RefNavbarButton) page.RefNavbarButton.interactable = false; if (page.Content) GameObject.Destroy(page.Content); } } // hide menu until each page has init layout (bit of a hack) initPos = MainPanel.transform.position; MainPanel.transform.position = new Vector3(9999, 9999); } internal Vector3 initPos; internal bool pageLayoutInit; internal int layoutInitIndex; public void Update() { if (!pageLayoutInit) { if (layoutInitIndex < Pages.Count) { SetPage(Pages[layoutInitIndex]); layoutInitIndex++; } else { pageLayoutInit = true; MainPanel.transform.position = initPos; SetPage(Pages[0]); } return; } m_activePage?.Update(); } public void SetPage(BaseMenuPage page) { if (page == null || m_activePage == page) return; // WIP, was going to hide current page if you press current page's button, // but the main panel does not resize so its just a big empty gap there. // Could be good if I resize that gap, not bothering for now. // Would need a fix in PanelDragger as well. //if (m_activePage == page) //{ // SetButtonInactiveColors(page.RefNavbarButton); // m_activePage.Content.SetActive(false); // m_activePage = null; // return; //} m_activePage?.Content?.SetActive(false); // unique case for console page, at the moment this will just go here if (m_activePage is CSharpConsole) AutoCompleter.m_mainObj?.SetActive(false); m_activePage = page; m_activePage.Content?.SetActive(true); Button button = page.RefNavbarButton; SetButtonActiveColors(button); if (m_lastNavButtonPressed && m_lastNavButtonPressed != button) SetButtonInactiveColors(m_lastNavButtonPressed); m_lastNavButtonPressed = button; } internal void SetButtonActiveColors(Button button) { ColorBlock colors = button.colors; colors.normalColor = m_navButtonSelected; button.colors = colors; } internal void SetButtonInactiveColors(Button button) { ColorBlock colors = button.colors; colors.normalColor = m_navButtonNormal; button.colors = colors; } #region UI Construction private void ConstructMenu() { MainPanel = UIFactory.CreatePanel(UIManager.CanvasRoot, "MainMenu", out GameObject content); RectTransform panelRect = MainPanel.GetComponent(); //panelRect.anchorMin = new Vector2(0.25f, 0.1f); //panelRect.anchorMax = new Vector2(0.78f, 0.95f); var anchors = ExplorerConfig.Instance.GetWindowAnchorsVector(); panelRect.anchorMin = new Vector2(anchors.x, anchors.y); panelRect.anchorMax = new Vector2(anchors.z, anchors.w); MainPanel.AddComponent(); ConstructTitleBar(content); ConstructNavbar(content); ConstructMainViewport(content); new DebugConsole(content); } private void ConstructTitleBar(GameObject content) { // Core title bar holder GameObject titleBar = UIFactory.CreateHorizontalGroup(content); HorizontalLayoutGroup titleGroup = titleBar.GetComponent(); 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; LayoutElement titleLayout = titleBar.AddComponent(); titleLayout.minHeight = 25; titleLayout.flexibleHeight = 0; // Explorer label GameObject textObj = UIFactory.CreateLabel(titleBar, TextAnchor.MiddleLeft); Text text = textObj.GetComponent(); text.text = $"UnityExplorer v{ExplorerCore.VERSION}"; text.fontSize = 15; LayoutElement textLayout = textObj.AddComponent(); textLayout.flexibleWidth = 5000; // Add PanelDragger using the label object Dragger = new PanelDragger(titleBar.GetComponent(), MainPanel.GetComponent()); // Hide button GameObject hideBtnObj = UIFactory.CreateButton(titleBar); Button hideBtn = hideBtnObj.GetComponent