Skip to content

Instantly share code, notes, and snippets.

@strich
Created October 19, 2017 13:42
Show Gist options
  • Save strich/aec5026f969c734939856f466237277c to your computer and use it in GitHub Desktop.
Save strich/aec5026f969c734939856f466237277c to your computer and use it in GitHub Desktop.
[OdinDrawer]
public class EntityHandleValueDrawer : OdinValueDrawer<EntityHandle>
{
protected override void DrawPropertyLayout(IPropertyValueEntry<EntityHandle> entry, GUIContent label)
{
var componentPropertyTreeContext = entry.Property.Context.Get(this, "component_tree", (PropertyTree<ComponentContainer>)null);
var isFoldoutActive = entry.Property.Context.Get(this, "is_expanded", false);
if (componentPropertyTreeContext.Value == null)
{
// Initialize the property tree
var container = new ComponentContainer();
componentPropertyTreeContext.Value = new PropertyTree<ComponentContainer>(new ComponentContainer[] { container });
}
SirenixEditorGUI.BeginBox();
{
SirenixEditorGUI.BeginBoxHeader();
{
var handle = entry.SmartValue;
var headingText = "[" + entry.TypeOfValue.Name + "] " + label?.text + " " + handle.Id + "/" + handle.Version;
isFoldoutActive.Value = SirenixEditorGUI.Foldout(isFoldoutActive.Value,
GUIHelper.TempContent(headingText));
}
SirenixEditorGUI.EndBoxHeader();
if (SirenixEditorGUI.BeginFadeGroup(UniqueDrawerKey.Create(entry, this), isFoldoutActive.Value))
{
if (!Engine.Instance.IsInitialized)
{
EditorGUILayout.HelpBox("Ravioli Engine is not currently running.", MessageType.Warning);
}
else
if (!Engine.Instance.Context.Entities.IsValid(entry.SmartValue))
{
EditorGUILayout.HelpBox("Invalid handle.", MessageType.Warning);
}
else
{
((ComponentContainer)componentPropertyTreeContext.Value.WeakTargets[0])
.Components = Engine.Instance.Context.Entities.GetAllComponentsOfEntity(entry.SmartValue.Id)
.ToList();
// Draw the tree for the component container, which will draw the components list that was fetched from the entity handle
componentPropertyTreeContext.Value.Draw(false);
}
}
SirenixEditorGUI.EndFadeGroup();
}
SirenixEditorGUI.EndBox();
}
[ShowOdinSerializedPropertiesInInspector]
private class ComponentContainer // Property trees can't draw lists directly, only normal types, so we need to store the list in a normal type
{
[ShowInInspector]
[ListDrawerSettings(DraggableItems = false, IsReadOnly = true, Expanded = false)]
[BlueprintComponentWrapper]
public List<object> Components;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment