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;
@@ -25,7 +25,7 @@ public class UIControlRefPickerControl : Control
private ActorTreeNode _linkedTreeNode; private ActorTreeNode _linkedTreeNode;
private string _valueName; private string _valueName;
private bool _supportsPickDropDown; private bool _supportsPickDropDown;
private bool _isMouseDown; private bool _isMouseDown;
private Float2 _mouseDownPos; private Float2 _mouseDownPos;
private Float2 _mousePos; private Float2 _mousePos;
@@ -33,17 +33,17 @@ public class UIControlRefPickerControl : Control
private bool _hasValidDragOver; private bool _hasValidDragOver;
private DragActors _dragActors; private DragActors _dragActors;
private DragHandlers _dragHandlers; private DragHandlers _dragHandlers;
/// <summary> /// <summary>
/// The presenter using this control. /// The presenter using this control.
/// </summary> /// </summary>
public IPresenterOwner PresenterContext; public IPresenterOwner PresenterContext;
/// <summary> /// <summary>
/// Occurs when value gets changed. /// Occurs when value gets changed.
/// </summary> /// </summary>
public event Action ValueChanged; public event Action ValueChanged;
/// <summary> /// <summary>
/// The type of the Control /// The type of the Control
/// </summary> /// </summary>
@@ -100,7 +100,7 @@ public class UIControlRefPickerControl : Control
OnValueChanged(); OnValueChanged();
} }
} }
/// <summary> /// <summary>
/// Gets or sets the selected object value by identifier. /// Gets or sets the selected object value by identifier.
/// </summary> /// </summary>
@@ -109,14 +109,13 @@ public class UIControlRefPickerControl : Control
get => _value ? _value.ID : Guid.Empty; get => _value ? _value.ID : Guid.Empty;
set => Value = Object.Find<UIControl>(ref value); set => Value = Object.Find<UIControl>(ref value);
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="UIControlRefPickerControl"/> class. /// Initializes a new instance of the <see cref="UIControlRefPickerControl"/> class.
/// </summary> /// </summary>
public UIControlRefPickerControl() public UIControlRefPickerControl()
: base(0, 0, 50, 16) : base(0, 0, 50, 16)
{ {
} }
private void OnValueChanged() private void OnValueChanged()
@@ -142,12 +141,12 @@ public class UIControlRefPickerControl : Control
{ {
return actor == null || actor is UIControl a && a.Control.GetType() == _controlType; return actor == null || actor is UIControl a && a.Control.GetType() == _controlType;
} }
private bool ValidateDragActor(ActorNode a) private bool ValidateDragActor(ActorNode a)
{ {
if (!IsValid(a.Actor)) if (!IsValid(a.Actor))
return false; return false;
if (PresenterContext is PrefabWindow prefabWindow) if (PresenterContext is PrefabWindow prefabWindow)
{ {
if (prefabWindow.Tree == a.TreeNode.ParentTree) if (prefabWindow.Tree == a.TreeNode.ParentTree)
@@ -216,7 +215,7 @@ public class UIControlRefPickerControl : Control
Render2D.DrawRectangle(bounds, style.SelectionBorder); Render2D.DrawRectangle(bounds, style.SelectionBorder);
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void OnMouseEnter(Float2 location) public override void OnMouseEnter(Float2 location)
{ {
@@ -225,8 +224,8 @@ public class UIControlRefPickerControl : Control
base.OnMouseEnter(location); base.OnMouseEnter(location);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void OnMouseLeave() public override void OnMouseLeave()
{ {
_mousePos = Float2.Minimum; _mousePos = Float2.Minimum;
@@ -326,7 +325,7 @@ public class UIControlRefPickerControl : Control
return base.OnMouseUp(location, button); return base.OnMouseUp(location, button);
} }
/// <inheritdoc /> /// <inheritdoc />
public override bool OnMouseDown(Float2 location, MouseButton button) public override bool OnMouseDown(Float2 location, MouseButton button)
{ {
@@ -339,7 +338,7 @@ public class UIControlRefPickerControl : Control
return base.OnMouseDown(location, button); return base.OnMouseDown(location, button);
} }
/// <inheritdoc /> /// <inheritdoc />
public override bool OnMouseDoubleClick(Float2 location, MouseButton button) public override bool OnMouseDoubleClick(Float2 location, MouseButton button)
{ {
@@ -361,7 +360,7 @@ public class UIControlRefPickerControl : Control
return base.OnMouseDoubleClick(location, button); return base.OnMouseDoubleClick(location, button);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void OnSubmit() public override void OnSubmit()
{ {
@@ -404,7 +403,7 @@ public class UIControlRefPickerControl : Control
return DragEffect; return DragEffect;
} }
/// <inheritdoc /> /// <inheritdoc />
public override DragDropEffect OnDragMove(ref Float2 location, DragData data) public override DragDropEffect OnDragMove(ref Float2 location, DragData data)
{ {
@@ -456,10 +455,10 @@ public class UIControlRefPickerControl : Control
public class ControlReferenceEditor : CustomEditor public class ControlReferenceEditor : CustomEditor
{ {
private CustomElement<UIControlRefPickerControl> _element; private CustomElement<UIControlRefPickerControl> _element;
/// <inheritdoc /> /// <inheritdoc />
public override DisplayStyle Style => DisplayStyle.Inline; public override DisplayStyle Style => DisplayStyle.Inline;
/// <inheritdoc /> /// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout) public override void Initialize(LayoutElementsContainer layout)
{ {
@@ -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;
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;
}
}
/// <inheritdoc />
public UIControl UIControl
{
get => _uiControl;
set
{
if (value == null)
{
_uiControl = null;
}
else if (value.Control is T t)
{
_uiControl = value;
}
else 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 set ControlReference but UIControl.Control is null or UIControl.Control is not the correct type.");
return null;
} }
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public UIControl UIControl => _uiControl; public Type ControlType => typeof(T);
/// <inheritdoc />
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.");
}
/// <summary>
/// Clears the UIControl reference.
/// </summary>
public void Clear()
{
_uiControl = null;
}
/// <summary> /// <summary>
/// The implicit operator for the Control. /// The implicit operator for the Control.