using System;
using UnityEngine.UI;
namespace UnityEngine.Rendering.UI
{
///
/// DebugUIHandler for enumerator widget.
///
public class DebugUIHandlerEnumField : DebugUIHandlerWidget
{
/// Text displayed for the "next" button.
public Text nextButtonText;
/// Text displayed for the "previous" button.
public Text previousButtonText;
/// Name of the enum field.
public Text nameLabel;
/// Value of the enum field.
public Text valueLabel;
internal protected DebugUI.EnumField m_Field;
internal override void SetWidget(DebugUI.Widget widget)
{
base.SetWidget(widget);
m_Field = CastWidget();
nameLabel.text = m_Field.displayName;
UpdateValueLabel();
}
///
/// OnSelection implementation.
///
/// True if the selection wrapped around.
/// Previous widget.
/// State of the widget.
public override bool OnSelection(bool fromNext, DebugUIHandlerWidget previous)
{
if (nextButtonText != null)
nextButtonText.color = colorSelected;
if (previousButtonText != null)
previousButtonText.color = colorSelected;
nameLabel.color = colorSelected;
valueLabel.color = colorSelected;
return true;
}
///
/// OnDeselection implementation.
///
public override void OnDeselection()
{
if (nextButtonText != null)
nextButtonText.color = colorDefault;
if (previousButtonText != null)
previousButtonText.color = colorDefault;
nameLabel.color = colorDefault;
valueLabel.color = colorDefault;
}
///
/// OnAction implementation.
///
public override void OnAction()
{
OnIncrement(false);
}
///
/// OnIncrement implementation.
///
/// True if incrementing fast.
public override void OnIncrement(bool fast)
{
if (m_Field.enumValues.Length == 0)
return;
var array = m_Field.enumValues;
int index = m_Field.currentIndex;
if (index == array.Length - 1)
{
index = 0;
}
else
{
if (fast)
{
//check if quickSeparators have not been constructed
//it is the case when not constructed with autoenum
var separators = m_Field.quickSeparators;
if (separators == null)
{
m_Field.InitQuickSeparators();
separators = m_Field.quickSeparators;
}
int idxSup = 0;
for (; idxSup < separators.Length && index + 1 > separators[idxSup]; ++idxSup) ;
if (idxSup == separators.Length)
{
index = 0;
}
else
{
index = separators[idxSup];
}
}
else
{
index += 1;
}
}
m_Field.SetValue(array[index]);
m_Field.currentIndex = index;
UpdateValueLabel();
}
///
/// OnDecrement implementation.
///
/// Trye if decrementing fast.
public override void OnDecrement(bool fast)
{
if (m_Field.enumValues.Length == 0)
return;
var array = m_Field.enumValues;
int index = m_Field.currentIndex;
if (index == 0)
{
if (fast)
{
//check if quickSeparators have not been constructed
//it is thecase when not constructed with autoenum
var separators = m_Field.quickSeparators;
if (separators == null)
{
m_Field.InitQuickSeparators();
separators = m_Field.quickSeparators;
}
index = separators[separators.Length - 1];
}
else
{
index = array.Length - 1;
}
}
else
{
if (fast)
{
//check if quickSeparators have not been constructed
//it is the case when not constructed with autoenum
var separators = m_Field.quickSeparators;
if (separators == null)
{
m_Field.InitQuickSeparators();
separators = m_Field.quickSeparators;
}
int idxInf = separators.Length - 1;
for (; idxInf > 0 && index <= separators[idxInf]; --idxInf) ;
index = separators[idxInf];
}
else
{
index -= 1;
}
}
m_Field.SetValue(array[index]);
m_Field.currentIndex = index;
UpdateValueLabel();
}
///
/// Update the label of the widget.
///
protected virtual void UpdateValueLabel()
{
int index = m_Field.currentIndex;
// Fallback just in case, we may be handling sub/sectionned enums here
if (index < 0)
index = 0;
string text = m_Field.enumNames[index].text;
// The UI implementation is tight with space, so let's just truncate the string here if too long.
const int maxLength = 26;
if (text.Length > maxLength)
{
text = text.Substring(0, maxLength - 3) + "...";
}
valueLabel.text = text;
}
}
}