Format code and simplify IControlReference a little bit

#3123
This commit is contained in:
Wojtek Figat
2025-03-04 16:06:48 +01:00
parent fee54f0f39
commit 35fa63852c
2 changed files with 54 additions and 82 deletions

View File

@@ -1,6 +1,6 @@
using System; // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System.Collections.Generic;
using FlaxEditor.CustomEditors; using System;
using FlaxEditor.CustomEditors.Elements; using FlaxEditor.CustomEditors.Elements;
using FlaxEditor.GUI; using FlaxEditor.GUI;
using FlaxEditor.GUI.Drag; using FlaxEditor.GUI.Drag;
@@ -116,7 +116,6 @@ public class UIControlRefPickerControl : Control
public UIControlRefPickerControl() public UIControlRefPickerControl()
: base(0, 0, 50, 16) : base(0, 0, 50, 16)
{ {
} }
private void OnValueChanged() private void OnValueChanged()
@@ -485,7 +484,7 @@ public class ControlReferenceEditor : CustomEditor
Type t = typeof(ControlReference<>); Type t = typeof(ControlReference<>);
Type tw = t.MakeGenericType(new Type[] { genericType }); Type tw = t.MakeGenericType(new Type[] { genericType });
var instance = Activator.CreateInstance(tw); var instance = Activator.CreateInstance(tw);
(instance as IControlReference).Set(_element.CustomControl.Value); ((IControlReference)instance).UIControl = _element.CustomControl.Value;
SetValue(instance); SetValue(instance);
} }
}; };

View File

@@ -1,58 +1,36 @@
using System; // Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System.Collections.Generic;
using FlaxEngine; using System;
using FlaxEngine.GUI; using FlaxEngine.GUI;
namespace FlaxEngine; namespace FlaxEngine;
/// <summary> /// <summary>
/// The control reference interface. /// Interface for control references access.
/// </summary> /// </summary>
public interface IControlReference public interface IControlReference
{ {
/// <summary> /// <summary>
/// The UIControl. /// Gets or sets the reference to <see cref="FlaxEngine.UIControl"/> actor.
/// </summary> /// </summary>
public UIControl UIControl { get; } public UIControl UIControl { get; set; }
/// <summary> /// <summary>
/// The Control type /// Gets the type of the control the interface uses.
/// </summary> /// </summary>
/// <returns>The Control Type</returns> public Type ControlType { get; }
public Type GetControlType();
/// <summary>
/// A safe set of the UI Control. Will warn if Control is of the wrong type.
/// </summary>
/// <param name="uiControl">The UIControl to set.</param>
public void Set(UIControl uiControl);
} }
/// <summary> /// <summary>
/// ControlReference class. /// UI Control reference utility. References UI Control actor with a typed control type.
/// </summary> /// </summary>
[Serializable] [Serializable]
public struct ControlReference<T> : IControlReference where T : Control public struct ControlReference<T> : IControlReference where T : Control
{ {
[Serialize, ShowInEditor]
private UIControl _uiControl; private UIControl _uiControl;
/// <summary> /// <summary>
/// Default constructor for ControlReference; /// Gets the typed UI control object owned by the referenced <see cref="FlaxEngine.UIControl"/> actor.
/// </summary>
public ControlReference()
{
_uiControl = null;
}
/// <inheritdoc />
public Type GetControlType()
{
return typeof(T);
}
/// <summary>
/// The Control attached to the UI Control.
/// </summary> /// </summary>
[HideInEditor] [HideInEditor]
public T Control public T Control
@@ -61,39 +39,34 @@ public struct ControlReference<T> : IControlReference where T : Control
{ {
if (_uiControl != null && _uiControl.Control is T t) if (_uiControl != null && _uiControl.Control is T t)
return 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."); 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; return null;
} }
} }
}
/// <inheritdoc /> /// <inheritdoc />
public UIControl UIControl => _uiControl; public UIControl UIControl
/// <inheritdoc />
public void Set(UIControl uiControl)
{ {
if (uiControl == null) get => _uiControl;
set
{ {
Clear(); if (value == null)
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.");
}
/// <summary>
/// Clears the UIControl reference.
/// </summary>
public void Clear()
{ {
_uiControl = 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.");
}
}
}
/// <inheritdoc />
public Type ControlType => typeof(T);
/// <summary> /// <summary>
/// The implicit operator for the Control. /// The implicit operator for the Control.