You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
287 lines
9.3 KiB
287 lines
9.3 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
10 years ago
|
using UnityEngine;
|
||
10 years ago
|
using UnityEngine.Serialization;
|
||
10 years ago
|
using System;
|
||
10 years ago
|
using System.Collections.Generic;
|
||
|
|
||
10 years ago
|
namespace Fungus
|
||
8 years ago
|
{
|
||
|
/// <summary>
|
||
|
/// Attribute class for Fungus commands.
|
||
|
/// </summary>
|
||
8 years ago
|
public class CommandInfoAttribute : Attribute
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Metadata atribute for the Command class.
|
||
|
/// </summary>
|
||
|
/// <param name="category">The category to place this command in.</param>
|
||
|
/// <param name="commandName">The display name of the command.</param>
|
||
|
/// <param name="helpText">Help information to display in the inspector.</param>
|
||
|
/// <param name="priority">If two command classes have the same name, the one with highest priority is listed. Negative priority removess the command from the list.</param>///
|
||
8 years ago
|
public CommandInfoAttribute(string category, string commandName, string helpText, int priority = 0)
|
||
|
{
|
||
|
this.Category = category;
|
||
|
this.CommandName = commandName;
|
||
|
this.HelpText = helpText;
|
||
|
this.Priority = priority;
|
||
|
}
|
||
|
|
||
|
public string Category { get; set; }
|
||
|
public string CommandName { get; set; }
|
||
|
public string HelpText { get; set; }
|
||
|
public int Priority { get; set; }
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Base class for Commands. Commands can be added to Blocks to create an execution sequence.
|
||
|
/// </summary>
|
||
8 years ago
|
public class Command : MonoBehaviour, ICommand
|
||
8 years ago
|
{
|
||
8 years ago
|
[FormerlySerializedAs("commandId")]
|
||
|
[HideInInspector]
|
||
|
[SerializeField] protected int itemId = -1; // Invalid flowchart item id
|
||
|
|
||
|
[HideInInspector]
|
||
|
[SerializeField] protected int indentLevel;
|
||
|
|
||
|
protected string errorMessage = "";
|
||
|
|
||
|
#region ICommand implementation
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Unique identifier for this command.
|
||
|
/// Unique for this Flowchart.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual int ItemId { get { return itemId; } set { itemId = value; } }
|
||
8 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Error message to display in the command inspector.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual string ErrorMessage { get { return errorMessage; } }
|
||
8 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Indent depth of the current commands.
|
||
|
/// Commands are indented inside If, While, etc. sections.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual int IndentLevel { get { return indentLevel; } set { indentLevel = value; } }
|
||
8 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Index of the command in the parent block's command list.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual int CommandIndex { get; set; }
|
||
8 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Set to true by the parent block while the command is executing.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual bool IsExecuting { get; set; }
|
||
8 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Timer used to control appearance of executing icon in inspector.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual float ExecutingIconTimer { get; set; }
|
||
8 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Reference to the Block object that this command belongs to.
|
||
|
/// This reference is only populated at runtime and in the editor when the
|
||
|
/// block is selected.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual Block ParentBlock { get; set; }
|
||
8 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Returns the Flowchart that this command belongs to.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual IFlowchart GetFlowchart()
|
||
8 years ago
|
{
|
||
8 years ago
|
IFlowchart flowchart = GetComponent<IFlowchart>();
|
||
8 years ago
|
if (flowchart == null &&
|
||
|
transform.parent != null)
|
||
|
{
|
||
8 years ago
|
flowchart = transform.parent.GetComponent<IFlowchart>();
|
||
8 years ago
|
}
|
||
|
return flowchart;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Execute the command.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void Execute()
|
||
|
{
|
||
|
OnEnter();
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// End execution of this command and continue execution at the next command.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void Continue()
|
||
|
{
|
||
|
// This is a noop if the Block has already been stopped
|
||
8 years ago
|
if (IsExecuting)
|
||
8 years ago
|
{
|
||
8 years ago
|
Continue(CommandIndex + 1);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// End execution of this command and continue execution at a specific command index.
|
||
|
/// </summary>
|
||
|
/// <param name="nextCommandIndex">Next command index.</param>
|
||
8 years ago
|
public virtual void Continue(int nextCommandIndex)
|
||
|
{
|
||
|
OnExit();
|
||
8 years ago
|
if (ParentBlock != null)
|
||
8 years ago
|
{
|
||
8 years ago
|
ParentBlock.JumpToCommandIndex = nextCommandIndex;
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Stops the parent Block executing.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void StopParentBlock()
|
||
|
{
|
||
|
OnExit();
|
||
8 years ago
|
if (ParentBlock != null)
|
||
8 years ago
|
{
|
||
8 years ago
|
ParentBlock.Stop();
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Called when the parent block has been requested to stop executing, and
|
||
|
/// this command is the currently executing command.
|
||
|
/// Use this callback to terminate any asynchronous operations and
|
||
|
/// cleanup state so that the command is ready to execute again later on.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void OnStopExecuting()
|
||
|
{}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Called when the new command is added to a block in the editor.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void OnCommandAdded(Block parentBlock)
|
||
8 years ago
|
{}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Called when the command is deleted from a block in the editor.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void OnCommandRemoved(Block parentBlock)
|
||
8 years ago
|
{}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Called when this command starts execution.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void OnEnter()
|
||
|
{}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Called when this command ends execution.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void OnExit()
|
||
|
{}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Called when this command is reset. This happens when the Reset command is used.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void OnReset()
|
||
|
{}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Populates a list with the Blocks that this command references.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual void GetConnectedBlocks(ref List<Block> connectedBlocks)
|
||
8 years ago
|
{}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Returns true if this command references the variable.
|
||
|
/// Used to highlight variables in the variable list when a command is selected.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual bool HasReference(Variable variable)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Returns the summary text to display in the command inspector.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual string GetSummary()
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Returns the help text to display for this command.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual string GetHelpText()
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Return true if this command opens a block of commands. Used for indenting commands.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual bool OpenBlock()
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Return true if this command closes a block of commands. Used for indenting commands.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual bool CloseBlock()
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Return the color for the command background in inspector.
|
||
|
/// </summary>
|
||
|
/// <returns>The button color.</returns>
|
||
8 years ago
|
public virtual Color GetButtonColor()
|
||
|
{
|
||
|
return Color.white;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Returns true if the specified property should be displayed in the inspector.
|
||
|
/// This is useful for hiding certain properties based on the value of another property.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual bool IsPropertyVisible(string propertyName)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Returns true if the specified property should be displayed as a reorderable list in the inspector.
|
||
|
/// This only applies for array properties and has no effect for non-array properties.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual bool IsReorderableArray(string propertyName)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Returns the localization id for the Flowchart that contains this command.
|
||
|
/// </summary>
|
||
8 years ago
|
public virtual string GetFlowchartLocalizationId()
|
||
|
{
|
||
|
// If no localization id has been set then use the Flowchart name
|
||
8 years ago
|
IFlowchart flowchart = GetFlowchart();
|
||
8 years ago
|
if (flowchart == null)
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
|
||
8 years ago
|
string localizationId = GetFlowchart().LocalizationId;
|
||
8 years ago
|
if (localizationId.Length == 0)
|
||
|
{
|
||
8 years ago
|
localizationId = flowchart.GetName();
|
||
8 years ago
|
}
|
||
|
|
||
|
return localizationId;
|
||
|
}
|
||
8 years ago
|
|
||
|
#endregion
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|