浏览代码
Renamed debug menu classes for consistency
Renamed debug menu classes for consistency
- DebugMenuItem => DebugItem - DebugMenu => DebugPanel/Branch_Batching2
Julien Ignace
8 年前
当前提交
6fd2dc66
共有 19 个文件被更改,包括 921 次插入 和 927 次删除
-
24Assets/ScriptableRenderPipeline/Core/Debugging/DebugActionManager.cs
-
110Assets/ScriptableRenderPipeline/Core/Debugging/DebugItemHandler.cs
-
129Assets/ScriptableRenderPipeline/Core/Debugging/DebugMenuManager.cs
-
32Assets/ScriptableRenderPipeline/Core/Debugging/DebugMenuState.cs
-
6Assets/ScriptableRenderPipeline/Core/Debugging/DebugMenuUI.cs
-
10Assets/ScriptableRenderPipeline/Core/Debugging/DebugMenuUpdater.cs
-
16Assets/ScriptableRenderPipeline/Core/Debugging/Editor/DebugMenuEditor.cs
-
2Assets/ScriptableRenderPipeline/Core/Debugging/Serialization/DebugItemStateBool.cs
-
2Assets/ScriptableRenderPipeline/Core/Debugging/Serialization/DebugItemStateColor.cs
-
2Assets/ScriptableRenderPipeline/Core/Debugging/Serialization/DebugItemStateFloat.cs
-
2Assets/ScriptableRenderPipeline/Core/Debugging/Serialization/DebugItemStateInt.cs
-
2Assets/ScriptableRenderPipeline/Core/Debugging/Serialization/DebugItemStateUInt.cs
-
16Assets/ScriptableRenderPipeline/HDRenderPipeline/Debug/DebugDisplay.cs
-
460Assets/ScriptableRenderPipeline/Core/Debugging/DebugItemUI.cs
-
281Assets/ScriptableRenderPipeline/Core/Debugging/DebugPanel.cs
-
294Assets/ScriptableRenderPipeline/Core/Debugging/DebugMenu.cs
-
460Assets/ScriptableRenderPipeline/Core/Debugging/DebugMenuItemUI.cs
-
0/Assets/ScriptableRenderPipeline/Core/Debugging/DebugPanel.cs.meta
-
0/Assets/ScriptableRenderPipeline/Core/Debugging/DebugItemUI.cs.meta
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering |
|||
{ |
|||
// Class that users need to extend for runtime debug menu item UI
|
|||
public abstract class DebugItemUI |
|||
{ |
|||
protected GameObject m_Root = null; |
|||
protected DebugItem m_DebugItem = null; |
|||
|
|||
public bool dynamicDisplay { get { return m_DebugItem.dynamicDisplay; } } |
|||
|
|||
protected DebugItemUI(DebugItem debugItem) |
|||
{ |
|||
m_DebugItem = debugItem; |
|||
} |
|||
|
|||
// Implement for selection specific beahavior (like changing color for example)
|
|||
public abstract void SetSelected(bool value); |
|||
// Implement behavior when user execute the "Validate" action
|
|||
public abstract void OnValidate(); |
|||
// Implement behavior when user execute the "Increment" action
|
|||
public abstract void OnIncrement(); |
|||
// Implement behavior when user execute the "Decrement" action
|
|||
public abstract void OnDecrement(); |
|||
// Implement this method to update the UI with current item value.
|
|||
// User must call it whenever Validate/Increment/Decrement is called. It will also be automatically called for dynamically displayed items.
|
|||
public abstract void Update(); |
|||
} |
|||
|
|||
public class DebugSimpleItemUI : DebugItemUI |
|||
{ |
|||
protected GameObject m_Name = null; |
|||
protected GameObject m_Value = null; |
|||
|
|||
protected DebugSimpleItemUI(GameObject parent, DebugItem debugItem, string name) |
|||
: base(debugItem) |
|||
{ |
|||
|
|||
m_Root = DebugMenuUI.CreateHorizontalLayoutGroup("", true, true, false, false, parent); |
|||
m_Name = DebugMenuUI.CreateTextElement(m_DebugItem.name, name, 10, TextAnchor.MiddleLeft, m_Root); |
|||
var layoutElem = m_Name.AddComponent<UI.LayoutElement>(); |
|||
layoutElem.minWidth = DebugMenuUI.kDebugItemNameWidth; |
|||
m_Value = DebugMenuUI.CreateTextElement(string.Format("{0} value", name), "", 10, TextAnchor.MiddleLeft, m_Root); |
|||
} |
|||
|
|||
public override void SetSelected(bool value) |
|||
{ |
|||
m_Name.GetComponent<UI.Text>().color = value ? DebugMenuUI.kColorSelected : DebugMenuUI.kColorUnSelected; |
|||
m_Value.GetComponent<UI.Text>().color = value ? DebugMenuUI.kColorSelected : DebugMenuUI.kColorUnSelected; |
|||
} |
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
} |
|||
|
|||
public class DebugBoolItemUI : DebugSimpleItemUI |
|||
{ |
|||
public DebugBoolItemUI(GameObject parent, DebugItem debugItem, string name) |
|||
: base(parent, debugItem, name) |
|||
{ |
|||
Update(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
m_Value.GetComponent<UI.Text>().text = (bool)m_DebugItem.GetValue() ? "True" : "False"; |
|||
} |
|||
|
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
m_DebugItem.SetValue(!(bool)m_DebugItem.GetValue()); |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
OnValidate(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
OnValidate(); |
|||
} |
|||
} |
|||
|
|||
public class DebugFloatItemUI : DebugSimpleItemUI |
|||
{ |
|||
bool m_SelectIncrementMode = false; |
|||
int m_CurrentIncrementIndex = -1; |
|||
|
|||
public DebugFloatItemUI(GameObject parent, DebugItem debugItem, string name) |
|||
: base(parent, debugItem, name) |
|||
{ |
|||
Update(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
float currentValue = (float)m_DebugItem.GetValue(); |
|||
bool isNegative = currentValue < 0.0f; |
|||
// Easier to format the string without caring about the '-' sign. We add it back at the end
|
|||
currentValue = Mathf.Abs(currentValue); |
|||
|
|||
char separator = System.Convert.ToChar(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); |
|||
|
|||
// Start with the maximum amount of trailing zeros
|
|||
string valueWithMaxDecimals = string.Format("{0:0.00000}", currentValue); |
|||
|
|||
// Remove trailing zeros until we reach the separator or we reach the decimal we are currently editing.
|
|||
int separatorIndex = valueWithMaxDecimals.LastIndexOf(separator); |
|||
int index = valueWithMaxDecimals.Length - 1; |
|||
while ( |
|||
valueWithMaxDecimals[index] == '0' // Remove trailing zeros
|
|||
&& index > (separatorIndex + 1) // until we reach the separator
|
|||
&& index > (System.Math.Abs(m_CurrentIncrementIndex) + separatorIndex)) // Or it's the index of the current decimal being edited (so as to display the last trailing zero in this case)
|
|||
{ |
|||
index--; |
|||
} |
|||
|
|||
string finalValue = new string(valueWithMaxDecimals.ToCharArray(), 0, index + 1); |
|||
|
|||
// Add leading zeros until we reach where the current order is being edited.
|
|||
if(m_CurrentIncrementIndex > 0) |
|||
{ |
|||
float incrementValue = Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex); |
|||
if(incrementValue > currentValue) |
|||
{ |
|||
float compareValue = currentValue + 1.0f; // Start at 1.0f because we know that we are going to increment by 10 or more
|
|||
while (incrementValue > compareValue) |
|||
{ |
|||
finalValue = finalValue.Insert(0, "0"); |
|||
compareValue *= 10.0f; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// When selecting which decimal/order you want to edit, we show brackets around the figure to show the user.
|
|||
if(m_SelectIncrementMode) |
|||
{ |
|||
separatorIndex = finalValue.LastIndexOf(separator); // separator may have changed place if we added leading zeros
|
|||
int bracketIndex = separatorIndex - m_CurrentIncrementIndex; |
|||
if(m_CurrentIncrementIndex >= 0) // Skip separator
|
|||
bracketIndex -= 1; |
|||
|
|||
finalValue = finalValue.Insert(bracketIndex, "["); |
|||
finalValue = finalValue.Insert(bracketIndex + 2, "]"); |
|||
} |
|||
|
|||
if(isNegative) |
|||
finalValue = finalValue.Insert(0, "-"); |
|||
|
|||
m_Value.GetComponent<UI.Text>().text = finalValue; |
|||
} |
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
m_SelectIncrementMode = !m_SelectIncrementMode; |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
if(!m_SelectIncrementMode) |
|||
{ |
|||
m_DebugItem.SetValue((float)m_DebugItem.GetValue() + Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex)); |
|||
} |
|||
else |
|||
{ |
|||
m_CurrentIncrementIndex -= 1; // * 0.1 (10^m_CurrentIncrementIndex)
|
|||
m_CurrentIncrementIndex = System.Math.Max(-5, m_CurrentIncrementIndex); |
|||
} |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
if (!m_SelectIncrementMode) |
|||
{ |
|||
m_DebugItem.SetValue((float)m_DebugItem.GetValue() - Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex)); |
|||
} |
|||
else |
|||
{ |
|||
m_CurrentIncrementIndex += 1; // * 10 (10^m_CurrentIncrementIndex)
|
|||
} |
|||
Update(); |
|||
} |
|||
} |
|||
|
|||
// Everything is done with int. We don't really care about values > 2b for debugging.
|
|||
public class DebugIntegerItemUI : DebugSimpleItemUI |
|||
{ |
|||
bool m_SelectIncrementMode = false; |
|||
int m_CurrentIncrementIndex = 0; |
|||
|
|||
public DebugIntegerItemUI(GameObject parent, DebugItem debugItem, string name) |
|||
: base(parent, debugItem, name) |
|||
{ |
|||
} |
|||
|
|||
protected void UpdateText(int value) |
|||
{ |
|||
bool isNegative = value < 0f; |
|||
// Easier to format the string without caring about the '-' sign. We add it back at the end
|
|||
value = System.Math.Abs(value); |
|||
|
|||
string finalValue = string.Format("{0}", value); |
|||
|
|||
// Add leading zeros until we reach where the current order is being edited.
|
|||
if(m_CurrentIncrementIndex > 0) |
|||
{ |
|||
int incrementValue = (int)System.Math.Pow(10, m_CurrentIncrementIndex); |
|||
if(incrementValue > value) |
|||
{ |
|||
int compareValue = System.Math.Max(value, 1); |
|||
while (incrementValue > compareValue) |
|||
{ |
|||
finalValue = finalValue.Insert(0, "0"); |
|||
compareValue *= 10; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// When selecting which decimal/order you want to edit, we show brackets around the figure to show the user.
|
|||
if(m_SelectIncrementMode) |
|||
{ |
|||
int bracketIndex = finalValue.Length - 1 - m_CurrentIncrementIndex; |
|||
|
|||
finalValue = finalValue.Insert(bracketIndex, "["); |
|||
finalValue = finalValue.Insert(bracketIndex + 2, "]"); |
|||
} |
|||
|
|||
if(isNegative) |
|||
finalValue = finalValue.Insert(0, "-"); |
|||
|
|||
m_Value.GetComponent<UI.Text>().text = finalValue; |
|||
} |
|||
|
|||
protected virtual int GetIntegerValue() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
protected virtual void SetIntegerValue(int value) |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
UpdateText(GetIntegerValue()); |
|||
} |
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
m_SelectIncrementMode = !m_SelectIncrementMode; |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
if (!m_SelectIncrementMode) |
|||
{ |
|||
SetIntegerValue(GetIntegerValue() + (int)Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex)); |
|||
} |
|||
else |
|||
{ |
|||
m_CurrentIncrementIndex -= 1; // *= 0.1 (10^m_CurrentIncrementIndex)
|
|||
m_CurrentIncrementIndex = System.Math.Max(0, m_CurrentIncrementIndex); |
|||
} |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
if (!m_SelectIncrementMode) |
|||
{ |
|||
SetIntegerValue(GetIntegerValue() - (int)Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex)); |
|||
} |
|||
else |
|||
{ |
|||
m_CurrentIncrementIndex += 1; // *= 10 (10^m_CurrentIncrementIndex)
|
|||
m_CurrentIncrementIndex = System.Math.Max(0, m_CurrentIncrementIndex); |
|||
} |
|||
Update(); |
|||
} |
|||
} |
|||
public class DebugIntItemUI : DebugIntegerItemUI |
|||
{ |
|||
public DebugIntItemUI(GameObject parent, DebugItem debugItem, string name) |
|||
: base(parent, debugItem, name) |
|||
{ |
|||
UpdateText((int)m_DebugItem.GetValue()); |
|||
} |
|||
|
|||
protected override int GetIntegerValue() |
|||
{ |
|||
return (int)m_DebugItem.GetValue(); |
|||
} |
|||
|
|||
protected override void SetIntegerValue(int value) |
|||
{ |
|||
m_DebugItem.SetValue(value); |
|||
} |
|||
} |
|||
|
|||
public class DebugUIntItemUI : DebugIntegerItemUI |
|||
{ |
|||
public DebugUIntItemUI(GameObject parent, DebugItem debugItem, string name) |
|||
: base(parent, debugItem, name) |
|||
{ |
|||
UpdateText((int)(uint)m_DebugItem.GetValue()); |
|||
} |
|||
|
|||
protected override int GetIntegerValue() |
|||
{ |
|||
return (int)(uint)m_DebugItem.GetValue(); |
|||
} |
|||
|
|||
protected override void SetIntegerValue(int value) |
|||
{ |
|||
m_DebugItem.SetValue((uint)System.Math.Max(0, value)); |
|||
} |
|||
} |
|||
|
|||
public class DebugEnumItemUI : DebugSimpleItemUI |
|||
{ |
|||
int m_CurrentValueIndex = 0; |
|||
GUIContent[] m_ValueNames; |
|||
int[] m_Values; |
|||
|
|||
public DebugEnumItemUI(GameObject parent, DebugItem debugItem, string name, GUIContent[] valueNames, int[] values) |
|||
: base(parent, debugItem, name) |
|||
{ |
|||
m_Values = values; |
|||
m_ValueNames = valueNames; |
|||
m_CurrentValueIndex = FindIndexForValue((int)m_DebugItem.GetValue()); |
|||
|
|||
Update(); |
|||
} |
|||
|
|||
private int FindIndexForValue(int value) |
|||
{ |
|||
for(int i = 0 ; i < m_Values.Length ; ++i) |
|||
{ |
|||
if (m_Values[i] == value) |
|||
return i; |
|||
} |
|||
|
|||
return -1; |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
if(m_CurrentValueIndex != -1) |
|||
{ |
|||
m_Value.GetComponent<UI.Text>().text = m_ValueNames[m_CurrentValueIndex].text; |
|||
} |
|||
} |
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
OnIncrement(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
m_CurrentValueIndex = (m_CurrentValueIndex + 1) % m_Values.Length; |
|||
m_DebugItem.SetValue(m_Values[m_CurrentValueIndex]); |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
m_CurrentValueIndex -= 1; |
|||
if (m_CurrentValueIndex < 0) |
|||
m_CurrentValueIndex = m_Values.Length - 1; |
|||
|
|||
m_DebugItem.SetValue(m_Values[m_CurrentValueIndex]); |
|||
Update(); |
|||
} |
|||
} |
|||
|
|||
public class DebugColorItemUI : DebugItemUI |
|||
{ |
|||
protected GameObject m_Name = null; |
|||
protected GameObject m_ColorRect = null; |
|||
|
|||
public DebugColorItemUI(GameObject parent, DebugItem debugItem, string name) |
|||
: base(debugItem) |
|||
{ |
|||
m_DebugItem = debugItem; |
|||
|
|||
m_Root = DebugMenuUI.CreateHorizontalLayoutGroup(name, true, true, false, false, parent); |
|||
|
|||
m_Name = DebugMenuUI.CreateTextElement(m_DebugItem.name, name, 10, TextAnchor.MiddleLeft, m_Root); |
|||
var layoutElemName = m_Name.AddComponent<UI.LayoutElement>(); |
|||
layoutElemName.minWidth = DebugMenuUI.kDebugItemNameWidth; |
|||
|
|||
// Force layout because we need the right height for the color rect element afterward.
|
|||
UI.LayoutRebuilder.ForceRebuildLayoutImmediate(m_Root.GetComponent<RectTransform>()); |
|||
RectTransform nameRect = m_Name.GetComponent<RectTransform>(); |
|||
|
|||
m_ColorRect = new GameObject(); |
|||
m_ColorRect.transform.SetParent(m_Root.transform, false); |
|||
m_ColorRect.AddComponent<UI.Image>(); |
|||
UI.LayoutElement layoutElem = m_ColorRect.AddComponent<UI.LayoutElement>(); |
|||
// We need to set min width/height because without an image, the size would be zero.
|
|||
layoutElem.minHeight = nameRect.rect.height; |
|||
layoutElem.minWidth = 40.0f; |
|||
|
|||
Update(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
Color currentValue = (Color)m_DebugItem.GetValue(); |
|||
UI.Image image = m_ColorRect.GetComponent<UI.Image>(); |
|||
image.color = currentValue; |
|||
} |
|||
|
|||
public override void SetSelected(bool value) |
|||
{ |
|||
m_Name.GetComponent<UI.Text>().color = value ? DebugMenuUI.kColorSelected : DebugMenuUI.kColorUnSelected; |
|||
} |
|||
|
|||
// TODO: Edit mode!
|
|||
public override void OnValidate() |
|||
{ |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
} |
|||
} |
|||
} |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using System; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering |
|||
{ |
|||
public class DebugItem |
|||
{ |
|||
public Type type { get { return m_Type; } } |
|||
public string name { get { return m_Name; } } |
|||
public DebugItemHandler handler { get { return m_Handler; } } |
|||
public bool dynamicDisplay { get { return m_DynamicDisplay; } } |
|||
public bool readOnly { get { return m_Setter == null; } } |
|||
|
|||
public DebugItem(string name, Type type, Func<object> getter, Action<object> setter, bool dynamicDisplay = false, DebugItemHandler handler = null) |
|||
{ |
|||
m_Type = type; |
|||
m_Setter = setter; |
|||
m_Getter = getter; |
|||
m_Name = name; |
|||
m_Handler = handler; |
|||
m_DynamicDisplay = dynamicDisplay; |
|||
} |
|||
|
|||
public Type GetItemType() |
|||
{ |
|||
return m_Type; |
|||
} |
|||
|
|||
public void SetValue(object value, bool record = true) |
|||
{ |
|||
// Setter can be null for readonly items
|
|||
if(m_Setter != null) |
|||
{ |
|||
m_Setter(value); |
|||
m_Handler.ClampValues(m_Getter, m_Setter); |
|||
|
|||
// Update state for serialization/undo
|
|||
if(record) |
|||
m_State.SetValue(m_Getter()); |
|||
} |
|||
} |
|||
|
|||
public object GetValue() |
|||
{ |
|||
return m_Getter(); |
|||
} |
|||
|
|||
public void SetDebugItemState(DebugItemState state) |
|||
{ |
|||
m_State = state; |
|||
} |
|||
|
|||
Func<object> m_Getter; |
|||
Action<object> m_Setter; |
|||
Type m_Type; |
|||
string m_Name; |
|||
DebugItemHandler m_Handler = null; |
|||
bool m_DynamicDisplay = false; |
|||
DebugItemState m_State = null; |
|||
} |
|||
|
|||
public class DebugPanel |
|||
{ |
|||
public string name { get { return m_Name; } } |
|||
public int itemCount { get { return m_Items.Count; } } |
|||
|
|||
protected string m_Name = "Unknown Debug Menu"; |
|||
private GameObject m_Root = null; |
|||
private List<DebugItem> m_Items = new List<DebugItem>(); |
|||
private List<DebugItemUI> m_ItemsUI = new List<DebugItemUI>(); |
|||
private int m_SelectedItem = -1; |
|||
|
|||
public DebugPanel(string name) |
|||
{ |
|||
m_Name = name; |
|||
} |
|||
|
|||
public DebugItem GetDebugItem(int index) |
|||
{ |
|||
if (index >= m_Items.Count || index < 0) |
|||
return null; |
|||
return m_Items[index]; |
|||
} |
|||
|
|||
public DebugItem GetDebugItem(string name) |
|||
{ |
|||
return m_Items.Find(x => x.name == name); |
|||
} |
|||
|
|||
public DebugItem GetSelectedDebugItem() |
|||
{ |
|||
if(m_SelectedItem != -1) |
|||
{ |
|||
return m_Items[m_SelectedItem]; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
public bool HasDebugItem(DebugItem debugItem) |
|||
{ |
|||
foreach(var item in m_Items) |
|||
{ |
|||
if (debugItem == item) |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public void RemoveDebugItem(DebugItem debugItem) |
|||
{ |
|||
m_Items.Remove(debugItem); |
|||
RebuildGUI(); |
|||
} |
|||
|
|||
public void AddDebugItem(DebugItem debugItem) |
|||
{ |
|||
m_Items.Add(debugItem); |
|||
RebuildGUI(); |
|||
} |
|||
|
|||
// TODO: Move this to UI classes
|
|||
public GameObject BuildGUI(GameObject parent) |
|||
{ |
|||
m_Root = new GameObject(string.Format("{0}", m_Name)); |
|||
m_Root.transform.SetParent(parent.transform); |
|||
m_Root.transform.localPosition = Vector3.zero; |
|||
m_Root.transform.localScale = Vector3.one; |
|||
|
|||
UI.VerticalLayoutGroup menuVL = m_Root.AddComponent<UI.VerticalLayoutGroup>(); |
|||
menuVL.spacing = 5.0f; |
|||
menuVL.childControlWidth = true; |
|||
menuVL.childControlHeight = true; |
|||
menuVL.childForceExpandWidth = true; |
|||
menuVL.childForceExpandHeight = false; |
|||
|
|||
RectTransform menuVLRectTransform = m_Root.GetComponent<RectTransform>(); |
|||
menuVLRectTransform.pivot = new Vector2(0.0f, 0.0f); |
|||
menuVLRectTransform.localPosition = new Vector3(0.0f, 0.0f); |
|||
menuVLRectTransform.anchorMin = new Vector2(0.0f, 0.0f); |
|||
menuVLRectTransform.anchorMax = new Vector2(1.0f, 1.0f); |
|||
|
|||
RebuildGUI(); |
|||
|
|||
m_Root.SetActive(false); |
|||
return m_Root; |
|||
} |
|||
|
|||
private void RebuildGUI() |
|||
{ |
|||
m_Root.transform.DetachChildren(); |
|||
|
|||
DebugMenuUI.CreateTextElement(string.Format("{0} Title", m_Name), m_Name, 14, TextAnchor.MiddleLeft, m_Root); |
|||
|
|||
m_ItemsUI.Clear(); |
|||
foreach (DebugItem debugItem in m_Items) |
|||
{ |
|||
DebugItemHandler handler = debugItem.handler; // Should never be null, we have at least the default handler
|
|||
m_ItemsUI.Add(handler.BuildGUI(m_Root)); |
|||
} |
|||
} |
|||
|
|||
|
|||
void SetSelectedItem(int index) |
|||
{ |
|||
if(m_SelectedItem != -1) |
|||
{ |
|||
m_ItemsUI[m_SelectedItem].SetSelected(false); |
|||
} |
|||
|
|||
m_SelectedItem = index; |
|||
m_ItemsUI[m_SelectedItem].SetSelected(true); |
|||
} |
|||
|
|||
public void SetSelected(bool value) |
|||
{ |
|||
m_Root.SetActive(value); |
|||
|
|||
if(value) |
|||
{ |
|||
if (m_SelectedItem == -1) |
|||
{ |
|||
NextItem(); |
|||
} |
|||
else |
|||
SetSelectedItem(m_SelectedItem); |
|||
} |
|||
} |
|||
|
|||
void NextItem() |
|||
{ |
|||
if(m_Items.Count != 0) |
|||
{ |
|||
int newSelected = (m_SelectedItem + 1) % m_Items.Count; |
|||
SetSelectedItem(newSelected); |
|||
} |
|||
} |
|||
|
|||
void PreviousItem() |
|||
{ |
|||
if(m_Items.Count != 0) |
|||
{ |
|||
int newSelected = m_SelectedItem - 1; |
|||
if (newSelected == -1) |
|||
newSelected = m_Items.Count - 1; |
|||
SetSelectedItem(newSelected); |
|||
} |
|||
} |
|||
|
|||
public void OnMoveHorizontal(float value) |
|||
{ |
|||
if(m_SelectedItem != -1 && !m_Items[m_SelectedItem].readOnly) |
|||
{ |
|||
if (value > 0.0f) |
|||
m_ItemsUI[m_SelectedItem].OnIncrement(); |
|||
else |
|||
m_ItemsUI[m_SelectedItem].OnDecrement(); |
|||
} |
|||
} |
|||
|
|||
public void OnMoveVertical(float value) |
|||
{ |
|||
if (value > 0.0f) |
|||
PreviousItem(); |
|||
else |
|||
NextItem(); |
|||
} |
|||
|
|||
public void OnValidate() |
|||
{ |
|||
if (m_SelectedItem != -1 && !m_Items[m_SelectedItem].readOnly) |
|||
m_ItemsUI[m_SelectedItem].OnValidate(); |
|||
} |
|||
|
|||
public void AddDebugItem<ItemType>(string name, Func<object> getter, Action<object> setter, bool dynamicDisplay = false, DebugItemHandler handler = null) |
|||
{ |
|||
if (handler == null) |
|||
handler = new DefaultDebugItemHandler(); |
|||
DebugItem newItem = new DebugItem(name, typeof(ItemType), getter, setter, dynamicDisplay, handler); |
|||
handler.SetDebugItem(newItem); |
|||
m_Items.Add(newItem); |
|||
|
|||
DebugMenuManager dmm = DebugMenuManager.instance; |
|||
DebugItemState itemState = dmm.FindDebugItemState(name, m_Name); |
|||
if(itemState == null) |
|||
{ |
|||
itemState = handler.CreateDebugItemState(); |
|||
itemState.Initialize(name, m_Name); |
|||
itemState.SetValue(getter()); |
|||
dmm.AddDebugItemState(itemState); |
|||
} |
|||
|
|||
newItem.SetDebugItemState(itemState); |
|||
} |
|||
|
|||
public void Update() |
|||
{ |
|||
// Can happen if the debug menu has been disabled (all UI is destroyed). We can't test DebugMenuManager directly though because of the persistant debug menu (which is always displayed no matter what)
|
|||
if (m_Root == null) |
|||
return; |
|||
|
|||
foreach(var itemUI in m_ItemsUI) |
|||
{ |
|||
if(itemUI.dynamicDisplay) |
|||
itemUI.Update(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public class LightingDebugPanel |
|||
: DebugPanel |
|||
{ |
|||
public LightingDebugPanel() |
|||
: base("Lighting") |
|||
{ |
|||
} |
|||
} |
|||
} |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using System; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering |
|||
{ |
|||
public class DebugMenuItem |
|||
{ |
|||
public Type type { get { return m_Type; } } |
|||
public string name { get { return m_Name; } } |
|||
public DebugItemHandler handler { get { return m_Handler; } } |
|||
public bool dynamicDisplay { get { return m_DynamicDisplay; } } |
|||
public bool readOnly { get { return m_Setter == null; } } |
|||
|
|||
public DebugMenuItem(string name, Type type, Func<object> getter, Action<object> setter, bool dynamicDisplay = false, DebugItemHandler handler = null) |
|||
{ |
|||
m_Type = type; |
|||
m_Setter = setter; |
|||
m_Getter = getter; |
|||
m_Name = name; |
|||
m_Handler = handler; |
|||
m_DynamicDisplay = dynamicDisplay; |
|||
} |
|||
|
|||
public Type GetItemType() |
|||
{ |
|||
return m_Type; |
|||
} |
|||
|
|||
public void SetValue(object value, bool record = true) |
|||
{ |
|||
// Setter can be null for readonly items
|
|||
if(m_Setter != null) |
|||
{ |
|||
m_Setter(value); |
|||
m_Handler.ClampValues(m_Getter, m_Setter); |
|||
|
|||
// Update state for serialization/undo
|
|||
if(record) |
|||
m_State.SetValue(m_Getter()); |
|||
} |
|||
} |
|||
|
|||
public object GetValue() |
|||
{ |
|||
return m_Getter(); |
|||
} |
|||
|
|||
public void SetDebugItemState(DebugMenuItemState state) |
|||
{ |
|||
m_State = state; |
|||
} |
|||
|
|||
Func<object> m_Getter; |
|||
Action<object> m_Setter; |
|||
Type m_Type; |
|||
string m_Name; |
|||
DebugItemHandler m_Handler = null; |
|||
bool m_DynamicDisplay = false; |
|||
DebugMenuItemState m_State = null; |
|||
} |
|||
|
|||
public class DebugMenu |
|||
{ |
|||
public string name { get { return m_Name; } } |
|||
public int itemCount { get { return m_Items.Count; } } |
|||
|
|||
protected string m_Name = "Unknown Debug Menu"; |
|||
|
|||
private GameObject m_Root = null; |
|||
private List<DebugMenuItem> m_Items = new List<DebugMenuItem>(); |
|||
private List<DebugMenuItemUI> m_ItemsUI = new List<DebugMenuItemUI>(); |
|||
private int m_SelectedItem = -1; |
|||
|
|||
public DebugMenu(string name) |
|||
{ |
|||
m_Name = name; |
|||
} |
|||
|
|||
public DebugMenuItem GetDebugMenuItem(int index) |
|||
{ |
|||
if (index >= m_Items.Count || index < 0) |
|||
return null; |
|||
return m_Items[index]; |
|||
} |
|||
|
|||
public DebugMenuItem GetDebugMenuItem(string name) |
|||
{ |
|||
return m_Items.Find(x => x.name == name); |
|||
} |
|||
|
|||
public DebugMenuItem GetSelectedDebugMenuItem() |
|||
{ |
|||
if(m_SelectedItem != -1) |
|||
{ |
|||
return m_Items[m_SelectedItem]; |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
public bool HasItem(DebugMenuItem debugItem) |
|||
{ |
|||
foreach(var item in m_Items) |
|||
{ |
|||
if (debugItem == item) |
|||
return true; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public void RemoveDebugItem(DebugMenuItem debugItem) |
|||
{ |
|||
m_Items.Remove(debugItem); |
|||
RebuildGUI(); |
|||
} |
|||
|
|||
public void AddDebugItem(DebugMenuItem debugItem) |
|||
{ |
|||
m_Items.Add(debugItem); |
|||
RebuildGUI(); |
|||
} |
|||
|
|||
// TODO: Move this to UI classes
|
|||
public GameObject BuildGUI(GameObject parent) |
|||
{ |
|||
m_Root = new GameObject(string.Format("{0}", m_Name)); |
|||
m_Root.transform.SetParent(parent.transform); |
|||
m_Root.transform.localPosition = Vector3.zero; |
|||
m_Root.transform.localScale = Vector3.one; |
|||
|
|||
UI.VerticalLayoutGroup menuVL = m_Root.AddComponent<UI.VerticalLayoutGroup>(); |
|||
menuVL.spacing = 5.0f; |
|||
menuVL.childControlWidth = true; |
|||
menuVL.childControlHeight = true; |
|||
menuVL.childForceExpandWidth = true; |
|||
menuVL.childForceExpandHeight = false; |
|||
|
|||
RectTransform menuVLRectTransform = m_Root.GetComponent<RectTransform>(); |
|||
menuVLRectTransform.pivot = new Vector2(0.0f, 0.0f); |
|||
menuVLRectTransform.localPosition = new Vector3(0.0f, 0.0f); |
|||
menuVLRectTransform.anchorMin = new Vector2(0.0f, 0.0f); |
|||
menuVLRectTransform.anchorMax = new Vector2(1.0f, 1.0f); |
|||
|
|||
RebuildGUI(); |
|||
|
|||
m_Root.SetActive(false); |
|||
return m_Root; |
|||
} |
|||
|
|||
private void RebuildGUI() |
|||
{ |
|||
m_Root.transform.DetachChildren(); |
|||
|
|||
DebugMenuUI.CreateTextElement(string.Format("{0} Title", m_Name), m_Name, 14, TextAnchor.MiddleLeft, m_Root); |
|||
|
|||
m_ItemsUI.Clear(); |
|||
foreach (DebugMenuItem menuItem in m_Items) |
|||
{ |
|||
DebugItemHandler handler = menuItem.handler; // Should never be null, we have at least the default handler
|
|||
m_ItemsUI.Add(handler.BuildGUI(m_Root)); |
|||
} |
|||
} |
|||
|
|||
|
|||
void SetSelectedItem(int index) |
|||
{ |
|||
if(m_SelectedItem != -1) |
|||
{ |
|||
m_ItemsUI[m_SelectedItem].SetSelected(false); |
|||
} |
|||
|
|||
m_SelectedItem = index; |
|||
m_ItemsUI[m_SelectedItem].SetSelected(true); |
|||
} |
|||
|
|||
public void SetSelected(bool value) |
|||
{ |
|||
m_Root.SetActive(value); |
|||
if(value) |
|||
{ |
|||
if (m_SelectedItem == -1) |
|||
{ |
|||
NextItem(); |
|||
} |
|||
else |
|||
SetSelectedItem(m_SelectedItem); |
|||
} |
|||
} |
|||
|
|||
int NextItemIndex(int current) |
|||
{ |
|||
return (current + 1) % m_Items.Count; |
|||
} |
|||
|
|||
void NextItem() |
|||
{ |
|||
if(m_Items.Count != 0) |
|||
{ |
|||
int newSelected = (m_SelectedItem + 1) % m_Items.Count; |
|||
SetSelectedItem(newSelected); |
|||
} |
|||
} |
|||
|
|||
int PreviousItemIndex(int current) |
|||
{ |
|||
int newSelected = current - 1; |
|||
if (newSelected == -1) |
|||
newSelected = m_Items.Count - 1; |
|||
return newSelected; |
|||
} |
|||
|
|||
void PreviousItem() |
|||
{ |
|||
if(m_Items.Count != 0) |
|||
{ |
|||
int newSelected = m_SelectedItem - 1; |
|||
if (newSelected == -1) |
|||
newSelected = m_Items.Count - 1; |
|||
SetSelectedItem(newSelected); |
|||
} |
|||
} |
|||
|
|||
public void OnMoveHorizontal(float value) |
|||
{ |
|||
if(m_SelectedItem != -1 && !m_Items[m_SelectedItem].readOnly) |
|||
{ |
|||
if (value > 0.0f) |
|||
m_ItemsUI[m_SelectedItem].OnIncrement(); |
|||
else |
|||
m_ItemsUI[m_SelectedItem].OnDecrement(); |
|||
} |
|||
} |
|||
|
|||
public void OnMoveVertical(float value) |
|||
{ |
|||
if (value > 0.0f) |
|||
PreviousItem(); |
|||
else |
|||
NextItem(); |
|||
} |
|||
|
|||
public void OnValidate() |
|||
{ |
|||
if (m_SelectedItem != -1 && !m_Items[m_SelectedItem].readOnly) |
|||
m_ItemsUI[m_SelectedItem].OnValidate(); |
|||
} |
|||
|
|||
public void AddDebugMenuItem<ItemType>(string name, Func<object> getter, Action<object> setter, bool dynamicDisplay = false, DebugItemHandler handler = null) |
|||
{ |
|||
if (handler == null) |
|||
handler = new DefaultDebugItemHandler(); |
|||
DebugMenuItem newItem = new DebugMenuItem(name, typeof(ItemType), getter, setter, dynamicDisplay, handler); |
|||
handler.SetDebugMenuItem(newItem); |
|||
m_Items.Add(newItem); |
|||
|
|||
DebugMenuManager dmm = DebugMenuManager.instance; |
|||
DebugMenuItemState itemState = dmm.FindDebugItemState(name, m_Name); |
|||
if(itemState == null) |
|||
{ |
|||
itemState = handler.CreateDebugMenuItemState(); |
|||
itemState.Initialize(name, m_Name); |
|||
itemState.SetValue(getter()); |
|||
dmm.AddDebugMenuItemState(itemState); |
|||
} |
|||
|
|||
newItem.SetDebugItemState(itemState); |
|||
} |
|||
|
|||
public void Update() |
|||
{ |
|||
// Can happen if the debug menu has been disabled (all UI is destroyed). We can't test DebugMenuManager directly though because of the persistant debug menu (which is always displayed no matter what)
|
|||
if (m_Root == null) |
|||
return; |
|||
|
|||
foreach(var itemUI in m_ItemsUI) |
|||
{ |
|||
if(itemUI.dynamicDisplay) |
|||
itemUI.Update(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public class LightingDebugMenu |
|||
: DebugMenu |
|||
{ |
|||
public LightingDebugMenu() |
|||
: base("Lighting") |
|||
{ |
|||
} |
|||
} |
|||
} |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering |
|||
{ |
|||
// Class that users need to extend for runtime debug menu item UI
|
|||
public abstract class DebugMenuItemUI |
|||
{ |
|||
protected GameObject m_Root = null; |
|||
protected DebugMenuItem m_MenuItem = null; |
|||
|
|||
public bool dynamicDisplay { get { return m_MenuItem.dynamicDisplay; } } |
|||
|
|||
protected DebugMenuItemUI(DebugMenuItem menuItem) |
|||
{ |
|||
m_MenuItem = menuItem; |
|||
} |
|||
|
|||
// Implement for selection specific beahavior (like changing color for example)
|
|||
public abstract void SetSelected(bool value); |
|||
// Implement behavior when user execute the "Validate" action
|
|||
public abstract void OnValidate(); |
|||
// Implement behavior when user execute the "Increment" action
|
|||
public abstract void OnIncrement(); |
|||
// Implement behavior when user execute the "Decrement" action
|
|||
public abstract void OnDecrement(); |
|||
// Implement this method to update the UI with current item value.
|
|||
// User must call it whenever Validate/Increment/Decrement is called. It will also be automatically called for dynamically displayed items.
|
|||
public abstract void Update(); |
|||
} |
|||
|
|||
public class DebugMenuSimpleItemUI : DebugMenuItemUI |
|||
{ |
|||
protected GameObject m_Name = null; |
|||
protected GameObject m_Value = null; |
|||
|
|||
protected DebugMenuSimpleItemUI(GameObject parent, DebugMenuItem menuItem, string name) |
|||
: base(menuItem) |
|||
{ |
|||
|
|||
m_Root = DebugMenuUI.CreateHorizontalLayoutGroup("", true, true, false, false, parent); |
|||
m_Name = DebugMenuUI.CreateTextElement(m_MenuItem.name, name, 10, TextAnchor.MiddleLeft, m_Root); |
|||
var layoutElem = m_Name.AddComponent<UI.LayoutElement>(); |
|||
layoutElem.minWidth = DebugMenuUI.kDebugItemNameWidth; |
|||
m_Value = DebugMenuUI.CreateTextElement(string.Format("{0} value", name), "", 10, TextAnchor.MiddleLeft, m_Root); |
|||
} |
|||
|
|||
public override void SetSelected(bool value) |
|||
{ |
|||
m_Name.GetComponent<UI.Text>().color = value ? DebugMenuUI.kColorSelected : DebugMenuUI.kColorUnSelected; |
|||
m_Value.GetComponent<UI.Text>().color = value ? DebugMenuUI.kColorSelected : DebugMenuUI.kColorUnSelected; |
|||
} |
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
} |
|||
|
|||
public class DebugMenuBoolItemUI : DebugMenuSimpleItemUI |
|||
{ |
|||
public DebugMenuBoolItemUI(GameObject parent, DebugMenuItem menuItem, string name) |
|||
: base(parent, menuItem, name) |
|||
{ |
|||
Update(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
m_Value.GetComponent<UI.Text>().text = (bool)m_MenuItem.GetValue() ? "True" : "False"; |
|||
} |
|||
|
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
m_MenuItem.SetValue(!(bool)m_MenuItem.GetValue()); |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
OnValidate(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
OnValidate(); |
|||
} |
|||
} |
|||
|
|||
public class DebugMenuFloatItemUI : DebugMenuSimpleItemUI |
|||
{ |
|||
bool m_SelectIncrementMode = false; |
|||
int m_CurrentIncrementIndex = -1; |
|||
|
|||
public DebugMenuFloatItemUI(GameObject parent, DebugMenuItem menuItem, string name) |
|||
: base(parent, menuItem, name) |
|||
{ |
|||
Update(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
float currentValue = (float)m_MenuItem.GetValue(); |
|||
bool isNegative = currentValue < 0.0f; |
|||
// Easier to format the string without caring about the '-' sign. We add it back at the end
|
|||
currentValue = Mathf.Abs(currentValue); |
|||
|
|||
char separator = System.Convert.ToChar(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); |
|||
|
|||
// Start with the maximum amount of trailing zeros
|
|||
string valueWithMaxDecimals = string.Format("{0:0.00000}", currentValue); |
|||
|
|||
// Remove trailing zeros until we reach the separator or we reach the decimal we are currently editing.
|
|||
int separatorIndex = valueWithMaxDecimals.LastIndexOf(separator); |
|||
int index = valueWithMaxDecimals.Length - 1; |
|||
while ( |
|||
valueWithMaxDecimals[index] == '0' // Remove trailing zeros
|
|||
&& index > (separatorIndex + 1) // until we reach the separator
|
|||
&& index > (System.Math.Abs(m_CurrentIncrementIndex) + separatorIndex)) // Or it's the index of the current decimal being edited (so as to display the last trailing zero in this case)
|
|||
{ |
|||
index--; |
|||
} |
|||
|
|||
string finalValue = new string(valueWithMaxDecimals.ToCharArray(), 0, index + 1); |
|||
|
|||
// Add leading zeros until we reach where the current order is being edited.
|
|||
if(m_CurrentIncrementIndex > 0) |
|||
{ |
|||
float incrementValue = Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex); |
|||
if(incrementValue > currentValue) |
|||
{ |
|||
float compareValue = currentValue + 1.0f; // Start at 1.0f because we know that we are going to increment by 10 or more
|
|||
while (incrementValue > compareValue) |
|||
{ |
|||
finalValue = finalValue.Insert(0, "0"); |
|||
compareValue *= 10.0f; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// When selecting which decimal/order you want to edit, we show brackets around the figure to show the user.
|
|||
if(m_SelectIncrementMode) |
|||
{ |
|||
separatorIndex = finalValue.LastIndexOf(separator); // separator may have changed place if we added leading zeros
|
|||
int bracketIndex = separatorIndex - m_CurrentIncrementIndex; |
|||
if(m_CurrentIncrementIndex >= 0) // Skip separator
|
|||
bracketIndex -= 1; |
|||
|
|||
finalValue = finalValue.Insert(bracketIndex, "["); |
|||
finalValue = finalValue.Insert(bracketIndex + 2, "]"); |
|||
} |
|||
|
|||
if(isNegative) |
|||
finalValue = finalValue.Insert(0, "-"); |
|||
|
|||
m_Value.GetComponent<UI.Text>().text = finalValue; |
|||
} |
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
m_SelectIncrementMode = !m_SelectIncrementMode; |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
if(!m_SelectIncrementMode) |
|||
{ |
|||
m_MenuItem.SetValue((float)m_MenuItem.GetValue() + Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex)); |
|||
} |
|||
else |
|||
{ |
|||
m_CurrentIncrementIndex -= 1; // * 0.1 (10^m_CurrentIncrementIndex)
|
|||
m_CurrentIncrementIndex = System.Math.Max(-5, m_CurrentIncrementIndex); |
|||
} |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
if (!m_SelectIncrementMode) |
|||
{ |
|||
m_MenuItem.SetValue((float)m_MenuItem.GetValue() - Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex)); |
|||
} |
|||
else |
|||
{ |
|||
m_CurrentIncrementIndex += 1; // * 10 (10^m_CurrentIncrementIndex)
|
|||
} |
|||
Update(); |
|||
} |
|||
} |
|||
|
|||
// Everything is done with int. We don't really care about values > 2b for debugging.
|
|||
public class DebugMenuIntegerItemUI : DebugMenuSimpleItemUI |
|||
{ |
|||
bool m_SelectIncrementMode = false; |
|||
int m_CurrentIncrementIndex = 0; |
|||
|
|||
public DebugMenuIntegerItemUI(GameObject parent, DebugMenuItem menuItem, string name) |
|||
: base(parent, menuItem, name) |
|||
{ |
|||
} |
|||
|
|||
protected void UpdateText(int value) |
|||
{ |
|||
bool isNegative = value < 0f; |
|||
// Easier to format the string without caring about the '-' sign. We add it back at the end
|
|||
value = System.Math.Abs(value); |
|||
|
|||
string finalValue = string.Format("{0}", value); |
|||
|
|||
// Add leading zeros until we reach where the current order is being edited.
|
|||
if(m_CurrentIncrementIndex > 0) |
|||
{ |
|||
int incrementValue = (int)System.Math.Pow(10, m_CurrentIncrementIndex); |
|||
if(incrementValue > value) |
|||
{ |
|||
int compareValue = System.Math.Max(value, 1); |
|||
while (incrementValue > compareValue) |
|||
{ |
|||
finalValue = finalValue.Insert(0, "0"); |
|||
compareValue *= 10; |
|||
} |
|||
} |
|||
} |
|||
|
|||
// When selecting which decimal/order you want to edit, we show brackets around the figure to show the user.
|
|||
if(m_SelectIncrementMode) |
|||
{ |
|||
int bracketIndex = finalValue.Length - 1 - m_CurrentIncrementIndex; |
|||
|
|||
finalValue = finalValue.Insert(bracketIndex, "["); |
|||
finalValue = finalValue.Insert(bracketIndex + 2, "]"); |
|||
} |
|||
|
|||
if(isNegative) |
|||
finalValue = finalValue.Insert(0, "-"); |
|||
|
|||
m_Value.GetComponent<UI.Text>().text = finalValue; |
|||
} |
|||
|
|||
protected virtual int GetIntegerValue() |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
protected virtual void SetIntegerValue(int value) |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
UpdateText(GetIntegerValue()); |
|||
} |
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
m_SelectIncrementMode = !m_SelectIncrementMode; |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
if (!m_SelectIncrementMode) |
|||
{ |
|||
SetIntegerValue(GetIntegerValue() + (int)Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex)); |
|||
} |
|||
else |
|||
{ |
|||
m_CurrentIncrementIndex -= 1; // *= 0.1 (10^m_CurrentIncrementIndex)
|
|||
m_CurrentIncrementIndex = System.Math.Max(0, m_CurrentIncrementIndex); |
|||
} |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
if (!m_SelectIncrementMode) |
|||
{ |
|||
SetIntegerValue(GetIntegerValue() - (int)Mathf.Pow(10.0f, (float)m_CurrentIncrementIndex)); |
|||
} |
|||
else |
|||
{ |
|||
m_CurrentIncrementIndex += 1; // *= 10 (10^m_CurrentIncrementIndex)
|
|||
m_CurrentIncrementIndex = System.Math.Max(0, m_CurrentIncrementIndex); |
|||
} |
|||
Update(); |
|||
} |
|||
} |
|||
public class DebugMenuIntItemUI : DebugMenuIntegerItemUI |
|||
{ |
|||
public DebugMenuIntItemUI(GameObject parent, DebugMenuItem menuItem, string name) |
|||
: base(parent, menuItem, name) |
|||
{ |
|||
UpdateText((int)m_MenuItem.GetValue()); |
|||
} |
|||
|
|||
protected override int GetIntegerValue() |
|||
{ |
|||
return (int)m_MenuItem.GetValue(); |
|||
} |
|||
|
|||
protected override void SetIntegerValue(int value) |
|||
{ |
|||
m_MenuItem.SetValue(value); |
|||
} |
|||
} |
|||
|
|||
public class DebugMenuUIntItemUI : DebugMenuIntegerItemUI |
|||
{ |
|||
public DebugMenuUIntItemUI(GameObject parent, DebugMenuItem menuItem, string name) |
|||
: base(parent, menuItem, name) |
|||
{ |
|||
UpdateText((int)(uint)m_MenuItem.GetValue()); |
|||
} |
|||
|
|||
protected override int GetIntegerValue() |
|||
{ |
|||
return (int)(uint)m_MenuItem.GetValue(); |
|||
} |
|||
|
|||
protected override void SetIntegerValue(int value) |
|||
{ |
|||
m_MenuItem.SetValue((uint)System.Math.Max(0, value)); |
|||
} |
|||
} |
|||
|
|||
public class DebugMenuEnumItemUI : DebugMenuSimpleItemUI |
|||
{ |
|||
int m_CurrentValueIndex = 0; |
|||
GUIContent[] m_ValueNames; |
|||
int[] m_Values; |
|||
|
|||
public DebugMenuEnumItemUI(GameObject parent, DebugMenuItem menuItem, string name, GUIContent[] valueNames, int[] values) |
|||
: base(parent, menuItem, name) |
|||
{ |
|||
m_Values = values; |
|||
m_ValueNames = valueNames; |
|||
m_CurrentValueIndex = FindIndexForValue((int)m_MenuItem.GetValue()); |
|||
|
|||
Update(); |
|||
} |
|||
|
|||
private int FindIndexForValue(int value) |
|||
{ |
|||
for(int i = 0 ; i < m_Values.Length ; ++i) |
|||
{ |
|||
if (m_Values[i] == value) |
|||
return i; |
|||
} |
|||
|
|||
return -1; |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
if(m_CurrentValueIndex != -1) |
|||
{ |
|||
m_Value.GetComponent<UI.Text>().text = m_ValueNames[m_CurrentValueIndex].text; |
|||
} |
|||
} |
|||
|
|||
public override void OnValidate() |
|||
{ |
|||
OnIncrement(); |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
m_CurrentValueIndex = (m_CurrentValueIndex + 1) % m_Values.Length; |
|||
m_MenuItem.SetValue(m_Values[m_CurrentValueIndex]); |
|||
Update(); |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
m_CurrentValueIndex -= 1; |
|||
if (m_CurrentValueIndex < 0) |
|||
m_CurrentValueIndex = m_Values.Length - 1; |
|||
|
|||
m_MenuItem.SetValue(m_Values[m_CurrentValueIndex]); |
|||
Update(); |
|||
} |
|||
} |
|||
|
|||
public class DebugMenuColorItemUI : DebugMenuItemUI |
|||
{ |
|||
protected GameObject m_Name = null; |
|||
protected GameObject m_ColorRect = null; |
|||
|
|||
public DebugMenuColorItemUI(GameObject parent, DebugMenuItem menuItem, string name) |
|||
: base(menuItem) |
|||
{ |
|||
m_MenuItem = menuItem; |
|||
|
|||
m_Root = DebugMenuUI.CreateHorizontalLayoutGroup(name, true, true, false, false, parent); |
|||
|
|||
m_Name = DebugMenuUI.CreateTextElement(m_MenuItem.name, name, 10, TextAnchor.MiddleLeft, m_Root); |
|||
var layoutElemName = m_Name.AddComponent<UI.LayoutElement>(); |
|||
layoutElemName.minWidth = DebugMenuUI.kDebugItemNameWidth; |
|||
|
|||
// Force layout because we need the right height for the color rect element afterward.
|
|||
UI.LayoutRebuilder.ForceRebuildLayoutImmediate(m_Root.GetComponent<RectTransform>()); |
|||
RectTransform nameRect = m_Name.GetComponent<RectTransform>(); |
|||
|
|||
m_ColorRect = new GameObject(); |
|||
m_ColorRect.transform.SetParent(m_Root.transform, false); |
|||
m_ColorRect.AddComponent<UI.Image>(); |
|||
UI.LayoutElement layoutElem = m_ColorRect.AddComponent<UI.LayoutElement>(); |
|||
// We need to set min width/height because without an image, the size would be zero.
|
|||
layoutElem.minHeight = nameRect.rect.height; |
|||
layoutElem.minWidth = 40.0f; |
|||
|
|||
Update(); |
|||
} |
|||
|
|||
public override void Update() |
|||
{ |
|||
Color currentValue = (Color)m_MenuItem.GetValue(); |
|||
UI.Image image = m_ColorRect.GetComponent<UI.Image>(); |
|||
image.color = currentValue; |
|||
} |
|||
|
|||
public override void SetSelected(bool value) |
|||
{ |
|||
m_Name.GetComponent<UI.Text>().color = value ? DebugMenuUI.kColorSelected : DebugMenuUI.kColorUnSelected; |
|||
} |
|||
|
|||
// TODO: Edit mode!
|
|||
public override void OnValidate() |
|||
{ |
|||
} |
|||
|
|||
public override void OnIncrement() |
|||
{ |
|||
} |
|||
|
|||
public override void OnDecrement() |
|||
{ |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue