A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
6.1KB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Reflection;
  6. using TechbloxModdingAPI.Utility;
  7. using HarmonyLib;
  8. using Svelto.Tasks;
  9. using Svelto.Tasks.Lean;
  10. using UnityEngine;
  11. using UnityEngine.AddressableAssets;
  12. using UnityEngine.ResourceManagement.AsyncOperations;
  13. namespace TechbloxModdingAPI.Interface.IMGUI
  14. {
  15. /// <summary>
  16. /// Convenient IMGUI values.
  17. /// </summary>
  18. public static class Constants
  19. {
  20. private static byte _defaultCompletion = 0;
  21. private static GUISkin _default = null;
  22. /// <summary>
  23. /// Best-effort imitation of Techblox's UI style.
  24. /// </summary>
  25. public static GUISkin Default
  26. {
  27. get
  28. {
  29. if (_defaultCompletion != 0) _default = BuildDefaultGUISkin();
  30. return _default;
  31. }
  32. }
  33. private static Font _font = null;
  34. private static Texture2D _blueBackground = null;
  35. private static Texture2D _grayBackground = null;
  36. private static Texture2D _whiteBackground = null;
  37. private static Texture2D _textInputBackground = null;
  38. private static Texture2D _areaBackground = null;
  39. internal static void Init()
  40. {
  41. LoadGUIAssets();
  42. }
  43. private static GUISkin BuildDefaultGUISkin()
  44. {
  45. _defaultCompletion = 0;
  46. //if (_font == null) return GUI.skin;
  47. // build GUISkin
  48. GUISkin gui = ScriptableObject.CreateInstance<GUISkin>();
  49. gui.font = _font;
  50. gui.settings.selectionColor = Color.white;
  51. gui.settings.tripleClickSelectsLine = true;
  52. Color textColour = new Color(0.706f, 0.706f, 0.706f);
  53. // set properties off all UI elements
  54. foreach (PropertyInfo p in typeof(GUISkin).GetProperties())
  55. {
  56. // for a "scriptable" GUI system, it's ironic there's no better way to do this
  57. if (p.GetValue(gui) is GUIStyle style)
  58. {
  59. style.richText = true;
  60. style.alignment = TextAnchor.MiddleCenter;
  61. style.fontSize = 16;
  62. style.wordWrap = true;
  63. style.border = new RectOffset(4, 4, 4, 4);
  64. style.margin = new RectOffset(4, 4, 4, 4);
  65. style.padding = new RectOffset(4, 4, 4, 4);
  66. // normal state
  67. style.normal.background = _grayBackground;
  68. style.normal.textColor = textColour;
  69. // hover state
  70. style.hover.background = _grayBackground;
  71. style.hover.textColor = textColour;
  72. // focused
  73. style.focused.background = _grayBackground;
  74. style.focused.textColor = textColour;
  75. // clicking state
  76. style.active.background = _whiteBackground;
  77. style.active.textColor = textColour;
  78. p.SetValue(gui, style); // probably unnecessary
  79. }
  80. }
  81. // set element-specific styles
  82. // label
  83. gui.label.fontSize = 20;
  84. gui.label.normal.background = null;
  85. gui.label.hover.background = null;
  86. gui.label.focused.background = null;
  87. gui.label.active.background = null;
  88. // text input
  89. gui.textField.normal.background = _textInputBackground;
  90. gui.textField.hover.background = _textInputBackground;
  91. gui.textField.focused.background = _textInputBackground;
  92. gui.textField.active.background = _textInputBackground;
  93. // text area
  94. gui.textArea.normal.background = _textInputBackground;
  95. gui.textArea.hover.background = _textInputBackground;
  96. gui.textArea.focused.background = _textInputBackground;
  97. gui.textArea.active.background = _textInputBackground;
  98. // window
  99. gui.window.normal.background = _areaBackground;
  100. gui.window.hover.background = _areaBackground;
  101. gui.window.focused.background = _areaBackground;
  102. gui.window.active.background = _areaBackground;
  103. // box (also used by layout groups & areas)
  104. gui.box.normal.background = _areaBackground;
  105. gui.box.hover.background = _areaBackground;
  106. gui.box.focused.background = _areaBackground;
  107. gui.box.active.background = _areaBackground;
  108. return gui;
  109. }
  110. private static void LoadGUIAssets()
  111. {
  112. /*AsyncOperationHandle<Font> fontHandle = Addressables.LoadAssetAsync<Font>("Assets/Plugins/TextMesh Pro/Fonts/LiberationSans.ttf");
  113. fontHandle.Completed += handle =>
  114. {
  115. _font = handle.Result;
  116. _defaultCompletion++;
  117. };*/
  118. _font = Resources.Load<Font>("Assets/Plugins/TextMesh Pro/Fonts/LiberationSans.ttf");
  119. // TODO generate larger textures with borders on them so it actually looks more like Techblox
  120. _blueBackground = new Texture2D(1, 1);
  121. _blueBackground.SetPixel(0, 0, new Color(0.004f, 0.522f, 0.847f) /* Gamecraft Blue, unused */);
  122. _blueBackground.Apply();
  123. _grayBackground = new Texture2D(1, 1);
  124. _grayBackground.SetPixel(0, 0, new Color(0.11f, 0.11f, 0.11f, 1f) /* Very dark gray */);
  125. _grayBackground.Apply();
  126. _whiteBackground = new Texture2D(1, 1);
  127. _whiteBackground.SetPixel(0, 0, new Color(0.961f, 0.961f, 0.961f) /* Very light gray */);
  128. _whiteBackground.Apply();
  129. _textInputBackground = new Texture2D(1, 1);
  130. _textInputBackground.SetPixel(0, 0, new Color(0.961f, 0.961f, 0.961f) /* Very light gray */);
  131. _textInputBackground.Apply();
  132. _areaBackground = new Texture2D(1, 1);
  133. _areaBackground.SetPixel(0, 0, new Color(0.051f, 0.051f, 0.051f, 1f) /* Very very dark gray */);
  134. _areaBackground.Apply();
  135. _defaultCompletion++;
  136. }
  137. }
  138. }