Add basic ui controller

feature-better-tweakables
Andrew Cassidy 4 years ago
parent 66dd0a6206
commit 1316dbb553

@ -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

@ -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

@ -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
}
}
}

@ -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
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.4 KiB

@ -85,7 +85,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Text\FontLoader.cs" />
<Compile Include="Text\TextRenderer.cs" />
<Compile Include="Text\TextSettings.cs" />
<Compile Include="Text\FormattedText.cs" />
<Compile Include="Test\TestLayers.cs" />
<Compile Include="UI\TextEntryController.cs" />
<Compile Include="UI\UILoader.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;
}
}

@ -1,5 +0,0 @@
namespace ConformalDecals.Text {
public struct TextSettings {
}
}

@ -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);
}
}
}

@ -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<TextMeshProUGUI>(), 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<Selectable>();
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;
}

Loading…
Cancel
Save