|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Reflection;
- using TechbloxModdingAPI.Utility;
- using HarmonyLib;
- using Svelto.Tasks;
- using Svelto.Tasks.Lean;
- using UnityEngine;
- using UnityEngine.AddressableAssets;
- using UnityEngine.ResourceManagement.AsyncOperations;
-
- namespace TechbloxModdingAPI.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 Techblox's UI style.
- /// </summary>
- public static GUISkin Default
- {
- get
- {
- if (_defaultCompletion != 0) _default = BuildDefaultGUISkin();
- return _default;
- }
- }
-
- private static Font _font = 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 (_font == null) return GUI.skin;
- // build GUISkin
- GUISkin gui = ScriptableObject.CreateInstance<GUISkin>();
- gui.font = _font;
- gui.settings.selectionColor = Color.white;
- gui.settings.tripleClickSelectsLine = true;
- Color textColour = new Color(0.706f, 0.706f, 0.706f);
- // 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 = 16;
- 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 = _grayBackground;
- style.normal.textColor = textColour;
- // hover state
- style.hover.background = _grayBackground;
- style.hover.textColor = textColour;
- // focused
- style.focused.background = _grayBackground;
- style.focused.textColor = textColour;
- // clicking state
- style.active.background = _whiteBackground;
- style.active.textColor = textColour;
-
- p.SetValue(gui, style); // probably unnecessary
- }
- }
- // set element-specific styles
- // label
- gui.label.fontSize = 20;
- 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> fontHandle = Addressables.LoadAssetAsync<Font>("Assets/Plugins/TextMesh Pro/Fonts/LiberationSans.ttf");
- fontHandle.Completed += handle =>
- {
- _font = handle.Result;
- _defaultCompletion++;
- };*/
- _font = Resources.Load<Font>("Assets/Plugins/TextMesh Pro/Fonts/LiberationSans.ttf");
- // TODO generate larger textures with borders on them so it actually looks more like Techblox
- _blueBackground = new Texture2D(1, 1);
- _blueBackground.SetPixel(0, 0, new Color(0.004f, 0.522f, 0.847f) /* Gamecraft Blue, unused */);
- _blueBackground.Apply();
- _grayBackground = new Texture2D(1, 1);
- _grayBackground.SetPixel(0, 0, new Color(0.11f, 0.11f, 0.11f, 1f) /* Very dark gray */);
- _grayBackground.Apply();
- _whiteBackground = new Texture2D(1, 1);
- _whiteBackground.SetPixel(0, 0, new Color(0.961f, 0.961f, 0.961f) /* Very light gray */);
- _whiteBackground.Apply();
- _textInputBackground = new Texture2D(1, 1);
- _textInputBackground.SetPixel(0, 0, new Color(0.961f, 0.961f, 0.961f) /* Very light gray */);
- _textInputBackground.Apply();
- _areaBackground = new Texture2D(1, 1);
- _areaBackground.SetPixel(0, 0, new Color(0.051f, 0.051f, 0.051f, 1f) /* Very very dark gray */);
- _areaBackground.Apply();
- _defaultCompletion++;
- }
- }
- }
|