TechbloxModdingAPI/GamecraftModdingAPI/Interface/IMGUI/Constants.cs
2020-12-28 13:47:08 -05:00

150 lines
No EOL
6.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using GamecraftModdingAPI.Utility;
using HarmonyLib;
using Svelto.Tasks;
using Svelto.Tasks.Lean;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace GamecraftModdingAPI.Interface.IMGUI
{
/// <summary>
/// Convenient IMGUI values.
/// </summary>
public static class Constants
{
private static byte _defaultCompletion = 0;
private static GUISkin _default = null;
/// <summary>
/// Best-effort imitation of Gamecraft's UI style.
/// </summary>
public static GUISkin Default
{
get
{
if (_defaultCompletion != 0) _default = BuildDefaultGUISkin();
return _default;
}
}
private static Font _riffic = null;
private static Texture2D _blueBackground = null;
private static Texture2D _grayBackground = null;
private static Texture2D _whiteBackground = null;
private static Texture2D _textInputBackground = null;
private static Texture2D _areaBackground = null;
internal static void Init()
{
LoadGUIAssets();
}
private static GUISkin BuildDefaultGUISkin()
{
_defaultCompletion = 0;
if (_riffic == null) return GUI.skin;
// build GUISkin
GUISkin gui = ScriptableObject.CreateInstance<GUISkin>();
gui.font = _riffic;
gui.settings.selectionColor = Color.white;
gui.settings.tripleClickSelectsLine = true;
// set properties off all UI elements
foreach (PropertyInfo p in typeof(GUISkin).GetProperties())
{
// for a "scriptable" GUI system, it's ironic there's no better way to do this
if (p.GetValue(gui) is GUIStyle style)
{
style.richText = true;
style.alignment = TextAnchor.MiddleCenter;
style.fontSize = 30;
style.wordWrap = true;
style.border = new RectOffset(4, 4, 4, 4);
style.margin = new RectOffset(4, 4, 4, 4);
style.padding = new RectOffset(4, 4, 4, 4);
// normal state
style.normal.background = _blueBackground;
style.normal.textColor = Color.white;
// hover state
style.hover.background = _grayBackground;
style.hover.textColor = Color.white;
// focused
style.focused.background = _grayBackground;
style.focused.textColor = Color.white;
// clicking state
style.active.background = _whiteBackground;
style.active.textColor = Color.white;
p.SetValue(gui, style); // probably unnecessary
}
}
// set element-specific styles
// label
gui.label.normal.background = null;
gui.label.hover.background = null;
gui.label.focused.background = null;
gui.label.active.background = null;
// text input
gui.textField.normal.background = _textInputBackground;
gui.textField.hover.background = _textInputBackground;
gui.textField.focused.background = _textInputBackground;
gui.textField.active.background = _textInputBackground;
// text area
gui.textArea.normal.background = _textInputBackground;
gui.textArea.hover.background = _textInputBackground;
gui.textArea.focused.background = _textInputBackground;
gui.textArea.active.background = _textInputBackground;
// window
gui.window.normal.background = _areaBackground;
gui.window.hover.background = _areaBackground;
gui.window.focused.background = _areaBackground;
gui.window.active.background = _areaBackground;
// box (also used by layout groups & areas)
gui.box.normal.background = _areaBackground;
gui.box.hover.background = _areaBackground;
gui.box.focused.background = _areaBackground;
gui.box.active.background = _areaBackground;
return gui;
}
private static void LoadGUIAssets()
{
AsyncOperationHandle<Font> rifficHandle = Addressables.LoadAssetAsync<Font>("Assets/Art/Fonts/riffic-bold.ttf");
rifficHandle.Completed += handle =>
{
_riffic = handle.Result;
_defaultCompletion++;
};
_blueBackground = new Texture2D(1, 1);
_blueBackground.SetPixel(0, 0, new Color(0.004f, 0.522f, 0.847f) /* Gamecraft Blue */);
_blueBackground.Apply();
_grayBackground = new Texture2D(1, 1);
_grayBackground.SetPixel(0, 0, new Color(0.745f, 0.745f, 0.745f) /* Gray */);
_grayBackground.Apply();
_whiteBackground = new Texture2D(1, 1);
_whiteBackground.SetPixel(0, 0, new Color(0.898f, 0.898f, 0.898f) /* Very light gray */);
_whiteBackground.Apply();
_textInputBackground = new Texture2D(1, 1);
_textInputBackground.SetPixel(0, 0, new Color(0f, 0f, 0f, 0.25f) /* Translucent gray */);
_textInputBackground.Apply();
_areaBackground = new Texture2D(1, 1);
_areaBackground.SetPixel(0, 0, new Color(0f, 0f, 0f, 0.25f) /* Translucent gray */);
_areaBackground.Apply();
/* // this is actually gray (used for the loading screen)
AsyncOperationHandle<Texture2D> backgroundHandle =
Addressables.LoadAssetAsync<Texture2D>("Assets/Art/Textures/UI/FrontEndMap/RCX_Blue_Background_5k.jpg");
backgroundHandle.Completed += handle =>
{
_blueBackground = handle.Result;
_defaultCompletion++;
};*/
_defaultCompletion++;
}
}
}