Merge remote-tracking branch 'origin/master' into 1.10

This commit is contained in:
Wojtek Figat
2025-03-07 23:41:02 +01:00
42 changed files with 1215 additions and 130 deletions

View File

@@ -0,0 +1,84 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
using FlaxEditor.CustomEditors.Elements;
using FlaxEditor.Scripting;
using FlaxEngine;
using FlaxEngine.GUI;
using Object = FlaxEngine.Object;
namespace FlaxEditor.CustomEditors.Editors;
/// <summary>
/// The reference picker control used for UIControls using ControlReference.
/// </summary>
internal class UIControlRefPickerControl : FlaxObjectRefPickerControl
{
/// <summary>
/// Type of the control to pick.
/// </summary>
public Type ControlType;
/// <inheritdoc />
protected override bool IsValid(Object obj)
{
return obj == null || (obj is UIControl control && control.Control.GetType() == ControlType);
}
}
/// <summary>
/// ControlReferenceEditor class.
/// </summary>
[CustomEditor(typeof(ControlReference<>)), DefaultEditor]
internal class ControlReferenceEditor : CustomEditor
{
private CustomElement<UIControlRefPickerControl> _element;
/// <inheritdoc />
public override DisplayStyle Style => DisplayStyle.Inline;
/// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout)
{
if (!HasDifferentTypes)
{
_element = layout.Custom<UIControlRefPickerControl>();
if (ValuesTypes == null || ValuesTypes[0] == null)
{
Editor.LogWarning("ControlReference needs to be assigned in code.");
return;
}
Type genType = ValuesTypes[0].GetGenericArguments()[0];
if (typeof(Control).IsAssignableFrom(genType))
{
_element.CustomControl.PresenterContext = Presenter.Owner;
_element.CustomControl.ControlType = genType;
_element.CustomControl.Type = new ScriptType(typeof(UIControl));
}
_element.CustomControl.ValueChanged += () =>
{
Type genericType = ValuesTypes[0].GetGenericArguments()[0];
if (typeof(Control).IsAssignableFrom(genericType))
{
Type t = typeof(ControlReference<>);
Type tw = t.MakeGenericType(new Type[] { genericType });
var instance = Activator.CreateInstance(tw);
((IControlReference)instance).UIControl = (UIControl)_element.CustomControl.Value;
SetValue(instance);
}
};
}
}
/// <inheritdoc />
public override void Refresh()
{
base.Refresh();
if (!HasDifferentValues)
{
if (Values[0] is IControlReference cr)
_element.CustomControl.Value = cr.UIControl;
}
}
}

View File

@@ -48,7 +48,7 @@ namespace FlaxEditor.CustomEditors.Editors
public IPresenterOwner PresenterContext;
/// <summary>
/// Gets or sets the allowed objects type (given type and all sub classes). Must be <see cref="Object"/> type of any subclass.
/// Gets or sets the allowed objects type (given type and all subclasses). Must be <see cref="Object"/> type of any subclass.
/// </summary>
public ScriptType Type
{
@@ -61,7 +61,8 @@ namespace FlaxEditor.CustomEditors.Editors
throw new ArgumentException(string.Format("Invalid type for FlaxObjectRefEditor. Input type: {0}", value != ScriptType.Null ? value.TypeName : "null"));
_type = value;
_supportsPickDropDown = new ScriptType(typeof(Actor)).IsAssignableFrom(value) || new ScriptType(typeof(Script)).IsAssignableFrom(value);
_supportsPickDropDown = new ScriptType(typeof(Actor)).IsAssignableFrom(value) ||
new ScriptType(typeof(Script)).IsAssignableFrom(value);
// Deselect value if it's not valid now
if (!IsValid(_value))
@@ -80,7 +81,7 @@ namespace FlaxEditor.CustomEditors.Editors
if (_value == value)
return;
if (!IsValid(value))
throw new ArgumentException("Invalid object type.");
value = null;
// Special case for missing objects (eg. referenced actor in script that is deleted in editor)
if (value != null && (Object.GetUnmanagedPtr(value) == IntPtr.Zero || value.ID == Guid.Empty))
@@ -91,27 +92,17 @@ namespace FlaxEditor.CustomEditors.Editors
// Get name to display
if (_value is Script script)
{
_valueName = script.Actor ? $"{type.Name} ({script.Actor.Name})" : type.Name;
}
else if (_value != null)
{
_valueName = _value.ToString();
}
else
{
_valueName = string.Empty;
}
// Update tooltip
if (_value is SceneObject sceneObject)
{
TooltipText = Utilities.Utils.GetTooltip(sceneObject);
}
else
{
TooltipText = string.Empty;
}
OnValueChanged();
}
@@ -150,7 +141,12 @@ namespace FlaxEditor.CustomEditors.Editors
_type = ScriptType.Object;
}
private bool IsValid(Object obj)
/// <summary>
/// Object validation check routine.
/// </summary>
/// <param name="obj">Input object to check.</param>
/// <returns>True if it can be assigned, otherwise false.</returns>
protected virtual bool IsValid(Object obj)
{
var type = TypeUtils.GetObjectType(obj);
return obj == null || _type.IsAssignableFrom(type) && (CheckValid == null || CheckValid(obj, type));
@@ -168,6 +164,15 @@ namespace FlaxEditor.CustomEditors.Editors
Focus();
}, PresenterContext);
}
else if (new ScriptType(typeof(Control)).IsAssignableFrom(_type))
{
ActorSearchPopup.Show(this, new Float2(0, Height), IsValid, actor =>
{
Value = actor as UIControl;
RootWindow.Focus();
Focus();
}, PresenterContext);
}
else
{
ScriptSearchPopup.Show(this, new Float2(0, Height), IsValid, script =>