using UnityEngine.UI;
namespace UnityEngine.Rendering.UI
{
///
/// DebugUIHandler for panels.
///
public class DebugUIHandlerPanel : MonoBehaviour
{
/// Name of the panel.
public Text nameLabel;
/// Scroll rect of the panel.
public ScrollRect scrollRect;
/// Viewport of the panel.
public RectTransform viewport;
/// Associated canvas.
public DebugUIHandlerCanvas Canvas;
RectTransform m_ScrollTransform;
RectTransform m_ContentTransform;
RectTransform m_MaskTransform;
DebugUIHandlerWidget m_ScrollTarget;
internal protected DebugUI.Panel m_Panel;
void OnEnable()
{
m_ScrollTransform = scrollRect.GetComponent();
m_ContentTransform = GetComponent().contentHolder;
m_MaskTransform = GetComponentInChildren(true).rectTransform;
}
internal void SetPanel(DebugUI.Panel panel)
{
m_Panel = panel;
nameLabel.text = panel.displayName;
}
internal DebugUI.Panel GetPanel()
{
return m_Panel;
}
///
/// Select next panel on the canvas.
///
public void SelectNextItem()
{
Canvas.SelectNextPanel();
}
///
/// Select previous panel on the canvas.
///
public void SelectPreviousItem()
{
Canvas.SelectPreviousPanel();
}
///
/// Scrollbar value clicked via mouse/touch.
///
public void OnScrollbarClicked()
{
DebugManager.instance.SetScrollTarget(null); // Release scroll target
}
internal void SetScrollTarget(DebugUIHandlerWidget target)
{
m_ScrollTarget = target;
}
// TODO: Jumps around with foldouts and the likes, fix me
internal void UpdateScroll()
{
if (m_ScrollTarget == null)
return;
var targetTransform = m_ScrollTarget.GetComponent();
float itemY = GetYPosInScroll(targetTransform);
float targetY = GetYPosInScroll(m_MaskTransform);
float normalizedDiffY = (targetY - itemY) / (m_ContentTransform.rect.size.y - m_ScrollTransform.rect.size.y);
float normalizedPosY = scrollRect.verticalNormalizedPosition - normalizedDiffY;
normalizedPosY = Mathf.Clamp01(normalizedPosY);
scrollRect.verticalNormalizedPosition = Mathf.Lerp(scrollRect.verticalNormalizedPosition, normalizedPosY, Time.deltaTime * 10f);
}
float GetYPosInScroll(RectTransform target)
{
var pivotOffset = new Vector3(
(0.5f - target.pivot.x) * target.rect.size.x,
(0.5f - target.pivot.y) * target.rect.size.y,
0f
);
var localPos = target.localPosition + pivotOffset;
var worldPos = target.parent.TransformPoint(localPos);
return m_ScrollTransform.TransformPoint(worldPos).y;
}
internal DebugUIHandlerWidget GetFirstItem()
{
return GetComponent()
.GetFirstItem();
}
///
/// Function to reset DebugManager, provided for UI.
///
public void ResetDebugManager()
{
DebugManager.instance.Reset();
}
}
}