Browse Source

Refactored Block to use IBlock

master
Christopher 8 years ago
parent
commit
1cf014aa18
  1. 61
      Assets/Fungus/Flowchart/Scripts/Block.cs
  2. 8
      Assets/Fungus/Flowchart/Scripts/Command.cs
  3. 4
      Assets/Fungus/Flowchart/Scripts/EventHandler.cs
  4. 64
      Assets/Fungus/Flowchart/Scripts/Flowchart.cs
  5. 2
      Assets/Fungus/Flowchart/Scripts/IBlock.cs
  6. 8
      Assets/Fungus/Flowchart/Scripts/ICommand.cs
  7. 15
      Assets/Fungus/Flowchart/Scripts/INode.cs
  8. 12
      Assets/Fungus/Flowchart/Scripts/INode.cs.meta
  9. 7
      Assets/Fungus/Flowchart/Scripts/Node.cs
  10. 4
      Assets/Fungus/Scripts/Commands/Call.cs
  11. 2
      Assets/Fungus/Scripts/Commands/ControlStage.cs
  12. 2
      Assets/Fungus/Scripts/Commands/Menu.cs
  13. 2
      Assets/Fungus/Scripts/Commands/MenuTimer.cs
  14. 2
      Assets/Fungus/Scripts/Commands/Portrait.cs
  15. 2
      Assets/Fungus/Scripts/Commands/SetInteractable.cs
  16. 2
      Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs
  17. 2
      Assets/Fungus/Scripts/Commands/StopBlock.cs
  18. 2
      Assets/Fungus/Scripts/Commands/TweenUI.cs
  19. 4
      Assets/Fungus/Scripts/Components/Localization.cs
  20. 8
      Assets/Fungus/Scripts/Components/MenuDialog.cs
  21. 62
      Assets/Fungus/Scripts/Editor/BlockEditor.cs
  22. 10
      Assets/Fungus/Scripts/Editor/BlockInspector.cs
  23. 8
      Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs
  24. 2
      Assets/Fungus/Scripts/Editor/FlowchartMenuItems.cs
  25. 120
      Assets/Fungus/Scripts/Editor/FlowchartWindow.cs
  26. 4
      Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs

61
Assets/Fungus/Flowchart/Scripts/Block.cs

@ -106,88 +106,44 @@ namespace Fungus
#region IBlock implementation
/// <summary>
/// The execution state of the Block.
/// </summary>
public virtual ExecutionState State { get { return executionState; } }
/// <summary>
/// Unique identifier for the Block.
/// </summary>
public virtual int ItemId { get { return itemId; } set { itemId = value; } }
/// <summary>
/// The name of the block node as displayed in the Flowchart window.
/// </summary>
public virtual string BlockName { get { return blockName; } set { blockName = value; } }
/// <summary>
/// Description text to display under the block node
/// </summary>
public virtual string Description { get { return description; } }
/// <summary>
/// An optional Event Handler which can execute the block when an event occurs.
/// </summary>
public virtual EventHandler _EventHandler { get { return eventHandler; } set { eventHandler = value; } }
/// <summary>
/// The currently executing command.
/// </summary>
public virtual Command ActiveCommand { get { return activeCommand; } }
/// <summary>
/// Timer for fading Block execution icon.
/// </summary>
public virtual float ExecutingIconTimer { get; set; }
/// <summary>
/// The list of commands in the sequence.
/// </summary>
public virtual List<Command> CommandList { get { return commandList; } }
/// <summary>
/// Controls the next command to execute in the block execution coroutine.
/// </summary>
public virtual int JumpToCommandIndex { set { jumpToCommandIndex = value; } }
/// <summary>
/// Returns the parent Flowchart for this Block.
/// </summary>
public virtual Flowchart GetFlowchart()
{
return GetComponent<Flowchart>();
}
/// <summary>
/// Returns true if the Block is executing a command.
/// </summary>
public virtual bool IsExecuting()
{
return (executionState == ExecutionState.Executing);
}
/// <summary>
/// Returns the number of times this Block has executed.
/// </summary>
public virtual int GetExecutionCount()
{
return executionCount;
}
/// <summary>
/// Start a coroutine which executes all commands in the Block. Only one running instance of each Block is permitted.
/// </summary>
public virtual void StartExecution()
{
StartCoroutine(Execute());
}
/// <summary>
/// A coroutine method that executes all commands in the Block. Only one running instance of each Block is permitted.
/// </summary>
/// <param name="commandIndex">Index of command to start execution at</param>
/// <param name="onComplete">Delegate function to call when execution completes</param>
public virtual IEnumerator Execute(int commandIndex = 0, Action onComplete = null)
{
if (executionState != ExecutionState.Idle)
@ -296,9 +252,6 @@ namespace Fungus
}
}
/// <summary>
/// Stop executing commands in this Block.
/// </summary>
public virtual void Stop()
{
// Tell the executing command to stop immediately
@ -312,12 +265,9 @@ namespace Fungus
jumpToCommandIndex = int.MaxValue;
}
/// <summary>
/// Returns a list of all Blocks connected to this one.
/// </summary>
public virtual List<Block> GetConnectedBlocks()
public virtual List<IBlock> GetConnectedBlocks()
{
List<Block> connectedBlocks = new List<Block>();
var connectedBlocks = new List<IBlock>();
foreach (Command command in commandList)
{
if (command != null)
@ -328,10 +278,6 @@ namespace Fungus
return connectedBlocks;
}
/// <summary>
/// Returns the type of the previously executing command.
/// </summary>
/// <returns>The previous active command type.</returns>
public virtual System.Type GetPreviousActiveCommandType()
{
if (previousActiveCommandIndex >= 0 &&
@ -343,9 +289,6 @@ namespace Fungus
return null;
}
/// <summary>
/// Recalculate the indent levels for all commands in the list.
/// </summary>
public virtual void UpdateIndentLevels()
{
int indentLevel = 0;

8
Assets/Fungus/Flowchart/Scripts/Command.cs

@ -87,7 +87,7 @@ namespace Fungus
/// This reference is only populated at runtime and in the editor when the
/// block is selected.
/// </summary>
public virtual Block ParentBlock { get; set; }
public virtual IBlock ParentBlock { get; set; }
/// <summary>
/// Returns the Flowchart that this command belongs to.
@ -160,13 +160,13 @@ namespace Fungus
/// <summary>
/// Called when the new command is added to a block in the editor.
/// </summary>
public virtual void OnCommandAdded(Block parentBlock)
public virtual void OnCommandAdded(IBlock parentBlock)
{}
/// <summary>
/// Called when the command is deleted from a block in the editor.
/// </summary>
public virtual void OnCommandRemoved(Block parentBlock)
public virtual void OnCommandRemoved(IBlock parentBlock)
{}
/// <summary>
@ -190,7 +190,7 @@ namespace Fungus
/// <summary>
/// Populates a list with the Blocks that this command references.
/// </summary>
public virtual void GetConnectedBlocks(ref List<Block> connectedBlocks)
public virtual void GetConnectedBlocks(ref List<IBlock> connectedBlocks)
{}
/// <summary>

4
Assets/Fungus/Flowchart/Scripts/EventHandler.cs

@ -32,7 +32,7 @@ namespace Fungus
/// Add an EventHandlerInfo attibute and your new EventHandler class will automatically appear in the
/// 'Execute On Event' dropdown menu when a block is selected.
/// </summary>
[RequireComponent(typeof(Block))]
[RequireComponent(typeof(IBlock))]
[RequireComponent(typeof(Flowchart))]
[AddComponentMenu("")]
public class EventHandler : MonoBehaviour
@ -44,7 +44,7 @@ namespace Fungus
[HideInInspector]
[FormerlySerializedAs("parentSequence")]
[SerializeField] protected Block parentBlock;
public virtual Block ParentBlock { get { return parentBlock; } set { parentBlock = value; } }
public virtual IBlock ParentBlock { get { return parentBlock; } set { parentBlock = (Block)value; } }
/// <summary>
/// The Event Handler should call this method when the event is detected.

64
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -94,7 +94,7 @@ namespace Fungus
[HideInInspector]
[FormerlySerializedAs("selectedSequence")]
[SerializeField] protected Block selectedBlock;
public virtual Block SelectedBlock { get { return selectedBlock; } set { selectedBlock = value; } }
public virtual IBlock SelectedBlock { get { return selectedBlock; } set { selectedBlock = (Block)value; } }
/// <summary>
/// Currently selected command in the Flowchart editor.
@ -203,8 +203,8 @@ namespace Fungus
public int NextItemId()
{
int maxId = -1;
Block[] blocks = GetComponents<Block>();
foreach (Block block in blocks)
IBlock[] blocks = GetComponents<IBlock>();
foreach (IBlock block in blocks)
{
maxId = Math.Max(maxId, block.ItemId);
}
@ -309,8 +309,8 @@ namespace Fungus
// Make sure item ids are unique and monotonically increasing.
// This should always be the case, but some legacy Flowcharts may have issues.
List<int> usedIds = new List<int>();
Block[] blocks = GetComponents<Block>();
foreach (Block block in blocks)
IBlock[] blocks = GetComponents<IBlock>();
foreach (IBlock block in blocks)
{
if (block.ItemId == -1 ||
usedIds.Contains(block.ItemId))
@ -338,7 +338,7 @@ namespace Fungus
// Unreferenced components don't have any effect on the flowchart behavior, but
// they waste memory so should be cleared out periodically.
Block[] blocks = GetComponents<Block>();
IBlock[] blocks = GetComponents<IBlock>();
// Remove any null entries in the variables list
// It shouldn't happen but it seemed to occur for a user on the forum
@ -355,7 +355,7 @@ namespace Fungus
foreach (Command command in GetComponents<Command>())
{
bool found = false;
foreach (Block block in blocks)
foreach (IBlock block in blocks)
{
if (block.CommandList.Contains(command))
{
@ -373,7 +373,7 @@ namespace Fungus
foreach (EventHandler eventHandler in GetComponents<EventHandler>())
{
bool found = false;
foreach (Block block in blocks)
foreach (IBlock block in blocks)
{
if (block._EventHandler == eventHandler)
{
@ -411,9 +411,9 @@ namespace Fungus
/// <summary>
/// Returns the named Block in the flowchart, or null if not found.
/// </summary>
public virtual Block FindBlock(string blockName)
public virtual IBlock FindBlock(string blockName)
{
Block [] blocks = GetComponents<Block>();
IBlock [] blocks = GetComponents<IBlock>();
foreach (Block block in blocks)
{
if (block.BlockName == blockName)
@ -430,8 +430,8 @@ namespace Fungus
/// You can use this method in a UI event. e.g. to handle a button click.
public virtual void ExecuteBlock(string blockName)
{
Block block = null;
foreach (Block b in GetComponents<Block>())
IBlock block = null;
foreach (IBlock b in GetComponents<IBlock>())
{
if (b.BlockName == blockName)
{
@ -458,7 +458,7 @@ namespace Fungus
/// This version provides extra options to control how the block is executed.
/// Returns true if the Block started execution.
/// </summary>
public virtual bool ExecuteBlock(Block block, int commandIndex = 0, Action onComplete = null)
public virtual bool ExecuteBlock(IBlock block, int commandIndex = 0, Action onComplete = null)
{
if (block == null)
{
@ -466,7 +466,7 @@ namespace Fungus
return false;
}
if (block.gameObject != gameObject)
if (((Block)block).gameObject != gameObject)
{
Debug.LogError("Block must belong to the same gameobject as this Flowchart");
return false;
@ -489,8 +489,8 @@ namespace Fungus
/// </summary>
public virtual void StopAllBlocks()
{
Block [] blocks = GetComponents<Block>();
foreach (Block block in blocks)
IBlock [] blocks = GetComponents<IBlock>();
foreach (IBlock block in blocks)
{
if (block.IsExecuting())
{
@ -577,7 +577,7 @@ namespace Fungus
/// <summary>
/// Returns a new Block key that is guaranteed not to clash with any existing Block in the Flowchart.
/// </summary>
public virtual string GetUniqueBlockKey(string originalKey, Block ignoreBlock = null)
public virtual string GetUniqueBlockKey(string originalKey, IBlock ignoreBlock = null)
{
int suffix = 0;
string baseKey = originalKey.Trim();
@ -588,13 +588,13 @@ namespace Fungus
baseKey = "New Block";
}
Block[] blocks = GetComponents<Block>();
IBlock[] blocks = GetComponents<IBlock>();
string key = baseKey;
while (true)
{
bool collision = false;
foreach(Block block in blocks)
foreach(IBlock block in blocks)
{
if (block == ignoreBlock ||
block.BlockName == null)
@ -631,7 +631,7 @@ namespace Fungus
baseKey = "New Label";
}
Block block = ignoreLabel.ParentBlock;
IBlock block = ignoreLabel.ParentBlock;
string key = baseKey;
while (true)
@ -871,13 +871,15 @@ namespace Fungus
{
if (hideComponents)
{
Block[] blocks = GetComponents<Block>();
foreach (Block block in blocks)
IBlock[] blocks = GetComponents<IBlock>();
foreach (IBlock block in blocks)
{
block.hideFlags = HideFlags.HideInInspector;
if (block.gameObject != gameObject)
GameObject go = ((Block)block).gameObject;
go.hideFlags = HideFlags.HideInInspector;
if (go != gameObject)
{
block.gameObject.hideFlags = HideFlags.HideInHierarchy;
go.hideFlags = HideFlags.HideInHierarchy;
}
}
@ -975,8 +977,8 @@ namespace Fungus
/// </summary>
public virtual bool HasExecutingBlocks()
{
Block[] blocks = GetComponents<Block>();
foreach (Block block in blocks)
IBlock[] blocks = GetComponents<IBlock>();
foreach (IBlock block in blocks)
{
if (block.IsExecuting())
{
@ -989,12 +991,12 @@ namespace Fungus
/// <summary>
/// Returns a list of all executing blocks in this Flowchart.
/// </summary>
public virtual List<Block> GetExecutingBlocks()
public virtual List<IBlock> GetExecutingBlocks()
{
List<Block> executingBlocks = new List<Block>();
List<IBlock> executingBlocks = new List<IBlock>();
Block[] blocks = GetComponents<Block>();
foreach (Block block in blocks)
IBlock[] blocks = GetComponents<IBlock>();
foreach (IBlock block in blocks)
{
if (block.IsExecuting())
{

2
Assets/Fungus/Flowchart/Scripts/IBlock.cs

@ -98,7 +98,7 @@ namespace Fungus
/// <summary>
/// Returns a list of all Blocks connected to this one.
/// </summary>
List<Block> GetConnectedBlocks();
List<IBlock> GetConnectedBlocks();
/// <summary>
/// Returns the type of the previously executing command.

8
Assets/Fungus/Flowchart/Scripts/ICommand.cs

@ -45,7 +45,7 @@ namespace Fungus
/// This reference is only populated at runtime and in the editor when the
/// block is selected.
/// </summary>
Block ParentBlock { get; set; }
IBlock ParentBlock { get; set; }
/// <summary>
/// Returns the Flowchart that this command belongs to.
@ -84,12 +84,12 @@ namespace Fungus
/// <summary>
/// Called when the new command is added to a block in the editor.
/// </summary>
void OnCommandAdded(Block parentBlock);
void OnCommandAdded(IBlock parentBlock);
/// <summary>
/// Called when the command is deleted from a block in the editor.
/// </summary>
void OnCommandRemoved(Block parentBlock);
void OnCommandRemoved(IBlock parentBlock);
/// <summary>
/// Called when this command starts execution.
@ -109,7 +109,7 @@ namespace Fungus
/// <summary>
/// Populates a list with the Blocks that this command references.
/// </summary>
void GetConnectedBlocks(ref List<Block> connectedBlocks);
void GetConnectedBlocks(ref List<IBlock> connectedBlocks);
/// <summary>
/// Returns true if this command references the variable.

15
Assets/Fungus/Flowchart/Scripts/INode.cs

@ -0,0 +1,15 @@
using UnityEngine;
namespace Fungus
{
/// <summary>
/// Flowchart nodes.
/// </summary>
public interface INode
{
/// <summary>
/// Returns the display rect.
/// </summary>
Rect _NodeRect { get; set; }
}
}

12
Assets/Fungus/Flowchart/Scripts/INode.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e9eea916a931c4fed81b254bae8c0b93
timeCreated: 1473862805
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

7
Assets/Fungus/Flowchart/Scripts/Node.cs

@ -9,9 +9,14 @@ namespace Fungus
/// Base class for Flowchart nodes.
/// </summary>
[AddComponentMenu("")]
public class Node : MonoBehaviour
public class Node : MonoBehaviour, INode
{
[SerializeField] protected Rect nodeRect = new Rect(0, 0, 120, 30);
#region INode implementation
public virtual Rect _NodeRect { get { return nodeRect; } set { nodeRect = value; } }
#endregion
}
}

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

@ -45,7 +45,7 @@ namespace Fungus
if (targetBlock != null)
{
// Check if calling your own parent block
if (targetBlock == ParentBlock)
if (ParentBlock.Equals(targetBlock))
{
// Just ignore the callmode in this case, and jump to first command in list
Continue(0);
@ -91,7 +91,7 @@ namespace Fungus
}
}
public override void GetConnectedBlocks(ref List<Block> connectedBlocks)
public override void GetConnectedBlocks(ref List<IBlock> connectedBlocks)
{
if (targetBlock != null)
{

2
Assets/Fungus/Scripts/Commands/ControlStage.cs

@ -197,7 +197,7 @@ namespace Fungus
return new Color32(230, 200, 250, 255);
}
public override void OnCommandAdded(Block parentBlock)
public override void OnCommandAdded(IBlock parentBlock)
{
//Default to display type: show
display = StageDisplayType.Show;

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

@ -62,7 +62,7 @@ namespace Fungus
Continue();
}
public override void GetConnectedBlocks(ref List<Block> connectedBlocks)
public override void GetConnectedBlocks(ref List<IBlock> connectedBlocks)
{
if (targetBlock != null)
{

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

@ -37,7 +37,7 @@ namespace Fungus
Continue();
}
public override void GetConnectedBlocks(ref List<Block> connectedBlocks)
public override void GetConnectedBlocks(ref List<IBlock> connectedBlocks)
{
if (targetBlock != null)
{

2
Assets/Fungus/Scripts/Commands/Portrait.cs

@ -207,7 +207,7 @@ namespace Fungus
return new Color32(230, 200, 250, 255);
}
public override void OnCommandAdded(Block parentBlock)
public override void OnCommandAdded(IBlock parentBlock)
{
//Default to display type: show
display = DisplayType.Show;

2
Assets/Fungus/Scripts/Commands/SetInteractable.cs

@ -81,7 +81,7 @@ namespace Fungus
return new Color32(180, 250, 250, 255);
}
public override void OnCommandAdded(Block parentBlock)
public override void OnCommandAdded(IBlock parentBlock)
{
targetObjects.Add(null);
}

2
Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs

@ -72,7 +72,7 @@ namespace Fungus
return false;
}
public override void OnCommandAdded(Block parentBlock)
public override void OnCommandAdded(IBlock parentBlock)
{
// Add a default empty entry
targetSprites.Add(null);

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

@ -31,7 +31,7 @@ namespace Fungus
flowchart = GetFlowchart();
}
Block block = flowchart.FindBlock(blockName.Value);
IBlock block = flowchart.FindBlock(blockName.Value);
if (block == null ||
!block.IsExecuting())
{

2
Assets/Fungus/Scripts/Commands/TweenUI.cs

@ -65,7 +65,7 @@ namespace Fungus
Continue();
}
public override void OnCommandAdded(Block parentBlock)
public override void OnCommandAdded(IBlock parentBlock)
{
// Add an empty slot by default. Saves an unnecessary user click.
if (targetObjects.Count == 0)

4
Assets/Fungus/Scripts/Components/Localization.cs

@ -138,8 +138,8 @@ namespace Fungus
Flowchart[] flowcharts = GameObject.FindObjectsOfType<Flowchart>();
foreach (Flowchart flowchart in flowcharts)
{
Block[] blocks = flowchart.GetComponents<Block>();
foreach (Block block in blocks)
IBlock[] blocks = flowchart.GetComponents<IBlock>();
foreach (IBlock block in blocks)
{
foreach (Command command in block.CommandList)
{

8
Assets/Fungus/Scripts/Components/MenuDialog.cs

@ -72,7 +72,7 @@ namespace Fungus
Canvas.ForceUpdateCanvases();
}
protected virtual IEnumerator WaitForTimeout(float timeoutDuration, Block targetBlock)
protected virtual IEnumerator WaitForTimeout(float timeoutDuration, IBlock targetBlock)
{
float elapsedTime = 0;
@ -147,7 +147,7 @@ namespace Fungus
}
}
public virtual bool AddOption(string text, bool interactable, Block targetBlock)
public virtual bool AddOption(string text, bool interactable, IBlock targetBlock)
{
bool addedOption = false;
foreach (Button button in cachedButtons)
@ -169,7 +169,7 @@ namespace Fungus
textComponent.text = text;
}
Block block = targetBlock;
IBlock block = targetBlock;
button.onClick.AddListener(delegate {
@ -244,7 +244,7 @@ namespace Fungus
return addedOption;
}
public virtual void ShowTimer(float duration, Block targetBlock)
public virtual void ShowTimer(float duration, IBlock targetBlock)
{
if (cachedSlider != null)
{

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

@ -20,13 +20,13 @@ namespace Fungus
{
protected class SetEventHandlerOperation
{
public Block block;
public IBlock block;
public Type eventHandlerType;
}
protected class AddCommandOperation
{
public Block block;
public IBlock block;
public Type commandType;
public int index;
}
@ -59,7 +59,7 @@ namespace Fungus
EditorGUI.PropertyField(blockNameRect, blockNameProperty, new GUIContent(""));
// Ensure block name is unique for this Flowchart
Block block = target as Block;
IBlock block = target as IBlock;
string uniqueName = flowchart.GetUniqueBlockKey(blockNameProperty.stringValue, block);
if (uniqueName != block.BlockName)
{
@ -89,7 +89,7 @@ namespace Fungus
actionList.Clear();
}
Block block = target as Block;
IBlock block = target as IBlock;
SerializedProperty commandListProperty = serializedObject.FindProperty("commandList");
@ -114,7 +114,7 @@ namespace Fungus
ReorderableListGUI.Title("Commands");
CommandListAdaptor adaptor = new CommandListAdaptor(commandListProperty, 0);
adaptor.nodeRect = block._NodeRect;
adaptor.nodeRect = (block as INode)._NodeRect;
ReorderableListFlags flags = ReorderableListFlags.HideAddButton | ReorderableListFlags.HideRemoveButtons | ReorderableListFlags.DisableContextMenu;
@ -305,7 +305,7 @@ namespace Fungus
// event handler selected.
List<System.Type> eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList();
Block block = target as Block;
IBlock block = target as IBlock;
System.Type currentType = null;
if (block._EventHandler != null)
{
@ -382,14 +382,14 @@ namespace Fungus
protected void OnSelectEventHandler(object obj)
{
SetEventHandlerOperation operation = obj as SetEventHandlerOperation;
Block block = operation.block;
IBlock block = operation.block;
System.Type selectedType = operation.eventHandlerType;
if (block == null)
{
return;
}
Undo.RecordObject(block, "Set Event Handler");
Undo.RecordObject((Block)block, "Set Event Handler");
if (block._EventHandler != null)
{
@ -398,13 +398,13 @@ namespace Fungus
if (selectedType != null)
{
EventHandler newHandler = Undo.AddComponent(block.gameObject, selectedType) as EventHandler;
EventHandler newHandler = Undo.AddComponent(((Block)block).gameObject, selectedType) as EventHandler;
newHandler.ParentBlock = block;
block._EventHandler = newHandler;
}
// Because this is an async call, we need to force prefab instances to record changes
PrefabUtility.RecordPrefabInstancePropertyModifications(block);
PrefabUtility.RecordPrefabInstancePropertyModifications((Block)block);
}
static public void BlockField(SerializedProperty property, GUIContent label, GUIContent nullLabel, Flowchart flowchart)
@ -414,14 +414,14 @@ namespace Fungus
return;
}
Block block = property.objectReferenceValue as Block;
IBlock block = property.objectReferenceValue as IBlock;
// Build dictionary of child blocks
List<GUIContent> blockNames = new List<GUIContent>();
int selectedIndex = 0;
blockNames.Add(nullLabel);
Block[] blocks = flowchart.GetComponents<Block>();
IBlock[] blocks = flowchart.GetComponents<IBlock>();
for (int i = 0; i < blocks.Length; ++i)
{
blockNames.Add(new GUIContent(blocks[i].BlockName));
@ -442,7 +442,7 @@ namespace Fungus
block = blocks[selectedIndex - 1];
}
property.objectReferenceValue = block;
property.objectReferenceValue = (Block)block;
}
static public Block BlockField(Rect position, GUIContent nullLabel, Flowchart flowchart, Block block)
@ -631,7 +631,7 @@ namespace Fungus
protected virtual void ShowCommandMenu()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
@ -722,7 +722,7 @@ namespace Fungus
{
AddCommandOperation commandOperation = obj as AddCommandOperation;
Block block = commandOperation.block;
IBlock block = commandOperation.block;
if (block == null)
{
return;
@ -732,7 +732,7 @@ namespace Fungus
flowchart.ClearSelectedCommands();
Command newCommand = Undo.AddComponent(block.gameObject, commandOperation.commandType) as Command;
Command newCommand = Undo.AddComponent(((Block)block).gameObject, commandOperation.commandType) as Command;
block.GetFlowchart().AddSelectedCommand(newCommand);
newCommand.ParentBlock = block;
newCommand.ItemId = flowchart.NextItemId();
@ -740,7 +740,7 @@ namespace Fungus
// Let command know it has just been added to the block
newCommand.OnCommandAdded(block);
Undo.RecordObject(block, "Set command type");
Undo.RecordObject((Block)block, "Set command type");
if (commandOperation.index < block.CommandList.Count - 1)
{
block.CommandList.Insert(commandOperation.index, newCommand);
@ -751,12 +751,12 @@ namespace Fungus
}
// Because this is an async call, we need to force prefab instances to record changes
PrefabUtility.RecordPrefabInstancePropertyModifications(block);
PrefabUtility.RecordPrefabInstancePropertyModifications((Block)block);
}
public virtual void ShowContextMenu()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
if (flowchart == null)
@ -844,7 +844,7 @@ namespace Fungus
protected void SelectAll()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
if (flowchart == null ||
@ -865,7 +865,7 @@ namespace Fungus
protected void SelectNone()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
if (flowchart == null ||
@ -888,7 +888,7 @@ namespace Fungus
protected void Copy()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
if (flowchart == null ||
@ -918,7 +918,7 @@ namespace Fungus
protected void Paste()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
if (flowchart == null ||
@ -970,14 +970,14 @@ namespace Fungus
}
// Because this is an async call, we need to force prefab instances to record changes
PrefabUtility.RecordPrefabInstancePropertyModifications(block);
PrefabUtility.RecordPrefabInstancePropertyModifications((Block)block);
Repaint();
}
protected void Delete()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
if (flowchart == null ||
@ -998,7 +998,7 @@ namespace Fungus
// Order of destruction is important here for undo to work
Undo.DestroyObjectImmediate(command);
Undo.RecordObject(flowchart.SelectedBlock, "Delete");
Undo.RecordObject((Block)flowchart.SelectedBlock, "Delete");
flowchart.SelectedBlock.CommandList.RemoveAt(i);
lastSelectedIndex = i;
@ -1022,7 +1022,7 @@ namespace Fungus
protected void PlayCommand()
{
Block targetBlock = target as Block;
IBlock targetBlock = target as IBlock;
Flowchart flowchart = targetBlock.GetFlowchart();
Command command = flowchart.SelectedCommands[0];
if (targetBlock.IsExecuting())
@ -1042,7 +1042,7 @@ namespace Fungus
protected void StopAllPlayCommand()
{
Block targetBlock = target as Block;
IBlock targetBlock = target as IBlock;
Flowchart flowchart = targetBlock.GetFlowchart();
Command command = flowchart.SelectedCommands[0];
@ -1051,7 +1051,7 @@ namespace Fungus
flowchart.StartCoroutine(RunBlock(flowchart, targetBlock, command.CommandIndex, 0.2f));
}
protected IEnumerator RunBlock(Flowchart flowchart, Block targetBlock, int commandIndex, float delay)
protected IEnumerator RunBlock(Flowchart flowchart, IBlock targetBlock, int commandIndex, float delay)
{
yield return new WaitForSeconds(delay);
flowchart.ExecuteBlock(targetBlock, commandIndex);
@ -1059,7 +1059,7 @@ namespace Fungus
protected void SelectPrevious()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
int firstSelectedIndex = flowchart.SelectedBlock.CommandList.Count;
@ -1099,7 +1099,7 @@ namespace Fungus
protected void SelectNext()
{
Block block = target as Block;
IBlock block = target as IBlock;
Flowchart flowchart = block.GetFlowchart();
int lastSelectedIndex = -1;

10
Assets/Fungus/Scripts/Editor/BlockInspector.cs

@ -70,20 +70,20 @@ namespace Fungus
public override void OnInspectorGUI ()
{
BlockInspector blockInspector = target as BlockInspector;
Block block = blockInspector.block;
if (block == null)
if (blockInspector.block == null)
{
return;
}
IBlock block = blockInspector.block;
Flowchart flowchart = block.GetFlowchart();
if (activeBlockEditor == null ||
block != activeBlockEditor.target)
!block.Equals(activeBlockEditor.target))
{
DestroyImmediate(activeBlockEditor);
activeBlockEditor = Editor.CreateEditor(block) as BlockEditor;
activeBlockEditor = Editor.CreateEditor((Block)block) as BlockEditor;
}
activeBlockEditor.DrawBlockName(flowchart);

8
Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs

@ -84,13 +84,13 @@ namespace Fungus
return null;
}
Block block = flowchart.SelectedBlock;
IBlock block = flowchart.SelectedBlock;
if (block == null)
{
return null;
}
Command newCommand = Undo.AddComponent<Comment>(block.gameObject) as Command;
Command newCommand = Undo.AddComponent<Comment>(((Block)block).gameObject) as Command;
newCommand.ItemId = flowchart.NextItemId();
flowchart.ClearSelectedCommands();
flowchart.AddSelectedCommand(newCommand);
@ -103,10 +103,10 @@ namespace Fungus
Command command = _arrayProperty.GetArrayElementAtIndex(index).objectReferenceValue as Command;
// Add the command as a new component
Block parentBlock = command.GetComponent<Block>();
IBlock parentBlock = command.GetComponent<IBlock>();
System.Type type = command.GetType();
Command newCommand = Undo.AddComponent(parentBlock.gameObject, type) as Command;
Command newCommand = Undo.AddComponent(((Block)parentBlock).gameObject, type) as Command;
newCommand.ItemId = newCommand.GetFlowchart().NextItemId();
System.Reflection.FieldInfo[] fields = type.GetFields();
foreach (System.Reflection.FieldInfo field in fields)

2
Assets/Fungus/Scripts/Editor/FlowchartMenuItems.cs

@ -24,7 +24,7 @@ namespace Fungus
// Only the first created Flowchart in the scene should have a default GameStarted block
if (GameObject.FindObjectsOfType<Flowchart>().Length > 1)
{
Block block = go.GetComponent<Block>();
IBlock block = go.GetComponent<IBlock>();
block._EventHandler = null;
GameObject.DestroyImmediate(block._EventHandler);
}

120
Assets/Fungus/Scripts/Editor/FlowchartWindow.cs

@ -12,9 +12,9 @@ namespace Fungus
{
public class FlowchartWindow : EditorWindow
{
public static List<Block> deleteList = new List<Block>();
public static List<IBlock> deleteList = new List<IBlock>();
protected List<Block> windowBlockMap = new List<Block>();
protected List<IBlock> windowBlockMap = new List<IBlock>();
// The ReorderableList control doesn't drag properly when used with GUI.DragWindow(),
// so we just implement dragging ourselves.
@ -74,7 +74,7 @@ namespace Fungus
{
ShowBlockInspector(flowchart);
}
blockInspector.block = flowchart.SelectedBlock;
blockInspector.block = (Block)flowchart.SelectedBlock;
}
forceRepaintCount--;
@ -117,7 +117,7 @@ namespace Fungus
}
// Delete any scheduled objects
foreach (Block deleteBlock in deleteList)
foreach (IBlock deleteBlock in deleteList)
{
bool isSelected = (flowchart.SelectedBlock == deleteBlock);
@ -126,7 +126,7 @@ namespace Fungus
Undo.DestroyObjectImmediate(command);
}
Undo.DestroyObjectImmediate(deleteBlock);
Undo.DestroyObjectImmediate((Block)deleteBlock);
flowchart.ClearSelectedCommands();
if (isSelected)
@ -219,15 +219,21 @@ namespace Fungus
protected virtual void DrawFlowchartView(Flowchart flowchart)
{
Block[] blocks = flowchart.GetComponents<Block>();
IBlock[] blocks = flowchart.GetComponents<IBlock>();
foreach (Block block in blocks)
foreach (IBlock block in blocks)
{
var node = block as INode;
if (node == null)
{
continue;
}
var newRect = new Rect();
newRect.xMin = Mathf.Min(flowchart.ScrollViewRect.xMin, block._NodeRect.xMin - 400);
newRect.xMax = Mathf.Max(flowchart.ScrollViewRect.xMax, block._NodeRect.xMax + 400);
newRect.yMin = Mathf.Min(flowchart.ScrollViewRect.yMin, block._NodeRect.yMin - 400);
newRect.yMax = Mathf.Max(flowchart.ScrollViewRect.yMax, block._NodeRect.yMax + 400);
newRect.xMin = Mathf.Min(flowchart.ScrollViewRect.xMin, node._NodeRect.xMin - 400);
newRect.xMax = Mathf.Max(flowchart.ScrollViewRect.xMax, node._NodeRect.xMax + 400);
newRect.yMin = Mathf.Min(flowchart.ScrollViewRect.yMin, node._NodeRect.yMin - 400);
newRect.yMax = Mathf.Max(flowchart.ScrollViewRect.yMax, node._NodeRect.yMax + 400);
flowchart.ScrollViewRect = newRect;
}
@ -257,11 +263,11 @@ namespace Fungus
CalcFlowchartCenter(flowchart, blocks);
// Draw connections
foreach (Block block in blocks)
foreach (IBlock block in blocks)
{
DrawConnections(flowchart, block, false);
}
foreach (Block block in blocks)
foreach (IBlock block in blocks)
{
DrawConnections(flowchart, block, true);
}
@ -274,7 +280,7 @@ namespace Fungus
windowBlockMap.Clear();
for (int i = 0; i < blocks.Length; ++i)
{
Block block = blocks[i];
IBlock block = blocks[i];
float nodeWidthA = nodeStyle.CalcSize(new GUIContent(block.BlockName)).x + 10;
float nodeWidthB = 0f;
@ -282,10 +288,12 @@ namespace Fungus
{
nodeWidthB = nodeStyle.CalcSize(new GUIContent(block._EventHandler.GetSummary())).x + 10;
}
var node = block as INode;
if (Event.current.button == 0)
{
Rect tempRect = block._NodeRect;
Rect tempRect = node._NodeRect;
tempRect.width = Mathf.Max(Mathf.Max(nodeWidthA, nodeWidthB), 120);
tempRect.height = 40;
@ -304,7 +312,7 @@ namespace Fungus
tempRect.x = startDragPosition.x;
tempRect.y = startDragPosition.y;
Undo.RecordObject(block, "Node Position");
Undo.RecordObject((Block)block, "Node Position");
tempRect.x = newPos.x;
tempRect.y = newPos.y;
@ -313,10 +321,10 @@ namespace Fungus
forceRepaintCount = 6;
}
block._NodeRect = tempRect;
node._NodeRect = tempRect;
}
Rect windowRect = new Rect(block._NodeRect);
Rect windowRect = new Rect(node._NodeRect);
windowRect.x += flowchart.ScrollPos.x;
windowRect.y += flowchart.ScrollPos.y;
@ -330,7 +338,7 @@ namespace Fungus
EndWindows();
// Draw Event Handler labels
foreach (Block block in blocks)
foreach (IBlock block in blocks)
{
if (block._EventHandler != null)
{
@ -347,8 +355,10 @@ namespace Fungus
handlerStyle.margin.bottom = 0;
handlerStyle.alignment = TextAnchor.MiddleCenter;
Rect rect = new Rect(block._NodeRect);
rect.height = handlerStyle.CalcHeight(new GUIContent(handlerLabel), block._NodeRect.width);
INode node = block as INode;
Rect rect = new Rect(node._NodeRect);
rect.height = handlerStyle.CalcHeight(new GUIContent(handlerLabel), node._NodeRect.width);
rect.x += flowchart.ScrollPos.x;
rect.y += flowchart.ScrollPos.y - rect.height;
@ -360,7 +370,7 @@ namespace Fungus
// Draw play icons beside all executing blocks
if (Application.isPlaying)
{
foreach (Block b in blocks)
foreach (IBlock b in blocks)
{
if (b.IsExecuting())
{
@ -371,7 +381,9 @@ namespace Fungus
if (b.ExecutingIconTimer > Time.realtimeSinceStartup)
{
Rect rect = new Rect(b._NodeRect);
INode node = b as INode;
Rect rect = new Rect(node._NodeRect);
rect.x += flowchart.ScrollPos.x - 37;
rect.y += flowchart.ScrollPos.y + 3;
@ -402,7 +414,7 @@ namespace Fungus
EditorZoomArea.End();
}
public virtual void CalcFlowchartCenter(Flowchart flowchart, Block[] blocks)
public virtual void CalcFlowchartCenter(Flowchart flowchart, IBlock[] blocks)
{
if (flowchart == null ||
blocks.Count() == 0)
@ -410,15 +422,17 @@ namespace Fungus
return;
}
Vector2 min = blocks[0]._NodeRect.min;
Vector2 max = blocks[0]._NodeRect.max;
Vector2 min = (blocks[0] as INode)._NodeRect.min;
Vector2 max = (blocks[0] as INode)._NodeRect.max;
foreach (Block block in blocks)
foreach (IBlock block in blocks)
{
min.x = Mathf.Min(min.x, block._NodeRect.center.x);
min.y = Mathf.Min(min.y, block._NodeRect.center.y);
max.x = Mathf.Max(max.x, block._NodeRect.center.x);
max.y = Mathf.Max(max.y, block._NodeRect.center.y);
INode node = block as INode;
min.x = Mathf.Min(min.x, node._NodeRect.center.x);
min.y = Mathf.Min(min.y, node._NodeRect.center.y);
max.x = Mathf.Max(max.x, node._NodeRect.center.x);
max.y = Mathf.Max(max.y, node._NodeRect.center.y);
}
Vector2 center = (min + max) * -0.5f;
@ -522,7 +536,7 @@ namespace Fungus
}
}
protected virtual void SelectBlock(Flowchart flowchart, Block block)
protected virtual void SelectBlock(Flowchart flowchart, IBlock block)
{
// Select the block and also select currently executing command
ShowBlockInspector(flowchart);
@ -545,20 +559,20 @@ namespace Fungus
return newBlock;
}
protected virtual void DeleteBlock(Flowchart flowchart, Block block)
protected virtual void DeleteBlock(Flowchart flowchart, IBlock block)
{
foreach (Command command in block.CommandList)
{
Undo.DestroyObjectImmediate(command);
}
Undo.DestroyObjectImmediate(block);
Undo.DestroyObjectImmediate((Block)block);
flowchart.ClearSelectedCommands();
}
protected virtual void DrawWindow(int windowId)
{
Block block = windowBlockMap[windowId];
IBlock block = windowBlockMap[windowId];
Flowchart flowchart = block.GetFlowchart();
if (flowchart == null)
@ -576,8 +590,11 @@ namespace Fungus
Event.current.alt == false)
{
dragWindowId = windowId;
startDragPosition.x = block._NodeRect.x;
startDragPosition.y = block._NodeRect.y;
var node = block as INode;
startDragPosition.x = node._NodeRect.x;
startDragPosition.y = node._NodeRect.y;
}
if (windowId < windowBlockMap.Count)
@ -601,9 +618,9 @@ namespace Fungus
else
{
// Count the number of unique connections (excluding self references)
List<Block> uniqueList = new List<Block>();
List<Block> connectedBlocks = block.GetConnectedBlocks();
foreach (Block connectedBlock in connectedBlocks)
List<IBlock> uniqueList = new List<IBlock>();
List<IBlock> connectedBlocks = block.GetConnectedBlocks();
foreach (IBlock connectedBlock in connectedBlocks)
{
if (connectedBlock == block ||
uniqueList.Contains(connectedBlock))
@ -626,13 +643,14 @@ namespace Fungus
nodeStyleCopy.normal.textColor = Color.black;
// Make sure node is wide enough to fit the node name text
var n = block as INode;
float width = nodeStyleCopy.CalcSize(new GUIContent(block.BlockName)).x;
Rect tempRect = block._NodeRect;
tempRect.width = Mathf.Max (block._NodeRect.width, width);
block._NodeRect = tempRect;
Rect tempRect = n._NodeRect;
tempRect.width = Mathf.Max (n._NodeRect.width, width);
n._NodeRect = tempRect;
GUI.backgroundColor = Color.white;
GUILayout.Box(block.BlockName, nodeStyleCopy, GUILayout.Width(block._NodeRect.width), GUILayout.Height(block._NodeRect.height));
GUILayout.Box(block.BlockName, nodeStyleCopy, GUILayout.Width(n._NodeRect.width), GUILayout.Height(n._NodeRect.height));
if (block.Description.Length > 0)
{
@ -652,14 +670,14 @@ namespace Fungus
}
}
protected virtual void DrawConnections(Flowchart flowchart, Block block, bool highlightedOnly)
protected virtual void DrawConnections(Flowchart flowchart, IBlock block, bool highlightedOnly)
{
if (block == null)
{
return;
}
List<Block> connectedBlocks = new List<Block>();
List<IBlock> connectedBlocks = new List<IBlock>();
bool blockIsSelected = (flowchart.SelectedBlock == block);
@ -691,7 +709,7 @@ namespace Fungus
connectedBlocks.Clear();
command.GetConnectedBlocks(ref connectedBlocks);
foreach (Block blockB in connectedBlocks)
foreach (IBlock blockB in connectedBlocks)
{
if (blockB == null ||
block == blockB ||
@ -700,11 +718,11 @@ namespace Fungus
continue;
}
Rect startRect = new Rect(block._NodeRect);
Rect startRect = new Rect((block as INode)._NodeRect);
startRect.x += flowchart.ScrollPos.x;
startRect.y += flowchart.ScrollPos.y;
Rect endRect = new Rect(blockB._NodeRect);
Rect endRect = new Rect((blockB as INode)._NodeRect);
endRect.x += flowchart.ScrollPos.x;
endRect.y += flowchart.ScrollPos.y;
@ -764,7 +782,7 @@ namespace Fungus
public static void DeleteBlock(object obj)
{
Block block = obj as Block;
IBlock block = obj as IBlock;
FlowchartWindow.deleteList.Add(block);
}

4
Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs

@ -44,7 +44,7 @@ namespace Fungus
/// <param name="text">The option text to display on the button.</param>
/// <param name="interactable">If false, the option is displayed but is not selectable.</param>
/// <param name="targetBlock">Block to execute when the option is selected.</param>
bool AddOption(string text, bool interactable, Block targetBlock);
bool AddOption(string text, bool interactable, IBlock targetBlock);
/// <summary>
/// Adds the option to the list of displayed options, calls a Lua function when selected.
@ -58,7 +58,7 @@ namespace Fungus
/// </summary>
/// <param name="duration">The duration during which the player can select an option.</param>
/// <param name="targetBlock">Block to execute if the player does not select an option in time.</param>
void ShowTimer(float duration, Block targetBlock);
void ShowTimer(float duration, IBlock targetBlock);
/// <summary>
/// Show a timer during which the player can select an option. Calls a Lua function when the timer expires.

Loading…
Cancel
Save