using System;
using System.Collections.Generic;
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEngine;
///
/// The control reference interface.
///
public interface IControlReference
{
///
/// The UIControl.
///
public UIControl UIControl { get; }
///
/// The Control type
///
/// The Control Type
public Type GetControlType();
///
/// A safe set of the UI Control. Will warn if Control is of the wrong type.
///
/// The UIControl to set.
public void Set(UIControl uiControl);
}
///
/// ControlReference class.
///
[Serializable]
public struct ControlReference : IControlReference where T : Control
{
[Serialize, ShowInEditor]
private UIControl _uiControl;
///
/// Default constructor for ControlReference;
///
public ControlReference()
{
_uiControl = null;
}
///
public Type GetControlType()
{
return typeof(T);
}
///
/// The Control attached to the UI Control.
///
[HideInEditor]
public T Control
{
get
{
if (_uiControl != null && _uiControl.Control is T t)
return t;
else
{
Debug.Write(LogType.Warning, "Trying to get Control from ControlReference but UIControl is null, or UIControl.Control is null, or UIControl.Control is not the correct type.");
return null;
}
}
}
///
public UIControl UIControl => _uiControl;
///
public void Set(UIControl uiControl)
{
if (uiControl == null)
{
Clear();
return;
}
if (uiControl.Control is T castedControl)
_uiControl = uiControl;
else
Debug.Write(LogType.Warning, "Trying to set ControlReference but UIControl.Control is null or UIControl.Control is not the correct type.");
}
///
/// Clears the UIControl reference.
///
public void Clear()
{
_uiControl = null;
}
///
/// The implicit operator for the Control.
///
/// The ControlReference
/// The Control.
public static implicit operator T(ControlReference reference) => reference.Control;
///
/// The implicit operator for the UIControl
///
/// The ControlReference
/// The UIControl.
public static implicit operator UIControl(ControlReference reference) => reference.UIControl;
}