From 35fa63852c533bda6e96050e53009b3ba9b4890d Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 4 Mar 2025 16:06:48 +0100 Subject: [PATCH] Format code and simplify `IControlReference` a little bit #3123 --- .../Editors/ControlReferenceEditor.cs | 45 +++++---- Source/Engine/UI/ControlReference.cs | 91 +++++++------------ 2 files changed, 54 insertions(+), 82 deletions(-) diff --git a/Source/Editor/CustomEditors/Editors/ControlReferenceEditor.cs b/Source/Editor/CustomEditors/Editors/ControlReferenceEditor.cs index 700accabb..174dc9b20 100644 --- a/Source/Editor/CustomEditors/Editors/ControlReferenceEditor.cs +++ b/Source/Editor/CustomEditors/Editors/ControlReferenceEditor.cs @@ -1,6 +1,6 @@ -using System; -using System.Collections.Generic; -using FlaxEditor.CustomEditors; +// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. + +using System; using FlaxEditor.CustomEditors.Elements; using FlaxEditor.GUI; using FlaxEditor.GUI.Drag; @@ -25,7 +25,7 @@ public class UIControlRefPickerControl : Control private ActorTreeNode _linkedTreeNode; private string _valueName; private bool _supportsPickDropDown; - + private bool _isMouseDown; private Float2 _mouseDownPos; private Float2 _mousePos; @@ -33,17 +33,17 @@ public class UIControlRefPickerControl : Control private bool _hasValidDragOver; private DragActors _dragActors; private DragHandlers _dragHandlers; - + /// /// The presenter using this control. /// public IPresenterOwner PresenterContext; - + /// /// Occurs when value gets changed. /// public event Action ValueChanged; - + /// /// The type of the Control /// @@ -100,7 +100,7 @@ public class UIControlRefPickerControl : Control OnValueChanged(); } } - + /// /// Gets or sets the selected object value by identifier. /// @@ -109,14 +109,13 @@ public class UIControlRefPickerControl : Control get => _value ? _value.ID : Guid.Empty; set => Value = Object.Find(ref value); } - + /// /// Initializes a new instance of the class. /// public UIControlRefPickerControl() - : base(0, 0, 50, 16) + : base(0, 0, 50, 16) { - } private void OnValueChanged() @@ -142,12 +141,12 @@ public class UIControlRefPickerControl : Control { return actor == null || actor is UIControl a && a.Control.GetType() == _controlType; } - + private bool ValidateDragActor(ActorNode a) { if (!IsValid(a.Actor)) return false; - + if (PresenterContext is PrefabWindow prefabWindow) { if (prefabWindow.Tree == a.TreeNode.ParentTree) @@ -216,7 +215,7 @@ public class UIControlRefPickerControl : Control Render2D.DrawRectangle(bounds, style.SelectionBorder); } } - + /// public override void OnMouseEnter(Float2 location) { @@ -225,8 +224,8 @@ public class UIControlRefPickerControl : Control base.OnMouseEnter(location); } - - /// + + /// public override void OnMouseLeave() { _mousePos = Float2.Minimum; @@ -326,7 +325,7 @@ public class UIControlRefPickerControl : Control return base.OnMouseUp(location, button); } - + /// public override bool OnMouseDown(Float2 location, MouseButton button) { @@ -339,7 +338,7 @@ public class UIControlRefPickerControl : Control return base.OnMouseDown(location, button); } - + /// public override bool OnMouseDoubleClick(Float2 location, MouseButton button) { @@ -361,7 +360,7 @@ public class UIControlRefPickerControl : Control return base.OnMouseDoubleClick(location, button); } - + /// public override void OnSubmit() { @@ -404,7 +403,7 @@ public class UIControlRefPickerControl : Control return DragEffect; } - + /// public override DragDropEffect OnDragMove(ref Float2 location, DragData data) { @@ -456,10 +455,10 @@ public class UIControlRefPickerControl : Control public class ControlReferenceEditor : CustomEditor { private CustomElement _element; - + /// public override DisplayStyle Style => DisplayStyle.Inline; - + /// public override void Initialize(LayoutElementsContainer layout) { @@ -485,7 +484,7 @@ public class ControlReferenceEditor : CustomEditor Type t = typeof(ControlReference<>); Type tw = t.MakeGenericType(new Type[] { genericType }); var instance = Activator.CreateInstance(tw); - (instance as IControlReference).Set(_element.CustomControl.Value); + ((IControlReference)instance).UIControl = _element.CustomControl.Value; SetValue(instance); } }; diff --git a/Source/Engine/UI/ControlReference.cs b/Source/Engine/UI/ControlReference.cs index 75b5d57a8..dba3a3397 100644 --- a/Source/Engine/UI/ControlReference.cs +++ b/Source/Engine/UI/ControlReference.cs @@ -1,58 +1,36 @@ -using System; -using System.Collections.Generic; -using FlaxEngine; +// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. + +using System; using FlaxEngine.GUI; namespace FlaxEngine; /// -/// The control reference interface. +/// Interface for control references access. /// public interface IControlReference { /// - /// The UIControl. + /// Gets or sets the reference to actor. /// - public UIControl UIControl { get; } - + public UIControl UIControl { get; set; } + /// - /// The Control type + /// Gets the type of the control the interface uses. /// - /// 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); + public Type ControlType { get; } } /// -/// ControlReference class. +/// UI Control reference utility. References UI Control actor with a typed control type. /// [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. + /// Gets the typed UI control object owned by the referenced actor. /// [HideInEditor] public T Control @@ -61,39 +39,34 @@ public struct ControlReference : IControlReference where T : Control { 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 get Control from ControlReference but UIControl is null, or UIControl.Control is null, or UIControl.Control is not the correct type."); - return null; + Debug.Write(LogType.Warning, "Trying to set ControlReference but UIControl.Control is null or UIControl.Control is not the correct type."); } } } /// - 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; - } + public Type ControlType => typeof(T); /// /// The implicit operator for the Control.