From 5e76fd7cd4b7b224e8a73bfc3be41330fbd2299d Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Wed, 15 Jul 2020 18:12:50 -0700 Subject: [PATCH] Add basic ui controller --- Assets/Shaders/DecalsCommon.cginc | 1 - Assets/Shaders/HSL.cginc | 11 ++ Assets/Shaders/HSLSlider.shader | 99 +++++++++++++++++ Assets/Shaders/HSLSquare.shader | 102 +++++++++++++++++ Assets/Textures/UI.png | 3 - .../Plugins/ConformalDecals.dll | 4 +- .../Resources/ui.conformaldecals | 4 +- Source/ConformalDecals/ConformalDecals.csproj | 2 +- Source/ConformalDecals/Text/FormattedText.cs | 15 +++ Source/ConformalDecals/Text/TextSettings.cs | 5 - .../ConformalDecals/UI/TextEntryController.cs | 103 +++++++++++++++++- Source/ConformalDecals/UI/UILoader.cs | 25 +++-- 12 files changed, 350 insertions(+), 24 deletions(-) create mode 100644 Assets/Shaders/HSL.cginc create mode 100644 Assets/Shaders/HSLSlider.shader create mode 100644 Assets/Shaders/HSLSquare.shader delete mode 100644 Assets/Textures/UI.png create mode 100644 Source/ConformalDecals/Text/FormattedText.cs delete mode 100644 Source/ConformalDecals/Text/TextSettings.cs diff --git a/Assets/Shaders/DecalsCommon.cginc b/Assets/Shaders/DecalsCommon.cginc index 878541d..3d9f7b0 100644 --- a/Assets/Shaders/DecalsCommon.cginc +++ b/Assets/Shaders/DecalsCommon.cginc @@ -157,7 +157,6 @@ inline float SDFAA(float dist) { float ddist = length(float2(ddx(dist), ddy(dist))); float pixelDist = dist / ddist; return saturate(0.5-pixelDist); - return saturate(0.5 - dist); } #endif \ No newline at end of file diff --git a/Assets/Shaders/HSL.cginc b/Assets/Shaders/HSL.cginc new file mode 100644 index 0000000..4118ba5 --- /dev/null +++ b/Assets/Shaders/HSL.cginc @@ -0,0 +1,11 @@ +#ifndef HSL_INCLUDED +#define HSL_INCLUDED + +inline float3 HSL2RGB(float3 hsl) { + int3 n = int3(0, 8, 4); + float3 k = (n + hsl.x * 12) % 12; + float a = hsl.y * min(hsl.z, 1 - hsl.z); + return hsl.z - a * max(-1, min(k - 3, min(9 - k, 1))); +} + +#endif \ No newline at end of file diff --git a/Assets/Shaders/HSLSlider.shader b/Assets/Shaders/HSLSlider.shader new file mode 100644 index 0000000..90e0bdb --- /dev/null +++ b/Assets/Shaders/HSLSlider.shader @@ -0,0 +1,99 @@ +Shader "ConformalDecals/UI/HSLSlider" +{ + Properties + { + _StencilComp ("Stencil Comparison", Float) = 8 + _Stencil ("Stencil ID", Float) = 0 + _StencilOp ("Stencil Operation", Float) = 0 + _StencilWriteMask ("Stencil Write Mask", Float) = 255 + _StencilReadMask ("Stencil Read Mask", Float) = 255 + + _ColorMask ("Color Mask", Float) = 15 + + [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0 + } + SubShader + { + Tags + { + "Queue"="Transparent" + "IgnoreProjector"="True" + "RenderType"="Transparent" + "PreviewType"="Plane" + "CanUseSpriteAtlas"="True" + } + Stencil + { + Ref [_Stencil] + Comp [_StencilComp] + Pass [_StencilOp] + ReadMask [_StencilReadMask] + WriteMask [_StencilWriteMask] + } + + Cull Off + Lighting Off + ZWrite Off + ZTest [unity_GUIZTestMode] + Blend SrcAlpha OneMinusSrcAlpha + ColorMask [_ColorMask] + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + #pragma require integers + + #include "UnityCG.cginc" + #include "UnityUI.cginc" + #include "HSL.cginc" + + #pragma multi_compile_local _ UNITY_UI_CLIP_RECT + #pragma multi_compile_local _ UNITY_UI_ALPHACLIP + + float4 _ClipRect; + + struct appdata + { + float4 vertex : POSITION; + float2 uv : TEXCOORD0; + }; + + struct v2f + { + float4 vertex : SV_POSITION; + float2 uv : TEXCOORD0; + float4 worldPosition : TEXCOORD1; + }; + + v2f vert (appdata v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.worldPosition = v.vertex; + o.uv = v.uv; + return o; + } + + fixed4 frag (v2f i) : SV_Target + { + // sample the texture + fixed4 color = 1; + color.rgb = HSL2RGB(float3(i.uv.y, 1, 0.5)); + + #ifdef UNITY_UI_CLIP_RECT + color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect); + #endif + + #ifdef UNITY_UI_ALPHACLIP + clip (color.a - 0.001); + #endif + + return color; + } + ENDCG + } + } +} \ No newline at end of file diff --git a/Assets/Shaders/HSLSquare.shader b/Assets/Shaders/HSLSquare.shader new file mode 100644 index 0000000..71327b4 --- /dev/null +++ b/Assets/Shaders/HSLSquare.shader @@ -0,0 +1,102 @@ +Shader "ConformalDecals/UI/HSLSquare" +{ + Properties + { + _Hue("Hue", Range(0,1)) = 0 + + _StencilComp ("Stencil Comparison", Float) = 8 + _Stencil ("Stencil ID", Float) = 0 + _StencilOp ("Stencil Operation", Float) = 0 + _StencilWriteMask ("Stencil Write Mask", Float) = 255 + _StencilReadMask ("Stencil Read Mask", Float) = 255 + + _ColorMask ("Color Mask", Float) = 15 + + [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0 + } + SubShader + { + Tags + { + "Queue"="Transparent" + "IgnoreProjector"="True" + "RenderType"="Transparent" + "PreviewType"="Plane" + "CanUseSpriteAtlas"="True" + } + Stencil + { + Ref [_Stencil] + Comp [_StencilComp] + Pass [_StencilOp] + ReadMask [_StencilReadMask] + WriteMask [_StencilWriteMask] + } + + Cull Off + Lighting Off + ZWrite Off + ZTest [unity_GUIZTestMode] + Blend SrcAlpha OneMinusSrcAlpha + ColorMask [_ColorMask] + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + #pragma require integers + + #include "UnityCG.cginc" + #include "UnityUI.cginc" + #include "HSL.cginc" + + #pragma multi_compile_local _ UNITY_UI_CLIP_RECT + #pragma multi_compile_local _ UNITY_UI_ALPHACLIP + + float _Hue; + float4 _ClipRect; + + struct appdata + { + float4 vertex : POSITION; + float2 uv : TEXCOORD0; + }; + + struct v2f + { + float4 vertex : SV_POSITION; + float2 uv : TEXCOORD0; + float4 worldPosition : TEXCOORD1; + }; + + v2f vert (appdata v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.worldPosition = v.vertex; + o.uv = v.uv; + return o; + } + + fixed4 frag (v2f i) : SV_Target + { + // sample the texture + fixed4 color = 1; + color.rgb = HSL2RGB(float3(_Hue, i.uv.x, i.uv.y)); + + #ifdef UNITY_UI_CLIP_RECT + color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect); + #endif + + #ifdef UNITY_UI_ALPHACLIP + clip (color.a - 0.001); + #endif + + return color; + } + ENDCG + } + } +} \ No newline at end of file diff --git a/Assets/Textures/UI.png b/Assets/Textures/UI.png deleted file mode 100644 index 72970e1..0000000 --- a/Assets/Textures/UI.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:9f62ac0ea2530eb82c0efa1ab15d434ba44741f707f79e63d197c70f4c319442 -size 8585 diff --git a/GameData/ConformalDecals/Plugins/ConformalDecals.dll b/GameData/ConformalDecals/Plugins/ConformalDecals.dll index 6d613d4..9df802e 100644 --- a/GameData/ConformalDecals/Plugins/ConformalDecals.dll +++ b/GameData/ConformalDecals/Plugins/ConformalDecals.dll @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:dc76280eebfaa6bfe954ea3e6aba95a91650fe5f60ba66a8ee5a9b6863e327fb -size 55808 +oid sha256:76514255b9119b836839251e18f77331fc083036812b6ee82e17891a1d1aeed9 +size 56832 diff --git a/GameData/ConformalDecals/Resources/ui.conformaldecals b/GameData/ConformalDecals/Resources/ui.conformaldecals index fe86c07..ad933b1 100644 --- a/GameData/ConformalDecals/Resources/ui.conformaldecals +++ b/GameData/ConformalDecals/Resources/ui.conformaldecals @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c3ec32bf0dd2571ed80f8e12e05b06591f35364e4d926b0dec6f543c7323d4df -size 267232 +oid sha256:438fb5934b8f81a704af64f974015f27dc2e9a5f490ef3890e78f63dd2b20dc6 +size 264787 diff --git a/Source/ConformalDecals/ConformalDecals.csproj b/Source/ConformalDecals/ConformalDecals.csproj index c66ad9a..2c7ba10 100644 --- a/Source/ConformalDecals/ConformalDecals.csproj +++ b/Source/ConformalDecals/ConformalDecals.csproj @@ -85,7 +85,7 @@ - + diff --git a/Source/ConformalDecals/Text/FormattedText.cs b/Source/ConformalDecals/Text/FormattedText.cs new file mode 100644 index 0000000..e69ab2c --- /dev/null +++ b/Source/ConformalDecals/Text/FormattedText.cs @@ -0,0 +1,15 @@ +using TMPro; +using UnityEngine; + +namespace ConformalDecals.Text { + public struct FormattedText { + public string text; + public TMP_FontAsset font; + public FontStyles style; + public bool vertical; + + public Color32 color; + public Color32 outlineColor; + public float outlineWidth; + } +} \ No newline at end of file diff --git a/Source/ConformalDecals/Text/TextSettings.cs b/Source/ConformalDecals/Text/TextSettings.cs deleted file mode 100644 index 366c02f..0000000 --- a/Source/ConformalDecals/Text/TextSettings.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace ConformalDecals.Text { - public struct TextSettings { - - } -} \ No newline at end of file diff --git a/Source/ConformalDecals/UI/TextEntryController.cs b/Source/ConformalDecals/UI/TextEntryController.cs index 4d4c04b..115e4c8 100644 --- a/Source/ConformalDecals/UI/TextEntryController.cs +++ b/Source/ConformalDecals/UI/TextEntryController.cs @@ -1,9 +1,108 @@ +using System; +using ConformalDecals.Text; +using TMPro; using UnityEngine; +using UnityEngine.UI; namespace ConformalDecals.UI { public class TextEntryController : MonoBehaviour { - public void OnClose() { - Debug.Log("Close!"); + private FormattedText _text; + + [SerializeField] private Selectable _textBox; + [SerializeField] private Toggle _fontColorButton; + [SerializeField] private Toggle _fontButton; + [SerializeField] private Toggle _outlineColorButton; + [SerializeField] private Slider _outlineWidthSlider; + + [SerializeField] private Toggle _boldButton; + [SerializeField] private Toggle _italicButton; + [SerializeField] private Toggle _underlineButton; + [SerializeField] private Toggle _smallCapsButton; + [SerializeField] private Toggle _verticalButton; + + public delegate void TextUpdateReceiver(FormattedText text); + + public delegate void TextCancelReceiver(); + + public TextUpdateReceiver textUpdateCallback; + public TextCancelReceiver textCancelCallback; + + private void Start() { + (_textBox as TMP_InputField).text = _text.text; + + _boldButton.isOn = (_text.style | FontStyles.Bold) != 0; + _italicButton.isOn = (_text.style | FontStyles.Italic) != 0; + _underlineButton.isOn = (_text.style | FontStyles.Underline) != 0; + _smallCapsButton.isOn = (_text.style | FontStyles.SmallCaps) != 0; + _verticalButton.isOn = _text.vertical; + } + + public void Close() { + Destroy(gameObject); + } + + public void OnCancel() { + textCancelCallback(); + Close(); + } + + public void OnApply() { + textUpdateCallback(_text); + Close(); + } + + public void OnTextUpdate(string text) { + _text.text = text; + textUpdateCallback(_text); + + } + + public void OnFontMenu(bool state) { } + public void OnColorMenu(bool state) { } + + public void OnOutlineColorMenu(bool state) { } + + public void OnOutlineUpdate(float value) { + _text.outlineWidth = value; + textUpdateCallback(_text); + + } + + public void OnBoldUpdate(bool state) { + if (state) _text.style |= FontStyles.Bold; + else _text.style &= ~FontStyles.Bold; + + textUpdateCallback(_text); + + } + + public void OnItalicUpdate(bool state) { + if (state) _text.style |= FontStyles.Italic; + else _text.style &= ~FontStyles.Italic; + + textUpdateCallback(_text); + + } + + public void OnUnderlineUpdate(bool state) { + if (state) _text.style |= FontStyles.Underline; + else _text.style &= ~FontStyles.Underline; + + textUpdateCallback(_text); + + } + + public void OnSmallCapsUpdate(bool state) { + if (state) _text.style |= FontStyles.SmallCaps; + else _text.style &= ~FontStyles.SmallCaps; + + textUpdateCallback(_text); + + } + + public void OnVerticalUpdate(bool state) { + _text.vertical = state; + textUpdateCallback(_text); } } } \ No newline at end of file diff --git a/Source/ConformalDecals/UI/UILoader.cs b/Source/ConformalDecals/UI/UILoader.cs index 81ef217..aa6fb8c 100644 --- a/Source/ConformalDecals/UI/UILoader.cs +++ b/Source/ConformalDecals/UI/UILoader.cs @@ -14,11 +14,21 @@ namespace ConformalDecals.UI { var prefabs = AssetBundle.LoadFromFile(Path + "ui.conformaldecals"); textEntryPrefab = prefabs.LoadAsset("TextEntryPanel") as GameObject; - + ProcessWindow(textEntryPrefab); Debug.Log("[ConformalDecals] UI prefabs loaded and modified"); - + Debug.Log($"[ConformalDecals] {MainCanvasUtil.MainCanvas.renderMode}"); + Debug.Log($"[ConformalDecals] {MainCanvasUtil.MainCanvas.sortingOrder}"); + Debug.Log($"[ConformalDecals] {MainCanvasUtil.MainCanvas.sortingLayerID}"); + Debug.Log($"[ConformalDecals] {MainCanvasUtil.MainCanvas.sortingLayerName}"); + foreach (var layer in SortingLayer.layers) { + Debug.Log(layer.name); + Debug.Log(layer.id); + Debug.Log(layer.value); + } + + var window = Instantiate(UILoader.textEntryPrefab, MainCanvasUtil.MainCanvas.transform, true); } @@ -54,7 +64,7 @@ namespace ConformalDecals.UI { ProcessSelectable(tag.gameObject, skin.toggle); break; case UITag.UIType.Slider: - ProcessSlider(tag.gameObject, skin.horizontalScrollbar, skin.horizontalScrollbarThumb, skin.verticalScrollbar, skin.verticalScrollbarThumb); + ProcessSlider(tag.gameObject, skin.horizontalSlider, skin.horizontalSliderThumb, skin.verticalSlider, skin.verticalSliderThumb); break; case UITag.UIType.Box: ProcessSelectable(tag.gameObject, skin.box); @@ -68,7 +78,7 @@ namespace ConformalDecals.UI { case UITag.UIType.Header: ProcessText(tag.GetComponent(), font, new Color(0.718f, 0.996f, 0.000f, 1.000f), 16); break; - } + } } } @@ -79,13 +89,13 @@ namespace ConformalDecals.UI { private static void ProcessImage(Image image, UIStyleState state) { image.sprite = state.background; image.color = Color.white; - image.type = Image.Type.Sliced; + 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(selectable.image, style.normal); selectable.transition = Selectable.Transition.SpriteSwap; @@ -94,7 +104,6 @@ namespace ConformalDecals.UI { state.highlightedSprite = style.highlight.background; state.pressedSprite = style.active.background; state.disabledSprite = style.disabled.background; - selectable.spriteState = state; }