using System; using System.Linq; using System.Collections.Generic; using UnityEngine; using UVC.UI.Util; namespace EnglewoodLAB.UI { public class MenuSelectionController : MonoBehaviour { private class MenuItemEntry { public TextColorChangeBehaviour ColorChanger; public System.Type PanelType; } private readonly List _menuItems = new List(); private MenuItemEntry _currentSelectedEntry; public void RegisterButton(TextColorChangeBehaviour colorChanger, System.Type panelType) { var entry = new MenuItemEntry { ColorChanger = colorChanger, PanelType = panelType }; if (colorChanger != null) { _menuItems.Add(entry); } } // Handles deselecting the button color, but NOT closing the panel private void DeselectCurrentButtonColorOnly() { if (_currentSelectedEntry != null) { _currentSelectedEntry.ColorChanger.SetSelected(false); _currentSelectedEntry = null; } } public void OnButtonSelected(TextColorChangeBehaviour selectedColorChanger) { var selectedEntry = _menuItems.FirstOrDefault(e => e.ColorChanger == selectedColorChanger); if (selectedEntry == null) return; if (selectedEntry == _currentSelectedEntry) { // User clicked the same button again: Deselect it and close the panel. if (_currentSelectedEntry.PanelType != null) { var panelToClose = FindObjectOfType(_currentSelectedEntry.PanelType, true) as UIPanel; panelToClose?.Close(); // Close the panel } DeselectCurrentButtonColorOnly(); // Deselect the button color } else { // Deselect the old button and close its panel (if one was selected) if (_currentSelectedEntry != null) { if (_currentSelectedEntry.PanelType != null) { var panelToClose = FindObjectOfType(_currentSelectedEntry.PanelType, true) as UIPanel; panelToClose?.Close(); // Close the old panel } DeselectCurrentButtonColorOnly(); // Deselect old button color } // Select the new button selectedEntry.ColorChanger.SetSelected(true); _currentSelectedEntry = selectedEntry; // Panel open logic is assumed to be handled by the original ICommand execution // No explicit call to Open() from here, as command already handles it. } } // For external closing of a specific panel (e.g. TotalProgressPanel's close button) public void NotifyPanelClosed(System.Type panelType) { if (_currentSelectedEntry != null && _currentSelectedEntry.PanelType == panelType) { DeselectCurrentButtonColorOnly(); // Deselect the button color if it was associated with the closed panel } } // For external closing of any panel (e.g. a global close event) public void NotifyAnyPanelClosed() { // If we don't know *which* panel closed, we have to assume the current one. DeselectCurrentButtonColorOnly(); } public void SetInitialSelection(TextColorChangeBehaviour initialButton) { var initialEntry = _menuItems.FirstOrDefault(e => e.ColorChanger == initialButton); if (initialEntry != null) { // Select the new button initialEntry.ColorChanger.SetSelected(true); _currentSelectedEntry = initialEntry; } } //internal void RegisterButton(UVC.UI.Util.TextColorChangeBehaviour colorChanger, Type panelType) //{ // throw new NotImplementedException(); //} //internal void OnButtonSelected(UVC.UI.Util.TextColorChangeBehaviour colorChanger) //{ // throw new NotImplementedException(); //} internal void RegisterButton(UVC.UI.Util.ImageColorChangeBehaviour colorChanger, Type panelType) { throw new NotImplementedException(); } internal void OnButtonSelected(UVC.UI.Util.ImageColorChangeBehaviour colorChanger) { throw new NotImplementedException(); } } }