From 0d33e6a0266faa6c07caca3d82442deccb5144b4 Mon Sep 17 00:00:00 2001 From: Christopher Date: Fri, 16 Sep 2016 15:36:10 +0100 Subject: [PATCH] Use concrete classes for Block and Command to ensure editor code is robust. --- Assets/Fungus/Flowchart/Scripts/Block.cs | 6 +- Assets/Fungus/Flowchart/Scripts/Command.cs | 8 +- .../Fungus/Flowchart/Scripts/EventHandler.cs | 12 +-- Assets/Fungus/Flowchart/Scripts/Flowchart.cs | 49 ++++----- Assets/Fungus/Flowchart/Scripts/IBlock.cs | 2 +- Assets/Fungus/Flowchart/Scripts/ICommand.cs | 8 +- .../Fungus/Flowchart/Scripts/IEventHandler.cs | 2 +- Assets/Fungus/Flowchart/Scripts/IFlowchart.cs | 2 +- Assets/Fungus/Flowchart/Scripts/INode.cs | 15 --- Assets/Fungus/Flowchart/Scripts/INode.cs.meta | 12 --- Assets/Fungus/Flowchart/Scripts/Node.cs | 4 +- Assets/Fungus/Scripts/Commands/Call.cs | 4 +- .../Fungus/Scripts/Commands/ControlStage.cs | 2 +- Assets/Fungus/Scripts/Commands/If.cs | 2 +- Assets/Fungus/Scripts/Commands/Jump.cs | 2 +- Assets/Fungus/Scripts/Commands/Menu.cs | 2 +- Assets/Fungus/Scripts/Commands/MenuTimer.cs | 2 +- Assets/Fungus/Scripts/Commands/Portrait.cs | 2 +- .../Scripts/Commands/SetInteractable.cs | 2 +- .../Fungus/Scripts/Commands/SetSpriteOrder.cs | 2 +- Assets/Fungus/Scripts/Commands/TweenUI.cs | 2 +- .../Fungus/Scripts/Components/Localization.cs | 6 +- .../Fungus/Scripts/Components/MenuDialog.cs | 8 +- Assets/Fungus/Scripts/Editor/BlockEditor.cs | 68 ++++++------ .../Fungus/Scripts/Editor/BlockInspector.cs | 16 +-- .../Scripts/Editor/CommandListAdaptor.cs | 8 +- .../Scripts/Editor/FlowchartMenuItems.cs | 2 +- .../Fungus/Scripts/Editor/FlowchartWindow.cs | 102 +++++++++--------- .../Fungus/Scripts/Interfaces/IMenuDialog.cs | 4 +- .../FungusLua/Scripts/LuaBindings.cs | 2 +- 30 files changed, 160 insertions(+), 198 deletions(-) delete mode 100644 Assets/Fungus/Flowchart/Scripts/INode.cs delete mode 100644 Assets/Fungus/Flowchart/Scripts/INode.cs.meta diff --git a/Assets/Fungus/Flowchart/Scripts/Block.cs b/Assets/Fungus/Flowchart/Scripts/Block.cs index 6bfb3d65..f007a31b 100644 --- a/Assets/Fungus/Flowchart/Scripts/Block.cs +++ b/Assets/Fungus/Flowchart/Scripts/Block.cs @@ -265,10 +265,10 @@ namespace Fungus jumpToCommandIndex = int.MaxValue; } - public virtual List GetConnectedBlocks() + public virtual List GetConnectedBlocks() { - var connectedBlocks = new List(); - foreach (ICommand command in commandList) + var connectedBlocks = new List(); + foreach (var command in commandList) { if (command != null) { diff --git a/Assets/Fungus/Flowchart/Scripts/Command.cs b/Assets/Fungus/Flowchart/Scripts/Command.cs index a5089f5b..059af20f 100644 --- a/Assets/Fungus/Flowchart/Scripts/Command.cs +++ b/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. /// - public virtual IBlock ParentBlock { get; set; } + public virtual Block ParentBlock { get; set; } /// /// Returns the Flowchart that this command belongs to. @@ -160,13 +160,13 @@ namespace Fungus /// /// Called when the new command is added to a block in the editor. /// - public virtual void OnCommandAdded(IBlock parentBlock) + public virtual void OnCommandAdded(Block parentBlock) {} /// /// Called when the command is deleted from a block in the editor. /// - public virtual void OnCommandRemoved(IBlock parentBlock) + public virtual void OnCommandRemoved(Block parentBlock) {} /// @@ -190,7 +190,7 @@ namespace Fungus /// /// Populates a list with the Blocks that this command references. /// - public virtual void GetConnectedBlocks(ref List connectedBlocks) + public virtual void GetConnectedBlocks(ref List connectedBlocks) {} /// diff --git a/Assets/Fungus/Flowchart/Scripts/EventHandler.cs b/Assets/Fungus/Flowchart/Scripts/EventHandler.cs index 00cd860d..6699cc39 100644 --- a/Assets/Fungus/Flowchart/Scripts/EventHandler.cs +++ b/Assets/Fungus/Flowchart/Scripts/EventHandler.cs @@ -43,29 +43,29 @@ namespace Fungus #region IEventHandler - public virtual IBlock ParentBlock { get { return parentBlock; } set { parentBlock = (Block)value; } } + public virtual Block ParentBlock { get { return parentBlock; } set { parentBlock = value; } } public virtual bool ExecuteBlock() { - if (parentBlock == null) + if (ParentBlock == null) { return false; } - if (parentBlock._EventHandler != this) + if (ParentBlock._EventHandler != this) { return false; } - var flowchart = parentBlock.GetFlowchart(); + var flowchart = ParentBlock.GetFlowchart(); // Auto-follow the executing block if none is currently selected if (flowchart.SelectedBlock == null) { - flowchart.SelectedBlock = parentBlock; + flowchart.SelectedBlock = ParentBlock; } - return flowchart.ExecuteBlock(parentBlock); + return flowchart.ExecuteBlock(ParentBlock); } public virtual string GetSummary() diff --git a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs index fcf212e3..28bb1843 100644 --- a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs +++ b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs @@ -206,8 +206,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 usedIds = new List(); - IBlock[] blocks = GetComponents(); - foreach (IBlock block in blocks) + var blocks = GetComponents(); + foreach (var block in blocks) { if (block.ItemId == -1 || usedIds.Contains(block.ItemId)) @@ -235,8 +235,6 @@ namespace Fungus // Unreferenced components don't have any effect on the flowchart behavior, but // they waste memory so should be cleared out periodically. - IBlock[] blocks = GetComponents(); - // Remove any null entries in the variables list // It shouldn't happen but it seemed to occur for a user on the forum variables.RemoveAll(item => item == null); @@ -249,10 +247,12 @@ namespace Fungus } } + var blocks = GetComponents(); + foreach (var command in GetComponents()) { bool found = false; - foreach (IBlock block in blocks) + foreach (var block in blocks) { if (block.CommandList.Contains(command)) { @@ -270,7 +270,7 @@ namespace Fungus foreach (EventHandler eventHandler in GetComponents()) { bool found = false; - foreach (IBlock block in blocks) + foreach (var block in blocks) { if (block._EventHandler == eventHandler) { @@ -306,7 +306,7 @@ namespace Fungus public virtual Rect ScrollViewRect { get { return scrollViewRect; } set { scrollViewRect = value; } } - public virtual IBlock SelectedBlock { get { return selectedBlock; } set { selectedBlock = (Block)value; } } + public virtual Block SelectedBlock { get { return selectedBlock; } set { selectedBlock = value; } } public virtual List SelectedCommands { get { return selectedCommands; } } @@ -345,8 +345,8 @@ namespace Fungus public int NextItemId() { int maxId = -1; - IBlock[] blocks = GetComponents(); - foreach (IBlock block in blocks) + var blocks = GetComponents(); + foreach (var block in blocks) { maxId = Math.Max(maxId, block.ItemId); } @@ -371,8 +371,8 @@ namespace Fungus public virtual IBlock FindBlock(string blockName) { - IBlock [] blocks = GetComponents(); - foreach (Block block in blocks) + var blocks = GetComponents(); + foreach (var block in blocks) { if (block.BlockName == blockName) { @@ -385,15 +385,7 @@ namespace Fungus public virtual void ExecuteBlock(string blockName) { - IBlock block = null; - foreach (IBlock b in GetComponents()) - { - if (b.BlockName == blockName) - { - block = b; - break; - } - } + var block = FindBlock(blockName); if (block == null) { @@ -435,7 +427,7 @@ namespace Fungus public virtual void StopAllBlocks() { - IBlock [] blocks = GetComponents(); + var blocks = GetComponents(); foreach (IBlock block in blocks) { if (block.IsExecuting()) @@ -511,13 +503,13 @@ namespace Fungus baseKey = "New Block"; } - IBlock[] blocks = GetComponents(); + var blocks = GetComponents(); string key = baseKey; while (true) { bool collision = false; - foreach(IBlock block in blocks) + foreach (var block in blocks) { if (block == ignoreBlock || block.BlockName == null) @@ -557,7 +549,7 @@ namespace Fungus while (true) { bool collision = false; - foreach (ICommand command in block.CommandList) + foreach (var command in block.CommandList) { Label label = command as Label; if (label == null || @@ -825,7 +817,7 @@ namespace Fungus public virtual bool HasExecutingBlocks() { - IBlock[] blocks = GetComponents(); + var blocks = GetComponents(); foreach (IBlock block in blocks) { if (block.IsExecuting()) @@ -838,10 +830,9 @@ namespace Fungus public virtual List GetExecutingBlocks() { - List executingBlocks = new List(); - - IBlock[] blocks = GetComponents(); - foreach (IBlock block in blocks) + var executingBlocks = new List(); + var blocks = GetComponents(); + foreach (var block in blocks) { if (block.IsExecuting()) { diff --git a/Assets/Fungus/Flowchart/Scripts/IBlock.cs b/Assets/Fungus/Flowchart/Scripts/IBlock.cs index 417ff2fe..a23e1a34 100644 --- a/Assets/Fungus/Flowchart/Scripts/IBlock.cs +++ b/Assets/Fungus/Flowchart/Scripts/IBlock.cs @@ -99,7 +99,7 @@ namespace Fungus /// /// Returns a list of all Blocks connected to this one. /// - List GetConnectedBlocks(); + List GetConnectedBlocks(); /// /// Returns the type of the previously executing command. diff --git a/Assets/Fungus/Flowchart/Scripts/ICommand.cs b/Assets/Fungus/Flowchart/Scripts/ICommand.cs index 636da53d..600e7071 100644 --- a/Assets/Fungus/Flowchart/Scripts/ICommand.cs +++ b/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. /// - IBlock ParentBlock { get; set; } + Block ParentBlock { get; set; } /// /// Returns the Flowchart that this command belongs to. @@ -84,12 +84,12 @@ namespace Fungus /// /// Called when the new command is added to a block in the editor. /// - void OnCommandAdded(IBlock parentBlock); + void OnCommandAdded(Block parentBlock); /// /// Called when the command is deleted from a block in the editor. /// - void OnCommandRemoved(IBlock parentBlock); + void OnCommandRemoved(Block parentBlock); /// /// Called when this command starts execution. @@ -109,7 +109,7 @@ namespace Fungus /// /// Populates a list with the Blocks that this command references. /// - void GetConnectedBlocks(ref List connectedBlocks); + void GetConnectedBlocks(ref List connectedBlocks); /// /// Returns true if this command references the variable. diff --git a/Assets/Fungus/Flowchart/Scripts/IEventHandler.cs b/Assets/Fungus/Flowchart/Scripts/IEventHandler.cs index 33998b3f..649340a5 100644 --- a/Assets/Fungus/Flowchart/Scripts/IEventHandler.cs +++ b/Assets/Fungus/Flowchart/Scripts/IEventHandler.cs @@ -15,7 +15,7 @@ namespace Fungus /// /// The parent Block which owns this Event Handler. /// - IBlock ParentBlock { get; set; } + Block ParentBlock { get; set; } /// /// The Event Handler should call this method when the event is detected to start executing the Block. diff --git a/Assets/Fungus/Flowchart/Scripts/IFlowchart.cs b/Assets/Fungus/Flowchart/Scripts/IFlowchart.cs index 2ea2f0b9..7bb76d01 100644 --- a/Assets/Fungus/Flowchart/Scripts/IFlowchart.cs +++ b/Assets/Fungus/Flowchart/Scripts/IFlowchart.cs @@ -43,7 +43,7 @@ namespace Fungus /// /// Currently selected block in the Flowchart editor. /// - IBlock SelectedBlock { get; set; } + Block SelectedBlock { get; set; } /// /// Currently selected command in the Flowchart editor. diff --git a/Assets/Fungus/Flowchart/Scripts/INode.cs b/Assets/Fungus/Flowchart/Scripts/INode.cs deleted file mode 100644 index 0285a067..00000000 --- a/Assets/Fungus/Flowchart/Scripts/INode.cs +++ /dev/null @@ -1,15 +0,0 @@ -using UnityEngine; - -namespace Fungus -{ - /// - /// Flowchart nodes. - /// - public interface INode - { - /// - /// Returns the display rect. - /// - Rect _NodeRect { get; set; } - } -} \ No newline at end of file diff --git a/Assets/Fungus/Flowchart/Scripts/INode.cs.meta b/Assets/Fungus/Flowchart/Scripts/INode.cs.meta deleted file mode 100644 index 9be7487a..00000000 --- a/Assets/Fungus/Flowchart/Scripts/INode.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: e9eea916a931c4fed81b254bae8c0b93 -timeCreated: 1473862805 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Fungus/Flowchart/Scripts/Node.cs b/Assets/Fungus/Flowchart/Scripts/Node.cs index 268b45a9..83908412 100644 --- a/Assets/Fungus/Flowchart/Scripts/Node.cs +++ b/Assets/Fungus/Flowchart/Scripts/Node.cs @@ -9,11 +9,11 @@ namespace Fungus /// Base class for Flowchart nodes. /// [AddComponentMenu("")] - public class Node : MonoBehaviour, INode + public class Node : MonoBehaviour { [SerializeField] protected Rect nodeRect = new Rect(0, 0, 120, 30); - #region INode implementation + #region Public methods public virtual Rect _NodeRect { get { return nodeRect; } set { nodeRect = value; } } diff --git a/Assets/Fungus/Scripts/Commands/Call.cs b/Assets/Fungus/Scripts/Commands/Call.cs index de22fe8e..3780a5c8 100644 --- a/Assets/Fungus/Scripts/Commands/Call.cs +++ b/Assets/Fungus/Scripts/Commands/Call.cs @@ -45,7 +45,7 @@ namespace Fungus if (targetBlock != null) { // Check if calling your own parent block - if (ParentBlock.Equals(targetBlock)) + if (ParentBlock != null && 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 connectedBlocks) + public override void GetConnectedBlocks(ref List connectedBlocks) { if (targetBlock != null) { diff --git a/Assets/Fungus/Scripts/Commands/ControlStage.cs b/Assets/Fungus/Scripts/Commands/ControlStage.cs index 73e67ef8..d0c43dbd 100644 --- a/Assets/Fungus/Scripts/Commands/ControlStage.cs +++ b/Assets/Fungus/Scripts/Commands/ControlStage.cs @@ -197,7 +197,7 @@ namespace Fungus return new Color32(230, 200, 250, 255); } - public override void OnCommandAdded(IBlock parentBlock) + public override void OnCommandAdded(Block parentBlock) { //Default to display type: show display = StageDisplayType.Show; diff --git a/Assets/Fungus/Scripts/Commands/If.cs b/Assets/Fungus/Scripts/Commands/If.cs index df7d54d5..52b98ce0 100644 --- a/Assets/Fungus/Scripts/Commands/If.cs +++ b/Assets/Fungus/Scripts/Commands/If.cs @@ -107,7 +107,7 @@ namespace Fungus // Find the next Else, ElseIf or End command at the same indent level as this If command for (int i = CommandIndex + 1; i < ParentBlock.CommandList.Count; ++i) { - ICommand nextCommand = ParentBlock.CommandList[i]; + Command nextCommand = ParentBlock.CommandList[i]; if (nextCommand == null) { diff --git a/Assets/Fungus/Scripts/Commands/Jump.cs b/Assets/Fungus/Scripts/Commands/Jump.cs index ff3214f2..65b193e6 100644 --- a/Assets/Fungus/Scripts/Commands/Jump.cs +++ b/Assets/Fungus/Scripts/Commands/Jump.cs @@ -27,7 +27,7 @@ namespace Fungus return; } - foreach (ICommand command in ParentBlock.CommandList) + foreach (var command in ParentBlock.CommandList) { Label label = command as Label; if (label != null && diff --git a/Assets/Fungus/Scripts/Commands/Menu.cs b/Assets/Fungus/Scripts/Commands/Menu.cs index 96b0fb58..845a62a5 100644 --- a/Assets/Fungus/Scripts/Commands/Menu.cs +++ b/Assets/Fungus/Scripts/Commands/Menu.cs @@ -62,7 +62,7 @@ namespace Fungus Continue(); } - public override void GetConnectedBlocks(ref List connectedBlocks) + public override void GetConnectedBlocks(ref List connectedBlocks) { if (targetBlock != null) { diff --git a/Assets/Fungus/Scripts/Commands/MenuTimer.cs b/Assets/Fungus/Scripts/Commands/MenuTimer.cs index 24ceb68a..4e17ed81 100644 --- a/Assets/Fungus/Scripts/Commands/MenuTimer.cs +++ b/Assets/Fungus/Scripts/Commands/MenuTimer.cs @@ -37,7 +37,7 @@ namespace Fungus Continue(); } - public override void GetConnectedBlocks(ref List connectedBlocks) + public override void GetConnectedBlocks(ref List connectedBlocks) { if (targetBlock != null) { diff --git a/Assets/Fungus/Scripts/Commands/Portrait.cs b/Assets/Fungus/Scripts/Commands/Portrait.cs index f1600d92..4e119220 100644 --- a/Assets/Fungus/Scripts/Commands/Portrait.cs +++ b/Assets/Fungus/Scripts/Commands/Portrait.cs @@ -207,7 +207,7 @@ namespace Fungus return new Color32(230, 200, 250, 255); } - public override void OnCommandAdded(IBlock parentBlock) + public override void OnCommandAdded(Block parentBlock) { //Default to display type: show display = DisplayType.Show; diff --git a/Assets/Fungus/Scripts/Commands/SetInteractable.cs b/Assets/Fungus/Scripts/Commands/SetInteractable.cs index b967b4df..5357c2ab 100644 --- a/Assets/Fungus/Scripts/Commands/SetInteractable.cs +++ b/Assets/Fungus/Scripts/Commands/SetInteractable.cs @@ -81,7 +81,7 @@ namespace Fungus return new Color32(180, 250, 250, 255); } - public override void OnCommandAdded(IBlock parentBlock) + public override void OnCommandAdded(Block parentBlock) { targetObjects.Add(null); } diff --git a/Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs b/Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs index 8fe4f663..06f615f1 100644 --- a/Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs +++ b/Assets/Fungus/Scripts/Commands/SetSpriteOrder.cs @@ -72,7 +72,7 @@ namespace Fungus return false; } - public override void OnCommandAdded(IBlock parentBlock) + public override void OnCommandAdded(Block parentBlock) { // Add a default empty entry targetSprites.Add(null); diff --git a/Assets/Fungus/Scripts/Commands/TweenUI.cs b/Assets/Fungus/Scripts/Commands/TweenUI.cs index 6a56e2ed..4cba1bf7 100644 --- a/Assets/Fungus/Scripts/Commands/TweenUI.cs +++ b/Assets/Fungus/Scripts/Commands/TweenUI.cs @@ -65,7 +65,7 @@ namespace Fungus Continue(); } - public override void OnCommandAdded(IBlock parentBlock) + public override void OnCommandAdded(Block parentBlock) { // Add an empty slot by default. Saves an unnecessary user click. if (targetObjects.Count == 0) diff --git a/Assets/Fungus/Scripts/Components/Localization.cs b/Assets/Fungus/Scripts/Components/Localization.cs index 429b20ec..38f53bc3 100644 --- a/Assets/Fungus/Scripts/Components/Localization.cs +++ b/Assets/Fungus/Scripts/Components/Localization.cs @@ -138,10 +138,10 @@ namespace Fungus var flowcharts = GameObject.FindObjectsOfType(); foreach (var flowchart in flowcharts) { - IBlock[] blocks = flowchart.GetComponents(); - foreach (IBlock block in blocks) + var blocks = flowchart.GetComponents(); + foreach (var block in blocks) { - foreach (Command command in block.CommandList) + foreach (var command in block.CommandList) { ILocalizable localizable = command as ILocalizable; if (localizable != null) diff --git a/Assets/Fungus/Scripts/Components/MenuDialog.cs b/Assets/Fungus/Scripts/Components/MenuDialog.cs index 52a1a16a..841e9bc6 100644 --- a/Assets/Fungus/Scripts/Components/MenuDialog.cs +++ b/Assets/Fungus/Scripts/Components/MenuDialog.cs @@ -72,7 +72,7 @@ namespace Fungus Canvas.ForceUpdateCanvases(); } - protected virtual IEnumerator WaitForTimeout(float timeoutDuration, IBlock targetBlock) + protected virtual IEnumerator WaitForTimeout(float timeoutDuration, Block targetBlock) { float elapsedTime = 0; @@ -147,7 +147,7 @@ namespace Fungus } } - public virtual bool AddOption(string text, bool interactable, IBlock targetBlock) + public virtual bool AddOption(string text, bool interactable, Block targetBlock) { bool addedOption = false; foreach (Button button in cachedButtons) @@ -169,7 +169,7 @@ namespace Fungus textComponent.text = text; } - IBlock block = targetBlock; + var block = targetBlock; button.onClick.AddListener(delegate { @@ -244,7 +244,7 @@ namespace Fungus return addedOption; } - public virtual void ShowTimer(float duration, IBlock targetBlock) + public virtual void ShowTimer(float duration, Block targetBlock) { if (cachedSlider != null) { diff --git a/Assets/Fungus/Scripts/Editor/BlockEditor.cs b/Assets/Fungus/Scripts/Editor/BlockEditor.cs index 9d197d35..acc8f36b 100644 --- a/Assets/Fungus/Scripts/Editor/BlockEditor.cs +++ b/Assets/Fungus/Scripts/Editor/BlockEditor.cs @@ -20,13 +20,13 @@ namespace Fungus { protected class SetEventHandlerOperation { - public IBlock block; + public Block block; public Type eventHandlerType; } protected class AddCommandOperation { - public IBlock block; + public Block 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 - IBlock block = target as IBlock; + var block = target as Block; string uniqueName = flowchart.GetUniqueBlockKey(blockNameProperty.stringValue, block); if (uniqueName != block.BlockName) { @@ -89,7 +89,7 @@ namespace Fungus actionList.Clear(); } - IBlock block = target as IBlock; + var block = target as Block; SerializedProperty commandListProperty = serializedObject.FindProperty("commandList"); @@ -103,7 +103,7 @@ namespace Fungus block.UpdateIndentLevels(); // Make sure each command has a reference to its parent block - foreach (ICommand command in block.CommandList) + foreach (var command in block.CommandList) { if (command == null) // Will be deleted from the list later on { @@ -114,7 +114,7 @@ namespace Fungus ReorderableListGUI.Title("Commands"); CommandListAdaptor adaptor = new CommandListAdaptor(commandListProperty, 0); - adaptor.nodeRect = (block as INode)._NodeRect; + adaptor.nodeRect = block._NodeRect; ReorderableListFlags flags = ReorderableListFlags.HideAddButton | ReorderableListFlags.HideRemoveButtons | ReorderableListFlags.DisableContextMenu; @@ -305,7 +305,7 @@ namespace Fungus // event handler selected. List eventHandlerTypes = EditorExtensions.FindDerivedTypes(typeof(EventHandler)).ToList(); - IBlock block = target as IBlock; + Block block = target as Block; System.Type currentType = null; if (block._EventHandler != null) { @@ -382,14 +382,14 @@ namespace Fungus protected void OnSelectEventHandler(object obj) { SetEventHandlerOperation operation = obj as SetEventHandlerOperation; - IBlock block = operation.block; + Block block = operation.block; System.Type selectedType = operation.eventHandlerType; if (block == null) { return; } - Undo.RecordObject((Block)block, "Set Event Handler"); + Undo.RecordObject(block, "Set Event Handler"); if (block._EventHandler != null) { @@ -398,13 +398,13 @@ namespace Fungus if (selectedType != null) { - EventHandler newHandler = Undo.AddComponent(((Block)block).gameObject, selectedType) as EventHandler; + EventHandler newHandler = Undo.AddComponent(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)block); + PrefabUtility.RecordPrefabInstancePropertyModifications(block); } static public void BlockField(SerializedProperty property, GUIContent label, GUIContent nullLabel, Flowchart flowchart) @@ -414,14 +414,14 @@ namespace Fungus return; } - IBlock block = property.objectReferenceValue as IBlock; + var block = property.objectReferenceValue as Block; // Build dictionary of child blocks List blockNames = new List(); int selectedIndex = 0; blockNames.Add(nullLabel); - IBlock[] blocks = flowchart.GetComponents(); + var blocks = flowchart.GetComponents(); 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)block; + property.objectReferenceValue = block; } static public Block BlockField(Rect position, GUIContent nullLabel, Flowchart flowchart, Block block) @@ -631,13 +631,13 @@ namespace Fungus protected virtual void ShowCommandMenu() { - IBlock block = target as IBlock; + var block = target as Block; var flowchart = (Flowchart)block.GetFlowchart(); // Use index of last selected command in list, or end of list if nothing selected. int index = -1; - foreach (ICommand command in flowchart.SelectedCommands) + foreach (var command in flowchart.SelectedCommands) { if (command.CommandIndex + 1 > index) { @@ -722,7 +722,7 @@ namespace Fungus { AddCommandOperation commandOperation = obj as AddCommandOperation; - IBlock block = commandOperation.block; + var block = commandOperation.block; if (block == null) { return; @@ -732,7 +732,7 @@ namespace Fungus flowchart.ClearSelectedCommands(); - var newCommand = Undo.AddComponent(((Block)block).gameObject, commandOperation.commandType) as Command; + var newCommand = Undo.AddComponent(block.gameObject, commandOperation.commandType) as Command; block.GetFlowchart().AddSelectedCommand(newCommand); newCommand.ParentBlock = block; newCommand.ItemId = flowchart.NextItemId(); @@ -740,23 +740,23 @@ namespace Fungus // Let command know it has just been added to the block newCommand.OnCommandAdded(block); - Undo.RecordObject((Block)block, "Set command type"); + Undo.RecordObject(block, "Set command type"); if (commandOperation.index < block.CommandList.Count - 1) { - block.CommandList.Insert(commandOperation.index, (Command)newCommand); + block.CommandList.Insert(commandOperation.index, newCommand); } else { - block.CommandList.Add((Command)newCommand); + block.CommandList.Add(newCommand); } // Because this is an async call, we need to force prefab instances to record changes - PrefabUtility.RecordPrefabInstancePropertyModifications((Block)block); + PrefabUtility.RecordPrefabInstancePropertyModifications(block); } public virtual void ShowContextMenu() { - IBlock block = target as IBlock; + var block = target as Block; var flowchart = (Flowchart)block.GetFlowchart(); if (flowchart == null) @@ -844,7 +844,7 @@ namespace Fungus protected void SelectAll() { - IBlock block = target as IBlock; + var block = target as Block; var flowchart = (Flowchart)block.GetFlowchart(); if (flowchart == null || @@ -865,7 +865,7 @@ namespace Fungus protected void SelectNone() { - IBlock block = target as IBlock; + var block = target as Block; var flowchart = (Flowchart)block.GetFlowchart(); if (flowchart == null || @@ -888,7 +888,7 @@ namespace Fungus protected void Copy() { - IBlock block = target as IBlock; + var block = target as Block; var flowchart = (Flowchart)block.GetFlowchart(); if (flowchart == null || @@ -918,7 +918,7 @@ namespace Fungus protected void Paste() { - IBlock block = target as IBlock; + var block = target as Block; var 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)block); + PrefabUtility.RecordPrefabInstancePropertyModifications(block); Repaint(); } protected void Delete() { - IBlock block = target as IBlock; + var block = target as Block; var flowchart = (Flowchart)block.GetFlowchart(); if (flowchart == null || @@ -1022,7 +1022,7 @@ namespace Fungus protected void PlayCommand() { - IBlock targetBlock = target as IBlock; + var targetBlock = target as Block; var flowchart = (Flowchart)targetBlock.GetFlowchart(); Command command = flowchart.SelectedCommands[0]; if (targetBlock.IsExecuting()) @@ -1042,7 +1042,7 @@ namespace Fungus protected void StopAllPlayCommand() { - IBlock targetBlock = target as IBlock; + var targetBlock = target as Block; var 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, IBlock targetBlock, int commandIndex, float delay) + protected IEnumerator RunBlock(Flowchart flowchart, Block targetBlock, int commandIndex, float delay) { yield return new WaitForSeconds(delay); flowchart.ExecuteBlock(targetBlock, commandIndex); @@ -1059,7 +1059,7 @@ namespace Fungus protected void SelectPrevious() { - IBlock block = target as IBlock; + var block = target as Block; var flowchart = (Flowchart)block.GetFlowchart(); int firstSelectedIndex = flowchart.SelectedBlock.CommandList.Count; @@ -1099,7 +1099,7 @@ namespace Fungus protected void SelectNext() { - IBlock block = target as IBlock; + var block = target as Block; var flowchart = (Flowchart)block.GetFlowchart(); int lastSelectedIndex = -1; diff --git a/Assets/Fungus/Scripts/Editor/BlockInspector.cs b/Assets/Fungus/Scripts/Editor/BlockInspector.cs index f7accb2f..c14e82f9 100644 --- a/Assets/Fungus/Scripts/Editor/BlockInspector.cs +++ b/Assets/Fungus/Scripts/Editor/BlockInspector.cs @@ -35,7 +35,7 @@ namespace Fungus // when a different block / command is selected. protected BlockEditor activeBlockEditor; protected CommandEditor activeCommandEditor; - protected ICommand activeCommand; // Command currently being inspected + protected Command activeCommand; // Command currently being inspected // Cached command editors to avoid creating / destroying editors more than necessary // This list is static so persists between @@ -75,7 +75,11 @@ namespace Fungus return; } - IBlock block = blockInspector.block; + var block = blockInspector.block; + if (block == null) + { + return; + } var flowchart = (Flowchart)block.GetFlowchart(); @@ -83,7 +87,7 @@ namespace Fungus !block.Equals(activeBlockEditor.target)) { DestroyImmediate(activeBlockEditor); - activeBlockEditor = Editor.CreateEditor((Block)block) as BlockEditor; + activeBlockEditor = Editor.CreateEditor(block) as BlockEditor; } activeBlockEditor.DrawBlockName(flowchart); @@ -101,7 +105,7 @@ namespace Fungus activeBlockEditor.DrawBlockGUI(flowchart); GUILayout.EndScrollView(); - ICommand inspectCommand = null; + Command inspectCommand = null; if (flowchart.SelectedCommands.Count == 1) { inspectCommand = flowchart.SelectedCommands[0]; @@ -109,7 +113,7 @@ namespace Fungus if (Application.isPlaying && inspectCommand != null && - inspectCommand.ParentBlock != block) + !inspectCommand.ParentBlock.Equals(block)) { GUILayout.EndArea(); Repaint(); @@ -143,7 +147,7 @@ namespace Fungus } } - public void DrawCommandUI(Flowchart flowchart, ICommand inspectCommand) + public void DrawCommandUI(Flowchart flowchart, Command inspectCommand) { ResizeScrollView(flowchart); diff --git a/Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs b/Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs index 1ef9add6..a6d5be45 100644 --- a/Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs +++ b/Assets/Fungus/Scripts/Editor/CommandListAdaptor.cs @@ -84,13 +84,13 @@ namespace Fungus return null; } - IBlock block = flowchart.SelectedBlock; + var block = flowchart.SelectedBlock; if (block == null) { return null; } - var newCommand = Undo.AddComponent(((Block)block).gameObject) as Command; + var newCommand = Undo.AddComponent(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 - IBlock parentBlock = command.GetComponent(); + var parentBlock = command.GetComponent(); System.Type type = command.GetType(); - Command newCommand = Undo.AddComponent(((Block)parentBlock).gameObject, type) as Command; + Command newCommand = Undo.AddComponent(parentBlock.gameObject, type) as Command; newCommand.ItemId = newCommand.GetFlowchart().NextItemId(); System.Reflection.FieldInfo[] fields = type.GetFields(); foreach (System.Reflection.FieldInfo field in fields) diff --git a/Assets/Fungus/Scripts/Editor/FlowchartMenuItems.cs b/Assets/Fungus/Scripts/Editor/FlowchartMenuItems.cs index 3ddaf290..26af4ccd 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartMenuItems.cs +++ b/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().Length > 1) { - IBlock block = go.GetComponent(); + var block = go.GetComponent(); GameObject.DestroyImmediate(block._EventHandler); block._EventHandler = null; } diff --git a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs index 00adf021..2b6669cd 100644 --- a/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs +++ b/Assets/Fungus/Scripts/Editor/FlowchartWindow.cs @@ -12,9 +12,9 @@ namespace Fungus { public class FlowchartWindow : EditorWindow { - public static List deleteList = new List(); + public static List deleteList = new List(); - protected List windowBlockMap = new List(); + protected List windowBlockMap = new List(); // The ReorderableList control doesn't drag properly when used with GUI.DragWindow(), // so we just implement dragging ourselves. @@ -117,7 +117,7 @@ namespace Fungus } // Delete any scheduled objects - foreach (IBlock deleteBlock in deleteList) + foreach (var deleteBlock in deleteList) { bool isSelected = (flowchart.SelectedBlock == deleteBlock); @@ -219,11 +219,11 @@ namespace Fungus protected virtual void DrawFlowchartView(Flowchart flowchart) { - IBlock[] blocks = flowchart.GetComponents(); + Block[] blocks = flowchart.GetComponents(); - foreach (IBlock block in blocks) + foreach (var block in blocks) { - var node = block as INode; + var node = block as Node; if (node == null) { continue; @@ -263,11 +263,11 @@ namespace Fungus CalcFlowchartCenter(flowchart, blocks); // Draw connections - foreach (IBlock block in blocks) + foreach (var block in blocks) { DrawConnections(flowchart, block, false); } - foreach (IBlock block in blocks) + foreach (var block in blocks) { DrawConnections(flowchart, block, true); } @@ -280,7 +280,7 @@ namespace Fungus windowBlockMap.Clear(); for (int i = 0; i < blocks.Length; ++i) { - IBlock block = blocks[i]; + var block = blocks[i]; float nodeWidthA = nodeStyle.CalcSize(new GUIContent(block.BlockName)).x + 10; float nodeWidthB = 0f; @@ -289,11 +289,9 @@ namespace Fungus nodeWidthB = nodeStyle.CalcSize(new GUIContent(block._EventHandler.GetSummary())).x + 10; } - var node = block as INode; - if (Event.current.button == 0) { - Rect tempRect = node._NodeRect; + Rect tempRect = block._NodeRect; tempRect.width = Mathf.Max(Mathf.Max(nodeWidthA, nodeWidthB), 120); tempRect.height = 40; @@ -321,10 +319,10 @@ namespace Fungus forceRepaintCount = 6; } - node._NodeRect = tempRect; + block._NodeRect = tempRect; } - Rect windowRect = new Rect(node._NodeRect); + Rect windowRect = new Rect(block._NodeRect); windowRect.x += flowchart.ScrollPos.x; windowRect.y += flowchart.ScrollPos.y; @@ -338,7 +336,7 @@ namespace Fungus EndWindows(); // Draw Event Handler labels - foreach (IBlock block in blocks) + foreach (var block in blocks) { if (block._EventHandler != null) { @@ -355,22 +353,19 @@ namespace Fungus handlerStyle.margin.bottom = 0; handlerStyle.alignment = TextAnchor.MiddleCenter; - INode node = block as INode; - - Rect rect = new Rect(node._NodeRect); - rect.height = handlerStyle.CalcHeight(new GUIContent(handlerLabel), node._NodeRect.width); + Rect rect = new Rect(block._NodeRect); + rect.height = handlerStyle.CalcHeight(new GUIContent(handlerLabel), block._NodeRect.width); rect.x += flowchart.ScrollPos.x; rect.y += flowchart.ScrollPos.y - rect.height; GUI.Label(rect, handlerLabel, handlerStyle); } } - - + // Draw play icons beside all executing blocks if (Application.isPlaying) { - foreach (IBlock b in blocks) + foreach (var b in blocks) { if (b.IsExecuting()) { @@ -381,9 +376,7 @@ namespace Fungus if (b.ExecutingIconTimer > Time.realtimeSinceStartup) { - INode node = b as INode; - - Rect rect = new Rect(node._NodeRect); + Rect rect = new Rect(b._NodeRect); rect.x += flowchart.ScrollPos.x - 37; rect.y += flowchart.ScrollPos.y + 3; @@ -414,7 +407,7 @@ namespace Fungus EditorZoomArea.End(); } - public virtual void CalcFlowchartCenter(Flowchart flowchart, IBlock[] blocks) + public virtual void CalcFlowchartCenter(Flowchart flowchart, Block[] blocks) { if (flowchart == null || blocks.Count() == 0) @@ -422,17 +415,15 @@ namespace Fungus return; } - Vector2 min = (blocks[0] as INode)._NodeRect.min; - Vector2 max = (blocks[0] as INode)._NodeRect.max; + Vector2 min = blocks[0]._NodeRect.min; + Vector2 max = blocks[0]._NodeRect.max; - foreach (IBlock block in blocks) + foreach (var block in blocks) { - 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); + 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); } Vector2 center = (min + max) * -0.5f; @@ -536,7 +527,7 @@ namespace Fungus } } - protected virtual void SelectBlock(Flowchart flowchart, IBlock block) + protected virtual void SelectBlock(Flowchart flowchart, Block block) { // Select the block and also select currently executing command ShowBlockInspector(flowchart); @@ -561,7 +552,7 @@ namespace Fungus protected virtual void DeleteBlock(Flowchart flowchart, IBlock block) { - foreach (Command command in block.CommandList) + foreach (var command in block.CommandList) { Undo.DestroyObjectImmediate(command); } @@ -572,7 +563,7 @@ namespace Fungus protected virtual void DrawWindow(int windowId) { - IBlock block = windowBlockMap[windowId]; + var block = windowBlockMap[windowId]; var flowchart = (Flowchart)block.GetFlowchart(); if (flowchart == null) @@ -591,10 +582,8 @@ namespace Fungus { dragWindowId = windowId; - var node = block as INode; - - startDragPosition.x = node._NodeRect.x; - startDragPosition.y = node._NodeRect.y; + startDragPosition.x = block._NodeRect.x; + startDragPosition.y = block._NodeRect.y; } if (windowId < windowBlockMap.Count) @@ -607,7 +596,12 @@ namespace Fungus } } - bool selected = (flowchart.SelectedBlock == block); + bool selected = false; + if (flowchart.SelectedBlock != null && + flowchart.SelectedBlock.Equals(block)) + { + selected = true; + } GUIStyle nodeStyleCopy = new GUIStyle(nodeStyle); @@ -618,9 +612,9 @@ namespace Fungus else { // Count the number of unique connections (excluding self references) - List uniqueList = new List(); - List connectedBlocks = block.GetConnectedBlocks(); - foreach (IBlock connectedBlock in connectedBlocks) + var uniqueList = new List(); + var connectedBlocks = block.GetConnectedBlocks(); + foreach (Block connectedBlock in connectedBlocks) { if (connectedBlock == block || uniqueList.Contains(connectedBlock)) @@ -643,7 +637,7 @@ namespace Fungus nodeStyleCopy.normal.textColor = Color.black; // Make sure node is wide enough to fit the node name text - var n = block as INode; + var n = block as Node; float width = nodeStyleCopy.CalcSize(new GUIContent(block.BlockName)).x; Rect tempRect = n._NodeRect; tempRect.width = Mathf.Max (n._NodeRect.width, width); @@ -670,16 +664,16 @@ namespace Fungus } } - protected virtual void DrawConnections(Flowchart flowchart, IBlock block, bool highlightedOnly) + protected virtual void DrawConnections(Flowchart flowchart, Block block, bool highlightedOnly) { if (block == null) { return; } - List connectedBlocks = new List(); + var connectedBlocks = new List(); - bool blockIsSelected = (flowchart.SelectedBlock == block); + bool blockIsSelected = (flowchart.SelectedBlock != block); foreach (Command command in block.CommandList) { @@ -709,7 +703,7 @@ namespace Fungus connectedBlocks.Clear(); command.GetConnectedBlocks(ref connectedBlocks); - foreach (IBlock blockB in connectedBlocks) + foreach (Block blockB in connectedBlocks) { if (blockB == null || block == blockB || @@ -718,11 +712,11 @@ namespace Fungus continue; } - Rect startRect = new Rect((block as INode)._NodeRect); + Rect startRect = new Rect(block._NodeRect); startRect.x += flowchart.ScrollPos.x; startRect.y += flowchart.ScrollPos.y; - Rect endRect = new Rect((blockB as INode)._NodeRect); + Rect endRect = new Rect(blockB._NodeRect); endRect.x += flowchart.ScrollPos.x; endRect.y += flowchart.ScrollPos.y; @@ -782,7 +776,7 @@ namespace Fungus public static void DeleteBlock(object obj) { - IBlock block = obj as IBlock; + var block = obj as Block; FlowchartWindow.deleteList.Add(block); } diff --git a/Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs b/Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs index 0037dc10..ae3a445c 100644 --- a/Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs +++ b/Assets/Fungus/Scripts/Interfaces/IMenuDialog.cs @@ -44,7 +44,7 @@ namespace Fungus /// The option text to display on the button. /// If false, the option is displayed but is not selectable. /// Block to execute when the option is selected. - bool AddOption(string text, bool interactable, IBlock targetBlock); + bool AddOption(string text, bool interactable, Block targetBlock); /// /// Adds the option to the list of displayed options, calls a Lua function when selected. @@ -58,7 +58,7 @@ namespace Fungus /// /// The duration during which the player can select an option. /// Block to execute if the player does not select an option in time. - void ShowTimer(float duration, IBlock targetBlock); + void ShowTimer(float duration, Block targetBlock); /// /// Show a timer during which the player can select an option. Calls a Lua function when the timer expires. diff --git a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs index 5b0aab3b..9013ea6e 100644 --- a/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs +++ b/Assets/Fungus/Thirdparty/FungusLua/Scripts/LuaBindings.cs @@ -84,7 +84,7 @@ namespace Fungus public override void AddBindings(ILuaEnvironment luaEnv) { if (!allEnvironments && - !luaEnvironment.Equals(luaEnv)) + (luaEnvironment != null && !luaEnvironment.Equals(luaEnv))) { // Don't add bindings to this environment return;