Skip to content

Commit

Permalink
Merge pull request #669 from stevehalliwell/BlockReferenceDrawer
Browse files Browse the repository at this point in the history
Added BlockReference, a simple data type with a property drawer that …
  • Loading branch information
chrisgregan committed Apr 16, 2018
2 parents 47e6a29 + a631aee commit bbc4c4f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs
@@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Fungus.EditorUtils
{
/// <summary>
/// Custom drawer for the BlockReference, allows for more easily selecting a target block in external c#
/// scripts.
/// </summary>
[CustomPropertyDrawer(typeof(Fungus.BlockReference))]
public class BlockReferenceDrawer : PropertyDrawer
{
public Fungus.Flowchart lastFlowchart;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var l = EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, l);
position.height = EditorGUIUtility.singleLineHeight;
var block = property.FindPropertyRelative("block");

Fungus.Block b = block.objectReferenceValue as Fungus.Block;

if (block.objectReferenceValue != null && lastFlowchart == null)
{
if (b != null)
{
lastFlowchart = b.GetFlowchart();
}
}

lastFlowchart = EditorGUI.ObjectField(position, lastFlowchart, typeof(Fungus.Flowchart), true) as Fungus.Flowchart;
position.y += EditorGUIUtility.singleLineHeight;
if (lastFlowchart != null)
b = Fungus.EditorUtils.BlockEditor.BlockField(position, new GUIContent("None"), lastFlowchart, b);
else
EditorGUI.PrefixLabel(position, new GUIContent("Flowchart Required"));

block.objectReferenceValue = b;

block.serializedObject.ApplyModifiedProperties();
property.serializedObject.ApplyModifiedProperties();
EditorGUI.EndProperty();
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight * 2;
}
}
}
13 changes: 13 additions & 0 deletions Assets/Fungus/Scripts/Editor/BlockReferenceDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Assets/Fungus/Scripts/Utils/BlockReference.cs
@@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Fungus
{
/// <summary>
/// A simple struct wrapping a reference to a Fungus Block. Allows for BlockReferenceDrawer.
/// This is the recommended way to directly reference a fungus block in external c# scripts,
/// as it will give you an inspector field that gives a drop down of all the blocks on a
/// flowchart, in a similar way to what you would expect from selecting a block on a command.
/// </summary>
[System.Serializable]
public struct BlockReference
{
public Block block;

public void Execute()
{
if (block != null)
block.StartExecution();
}
}
}
13 changes: 13 additions & 0 deletions Assets/Fungus/Scripts/Utils/BlockReference.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bbc4c4f

Please sign in to comment.