diff --git a/Assets/Textures/UI.png b/Assets/Textures/UI.png new file mode 100644 index 0000000..3c1e63a Binary files /dev/null and b/Assets/Textures/UI.png differ diff --git a/GameData/ConformalDecals/Plugins/ConformalDecals.dll b/GameData/ConformalDecals/Plugins/ConformalDecals.dll index 0e163a2..ea74e88 100644 Binary files a/GameData/ConformalDecals/Plugins/ConformalDecals.dll and b/GameData/ConformalDecals/Plugins/ConformalDecals.dll differ diff --git a/GameData/ConformalDecals/Resources/conformaldecals.kspfont b/GameData/ConformalDecals/Resources/conformaldecals.kspfont new file mode 100644 index 0000000..94dc394 --- /dev/null +++ b/GameData/ConformalDecals/Resources/conformaldecals.kspfont @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c543a58c9bc1ea96e26ce85625ca1fd765f5f1067f0eeca8fbdf68a2fe7e66ab +size 139184 diff --git a/GameData/ConformalDecals/Resources/ui.conformaldecals b/GameData/ConformalDecals/Resources/ui.conformaldecals new file mode 100644 index 0000000..c5f886f Binary files /dev/null and b/GameData/ConformalDecals/Resources/ui.conformaldecals differ diff --git a/GameData/ConformalDecals/Resources/waukegan_ldo_extended_bold_sdf.kspfont b/GameData/ConformalDecals/Resources/waukegan_ldo_extended_bold_sdf.kspfont deleted file mode 100644 index 17ab165..0000000 Binary files a/GameData/ConformalDecals/Resources/waukegan_ldo_extended_bold_sdf.kspfont and /dev/null differ diff --git a/Source/ConformalDecals/ConformalDecals.csproj b/Source/ConformalDecals/ConformalDecals.csproj index 97909ce..067412d 100644 --- a/Source/ConformalDecals/ConformalDecals.csproj +++ b/Source/ConformalDecals/ConformalDecals.csproj @@ -52,9 +52,21 @@ dlls\UnityEngine.PhysicsModule.dll + + dlls\UnityEngine.TextCoreModule.dll + + + dlls\UnityEngine.TextRenderingModule.dll + dlls\UnityEngine.UI.dll + + dlls\UnityEngine.UIElementsModule.dll + + + dlls\UnityEngine.UIModule.dll + @@ -75,6 +87,8 @@ + + diff --git a/Source/ConformalDecals/ModuleConformalText.cs b/Source/ConformalDecals/ModuleConformalText.cs index eed4e0d..e805e9a 100644 --- a/Source/ConformalDecals/ModuleConformalText.cs +++ b/Source/ConformalDecals/ModuleConformalText.cs @@ -1,40 +1,34 @@ using ConformalDecals.Text; +using ConformalDecals.UI; using ConformalDecals.Util; using TMPro; using UnityEngine; +using UnityEngine.UI; namespace ConformalDecals { public class ModuleConformalText: ModuleConformalDecal { - private const string DefaultFlag = "Squad/Flags/default"; - [KSPField(isPersistant = true)] public string text = "Hello World!"; + private GameObject _textEntryGui; + public override void OnLoad(ConfigNode node) { base.OnLoad(node); - SetText(text); } public override void OnStart(StartState state) { base.OnStart(state); - SetText(text); } - private void SetText(string newText) { - this.Log("Rendering text for part"); - var fonts = Resources.FindObjectsOfTypeAll(); - - foreach (var font in fonts) { - this.Log($"Font: {font.name}"); - foreach (var fallback in font.fallbackFontAssets) { - this.Log($" Fallback: {fallback.name}"); - } + [KSPEvent(guiActive = false, guiActiveEditor = true, guiName = "#LOC_ConformalDecals_gui-select-flag")] + public void SetText() + { + if (_textEntryGui == null) { + _textEntryGui = Instantiate(UILoader.textEntryPrefab, MainCanvasUtil.MainCanvas.transform, true); + _textEntryGui.AddComponent(); + MenuNavigation.SpawnMenuNavigation(_textEntryGui, Navigation.Mode.Automatic, true); } - - //materialProperties.AddOrGetTextureProperty("_Decal", true).Texture = TextRenderer.RenderToTexture(fonts[0], newText); - - UpdateMaterials(); } } } \ No newline at end of file diff --git a/Source/ConformalDecals/Properties/AssemblyInfo.cs b/Source/ConformalDecals/Properties/AssemblyInfo.cs index 27895b0..3eab961 100644 --- a/Source/ConformalDecals/Properties/AssemblyInfo.cs +++ b/Source/ConformalDecals/Properties/AssemblyInfo.cs @@ -5,7 +5,7 @@ using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("Conformal Decals")] +[assembly: AssemblyTitle("ConformalDecals")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Cineboxandrew")] diff --git a/Source/ConformalDecals/UI/UILoader.cs b/Source/ConformalDecals/UI/UILoader.cs new file mode 100644 index 0000000..e0743a3 --- /dev/null +++ b/Source/ConformalDecals/UI/UILoader.cs @@ -0,0 +1,128 @@ +using System; +using TMPro; +using UnityEngine; +using UnityEngine.UI; + +namespace ConformalDecals.UI { + [KSPAddon(KSPAddon.Startup.Instantly, true)] + public class UILoader : MonoBehaviour { + private static readonly string Path = KSPUtil.ApplicationRootPath + "GameData/ConformalDecals/Resources/"; + + public static GameObject textEntryPrefab; + + private void Awake() { + var prefabs = AssetBundle.LoadFromFile(Path + "ui.conformaldecals"); + + textEntryPrefab = prefabs.LoadAsset("TextEntryPanel") as GameObject; + + ProcessWindow(textEntryPrefab); + + Debug.Log("[ConformalDecals] UI prefabs loaded and modified"); + } + + private static void ProcessWindow(GameObject window) { + var skin = UISkinManager.defaultSkin; + var font = UISkinManager.TMPFont; + + var background = window.GetComponent(); + background.sprite = skin.window.normal.background; + background.type = Image.Type.Sliced; + + var texts = window.GetComponentsInChildren(true); + + foreach (var text in texts) { + ProcessText(text, font, Color.white); + } + + var tags = window.GetComponentsInChildren(true); + + foreach (var tag in tags) { + Debug.Log($"Handling object ${tag.gameObject.name}"); + 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: + ProcessSelectable(tag.gameObject, skin.toggle); + break; + case UITag.UIType.Slider: + ProcessSlider(tag.gameObject, skin.horizontalScrollbar, skin.horizontalScrollbarThumb, skin.verticalScrollbar, skin.verticalScrollbarThumb); + 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: + ProcessText(tag.GetComponent(), font, Color.white, 14); + break; + case UITag.UIType.Header: + ProcessText(tag.GetComponent(), font, new Color(0.718f, 0.996f, 0.000f, 1.000f), 16); + break; + } + } + } + + private static void ProcessImage(GameObject gameObject, UIStyle style) { + var image = gameObject.GetComponent(); + if (image == null) throw new FormatException("No Image component present"); + + image.sprite = style.normal.background; + image.type = Image.Type.Sliced; + } + + private static void ProcessSelectable(GameObject gameObject, UIStyle style) { + var selectable = gameObject.GetComponent(); + if (selectable == null) throw new FormatException("No Selectable component present"); + + ProcessImage(gameObject, style); + + selectable.transition = Selectable.Transition.SpriteSwap; + + var state = selectable.spriteState; + state.highlightedSprite = style.highlight.background; + state.pressedSprite = style.active.background; + state.disabledSprite = style.disabled.background; + } + + private static void ProcessSlider(GameObject gameObject, UIStyle horizontalStyle, UIStyle horizontalThumbStyle, UIStyle verticalStyle, UIStyle verticalThumbStyle) { + var slider = gameObject.GetComponent(); + if (slider == null) throw new FormatException("No Slider component present"); + + UIStyle sliderStyle; + UIStyle thumbStyle; + if (slider.direction == Slider.Direction.BottomToTop || slider.direction == Slider.Direction.TopToBottom) { + sliderStyle = verticalStyle; + thumbStyle = verticalThumbStyle; + } + else { + sliderStyle = horizontalStyle; + thumbStyle = horizontalThumbStyle; + } + + ProcessSelectable(gameObject, thumbStyle); + + var back = gameObject.GetComponentInChildren(); + + back.sprite = sliderStyle.normal.background; + back.type = Image.Type.Sliced; + } + + private static void ProcessDropdown(GameObject gameObject, UIStyle buttonStyle, UIStyle windowStyle) { + ProcessSelectable(gameObject, buttonStyle); + + var template = gameObject.transform.Find("Template").gameObject; + ProcessImage(template, windowStyle); + } + + 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; + } + } +} \ No newline at end of file diff --git a/Source/ConformalDecals/UI/UITag.cs b/Source/ConformalDecals/UI/UITag.cs new file mode 100644 index 0000000..ecc2397 --- /dev/null +++ b/Source/ConformalDecals/UI/UITag.cs @@ -0,0 +1,19 @@ +using UnityEngine; + +namespace ConformalDecals.UI { + public class UITag : MonoBehaviour { + public enum UIType { + None, + Window, + Box, + Button, + ButtonToggle, + Slider, + Dropdown, + Label, + Header + } + + [SerializeField] public UIType type = UIType.None; + } +} \ No newline at end of file