193 lines
5.2 KiB
C#
193 lines
5.2 KiB
C#
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using GameCult.Aetheria.EveRuntime;
|
|
using GameCult.Aetheria.State.Verse;
|
|
using GameCult.Eve.Surface;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class MenuPanel : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
private sealed class MenuTabBinding
|
|
{
|
|
public MenuTab Tab;
|
|
public GameObject TabContents;
|
|
public string Label;
|
|
public bool RequireDock;
|
|
}
|
|
|
|
public ActionGameManager GameManager;
|
|
public RectTransform TabButtons;
|
|
[SerializeField] private MenuTabBinding[] TabBindings = Array.Empty<MenuTabBinding>();
|
|
|
|
public event Action<MenuTab> TabChanged;
|
|
|
|
private readonly Dictionary<MenuTab, MenuTabBinding> _tabs = new Dictionary<MenuTab, MenuTabBinding>();
|
|
private MenuTabBinding _current;
|
|
private UIDocument _tabSurfaceDocument;
|
|
private readonly AetheriaEveUnitySurfaceChrome _tabSurfaceChrome = new AetheriaEveUnitySurfaceChrome();
|
|
|
|
public MenuTab CurrentTab { get; private set; }
|
|
|
|
public void ShowTab(MenuTab tab)
|
|
{
|
|
gameObject.SetActive(true);
|
|
|
|
if (!_tabs.TryGetValue(tab, out var next))
|
|
return;
|
|
|
|
if (_current == next)
|
|
{
|
|
RenderTabSurface();
|
|
return;
|
|
}
|
|
|
|
if(_current != null)
|
|
{
|
|
_current.TabContents.SetActive(false);
|
|
}
|
|
|
|
CurrentTab = tab;
|
|
_current = next;
|
|
|
|
_current.TabContents.SetActive(true);
|
|
RenderTabSurface();
|
|
|
|
TabChanged?.Invoke(tab);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (TabButtons != null)
|
|
TabButtons.gameObject.SetActive(false);
|
|
|
|
RenderTabSurface();
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
foreach (var tabBinding in TabBindings)
|
|
{
|
|
if (tabBinding?.TabContents == null)
|
|
continue;
|
|
|
|
tabBinding.TabContents.SetActive(false);
|
|
_tabs[tabBinding.Tab] = tabBinding;
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
HideTabSurface();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_tabSurfaceDocument != null)
|
|
{
|
|
AetheriaEveUnitySurfaceHost.DestroyDocument(_tabSurfaceDocument);
|
|
_tabSurfaceDocument = null;
|
|
}
|
|
}
|
|
|
|
private void RenderTabSurface()
|
|
{
|
|
if (_tabs.Count == 0)
|
|
return;
|
|
|
|
if (TabButtons != null)
|
|
TabButtons.gameObject.SetActive(false);
|
|
|
|
_tabSurfaceDocument = AetheriaEveUnitySurfaceHost.RenderRuntime(
|
|
transform,
|
|
_tabSurfaceDocument,
|
|
"Aetheria Runtime Menu Tabs Surface",
|
|
AetheriaRuntimeMenuTabsSurfaceBuilder.Build(ProjectTabSurfaceState()),
|
|
HandleTabSurfaceCommand,
|
|
_tabSurfaceChrome);
|
|
}
|
|
|
|
private void HandleTabSurfaceCommand(EveSurfaceCommandRequest request)
|
|
{
|
|
if (!AetheriaRuntimeMenuTabsSurfaceCommands.TryRead(request, out var command))
|
|
{
|
|
Debug.LogWarning($"Unknown runtime menu tab command: {request?.Command}");
|
|
return;
|
|
}
|
|
|
|
foreach (var tab in _tabs.Keys)
|
|
{
|
|
if (command.Kind == AetheriaRuntimeMenuTabCommandKind.SelectTab &&
|
|
string.Equals(command.TabKey, TabKey(tab), StringComparison.Ordinal))
|
|
{
|
|
ShowTab(tab);
|
|
return;
|
|
}
|
|
}
|
|
|
|
Debug.LogWarning($"Unknown runtime menu tab command: {request?.Command}");
|
|
}
|
|
|
|
private void HideTabSurface()
|
|
{
|
|
if (_tabSurfaceDocument == null)
|
|
return;
|
|
|
|
AetheriaEveUnitySurfaceHost.Hide(_tabSurfaceDocument);
|
|
}
|
|
|
|
private AetheriaRuntimeMenuTabsSurfaceState ProjectTabSurfaceState()
|
|
{
|
|
var visibleTabs = ResolveVisibleTabs();
|
|
return new AetheriaRuntimeMenuTabsSurfaceState(
|
|
TabKey(CurrentTab),
|
|
visibleTabs
|
|
.Select(tabBinding => new AetheriaRuntimeMenuTabSurfaceEntry(
|
|
TabKey(tabBinding.Tab),
|
|
GetTabLabel(tabBinding),
|
|
tabBinding.Tab == CurrentTab))
|
|
.ToArray(),
|
|
DateTime.UtcNow.ToString("O"));
|
|
}
|
|
|
|
private MenuTabBinding[] ResolveVisibleTabs()
|
|
{
|
|
return _tabs.Values
|
|
.Where(tabBinding => !tabBinding.RequireDock || GameManager.IsObservedDocked)
|
|
.Where(tabBinding => tabBinding.Tab != MenuTab.Local || GameManager.TryGetObservedDockedLocalStory(out _))
|
|
.OrderBy(tabBinding => (int)tabBinding.Tab)
|
|
.ToArray();
|
|
}
|
|
|
|
private static string GetTabLabel(MenuTabBinding tabBinding)
|
|
{
|
|
return string.IsNullOrWhiteSpace(tabBinding.Label)
|
|
? tabBinding.Tab.ToString()
|
|
: tabBinding.Label;
|
|
}
|
|
|
|
private static string TabKey(MenuTab tab)
|
|
{
|
|
return tab.ToString().ToLowerInvariant();
|
|
}
|
|
|
|
// void Start()
|
|
// {
|
|
// ShowTab(MenuTab.Inventory);
|
|
// }
|
|
}
|
|
|
|
public enum MenuTab
|
|
{
|
|
Map,
|
|
Inventory,
|
|
Trade,
|
|
Galaxy,
|
|
Local
|
|
}
|