2020-07-13 03:27:19 +00:00
|
|
|
using TMPro;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
namespace ConformalDecals.UI {
|
|
|
|
[KSPAddon(KSPAddon.Startup.Instantly, true)]
|
|
|
|
public class UILoader : MonoBehaviour {
|
|
|
|
|
2020-10-07 07:12:15 +00:00
|
|
|
private static string _path;
|
2020-07-20 04:12:48 +00:00
|
|
|
private static GameObject _textEntryPrefab;
|
|
|
|
private static GameObject _fontMenuPrefab;
|
2020-07-21 07:07:18 +00:00
|
|
|
private static GameObject _colorPickerPrefab;
|
2020-07-20 04:12:48 +00:00
|
|
|
|
|
|
|
public static GameObject FontMenuPrefab => _fontMenuPrefab;
|
|
|
|
public static GameObject TextEntryPrefab => _textEntryPrefab;
|
2020-07-21 07:07:18 +00:00
|
|
|
public static GameObject ColorPickerPrefab => _colorPickerPrefab;
|
2020-07-13 03:27:19 +00:00
|
|
|
|
|
|
|
private void Awake() {
|
2020-10-07 07:12:15 +00:00
|
|
|
_path = KSPUtil.ApplicationRootPath + "GameData/ConformalDecals/Resources/";
|
|
|
|
var prefabs = AssetBundle.LoadFromFile(_path + "ui.conformaldecals");
|
2020-07-13 03:27:19 +00:00
|
|
|
|
2020-07-20 04:12:48 +00:00
|
|
|
_textEntryPrefab = prefabs.LoadAsset("TextEntryPanel") as GameObject;
|
|
|
|
_fontMenuPrefab = prefabs.LoadAsset("FontMenuPanel") as GameObject;
|
2020-07-21 07:07:18 +00:00
|
|
|
_colorPickerPrefab = prefabs.LoadAsset("ColorPickerPanel") as GameObject;
|
2020-07-16 01:12:50 +00:00
|
|
|
|
2020-07-20 04:12:48 +00:00
|
|
|
ProcessWindow(_textEntryPrefab);
|
|
|
|
ProcessWindow(_fontMenuPrefab);
|
2020-07-21 07:07:18 +00:00
|
|
|
ProcessWindow(_colorPickerPrefab);
|
2020-07-13 03:27:19 +00:00
|
|
|
}
|
2020-07-20 06:56:38 +00:00
|
|
|
|
2020-07-13 03:27:19 +00:00
|
|
|
private static void ProcessWindow(GameObject window) {
|
|
|
|
var skin = UISkinManager.defaultSkin;
|
|
|
|
var font = UISkinManager.TMPFont;
|
|
|
|
|
|
|
|
var background = window.GetComponent<Image>();
|
|
|
|
background.sprite = skin.window.normal.background;
|
|
|
|
background.type = Image.Type.Sliced;
|
|
|
|
|
|
|
|
var texts = window.GetComponentsInChildren<TextMeshProUGUI>(true);
|
|
|
|
|
|
|
|
foreach (var text in texts) {
|
|
|
|
ProcessText(text, font, Color.white);
|
|
|
|
}
|
|
|
|
|
|
|
|
var tags = window.GetComponentsInChildren<UITag>(true);
|
|
|
|
|
|
|
|
foreach (var tag in tags) {
|
|
|
|
switch (tag.type) {
|
|
|
|
case UITag.UIType.Window:
|
|
|
|
ProcessImage(tag.gameObject, skin.window);
|
|
|
|
break;
|
|
|
|
case UITag.UIType.Button:
|
|
|
|
ProcessSelectable(tag.gameObject, skin.button);
|
|
|
|
break;
|
|
|
|
case UITag.UIType.ButtonToggle:
|
2020-07-13 05:38:37 +00:00
|
|
|
ProcessToggleButton(tag.gameObject, skin.button);
|
|
|
|
break;
|
|
|
|
case UITag.UIType.RadioToggle:
|
2020-07-13 03:27:19 +00:00
|
|
|
ProcessSelectable(tag.gameObject, skin.toggle);
|
|
|
|
break;
|
2020-07-21 07:52:23 +00:00
|
|
|
case UITag.UIType.BoxSlider:
|
2020-07-13 03:27:19 +00:00
|
|
|
case UITag.UIType.Slider:
|
2020-07-24 00:49:42 +00:00
|
|
|
ProcessSlider(tag.gameObject, skin.horizontalSlider, skin.horizontalSliderThumb);
|
2020-07-13 03:27:19 +00:00
|
|
|
break;
|
|
|
|
case UITag.UIType.Box:
|
|
|
|
ProcessSelectable(tag.gameObject, skin.box);
|
|
|
|
break;
|
|
|
|
case UITag.UIType.Dropdown:
|
|
|
|
ProcessDropdown(tag.gameObject, skin.button, skin.window);
|
|
|
|
break;
|
|
|
|
case UITag.UIType.Label:
|
2020-07-20 06:56:38 +00:00
|
|
|
ProcessText(tag.GetComponent<TextMeshProUGUI>(), font, new Color(0.718f, 0.996f, 0.000f, 1.000f), 14);
|
2020-07-13 03:27:19 +00:00
|
|
|
break;
|
|
|
|
case UITag.UIType.Header:
|
|
|
|
ProcessText(tag.GetComponent<TextMeshProUGUI>(), font, new Color(0.718f, 0.996f, 0.000f, 1.000f), 16);
|
|
|
|
break;
|
2020-07-16 01:12:50 +00:00
|
|
|
}
|
2020-07-13 03:27:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ProcessImage(GameObject gameObject, UIStyle style) {
|
2020-07-23 05:37:16 +00:00
|
|
|
var image = gameObject.GetComponent<Image>();
|
|
|
|
if (image != null) {
|
|
|
|
ProcessImage(image, style.normal);
|
|
|
|
}
|
2020-07-13 05:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void ProcessImage(Image image, UIStyleState state) {
|
|
|
|
image.sprite = state.background;
|
|
|
|
image.color = Color.white;
|
2020-07-16 01:12:50 +00:00
|
|
|
image.type = Image.Type.Sliced;
|
2020-07-13 03:27:19 +00:00
|
|
|
}
|
2020-07-16 01:12:50 +00:00
|
|
|
|
2020-07-13 03:27:19 +00:00
|
|
|
private static void ProcessSelectable(GameObject gameObject, UIStyle style) {
|
|
|
|
var selectable = gameObject.GetComponent<Selectable>();
|
2020-07-21 07:52:23 +00:00
|
|
|
if (selectable == null) {
|
|
|
|
ProcessImage(gameObject, style);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ProcessImage(selectable.image, style.normal);
|
2020-07-13 03:27:19 +00:00
|
|
|
|
2020-07-21 07:52:23 +00:00
|
|
|
selectable.transition = Selectable.Transition.SpriteSwap;
|
2020-07-13 03:27:19 +00:00
|
|
|
|
2020-07-21 07:52:23 +00:00
|
|
|
var state = selectable.spriteState;
|
|
|
|
state.highlightedSprite = style.highlight.background;
|
2020-07-24 00:49:42 +00:00
|
|
|
state.selectedSprite = style.highlight.background;
|
2020-07-21 07:52:23 +00:00
|
|
|
state.pressedSprite = style.active.background;
|
|
|
|
state.disabledSprite = style.disabled.background;
|
|
|
|
selectable.spriteState = state;
|
|
|
|
}
|
2020-07-13 05:38:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void ProcessToggleButton(GameObject gameObject, UIStyle style) {
|
|
|
|
ProcessSelectable(gameObject, style);
|
|
|
|
|
|
|
|
var toggle = gameObject.GetComponent<Toggle>();
|
2020-07-21 07:52:23 +00:00
|
|
|
if (toggle != null) ProcessImage(toggle.graphic as Image, style.active);
|
2020-07-13 03:27:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 00:49:42 +00:00
|
|
|
private static void ProcessSlider(GameObject gameObject, UIStyle backgroundStyle, UIStyle thumbStyle) {
|
2020-07-13 03:27:19 +00:00
|
|
|
ProcessSelectable(gameObject, thumbStyle);
|
|
|
|
|
2020-07-23 05:37:16 +00:00
|
|
|
var background = gameObject.transform.Find("Background");
|
2020-07-24 00:49:42 +00:00
|
|
|
if (background != null) ProcessImage(background.gameObject, backgroundStyle);
|
2020-07-13 03:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void ProcessDropdown(GameObject gameObject, UIStyle buttonStyle, UIStyle windowStyle) {
|
|
|
|
ProcessSelectable(gameObject, buttonStyle);
|
|
|
|
|
|
|
|
var template = gameObject.transform.Find("Template").gameObject;
|
2020-07-21 07:52:23 +00:00
|
|
|
if (template != null) ProcessImage(template, windowStyle);
|
2020-07-13 03:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void ProcessText(TextMeshProUGUI text, TMP_FontAsset font, Color color, int size = -1) {
|
|
|
|
text.font = font;
|
|
|
|
text.color = color;
|
|
|
|
if (size > 0) text.fontSize = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|