2021-03-30 19:50:04 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using UnityEngine;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityEngine.UI;
|
2021-03-18 17:17:29 +11:00
|
|
|
|
using UnityExplorer.Core.Config;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
using UnityExplorer.Core.Input;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
using UnityExplorer.UI.Models;
|
|
|
|
|
using UnityExplorer.UI.Panels;
|
2021-03-18 17:17:29 +11:00
|
|
|
|
using UnityExplorer.UI.Utility;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
using UnityExplorer.UI.Widgets;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-03 20:59:13 +11:00
|
|
|
|
namespace UnityExplorer.UI
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
|
|
|
|
public static class UIManager
|
|
|
|
|
{
|
|
|
|
|
public static GameObject CanvasRoot { get; private set; }
|
|
|
|
|
public static EventSystem EventSys { get; private set; }
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
// panels
|
|
|
|
|
public static SceneExplorer SceneExplorer { get; private set; }
|
2021-04-22 17:53:29 +10:00
|
|
|
|
public static InspectorTest Inspector { get; private set; }
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
|
|
|
|
// bundle assets
|
2020-11-10 20:18:14 +11:00
|
|
|
|
internal static Font ConsoleFont { get; private set; }
|
2020-11-25 16:40:36 +11:00
|
|
|
|
internal static Shader BackupShader { get; private set; }
|
|
|
|
|
|
2021-03-11 17:57:58 +11:00
|
|
|
|
public static bool ShowMenu
|
|
|
|
|
{
|
|
|
|
|
get => s_showMenu;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
set
|
2021-03-11 17:57:58 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (s_showMenu == value || !CanvasRoot)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
s_showMenu = value;
|
|
|
|
|
CanvasRoot.SetActive(value);
|
|
|
|
|
CursorUnlocker.UpdateCursorControl();
|
2021-03-11 17:57:58 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-30 19:50:04 +11:00
|
|
|
|
public static bool s_showMenu = true;
|
2021-03-11 17:57:58 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
internal static void InitUI()
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
LoadBundle();
|
2021-03-26 19:49:53 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
UIFactory.Init();
|
2021-03-26 19:49:53 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
CreateRootCanvas();
|
|
|
|
|
|
|
|
|
|
SceneExplorer = new SceneExplorer();
|
|
|
|
|
SceneExplorer.ConstructUI(CanvasRoot);
|
2021-04-16 21:07:32 +10:00
|
|
|
|
|
2021-04-22 17:53:29 +10:00
|
|
|
|
Inspector = new InspectorTest();
|
|
|
|
|
Inspector.ConstructUI(CanvasRoot);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
|
|
|
|
//MainMenu.Create();
|
|
|
|
|
//InspectUnderMouse.ConstructUI();
|
|
|
|
|
//PanelDragger.CreateCursorUI();
|
2021-03-26 19:49:53 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
// Force refresh of anchors etc
|
|
|
|
|
Canvas.ForceUpdateCanvases();
|
2021-03-26 19:49:53 +11:00
|
|
|
|
|
2021-04-01 17:13:31 +11:00
|
|
|
|
ShowMenu = !ConfigManager.Hide_On_Startup.Value;
|
2021-03-26 19:49:53 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
ExplorerCore.Log("UI initialized.");
|
2021-03-26 19:49:53 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
public static void Update()
|
2021-03-11 17:57:58 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (!CanvasRoot)
|
2021-03-11 17:57:58 +11:00
|
|
|
|
return;
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
//if (InspectUnderMouse.Inspecting)
|
|
|
|
|
//{
|
|
|
|
|
// InspectUnderMouse.UpdateInspect();
|
|
|
|
|
// return;
|
|
|
|
|
//}
|
2021-03-11 17:57:58 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (InputManager.GetKeyDown(ConfigManager.Main_Menu_Toggle.Value))
|
2021-03-11 17:57:58 +11:00
|
|
|
|
ShowMenu = !ShowMenu;
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (!ShowMenu)
|
2021-03-11 17:57:58 +11:00
|
|
|
|
return;
|
|
|
|
|
|
2021-04-16 18:24:31 +10:00
|
|
|
|
if (InputManager.GetKeyDown(ConfigManager.Force_Unlock_Keybind.Value))
|
|
|
|
|
CursorUnlocker.Unlock = !CursorUnlocker.Unlock;
|
|
|
|
|
|
2021-04-17 04:05:27 +10:00
|
|
|
|
UIPanel.UpdateFocus();
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
UIBehaviourModel.UpdateInstances();
|
2020-10-25 20:57:34 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (EventSystem.current != EventSys)
|
|
|
|
|
CursorUnlocker.SetEventSystem();
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-04-16 02:49:46 +10:00
|
|
|
|
// TODO could make these UIBehaviourModels
|
2021-04-15 20:18:03 +10:00
|
|
|
|
PanelDragger.UpdateInstances();
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SliderScrollbar.UpdateInstances();
|
|
|
|
|
InputFieldScroller.UpdateInstances();
|
|
|
|
|
}
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
private static void CreateRootCanvas()
|
|
|
|
|
{
|
|
|
|
|
CanvasRoot = new GameObject("ExplorerCanvas");
|
|
|
|
|
UnityEngine.Object.DontDestroyOnLoad(CanvasRoot);
|
|
|
|
|
CanvasRoot.hideFlags |= HideFlags.HideAndDontSave;
|
|
|
|
|
CanvasRoot.layer = 5;
|
|
|
|
|
CanvasRoot.transform.position = new Vector3(0f, 0f, 1f);
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
EventSys = CanvasRoot.AddComponent<EventSystem>();
|
|
|
|
|
InputManager.AddUIModule();
|
|
|
|
|
|
|
|
|
|
Canvas canvas = CanvasRoot.AddComponent<Canvas>();
|
|
|
|
|
canvas.renderMode = RenderMode.ScreenSpaceCamera;
|
|
|
|
|
canvas.referencePixelsPerUnit = 100;
|
|
|
|
|
canvas.sortingOrder = 999;
|
|
|
|
|
|
|
|
|
|
CanvasScaler scaler = CanvasRoot.AddComponent<CanvasScaler>();
|
|
|
|
|
scaler.referenceResolution = new Vector2(1920, 1080);
|
|
|
|
|
scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;
|
|
|
|
|
|
|
|
|
|
CanvasRoot.AddComponent<GraphicRaycaster>();
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
private static void LoadBundle()
|
|
|
|
|
{
|
|
|
|
|
AssetBundle bundle = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-04-15 20:18:03 +10:00
|
|
|
|
bundle = LoadBundle("modern");
|
2021-03-26 19:49:53 +11:00
|
|
|
|
if (bundle == null)
|
2021-04-15 20:18:03 +10:00
|
|
|
|
bundle = LoadBundle("legacy");
|
2021-01-02 19:38:01 +01:00
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
catch { }
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
if (bundle == null)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning("Could not load the ExplorerUI Bundle!");
|
2021-03-26 19:49:53 +11:00
|
|
|
|
ConsoleFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
2021-01-02 19:38:01 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
BackupShader = bundle.LoadAsset<Shader>("DefaultUI");
|
2020-11-20 17:12:40 +11:00
|
|
|
|
|
2021-01-02 19:38:01 +01:00
|
|
|
|
// Fix for games which don't ship with 'UI/Default' shader.
|
|
|
|
|
if (Graphic.defaultGraphicMaterial.shader?.name != "UI/Default")
|
2020-11-20 17:12:40 +11:00
|
|
|
|
{
|
2021-01-02 19:38:01 +01:00
|
|
|
|
ExplorerCore.Log("This game does not ship with the 'UI/Default' shader, using manual Default Shader...");
|
|
|
|
|
Graphic.defaultGraphicMaterial.shader = BackupShader;
|
2020-11-20 17:12:40 +11:00
|
|
|
|
}
|
2021-04-05 16:28:30 +10:00
|
|
|
|
else
|
|
|
|
|
BackupShader = Graphic.defaultGraphicMaterial.shader;
|
2021-01-02 19:38:01 +01:00
|
|
|
|
|
|
|
|
|
ConsoleFont = bundle.LoadAsset<Font>("CONSOLA");
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
ExplorerCore.Log("Loaded UI AssetBundle");
|
2020-11-20 17:12:40 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
private static AssetBundle LoadBundle(string id)
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2021-04-15 20:18:03 +10:00
|
|
|
|
var stream = typeof(ExplorerCore).Assembly
|
2021-04-05 16:28:30 +10:00
|
|
|
|
.GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle");
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
return AssetBundle.LoadFromMemory(ReadFully(stream));
|
2021-03-26 19:49:53 +11:00
|
|
|
|
}
|
2020-11-08 21:04:41 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
private static byte[] ReadFully(Stream input)
|
2021-03-26 19:49:53 +11:00
|
|
|
|
{
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[81920];
|
|
|
|
|
int read;
|
|
|
|
|
while ((read = input.Read(buffer, 0, buffer.Length)) != 0)
|
|
|
|
|
ms.Write(buffer, 0, read);
|
|
|
|
|
return ms.ToArray();
|
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|