mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
UI experiments
This commit is contained in:
parent
a9b05a677e
commit
4a3569a7be
BIN
Assets/Textures/UI.png
Normal file
BIN
Assets/Textures/UI.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.4 KiB |
Binary file not shown.
@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:c543a58c9bc1ea96e26ce85625ca1fd765f5f1067f0eeca8fbdf68a2fe7e66ab
|
||||
size 139184
|
BIN
GameData/ConformalDecals/Resources/ui.conformaldecals
Normal file
BIN
GameData/ConformalDecals/Resources/ui.conformaldecals
Normal file
Binary file not shown.
Binary file not shown.
@ -52,9 +52,21 @@
|
||||
<Reference Include="UnityEngine.PhysicsModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>dlls\UnityEngine.PhysicsModule.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.TextCoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>dlls\UnityEngine.TextCoreModule.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.TextRenderingModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>dlls\UnityEngine.TextRenderingModule.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>dlls\UnityEngine.UI.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.UIElementsModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>dlls\UnityEngine.UIElementsModule.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.UIModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||
<HintPath>dlls\UnityEngine.UIModule.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DecalConfig.cs" />
|
||||
@ -75,6 +87,8 @@
|
||||
<Compile Include="Text\TextRenderer.cs" />
|
||||
<Compile Include="Text\TextSettings.cs" />
|
||||
<Compile Include="Test\TestLayers.cs" />
|
||||
<Compile Include="UI\UILoader.cs" />
|
||||
<Compile Include="UI\UITag.cs" />
|
||||
<Compile Include="Util\Logging.cs" />
|
||||
<Compile Include="Util\OrientedBounds.cs" />
|
||||
<Compile Include="Util\TextureUtils.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<TMP_FontAsset>();
|
||||
|
||||
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<DragPanel>();
|
||||
MenuNavigation.SpawnMenuNavigation(_textEntryGui, Navigation.Mode.Automatic, true);
|
||||
}
|
||||
|
||||
//materialProperties.AddOrGetTextureProperty("_Decal", true).Texture = TextRenderer.RenderToTexture(fonts[0], newText);
|
||||
|
||||
UpdateMaterials();
|
||||
}
|
||||
}
|
||||
}
|
@ -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")]
|
||||
|
128
Source/ConformalDecals/UI/UILoader.cs
Normal file
128
Source/ConformalDecals/UI/UILoader.cs
Normal file
@ -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<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) {
|
||||
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<TextMeshProUGUI>(), font, Color.white, 14);
|
||||
break;
|
||||
case UITag.UIType.Header:
|
||||
ProcessText(tag.GetComponent<TextMeshProUGUI>(), 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<Image>();
|
||||
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<Selectable>();
|
||||
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<Slider>();
|
||||
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<Image>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
19
Source/ConformalDecals/UI/UITag.cs
Normal file
19
Source/ConformalDecals/UI/UITag.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user