namespace UIWidgets
{
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// Base class for the lines drawer.
///
[RequireComponent(typeof(RectTransform))]
[RequireComponent(typeof(Graphic))]
public abstract class LinesDrawerBase : UV1Effect
{
///
/// IDs of shader properties.
///
protected struct SnapGridShaderIDs : IEquatable
{
///
/// Line color ID.
///
public readonly int LineColor;
///
/// Line thickness ID.
///
public readonly int LineThickness;
///
/// Resolution X ID.
///
public readonly int ResolutionX;
///
/// Resolution Y ID.
///
public readonly int ResolutionY;
///
/// Horizontal lines count ID.
///
public readonly int HorizontalLinesCount;
///
/// Horizontal lines ID.
///
public readonly int HorizontalLines;
///
/// Vertical lines count ID.
///
public readonly int VerticalLinesCount;
///
/// Vertical lines ID.
///
public readonly int VerticalLines;
///
/// Transparent ID.
///
public readonly int Transparent;
private SnapGridShaderIDs(
int lineColor,
int lineThickness,
int resolutionX,
int resolutionY,
int horizontalLinesCount,
int horizontalLines,
int verticalLinesCount,
int verticalLines,
int transparent)
{
LineColor = lineColor;
LineThickness = lineThickness;
ResolutionX = resolutionX;
ResolutionY = resolutionY;
HorizontalLinesCount = horizontalLinesCount;
HorizontalLines = horizontalLines;
VerticalLinesCount = verticalLinesCount;
VerticalLines = verticalLines;
Transparent = transparent;
}
///
/// Get RingShaderIDs instance.
///
public static SnapGridShaderIDs Instance
{
get
{
return new SnapGridShaderIDs(
Shader.PropertyToID("_LineColor"),
Shader.PropertyToID("_LineThickness"),
Shader.PropertyToID("_ResolutionX"),
Shader.PropertyToID("_ResolutionY"),
Shader.PropertyToID("_HLinesCount"),
Shader.PropertyToID("_HLines"),
Shader.PropertyToID("_VLinesCount"),
Shader.PropertyToID("_VLines"),
Shader.PropertyToID("_Transparent"));
}
}
///
/// Determines whether the specified object is equal to the current object.
///
/// The object to compare with the current object.
/// true if the specified object is equal to the current object; otherwise, false.
public override bool Equals(object obj)
{
if (obj is SnapGridShaderIDs)
{
return Equals((SnapGridShaderIDs)obj);
}
return false;
}
///
/// Determines whether the specified object is equal to the current object.
///
/// The object to compare with the current object.
/// true if the specified object is equal to the current object; otherwise, false.
public bool Equals(SnapGridShaderIDs other)
{
return LineColor == other.LineColor
&& LineThickness == other.LineThickness
&& ResolutionX == other.ResolutionX
&& ResolutionY == other.ResolutionY
&& HorizontalLinesCount == other.HorizontalLinesCount
&& HorizontalLines == other.HorizontalLines
&& VerticalLinesCount == other.VerticalLinesCount
&& VerticalLines == other.VerticalLines
&& Transparent == other.Transparent;
}
///
/// Hash function.
///
/// A hash code for the current object.
public override int GetHashCode()
{
return LineColor ^ LineThickness ^ ResolutionX ^ ResolutionY ^ HorizontalLinesCount ^ HorizontalLines ^ VerticalLinesCount ^ VerticalLines ^ Transparent;
}
///
/// Compare specified instances.
///
/// Left instance.
/// Right instances.
/// true if the instances are equal; otherwise, false.
public static bool operator ==(SnapGridShaderIDs left, SnapGridShaderIDs right)
{
return left.Equals(right);
}
///
/// Compare specified instances.
///
/// Left instance.
/// Right instances.
/// true if the instances are now equal; otherwise, false.
public static bool operator !=(SnapGridShaderIDs left, SnapGridShaderIDs right)
{
return !left.Equals(right);
}
}
[SerializeField]
Color lineColor = Color.black;
///
/// Line color.
///
public Color LineColor
{
get
{
return lineColor;
}
set
{
if (lineColor != value)
{
lineColor = value;
UpdateMaterial();
}
}
}
[SerializeField]
float lineThickness = 1f;
///
/// Line thickness.
///
public float LineThickness
{
get
{
return lineThickness;
}
set
{
if (lineThickness != value)
{
lineThickness = value;
UpdateMaterial();
}
}
}
[SerializeField]
[Tooltip("Make the background transparent.")]
bool transparentBackground = false;
///
/// Make the background transparent.
///
public bool TransparentBackground
{
get
{
return transparentBackground;
}
set
{
if (transparentBackground != value)
{
transparentBackground = value;
UpdateMaterial();
}
}
}
///
/// Horizontal lines.
///
protected List HorizontalLines = new List();
///
/// Vertical lines.
///
protected List VerticalLines = new List();
///
/// Shader horizontal lines.
///
protected List ShaderHorizontalLines = new List(200);
///
/// Shader vertical lines.
///
protected List ShaderVerticalLines = new List(200);
///
/// Max lines count. Should match with shader setting.
///
protected int MaxLinesCount = 200;
///
/// Ring shader ids.
///
protected static SnapGridShaderIDs ShaderIDs = SnapGridShaderIDs.Instance;
///
protected override void OnEnable()
{
base.OnEnable();
Mode = UVMode.One;
UpdateLines();
}
///
/// Update lines.
///
protected virtual void UpdateLines()
{
UpdateMaterial();
}
///
/// Set material properties.
///
protected override void SetMaterialProperties()
{
if (EffectMaterial != null)
{
var size = RectTransform.rect.size;
UpdateShaderLines(HorizontalLines, ShaderHorizontalLines);
UpdateShaderLines(VerticalLines, ShaderVerticalLines);
EffectMaterial.SetColor(ShaderIDs.LineColor, lineColor);
EffectMaterial.SetFloat(ShaderIDs.LineThickness, lineThickness);
EffectMaterial.SetFloat(ShaderIDs.ResolutionX, size.x);
EffectMaterial.SetFloat(ShaderIDs.ResolutionY, size.y);
EffectMaterial.SetFloat(ShaderIDs.Transparent, transparentBackground ? 1 : 0);
EffectMaterial.SetInt(ShaderIDs.HorizontalLinesCount, HorizontalLines.Count);
EffectMaterial.SetFloatArray(ShaderIDs.HorizontalLines, ShaderHorizontalLines);
EffectMaterial.SetInt(ShaderIDs.VerticalLinesCount, VerticalLines.Count);
EffectMaterial.SetFloatArray(ShaderIDs.VerticalLines, ShaderVerticalLines);
}
}
///
/// Update shader lines.
///
/// Lines.
/// Shader lines.
protected void UpdateShaderLines(List lines, List shaderLines)
{
shaderLines.Clear();
shaderLines.AddRange(lines);
for (int i = shaderLines.Count; i < MaxLinesCount; i++)
{
shaderLines.Add(0);
}
}
}
}