Browse Source

Refactored code to use ICommand interface where possible

master
Christopher 8 years ago
parent
commit
6097c4ee1e
  1. 14
      Assets/Fungus/Flowchart/Scripts/Block.cs
  2. 24
      Assets/Fungus/Flowchart/Scripts/Flowchart.cs
  3. 2
      Assets/Fungus/Flowchart/Scripts/IBlock.cs
  4. 2
      Assets/Fungus/Scripts/Commands/Else.cs
  5. 2
      Assets/Fungus/Scripts/Commands/ElseIf.cs
  6. 4
      Assets/Fungus/Scripts/Commands/If.cs
  7. 2
      Assets/Fungus/Scripts/Commands/Jump.cs
  8. 10
      Assets/Fungus/Scripts/Editor/BlockEditor.cs
  9. 12
      Assets/Fungus/Scripts/Editor/BlockInspector.cs

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

@ -34,7 +34,7 @@ namespace Fungus
protected ExecutionState executionState;
protected Command activeCommand;
protected ICommand activeCommand;
/// <summary>
// Index of last command executed before the current one.
@ -66,7 +66,7 @@ namespace Fungus
// Give each child command a reference back to its parent block
// and tell each command its index in the list.
int index = 0;
foreach (Command command in commandList)
foreach (ICommand command in commandList)
{
if (command == null)
{
@ -92,7 +92,7 @@ namespace Fungus
protected virtual void Update()
{
int index = 0;
foreach (Command command in commandList)
foreach (ICommand command in commandList)
{
if (command == null) // Null entry will be deleted automatically later
{
@ -116,7 +116,7 @@ namespace Fungus
public virtual EventHandler _EventHandler { get { return eventHandler; } set { eventHandler = value; } }
public virtual Command ActiveCommand { get { return activeCommand; } }
public virtual ICommand ActiveCommand { get { return activeCommand; } }
public virtual float ExecutingIconTimer { get; set; }
@ -207,7 +207,7 @@ namespace Fungus
previousActiveCommandIndex = activeCommand.CommandIndex;
}
Command command = commandList[i];
ICommand command = commandList[i];
activeCommand = command;
if (flowchart.gameObject.activeInHierarchy)
@ -268,7 +268,7 @@ namespace Fungus
public virtual List<IBlock> GetConnectedBlocks()
{
var connectedBlocks = new List<IBlock>();
foreach (Command command in commandList)
foreach (ICommand command in commandList)
{
if (command != null)
{
@ -292,7 +292,7 @@ namespace Fungus
public virtual void UpdateIndentLevels()
{
int indentLevel = 0;
foreach(Command command in commandList)
foreach (ICommand command in commandList)
{
if (command == null)
{

24
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -209,8 +209,8 @@ namespace Fungus
maxId = Math.Max(maxId, block.ItemId);
}
Command[] commands = GetComponents<Command>();
foreach (Command command in commands)
var commands = GetComponents<ICommand>();
foreach (var command in commands)
{
maxId = Math.Max(maxId, command.ItemId);
}
@ -320,8 +320,8 @@ namespace Fungus
usedIds.Add(block.ItemId);
}
Command[] commands = GetComponents<Command>();
foreach (Command command in commands)
var commands = GetComponents<ICommand>();
foreach (var command in commands)
{
if (command.ItemId == -1 ||
usedIds.Contains(command.ItemId))
@ -352,7 +352,7 @@ namespace Fungus
}
}
foreach (Command command in GetComponents<Command>())
foreach (var command in GetComponents<Command>())
{
bool found = false;
foreach (IBlock block in blocks)
@ -637,7 +637,7 @@ namespace Fungus
while (true)
{
bool collision = false;
foreach(Command command in block.CommandList)
foreach (ICommand command in block.CommandList)
{
Label label = command as Label;
if (label == null ||
@ -884,7 +884,7 @@ namespace Fungus
}
Command[] commands = GetComponents<Command>();
foreach (Command command in commands)
foreach (var command in commands)
{
command.hideFlags = HideFlags.HideInInspector;
}
@ -923,11 +923,11 @@ namespace Fungus
/// <summary>
/// Adds a command to the list of selected commands.
/// </summary>
public virtual void AddSelectedCommand(Command command)
public virtual void AddSelectedCommand(ICommand command)
{
if (!selectedCommands.Contains(command))
if (!selectedCommands.Contains((Command)command))
{
selectedCommands.Add(command);
selectedCommands.Add((Command)command);
}
}
@ -938,8 +938,8 @@ namespace Fungus
{
if (resetCommands)
{
Command[] commands = GetComponents<Command>();
foreach (Command command in commands)
ICommand[] commands = GetComponents<ICommand>();
foreach (var command in commands)
{
command.OnReset();
}

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

@ -46,7 +46,7 @@ namespace Fungus
/// <summary>
/// The currently executing command.
/// </summary>
Command ActiveCommand { get; }
ICommand ActiveCommand { get; }
/// <summary>
/// Timer for fading Block execution icon.

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

@ -32,7 +32,7 @@ namespace Fungus
int indent = indentLevel;
for (int i = CommandIndex + 1; i < ParentBlock.CommandList.Count; ++i)
{
Command command = ParentBlock.CommandList[i];
ICommand command = ParentBlock.CommandList[i];
if (command.IndentLevel == indent)
{

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

@ -40,7 +40,7 @@ namespace Fungus
int indent = indentLevel;
for (int i = CommandIndex + 1; i < ParentBlock.CommandList.Count; ++i)
{
Command command = ParentBlock.CommandList[i];
ICommand command = ParentBlock.CommandList[i];
if (command.IndentLevel == indent)
{

4
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)
{
Command nextCommand = ParentBlock.CommandList[i];
ICommand nextCommand = ParentBlock.CommandList[i];
if (nextCommand == null)
{
@ -116,7 +116,7 @@ namespace Fungus
// Find next command at same indent level as this If command
// Skip disabled commands, comments & labels
if (!nextCommand.enabled ||
if (!((Command)nextCommand).enabled ||
nextCommand.GetType() == typeof(Comment) ||
nextCommand.GetType() == typeof(Label) ||
nextCommand.IndentLevel != indentLevel)

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

@ -27,7 +27,7 @@ namespace Fungus
return;
}
foreach (Command command in ParentBlock.CommandList)
foreach (ICommand command in ParentBlock.CommandList)
{
Label label = command as Label;
if (label != null &&

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

@ -103,7 +103,7 @@ namespace Fungus
block.UpdateIndentLevels();
// Make sure each command has a reference to its parent block
foreach (Command command in block.CommandList)
foreach (ICommand command in block.CommandList)
{
if (command == null) // Will be deleted from the list later on
{
@ -637,7 +637,7 @@ namespace Fungus
// Use index of last selected command in list, or end of list if nothing selected.
int index = -1;
foreach (Command command in flowchart.SelectedCommands)
foreach (ICommand command in flowchart.SelectedCommands)
{
if (command.CommandIndex + 1 > index)
{
@ -732,7 +732,7 @@ namespace Fungus
flowchart.ClearSelectedCommands();
Command newCommand = Undo.AddComponent(((Block)block).gameObject, commandOperation.commandType) as Command;
ICommand newCommand = Undo.AddComponent(((Block)block).gameObject, commandOperation.commandType) as Command;
block.GetFlowchart().AddSelectedCommand(newCommand);
newCommand.ParentBlock = block;
newCommand.ItemId = flowchart.NextItemId();
@ -743,11 +743,11 @@ namespace Fungus
Undo.RecordObject((Block)block, "Set command type");
if (commandOperation.index < block.CommandList.Count - 1)
{
block.CommandList.Insert(commandOperation.index, newCommand);
block.CommandList.Insert(commandOperation.index, (Command)newCommand);
}
else
{
block.CommandList.Add(newCommand);
block.CommandList.Add((Command)newCommand);
}
// Because this is an async call, we need to force prefab instances to record changes

12
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 Command activeCommand; // Command currently being inspected
protected ICommand activeCommand; // Command currently being inspected
// Cached command editors to avoid creating / destroying editors more than necessary
// This list is static so persists between
@ -101,7 +101,7 @@ namespace Fungus
activeBlockEditor.DrawBlockGUI(flowchart);
GUILayout.EndScrollView();
Command inspectCommand = null;
ICommand inspectCommand = null;
if (flowchart.SelectedCommands.Count == 1)
{
inspectCommand = flowchart.SelectedCommands[0];
@ -143,7 +143,7 @@ namespace Fungus
}
}
public void DrawCommandUI(Flowchart flowchart, Command inspectCommand)
public void DrawCommandUI(Flowchart flowchart, ICommand inspectCommand)
{
ResizeScrollView(flowchart);
@ -156,10 +156,10 @@ namespace Fungus
if (inspectCommand != null)
{
if (activeCommandEditor == null ||
inspectCommand != activeCommandEditor.target)
!inspectCommand.Equals(activeCommandEditor.target))
{
// See if we have a cached version of the command editor already,
var editors = (from e in cachedCommandEditors where (e != null && e.target == inspectCommand) select e);
var editors = (from e in cachedCommandEditors where (e != null && (e.target.Equals(inspectCommand))) select e);
if (editors.Count() > 0)
{
@ -169,7 +169,7 @@ namespace Fungus
else
{
// No cached editor, so create a new one.
activeCommandEditor = Editor.CreateEditor(inspectCommand) as CommandEditor;
activeCommandEditor = Editor.CreateEditor((Command)inspectCommand) as CommandEditor;
cachedCommandEditors.Add(activeCommandEditor);
}
}

Loading…
Cancel
Save