Browse Source

Merge pull request #770 from stevehalliwell/BlockCallers

Show all IBlockCallers in BlockEditor
master
Steve Halliwell 5 years ago committed by GitHub
parent
commit
5300d0c164
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      Assets/Fungus/Scripts/Commands/Call.cs
  2. 12
      Assets/Fungus/Scripts/Commands/Menu.cs
  3. 12
      Assets/Fungus/Scripts/Commands/MenuTimer.cs
  4. 14
      Assets/Fungus/Scripts/Commands/StopBlock.cs
  5. 35
      Assets/Fungus/Scripts/Editor/BlockEditor.cs
  6. 12
      Assets/Fungus/Scripts/Interfaces/IBlockCaller.cs
  7. 11
      Assets/Fungus/Scripts/Interfaces/IBlockCaller.cs.meta
  8. 3
      Assets/Fungus/Scripts/Utils/BlockReference.cs

12
Assets/Fungus/Scripts/Commands/Call.cs

@ -30,7 +30,7 @@ namespace Fungus
"Call",
"Execute another block in the same Flowchart as the command, or in a different Flowchart.")]
[AddComponentMenu("")]
public class Call : Command
public class Call : Command, IBlockCaller
{
[Tooltip("Flowchart which contains the block to execute. If none is specified then the current Flowchart is used.")]
[SerializeField] protected Flowchart targetFlowchart;
@ -166,6 +166,16 @@ namespace Fungus
return startLabel.stringRef == variable || base.HasReference(variable);
}
public bool MayCallBlock(Block block)
{
return block == targetBlock;
}
public string GetLocationIdentifier()
{
return ParentBlock.GetFlowchart().GetName() + ":" + ParentBlock.BlockName + ".Call:" + CommandIndex.ToString();
}
#endregion
}
}

12
Assets/Fungus/Scripts/Commands/Menu.cs

@ -14,7 +14,7 @@ namespace Fungus
"Menu",
"Displays a button in a multiple choice menu")]
[AddComponentMenu("")]
public class Menu : Command, ILocalizable
public class Menu : Command, ILocalizable, IBlockCaller
{
[Tooltip("Text to display on the menu button")]
[TextArea()]
@ -101,6 +101,16 @@ namespace Fungus
base.HasReference(variable);
}
public bool MayCallBlock(Block block)
{
return block == targetBlock;
}
public string GetLocationIdentifier()
{
return ParentBlock.GetFlowchart().GetName() + ":" + ParentBlock.BlockName + ".Menu:" + CommandIndex.ToString();
}
#endregion
#region ILocalizable implementation

12
Assets/Fungus/Scripts/Commands/MenuTimer.cs

@ -15,7 +15,7 @@ namespace Fungus
"Displays a timer bar and executes a target block if the player fails to select a menu option in time.")]
[AddComponentMenu("")]
[ExecuteInEditMode]
public class MenuTimer : Command
public class MenuTimer : Command, IBlockCaller
{
[Tooltip("Length of time to display the timer for")]
[SerializeField] protected FloatData _duration = new FloatData(1);
@ -68,6 +68,16 @@ namespace Fungus
base.HasReference(variable);
}
public bool MayCallBlock(Block block)
{
return block == targetBlock;
}
public string GetLocationIdentifier()
{
return ParentBlock.GetFlowchart().GetName() + ":" + ParentBlock.BlockName + ".Menu Timer:" + CommandIndex.ToString();
}
#endregion
#region Backwards compatibility

14
Assets/Fungus/Scripts/Commands/StopBlock.cs

@ -11,7 +11,7 @@ namespace Fungus
[CommandInfo("Flow",
"Stop Block",
"Stops executing the named Block")]
public class StopBlock : Command
public class StopBlock : Command, IBlockCaller
{
[Tooltip("Flowchart containing the Block. If none is specified, the parent Flowchart is used.")]
[SerializeField] protected Flowchart flowchart;
@ -60,6 +60,18 @@ namespace Fungus
return blockName.stringRef == variable || base.HasReference(variable);
}
public bool MayCallBlock(Block block)
{
if(flowchart != null)
return block == flowchart.FindBlock(blockName.Value);
return false;
}
public string GetLocationIdentifier()
{
return ParentBlock.GetFlowchart().GetName() + ":" + ParentBlock.BlockName + ".StopBlock:" + CommandIndex.ToString();
}
#endregion
}
}

35
Assets/Fungus/Scripts/Editor/BlockEditor.cs

@ -33,6 +33,9 @@ namespace Fungus.EditorUtils
private Rect lastEventPopupPos, lastCMDpopupPos;
private string callersString;
private bool callersFoldout;
protected virtual void OnEnable()
{
@ -56,6 +59,25 @@ namespace Fungus.EditorUtils
commandListProperty = serializedObject.FindProperty("commandList");
commandListAdaptor = new CommandListAdaptor(target as Block, commandListProperty);
}
protected void CacheCallerString()
{
if (!string.IsNullOrEmpty(callersString))
return;
var targetBlock = target as Block;
var callers = FindObjectsOfType<MonoBehaviour>()
.Where(x => x is IBlockCaller)
.Select(x => x as IBlockCaller)
.Where(x => x.MayCallBlock(targetBlock))
.Select(x => x.GetLocationIdentifier()).ToList();
if (callers.Count > 0)
callersString = string.Join("\n", callers);
else
callersString = "None";
}
@ -132,6 +154,19 @@ namespace Fungus.EditorUtils
SerializedProperty descriptionProp = serializedObject.FindProperty("description");
EditorGUILayout.PropertyField(descriptionProp);
EditorGUI.indentLevel++;
if (callersFoldout = EditorGUILayout.Foldout(callersFoldout, "Callers"))
{
CacheCallerString();
GUI.enabled = false;
EditorGUILayout.TextArea(callersString);
GUI.enabled = true;
}
EditorGUI.indentLevel--;
EditorGUILayout.Space();
DrawEventHandlerGUI(flowchart);

12
Assets/Fungus/Scripts/Interfaces/IBlockCaller.cs

@ -0,0 +1,12 @@
namespace Fungus
{
/// <summary>
/// Interface for indicating that the class holds a reference to and may call a block
/// </summary>
public interface IBlockCaller
{
bool MayCallBlock(Block block);
string GetLocationIdentifier();
}
}

11
Assets/Fungus/Scripts/Interfaces/IBlockCaller.cs.meta

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c14d678ff547d8944a1a28e12c00868e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

3
Assets/Fungus/Scripts/Utils/BlockReference.cs

@ -9,6 +9,9 @@ namespace Fungus
/// 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.
///
/// If you want to showup in the Callers section of the block, ensure your monobehaviours
/// that have these also implement IBlockCaller.
/// </summary>
[System.Serializable]
public struct BlockReference

Loading…
Cancel
Save