mirror of
https://github.com/Facepunch/sbox-public.git
synced 2025-12-23 22:48:07 -05:00
Advanced properties (#3652)
This commit is contained in:
@@ -50,6 +50,11 @@ public enum ComponentFlags
|
||||
/// Don't serialize this component when cloning
|
||||
/// </summary>
|
||||
NotCloned = 256,
|
||||
|
||||
/// <summary>
|
||||
/// Can edit advanced properties in the component inspector
|
||||
/// </summary>
|
||||
ShowAdvancedProperties = 512,
|
||||
}
|
||||
|
||||
public partial class Component
|
||||
|
||||
@@ -37,7 +37,8 @@ public abstract partial class Component : BytePack.ISerializer
|
||||
{
|
||||
{ JsonKeys.Type, t.SerializedName },
|
||||
{ JsonKeys.Id, Id },
|
||||
{ JsonKeys.Enabled, Enabled }
|
||||
{ JsonKeys.Enabled, Enabled },
|
||||
{ JsonKeys.Flags, (long)Flags }
|
||||
};
|
||||
|
||||
if ( (isSceneForNetwork || isSingleNetworkObject) && this is INetworkSnapshot sw )
|
||||
@@ -102,6 +103,8 @@ public abstract partial class Component : BytePack.ISerializer
|
||||
Id = (Guid)id;
|
||||
}
|
||||
|
||||
DeserializeFlags( node );
|
||||
|
||||
if ( node.TryGetPropertyValue( JsonKeys.Snapshot, out var snapshotNode ) && this is INetworkSnapshot sw )
|
||||
{
|
||||
var data = snapshotNode.Deserialize<byte[]>();
|
||||
@@ -117,6 +120,18 @@ public abstract partial class Component : BytePack.ISerializer
|
||||
Enabled = (bool)(jsonData[JsonKeys.Enabled] ?? true);
|
||||
}
|
||||
|
||||
private void DeserializeFlags( JsonObject node )
|
||||
{
|
||||
if ( !node.TryGetPropertyValue( JsonKeys.Flags, out var inFlagNode ) )
|
||||
return;
|
||||
|
||||
var inFlags = (ComponentFlags)(long)inFlagNode;
|
||||
|
||||
const ComponentFlags savedFlags = ComponentFlags.ShowAdvancedProperties;
|
||||
|
||||
Flags = (Flags & ~savedFlags) | (inFlags & savedFlags);
|
||||
}
|
||||
|
||||
internal void PostDeserialize()
|
||||
{
|
||||
if ( jsonData is null )
|
||||
|
||||
@@ -15,7 +15,6 @@ public class PropertyAttribute : Attribute, IClassNameProvider, ITitleProvider
|
||||
/// </summary>
|
||||
public string Title { get; set; }
|
||||
|
||||
|
||||
public PropertyAttribute() : base()
|
||||
{
|
||||
}
|
||||
@@ -49,3 +48,11 @@ public class InlineEditorAttribute : Attribute
|
||||
{
|
||||
public bool Label { get; set; } = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Some properties are not meant for the average user, hide them unless they really want to see them.
|
||||
/// </summary>
|
||||
[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field )]
|
||||
public class AdvancedAttribute : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
@@ -50,6 +50,25 @@ public partial class ComponentSheet : Widget
|
||||
/// </summary>
|
||||
internal bool Expanded { get; set; } = true;
|
||||
|
||||
internal bool ShowAdvanced
|
||||
{
|
||||
get
|
||||
{
|
||||
return TargetObject.Targets.OfType<Component>()
|
||||
.Any( x => x.IsValid() && x.Flags.Contains( ComponentFlags.ShowAdvancedProperties ) );
|
||||
}
|
||||
set
|
||||
{
|
||||
foreach ( var component in TargetObject.Targets.OfType<Component>() )
|
||||
{
|
||||
if ( component.IsValid() )
|
||||
{
|
||||
component.Flags = component.Flags.WithFlag( ComponentFlags.ShowAdvancedProperties, value );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Expands/shrinks the component in the component list.
|
||||
/// </summary>
|
||||
@@ -163,6 +182,7 @@ public partial class ComponentSheet : Widget
|
||||
|
||||
hc.Add( TargetObject );
|
||||
hc.Add( ViewMode );
|
||||
hc.Add( ShowAdvanced );
|
||||
|
||||
foreach ( var condition in hideShowConditions )
|
||||
{
|
||||
@@ -218,10 +238,11 @@ public partial class ComponentSheet : Widget
|
||||
|
||||
Content.Add( cs );
|
||||
|
||||
cs.AddObject( TargetObject, FilterProperties );
|
||||
var showAdvanced = ShowAdvanced;
|
||||
cs.AddObject( TargetObject, ( o ) => FilterProperties( o, showAdvanced ) );
|
||||
}
|
||||
|
||||
bool FilterProperties( SerializedProperty o )
|
||||
bool FilterProperties( SerializedProperty o, bool showAdvanced )
|
||||
{
|
||||
if ( o.PropertyType is null ) return false;
|
||||
|
||||
@@ -236,7 +257,8 @@ public partial class ComponentSheet : Widget
|
||||
if ( ViewMode != ComponentViewMode.Events && hideInEventTab ) return false;
|
||||
|
||||
if ( o.IsMethod ) return true;
|
||||
if ( !o.HasAttribute<PropertyAttribute>() ) return false;
|
||||
if ( o.HasAttribute<PropertyAttribute>() == false ) return false;
|
||||
if ( o.HasAttribute<AdvancedAttribute>() && showAdvanced == false ) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -114,10 +114,18 @@ public class ComponentSheetHeader : InspectorHeader
|
||||
|
||||
void FillContextMenu( Menu menu )
|
||||
{
|
||||
var o = menu.AddOption( "View Legacy Actions", "sentiment_very_dissatisfied" );
|
||||
o.Checkable = true;
|
||||
o.Checked = Sheet.ViewMode == ComponentViewMode.Events;
|
||||
o.Toggled = b => Sheet.ViewMode = b ? ComponentViewMode.Events : ComponentViewMode.Default;
|
||||
{
|
||||
var o = menu.AddOption( "View Legacy Actions", "sentiment_very_dissatisfied" );
|
||||
o.Checkable = true;
|
||||
o.Checked = Sheet.ViewMode == ComponentViewMode.Events;
|
||||
o.Toggled = b => Sheet.ViewMode = b ? ComponentViewMode.Events : ComponentViewMode.Default;
|
||||
}
|
||||
{
|
||||
var o = menu.AddOption( "View Advanced Properties", "tune" );
|
||||
o.Checkable = true;
|
||||
o.Checked = Sheet.ShowAdvanced;
|
||||
o.Toggled = b => Sheet.ShowAdvanced = b;
|
||||
}
|
||||
menu.AddSeparator();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user