mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-04-19 13:59:22 -04:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
57 lines
1.4 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|