Files
sbox-public/game/addons/tools/Code/Widgets/ControlWidgets/ControlObjectWidget.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

57 lines
1.4 KiB
C#

using System.Diagnostics.CodeAnalysis;
namespace Editor;
/// <summary>
/// A control widget that converts its property into a SerializedObject, so it can edit subproperties
/// </summary>
public class ControlObjectWidget : ControlWidget
{
public SerializedObject SerializedObject { get; private set; }
/// <summary>
/// Do we want to create a new instance when editing a property of
/// the given type containing null?
/// </summary>
private static bool ShouldCreateInstanceWhenNull( SerializedProperty property )
{
var type = property.PropertyType;
if ( type.IsAbstract ) return false;
// Allow Nullable<T> to be null
if ( type.IsValueType ) return false;
// There's nothing to edit inside a plain System.Object instance
if ( type == typeof( object ) ) return false;
if ( property.HasAttribute<AllowNullAttribute>() ) return false;
return true;
}
public ControlObjectWidget( SerializedProperty property, bool create ) : base( property )
{
try
{
//
// Create new entry, no null?
//
if ( create && property.GetValue<object>() == null && ShouldCreateInstanceWhenNull( property ) )
{
var newValue = Activator.CreateInstance( property.PropertyType );
property.SetValue( newValue );
}
}
catch ( Exception e )
{
Log.Warning( $"Couldn't create ControlObjectWidget: {e}" );
}
if ( property.TryGetAsObject( out var obj ) )
{
SerializedObject = obj;
}
}
}