// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
using FlaxEngine.GUI;
namespace FlaxEngine;
///
/// Interface for control references access.
///
public interface IControlReference
{
///
/// Gets or sets the reference to actor.
///
public UIControl UIControl { get; set; }
///
/// Gets the type of the control the interface uses.
///
public Type ControlType { get; }
}
///
/// UI Control reference utility. References UI Control actor with a typed control type.
///
[Serializable]
public struct ControlReference : IControlReference where T : Control
{
private UIControl _uiControl;
///
/// Gets the typed UI control object owned by the referenced actor.
///
[HideInEditor]
public T Control
{
get
{
if (_uiControl != null && _uiControl.Control is T t)
return t;
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
{
get => _uiControl;
set
{
if (value == null)
{
_uiControl = null;
}
else if (value.Control is T t)
{
_uiControl = value;
}
else
{
Debug.Write(LogType.Warning, "Trying to set ControlReference but UIControl.Control is null or UIControl.Control is not the correct type.");
}
}
}
///
public Type ControlType => typeof(T);
///
/// 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;
}