Skip to content

Commit

Permalink
Added PropertyDrawer Fallback for Null Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsakharov committed Apr 4, 2024
1 parent 11265b7 commit 9517c4a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion Prowl.Editor/Drawers/PropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ public static bool Draw(object container, FieldInfo fieldInfo, float width = -1,
var attributes = fieldInfo.GetCustomAttributes(true);
var imGuiAttributes = attributes.Where(attr => attr is IImGUIAttri).Cast<IImGUIAttri>();
EditorGui.HandleBeginImGUIAttributes(imGuiAttributes);

if (width == -1) width = ImGui.GetContentRegionAvail().X;

var value = fieldInfo.GetValue(container);
if (value == null)
{
CreateInstanceButton(label ?? fieldInfo.Name, width);
return false;
}

bool changed = Draw(label ?? fieldInfo.Name, ref value, width);
if (changed) fieldInfo.SetValue(container, value);

Expand All @@ -36,12 +42,27 @@ public static bool Draw(object container, PropertyInfo propertyInfo, float width
{
if (propertyInfo == null) return false;
if (width == -1) width = ImGui.GetContentRegionAvail().X;

var value = propertyInfo.GetValue(container);
if (value == null)
{
CreateInstanceButton(label ?? propertyInfo.Name, width);
return false;
}

bool changed = Draw(label ?? propertyInfo.Name, ref value, width);
if (changed) propertyInfo.SetValue(container, value);
return changed;
}

private static void CreateInstanceButton(string name, float width)
{
float w = width;
DrawLabel(name, ref w);
ImGui.SetNextItemWidth(width);
ImGui.TextDisabled("null");
ImGui.Columns(1);
}

public static bool Draw(string label, ref object value, float width = -1)
{
Expand Down

0 comments on commit 9517c4a

Please sign in to comment.