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.

Constants.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Reflection;
  6. using GamecraftModdingAPI.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 GamecraftModdingAPI.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 Gamecraft'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 _riffic = 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 (_riffic == null) return GUI.skin;
  47. // build GUISkin
  48. GUISkin gui = ScriptableObject.CreateInstance<GUISkin>();
  49. gui.font = _riffic;
  50. gui.settings.selectionColor = Color.white;
  51. gui.settings.tripleClickSelectsLine = true;
  52. // set properties off all UI elements
  53. foreach (PropertyInfo p in typeof(GUISkin).GetProperties())
  54. {
  55. // for a "scriptable" GUI system, it's ironic there's no better way to do this
  56. if (p.GetValue(gui) is GUIStyle style)
  57. {
  58. style.richText = true;
  59. style.alignment = TextAnchor.MiddleCenter;
  60. style.fontSize = 30;
  61. style.wordWrap = true;
  62. style.border = new RectOffset(4, 4, 4, 4);
  63. style.margin = new RectOffset(4, 4, 4, 4);
  64. style.padding = new RectOffset(4, 4, 4, 4);
  65. // normal state
  66. style.normal.background = _blueBackground;
  67. style.normal.textColor = Color.white;
  68. // hover state
  69. style.hover.background = _grayBackground;
  70. style.hover.textColor = Color.white;
  71. // focused
  72. style.focused.background = _grayBackground;
  73. style.focused.textColor = Color.white;
  74. // clicking state
  75. style.active.background = _whiteBackground;
  76. style.active.textColor = Color.white;
  77. p.SetValue(gui, style); // probably unnecessary
  78. }
  79. }
  80. // set element-specific styles
  81. // label
  82. gui.label.normal.background = null;
  83. gui.label.hover.background = null;
  84. gui.label.focused.background = null;
  85. gui.label.active.background = null;
  86. // text input
  87. gui.textField.normal.background = _textInputBackground;
  88. gui.textField.hover.background = _textInputBackground;
  89. gui.textField.focused.background = _textInputBackground;
  90. gui.textField.active.background = _textInputBackground;
  91. // text area
  92. gui.textArea.normal.background = _textInputBackground;
  93. gui.textArea.hover.background = _textInputBackground;
  94. gui.textArea.focused.background = _textInputBackground;
  95. gui.textArea.active.background = _textInputBackground;
  96. // window
  97. gui.window.normal.background = _areaBackground;
  98. gui.window.hover.background = _areaBackground;
  99. gui.window.focused.background = _areaBackground;
  100. gui.window.active.background = _areaBackground;
  101. // box (also used by layout groups & areas)
  102. gui.box.normal.background = _areaBackground;
  103. gui.box.hover.background = _areaBackground;
  104. gui.box.focused.background = _areaBackground;
  105. gui.box.active.background = _areaBackground;
  106. return gui;
  107. }
  108. private static void LoadGUIAssets()
  109. {
  110. AsyncOperationHandle<Font> rifficHandle = Addressables.LoadAssetAsync<Font>("Assets/Art/Fonts/riffic-bold.ttf");
  111. rifficHandle.Completed += handle =>
  112. {
  113. _riffic = handle.Result;
  114. _defaultCompletion++;
  115. };
  116. _blueBackground = new Texture2D(1, 1);
  117. _blueBackground.SetPixel(0, 0, new Color(0.004f, 0.522f, 0.847f) /* Gamecraft Blue */);
  118. _blueBackground.Apply();
  119. _grayBackground = new Texture2D(1, 1);
  120. _grayBackground.SetPixel(0, 0, new Color(0.745f, 0.745f, 0.745f) /* Gray */);
  121. _grayBackground.Apply();
  122. _whiteBackground = new Texture2D(1, 1);
  123. _whiteBackground.SetPixel(0, 0, new Color(0.898f, 0.898f, 0.898f) /* Very light gray */);
  124. _whiteBackground.Apply();
  125. _textInputBackground = new Texture2D(1, 1);
  126. _textInputBackground.SetPixel(0, 0, new Color(0f, 0f, 0f, 0.25f) /* Translucent gray */);
  127. _textInputBackground.Apply();
  128. _areaBackground = new Texture2D(1, 1);
  129. _areaBackground.SetPixel(0, 0, new Color(0f, 0f, 0f, 0.25f) /* Translucent gray */);
  130. _areaBackground.Apply();
  131. /* // this is actually gray (used for the loading screen)
  132. AsyncOperationHandle<Texture2D> backgroundHandle =
  133. Addressables.LoadAssetAsync<Texture2D>("Assets/Art/Textures/UI/FrontEndMap/RCX_Blue_Background_5k.jpg");
  134. backgroundHandle.Completed += handle =>
  135. {
  136. _blueBackground = handle.Result;
  137. _defaultCompletion++;
  138. };*/
  139. _defaultCompletion++;
  140. }
  141. }
  142. }