namespace Editor;
///
/// This is a callback control widget, which is used to edit classes.
/// It shows key properties, and an edit button. On clicking the edit button
/// it'll show a propery sheet popup.
///
public class GenericControlWidget : ControlObjectWidget
{
public override bool SupportsMultiEdit => true;
public override bool IsWideMode => _isInlineEditor;
public override bool IncludeLabel => !_isInlineEditor;
bool _isInlineEditor;
RealTimeSince _visibilityDebounce = 0;
Widget PlaceholderWidget;
Dictionary KeyPropertyWidgets;
public GenericControlWidget( SerializedProperty property ) : base( property, true )
{
PaintBackground = false;
if ( property.TryGetAttribute( out var inlineEditor ) )
{
_isInlineEditor = true;
BuildInlineEditor( inlineEditor );
return;
}
Layout = Layout.Row();
Layout.Spacing = 2;
KeyPropertyWidgets = new();
var keys = SerializedObject?.Where( x => x.HasAttribute() ).ToArray();
if ( keys is not null && keys.Length > 0 )
{
foreach ( var key in keys )
{
var widget = Layout.Add( ControlSheetRow.CreateEditor( key ) );
widget.Visible = key.ShouldShow();
KeyPropertyWidgets.Add( key, widget );
}
// Only show the full edit button if we have editable properties that aren't showing on the front bar.
var propertiesThatArentKeys = SerializedObject?.Where( x => x.IsEditable && x.ShouldShow() && !x.IsMethod && !x.HasAttribute() ).ToArray();
if ( propertiesThatArentKeys.Length > 0 )
{
var row = Layout.AddColumn();
var popupButton = new IconButton( "edit_note" );
popupButton.OnClick = OpenPopup;
popupButton.ToolTip = $"Edit";
popupButton.Background = Theme.ControlBackground;
popupButton.Foreground = Theme.Text;
popupButton.IconSize = 16;
row.Add( popupButton );
row.AddStretchCell( 1 );
}
}
else
{
PlaceholderWidget = new Widget( this );
PlaceholderWidget.Size = Theme.RowHeight;
PlaceholderWidget.VerticalSizeMode = SizeMode.CanGrow;
PlaceholderWidget.HorizontalSizeMode = SizeMode.Flexible;
PlaceholderWidget.OnPaintOverride = PaintPlaceholder;
PlaceholderWidget.MouseClick = OpenPopup;
PlaceholderWidget.Cursor = CursorShape.Finger;
PlaceholderWidget.ToolTip = $"Edit";
Layout.Add( PlaceholderWidget, 1 );
}
}
StickyPopup Popup;
public override void OnDestroyed()
{
Popup?.Destroy();
Popup = null;
base.OnDestroyed();
}
protected void OpenPopup()
{
if ( Popup.IsValid() )
{
Popup?.Destroy();
Popup = null;
return;
}
var obj = SerializedObject;
// if it's nullable, create for the actual value rather than the nullable container
if ( SerializedProperty.IsNullable )
{
// best way to do this?
obj = SerializedProperty.GetValue