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.
944 lines
30 KiB
944 lines
30 KiB
9 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
|
|
||
11 years ago
|
using UnityEngine;
|
||
10 years ago
|
using UnityEngine.EventSystems;
|
||
10 years ago
|
using UnityEngine.Serialization;
|
||
11 years ago
|
using System;
|
||
9 years ago
|
using System.Text;
|
||
11 years ago
|
using System.Linq;
|
||
11 years ago
|
using System.Collections.Generic;
|
||
10 years ago
|
using System.Text.RegularExpressions;
|
||
11 years ago
|
|
||
11 years ago
|
namespace Fungus
|
||
11 years ago
|
{
|
||
9 years ago
|
/// <summary>
|
||
|
/// Visual scripting controller for the Flowchart programming language.
|
||
|
/// Flowchart objects may be edited visually using the Flowchart editor window.
|
||
|
/// </summary>
|
||
9 years ago
|
[ExecuteInEditMode]
|
||
9 years ago
|
public class Flowchart : MonoBehaviour, IFlowchart, ISubstitutionHandler
|
||
9 years ago
|
{
|
||
9 years ago
|
/// <summary>
|
||
|
/// The current version of the Flowchart. Used for updating components.
|
||
|
/// </summary>
|
||
9 years ago
|
public const int CURRENT_VERSION = 1;
|
||
10 years ago
|
|
||
9 years ago
|
/// <summary>
|
||
|
/// The name of the initial block in a new flowchart.
|
||
|
/// </summary>
|
||
10 years ago
|
public const string DEFAULT_BLOCK_NAME = "New Block";
|
||
|
|
||
9 years ago
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected int version = 0; // Default to 0 to always trigger an update for older versions of Fungus.
|
||
9 years ago
|
|
||
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected Vector2 scrollPos;
|
||
9 years ago
|
|
||
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected Vector2 variablesScrollPos;
|
||
9 years ago
|
|
||
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected bool variablesExpanded = true;
|
||
9 years ago
|
|
||
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected float blockViewHeight = 400;
|
||
9 years ago
|
|
||
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected float zoom = 1f;
|
||
9 years ago
|
|
||
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected Rect scrollViewRect;
|
||
9 years ago
|
|
||
|
[HideInInspector]
|
||
|
[FormerlySerializedAs("selectedSequence")]
|
||
9 years ago
|
[SerializeField] protected Block selectedBlock;
|
||
9 years ago
|
|
||
9 years ago
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected List<Command> selectedCommands = new List<Command>();
|
||
9 years ago
|
|
||
|
[HideInInspector]
|
||
9 years ago
|
[SerializeField] protected List<Variable> variables = new List<Variable>();
|
||
9 years ago
|
|
||
|
[TextArea(3, 5)]
|
||
|
[Tooltip("Description text displayed in the Flowchart editor window")]
|
||
9 years ago
|
[SerializeField] protected string description = "";
|
||
9 years ago
|
|
||
|
[Range(0f, 5f)]
|
||
|
[Tooltip("Adds a pause after each execution step to make it easier to visualise program flow. Editor only, has no effect in platform builds.")]
|
||
9 years ago
|
[SerializeField] protected float stepPause = 0f;
|
||
9 years ago
|
|
||
|
[Tooltip("Use command color when displaying the command list in the Fungus Editor window")]
|
||
9 years ago
|
[SerializeField] protected bool colorCommands = true;
|
||
9 years ago
|
|
||
9 years ago
|
[Tooltip("Hides the Flowchart block and command components in the inspector. Deselect to inspect the block and command components that make up the Flowchart.")]
|
||
9 years ago
|
[SerializeField] protected bool hideComponents = true;
|
||
9 years ago
|
|
||
9 years ago
|
[Tooltip("Saves the selected block and commands when saving the scene. Helps avoid version control conflicts if you've only changed the active selection.")]
|
||
9 years ago
|
[SerializeField] protected bool saveSelection = true;
|
||
9 years ago
|
|
||
|
[Tooltip("Unique identifier for this flowchart in localized string keys. If no id is specified then the name of the Flowchart object will be used.")]
|
||
9 years ago
|
[SerializeField] protected string localizationId = "";
|
||
9 years ago
|
|
||
|
[Tooltip("Display line numbers in the command list in the Block inspector.")]
|
||
9 years ago
|
[SerializeField] protected bool showLineNumbers = false;
|
||
9 years ago
|
|
||
|
[Tooltip("List of commands to hide in the Add Command menu. Use this to restrict the set of commands available when editing a Flowchart.")]
|
||
9 years ago
|
[SerializeField] protected List<string> hideCommands = new List<string>();
|
||
10 years ago
|
|
||
9 years ago
|
[Tooltip("Lua Environment to be used by default for all Execute Lua commands in this Flowchart")]
|
||
9 years ago
|
[SerializeField] protected LuaEnvironment luaEnvironment;
|
||
9 years ago
|
|
||
|
[Tooltip("The ExecuteLua command adds a global Lua variable with this name bound to the flowchart prior to executing.")]
|
||
9 years ago
|
[SerializeField] protected string luaBindingName = "flowchart";
|
||
9 years ago
|
|
||
9 years ago
|
/// <summary>
|
||
|
/// Cached list of flowchart objects in the scene for fast lookup.
|
||
|
/// </summary>
|
||
9 years ago
|
public static List<Flowchart> cachedFlowcharts = new List<Flowchart>();
|
||
|
|
||
9 years ago
|
/// <summary>
|
||
9 years ago
|
/// Sends a message to all Flowchart objects in the current scene.
|
||
|
/// Any block with a matching MessageReceived event handler will start executing.
|
||
9 years ago
|
/// </summary>
|
||
9 years ago
|
public static void BroadcastFungusMessage(string messageName)
|
||
9 years ago
|
{
|
||
9 years ago
|
MessageReceived[] eventHandlers = UnityEngine.Object.FindObjectsOfType<MessageReceived>();
|
||
|
foreach (MessageReceived eventHandler in eventHandlers)
|
||
9 years ago
|
{
|
||
9 years ago
|
eventHandler.OnSendFungusMessage(messageName);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
protected static bool eventSystemPresent;
|
||
|
|
||
|
protected StringSubstituter stringSubstituer;
|
||
|
|
||
9 years ago
|
#if UNITY_5_4_OR_NEWER
|
||
|
protected virtual void Awake()
|
||
|
{
|
||
|
UnityEngine.SceneManagement.SceneManager.activeSceneChanged += (A, B) => {
|
||
|
LevelWasLoaded();
|
||
|
};
|
||
|
}
|
||
|
#else
|
||
|
public virtual void OnLevelWasLoaded(int level)
|
||
|
{
|
||
|
LevelWasLoaded();
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
protected virtual void LevelWasLoaded()
|
||
|
{
|
||
|
// Reset the flag for checking for an event system as there may not be one in the newly loaded scene.
|
||
|
eventSystemPresent = false;
|
||
|
}
|
||
|
|
||
|
protected virtual void Start()
|
||
|
{
|
||
|
CheckEventSystem();
|
||
|
}
|
||
|
|
||
|
// There must be an Event System in the scene for Say and Menu input to work.
|
||
|
// This method will automatically instantiate one if none exists.
|
||
|
protected virtual void CheckEventSystem()
|
||
|
{
|
||
|
if (eventSystemPresent)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
EventSystem eventSystem = GameObject.FindObjectOfType<EventSystem>();
|
||
|
if (eventSystem == null)
|
||
|
{
|
||
|
// Auto spawn an Event System from the prefab
|
||
9 years ago
|
GameObject prefab = Resources.Load<GameObject>("Prefabs/EventSystem");
|
||
9 years ago
|
if (prefab != null)
|
||
|
{
|
||
|
GameObject go = Instantiate(prefab) as GameObject;
|
||
|
go.name = "EventSystem";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
eventSystemPresent = true;
|
||
|
}
|
||
|
|
||
9 years ago
|
protected virtual void OnEnable()
|
||
9 years ago
|
{
|
||
|
if (!cachedFlowcharts.Contains(this))
|
||
|
{
|
||
|
cachedFlowcharts.Add(this);
|
||
|
}
|
||
|
|
||
|
CheckItemIds();
|
||
|
CleanupComponents();
|
||
|
UpdateVersion();
|
||
|
}
|
||
|
|
||
|
protected virtual void UpdateVersion()
|
||
|
{
|
||
|
if (version == CURRENT_VERSION)
|
||
|
{
|
||
|
// No need to update
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Tell all components that implement IUpdateable to update to the new version
|
||
|
foreach (Component component in GetComponents<Component>())
|
||
|
{
|
||
|
IUpdateable u = component as IUpdateable;
|
||
|
if (u != null)
|
||
|
{
|
||
|
u.UpdateToVersion(version, CURRENT_VERSION);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
version = CURRENT_VERSION;
|
||
|
}
|
||
|
|
||
9 years ago
|
protected virtual void OnDisable()
|
||
9 years ago
|
{
|
||
|
cachedFlowcharts.Remove(this);
|
||
|
}
|
||
|
|
||
|
protected virtual void CheckItemIds()
|
||
|
{
|
||
|
// 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>();
|
||
9 years ago
|
var blocks = GetComponents<IBlock>();
|
||
|
foreach (var block in blocks)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (block.ItemId == -1 ||
|
||
|
usedIds.Contains(block.ItemId))
|
||
9 years ago
|
{
|
||
9 years ago
|
block.ItemId = NextItemId();
|
||
9 years ago
|
}
|
||
9 years ago
|
usedIds.Add(block.ItemId);
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
var commands = GetComponents<ICommand>();
|
||
|
foreach (var command in commands)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (command.ItemId == -1 ||
|
||
|
usedIds.Contains(command.ItemId))
|
||
9 years ago
|
{
|
||
9 years ago
|
command.ItemId = NextItemId();
|
||
9 years ago
|
}
|
||
9 years ago
|
usedIds.Add(command.ItemId);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual void CleanupComponents()
|
||
|
{
|
||
|
// Delete any unreferenced components which shouldn't exist any more
|
||
|
// Unreferenced components don't have any effect on the flowchart behavior, but
|
||
|
// they waste memory so should be cleared out periodically.
|
||
|
|
||
|
// 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);
|
||
|
|
||
|
foreach (Variable variable in GetComponents<Variable>())
|
||
|
{
|
||
|
if (!variables.Contains(variable))
|
||
|
{
|
||
|
DestroyImmediate(variable);
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
var blocks = GetComponents<IBlock>();
|
||
|
|
||
9 years ago
|
foreach (var command in GetComponents<Command>())
|
||
9 years ago
|
{
|
||
|
bool found = false;
|
||
9 years ago
|
foreach (var block in blocks)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (block.CommandList.Contains(command))
|
||
9 years ago
|
{
|
||
|
found = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!found)
|
||
|
{
|
||
|
DestroyImmediate(command);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
foreach (EventHandler eventHandler in GetComponents<EventHandler>())
|
||
|
{
|
||
|
bool found = false;
|
||
9 years ago
|
foreach (var block in blocks)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (block._EventHandler == eventHandler)
|
||
9 years ago
|
{
|
||
|
found = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!found)
|
||
|
{
|
||
|
DestroyImmediate(eventHandler);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected virtual Block CreateBlockComponent(GameObject parent)
|
||
|
{
|
||
|
Block block = parent.AddComponent<Block>();
|
||
|
return block;
|
||
|
}
|
||
|
|
||
9 years ago
|
#region IFlowchart implementation
|
||
|
|
||
|
public virtual Vector2 ScrollPos { get { return scrollPos; } set { scrollPos = value; } }
|
||
|
|
||
|
public virtual Vector2 VariablesScrollPos { get { return variablesScrollPos; } set { variablesScrollPos = value; } }
|
||
|
|
||
|
public virtual bool VariablesExpanded { get { return variablesExpanded; } set { variablesExpanded = value; } }
|
||
|
|
||
|
public virtual float BlockViewHeight { get { return blockViewHeight; } set { blockViewHeight = value; } }
|
||
|
|
||
|
public virtual float Zoom { get { return zoom; } set { zoom = value; } }
|
||
|
|
||
|
public virtual Rect ScrollViewRect { get { return scrollViewRect; } set { scrollViewRect = value; } }
|
||
|
|
||
9 years ago
|
public virtual Block SelectedBlock { get { return selectedBlock; } set { selectedBlock = value; } }
|
||
9 years ago
|
|
||
|
public virtual List<Command> SelectedCommands { get { return selectedCommands; } }
|
||
|
|
||
|
public virtual List<Variable> Variables { get { return variables; } }
|
||
|
|
||
|
public virtual string Description { get { return description; } }
|
||
|
|
||
|
public virtual float StepPause { get { return stepPause; } }
|
||
|
|
||
|
public virtual bool ColorCommands { get { return colorCommands; } }
|
||
|
|
||
|
public virtual bool SaveSelection { get { return saveSelection; } }
|
||
|
|
||
|
public virtual string LocalizationId { get { return localizationId; } }
|
||
|
|
||
|
public virtual bool ShowLineNumbers { get { return showLineNumbers; } }
|
||
|
|
||
|
public virtual ILuaEnvironment LuaEnv { get { return luaEnvironment; } }
|
||
|
|
||
|
public virtual string LuaBindingName { get { return luaBindingName; } }
|
||
|
|
||
|
public virtual Vector2 CenterPosition { set; get; }
|
||
|
|
||
|
public int Version { set { version = value; } }
|
||
|
|
||
|
public bool IsActive()
|
||
|
{
|
||
|
return gameObject.activeInHierarchy;
|
||
|
}
|
||
|
|
||
9 years ago
|
public string GetName()
|
||
|
{
|
||
|
return gameObject.name;
|
||
|
}
|
||
|
|
||
9 years ago
|
public int NextItemId()
|
||
|
{
|
||
|
int maxId = -1;
|
||
9 years ago
|
var blocks = GetComponents<IBlock>();
|
||
|
foreach (var block in blocks)
|
||
9 years ago
|
{
|
||
|
maxId = Math.Max(maxId, block.ItemId);
|
||
|
}
|
||
|
|
||
|
var commands = GetComponents<ICommand>();
|
||
|
foreach (var command in commands)
|
||
|
{
|
||
|
maxId = Math.Max(maxId, command.ItemId);
|
||
|
}
|
||
|
return maxId + 1;
|
||
|
}
|
||
|
|
||
9 years ago
|
public virtual Block CreateBlock(Vector2 position)
|
||
|
{
|
||
|
Block b = CreateBlockComponent(gameObject);
|
||
9 years ago
|
b._NodeRect = new Rect(position.x, position.y, 0, 0);
|
||
|
b.BlockName = GetUniqueBlockKey(b.BlockName, b);
|
||
|
b.ItemId = NextItemId();
|
||
9 years ago
|
|
||
|
return b;
|
||
|
}
|
||
|
|
||
9 years ago
|
public virtual IBlock FindBlock(string blockName)
|
||
9 years ago
|
{
|
||
9 years ago
|
var blocks = GetComponents<IBlock>();
|
||
|
foreach (var block in blocks)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (block.BlockName == blockName)
|
||
9 years ago
|
{
|
||
|
return block;
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
return null;
|
||
|
}
|
||
|
|
||
9 years ago
|
public virtual void ExecuteBlock(string blockName)
|
||
9 years ago
|
{
|
||
9 years ago
|
var block = FindBlock(blockName);
|
||
9 years ago
|
|
||
|
if (block == null)
|
||
|
{
|
||
|
Debug.LogError("Block " + blockName + "does not exist");
|
||
9 years ago
|
return;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if (!ExecuteBlock(block))
|
||
|
{
|
||
|
Debug.LogWarning("Block " + blockName + "failed to execute");
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
public virtual bool ExecuteBlock(IBlock block, int commandIndex = 0, Action onComplete = null)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (block == null)
|
||
|
{
|
||
|
Debug.LogError("Block must not be null");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (((Block)block).gameObject != gameObject)
|
||
9 years ago
|
{
|
||
|
Debug.LogError("Block must belong to the same gameobject as this Flowchart");
|
||
|
return false;
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
// Can't restart a running block, have to wait until it's idle again
|
||
|
if (block.IsExecuting())
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
// Start executing the Block as a new coroutine
|
||
|
StartCoroutine(block.Execute(commandIndex, onComplete));
|
||
10 years ago
|
|
||
9 years ago
|
return true;
|
||
9 years ago
|
}
|
||
|
|
||
|
public virtual void StopAllBlocks()
|
||
|
{
|
||
9 years ago
|
var blocks = GetComponents<IBlock>();
|
||
9 years ago
|
foreach (IBlock block in blocks)
|
||
9 years ago
|
{
|
||
|
if (block.IsExecuting())
|
||
|
{
|
||
|
block.Stop();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
public virtual void SendFungusMessage(string messageName)
|
||
|
{
|
||
9 years ago
|
MessageReceived[] eventHandlers = GetComponents<MessageReceived>();
|
||
9 years ago
|
foreach (MessageReceived eventHandler in eventHandlers)
|
||
|
{
|
||
|
eventHandler.OnSendFungusMessage(messageName);
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
public virtual string GetUniqueVariableKey(string originalKey, Variable ignoreVariable = null)
|
||
|
{
|
||
|
int suffix = 0;
|
||
|
string baseKey = originalKey;
|
||
|
|
||
|
// Only letters and digits allowed
|
||
|
char[] arr = baseKey.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray();
|
||
|
baseKey = new string(arr);
|
||
|
|
||
|
// No leading digits allowed
|
||
|
baseKey = baseKey.TrimStart('0','1','2','3','4','5','6','7','8','9');
|
||
|
|
||
|
// No empty keys allowed
|
||
|
if (baseKey.Length == 0)
|
||
|
{
|
||
|
baseKey = "Var";
|
||
|
}
|
||
|
|
||
|
string key = baseKey;
|
||
|
while (true)
|
||
|
{
|
||
|
bool collision = false;
|
||
|
foreach(Variable variable in variables)
|
||
|
{
|
||
|
if (variable == null ||
|
||
|
variable == ignoreVariable ||
|
||
9 years ago
|
variable.Key == null)
|
||
9 years ago
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (variable.Key.Equals(key, StringComparison.CurrentCultureIgnoreCase))
|
||
9 years ago
|
{
|
||
|
collision = true;
|
||
|
suffix++;
|
||
|
key = baseKey + suffix;
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (!collision)
|
||
|
{
|
||
|
return key;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
public virtual string GetUniqueBlockKey(string originalKey, IBlock ignoreBlock = null)
|
||
9 years ago
|
{
|
||
|
int suffix = 0;
|
||
|
string baseKey = originalKey.Trim();
|
||
9 years ago
|
|
||
9 years ago
|
// No empty keys allowed
|
||
|
if (baseKey.Length == 0)
|
||
|
{
|
||
|
baseKey = "New Block";
|
||
|
}
|
||
|
|
||
9 years ago
|
var blocks = GetComponents<IBlock>();
|
||
9 years ago
|
|
||
|
string key = baseKey;
|
||
|
while (true)
|
||
|
{
|
||
|
bool collision = false;
|
||
9 years ago
|
foreach (var block in blocks)
|
||
9 years ago
|
{
|
||
|
if (block == ignoreBlock ||
|
||
9 years ago
|
block.BlockName == null)
|
||
9 years ago
|
{
|
||
|
continue;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (block.BlockName.Equals(key, StringComparison.CurrentCultureIgnoreCase))
|
||
9 years ago
|
{
|
||
|
collision = true;
|
||
|
suffix++;
|
||
|
key = baseKey + suffix;
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (!collision)
|
||
|
{
|
||
|
return key;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public virtual string GetUniqueLabelKey(string originalKey, Label ignoreLabel)
|
||
|
{
|
||
|
int suffix = 0;
|
||
|
string baseKey = originalKey.Trim();
|
||
9 years ago
|
|
||
9 years ago
|
// No empty keys allowed
|
||
|
if (baseKey.Length == 0)
|
||
|
{
|
||
|
baseKey = "New Label";
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
IBlock block = ignoreLabel.ParentBlock;
|
||
9 years ago
|
|
||
9 years ago
|
string key = baseKey;
|
||
|
while (true)
|
||
|
{
|
||
|
bool collision = false;
|
||
9 years ago
|
foreach (var command in block.CommandList)
|
||
9 years ago
|
{
|
||
|
Label label = command as Label;
|
||
|
if (label == null ||
|
||
|
label == ignoreLabel)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (label.Key.Equals(key, StringComparison.CurrentCultureIgnoreCase))
|
||
9 years ago
|
{
|
||
|
collision = true;
|
||
|
suffix++;
|
||
|
key = baseKey + suffix;
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (!collision)
|
||
|
{
|
||
|
return key;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
public Variable GetVariable(string key)
|
||
|
{
|
||
|
foreach (Variable variable in variables)
|
||
|
{
|
||
9 years ago
|
if (variable != null && variable.Key == key)
|
||
9 years ago
|
{
|
||
|
return variable;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
9 years ago
|
public T GetVariable<T>(string key) where T : Variable
|
||
|
{
|
||
|
foreach (Variable variable in variables)
|
||
|
{
|
||
9 years ago
|
if (variable != null && variable.Key == key)
|
||
9 years ago
|
{
|
||
|
return variable as T;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
Debug.LogWarning("Variable " + key + " not found.");
|
||
|
return null;
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
public void SetVariable<T>(string key, T newvariable) where T : Variable
|
||
|
{
|
||
|
foreach (Variable v in variables)
|
||
|
{
|
||
9 years ago
|
if (v != null && v.Key == key)
|
||
9 years ago
|
{
|
||
|
T variable = v as T;
|
||
|
if (variable != null)
|
||
|
{
|
||
|
variable = newvariable;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
Debug.LogWarning("Variable " + key + " not found.");
|
||
|
}
|
||
|
|
||
|
public virtual List<Variable> GetPublicVariables()
|
||
9 years ago
|
{
|
||
|
List<Variable> publicVariables = new List<Variable>();
|
||
|
foreach (Variable v in variables)
|
||
|
{
|
||
9 years ago
|
if (v != null && v.Scope == VariableScope.Public)
|
||
9 years ago
|
{
|
||
|
publicVariables.Add(v);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return publicVariables;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
public virtual bool GetBooleanVariable(string key)
|
||
|
{
|
||
|
BooleanVariable variable = GetVariable<BooleanVariable>(key);
|
||
|
|
||
|
if(variable != null)
|
||
|
{
|
||
9 years ago
|
return GetVariable<BooleanVariable>(key).Value;
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public virtual void SetBooleanVariable(string key, bool value)
|
||
|
{
|
||
|
BooleanVariable variable = GetVariable<BooleanVariable>(key);
|
||
|
if(variable != null)
|
||
|
{
|
||
9 years ago
|
variable.Value = value;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public virtual int GetIntegerVariable(string key)
|
||
|
{
|
||
|
IntegerVariable variable = GetVariable<IntegerVariable>(key);
|
||
|
|
||
|
if (variable != null)
|
||
|
{
|
||
9 years ago
|
return GetVariable<IntegerVariable>(key).Value;
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public virtual void SetIntegerVariable(string key, int value)
|
||
|
{
|
||
|
IntegerVariable variable = GetVariable<IntegerVariable>(key);
|
||
|
if (variable != null)
|
||
|
{
|
||
9 years ago
|
variable.Value = value;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public virtual float GetFloatVariable(string key)
|
||
|
{
|
||
|
FloatVariable variable = GetVariable<FloatVariable>(key);
|
||
|
|
||
|
if (variable != null)
|
||
|
{
|
||
9 years ago
|
return GetVariable<FloatVariable>(key).Value;
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
return 0f;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public virtual void SetFloatVariable(string key, float value)
|
||
|
{
|
||
|
FloatVariable variable = GetVariable<FloatVariable>(key);
|
||
|
if (variable != null)
|
||
|
{
|
||
9 years ago
|
variable.Value = value;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public virtual string GetStringVariable(string key)
|
||
|
{
|
||
|
StringVariable variable = GetVariable<StringVariable>(key);
|
||
|
|
||
|
if (variable != null)
|
||
|
{
|
||
9 years ago
|
return GetVariable<StringVariable>(key).Value;
|
||
9 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public virtual void SetStringVariable(string key, string value)
|
||
|
{
|
||
|
StringVariable variable = GetVariable<StringVariable>(key);
|
||
|
if (variable != null)
|
||
|
{
|
||
9 years ago
|
variable.Value = value;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public virtual void UpdateHideFlags()
|
||
9 years ago
|
{
|
||
|
if (hideComponents)
|
||
|
{
|
||
9 years ago
|
Block[] blocks = GetComponents<Block>();
|
||
|
foreach (Block block in blocks)
|
||
9 years ago
|
{
|
||
9 years ago
|
block.hideFlags = HideFlags.HideInInspector;
|
||
|
if (block.gameObject != gameObject)
|
||
9 years ago
|
{
|
||
9 years ago
|
block.hideFlags = HideFlags.HideInHierarchy;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
Command[] commands = GetComponents<Command>();
|
||
9 years ago
|
foreach (var command in commands)
|
||
9 years ago
|
{
|
||
|
command.hideFlags = HideFlags.HideInInspector;
|
||
|
}
|
||
|
|
||
|
EventHandler[] eventHandlers = GetComponents<EventHandler>();
|
||
9 years ago
|
foreach (var eventHandler in eventHandlers)
|
||
9 years ago
|
{
|
||
|
eventHandler.hideFlags = HideFlags.HideInInspector;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
MonoBehaviour[] monoBehaviours = GetComponents<MonoBehaviour>();
|
||
|
foreach (MonoBehaviour monoBehaviour in monoBehaviours)
|
||
|
{
|
||
|
if (monoBehaviour == null)
|
||
|
{
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
monoBehaviour.hideFlags = HideFlags.None;
|
||
|
monoBehaviour.gameObject.hideFlags = HideFlags.None;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public virtual void ClearSelectedCommands()
|
||
|
{
|
||
|
selectedCommands.Clear();
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
public virtual void AddSelectedCommand(Command command)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (!selectedCommands.Contains(command))
|
||
9 years ago
|
{
|
||
9 years ago
|
selectedCommands.Add(command);
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
public virtual void Reset(bool resetCommands, bool resetVariables)
|
||
|
{
|
||
|
if (resetCommands)
|
||
|
{
|
||
9 years ago
|
ICommand[] commands = GetComponents<ICommand>();
|
||
|
foreach (var command in commands)
|
||
9 years ago
|
{
|
||
|
command.OnReset();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (resetVariables)
|
||
|
{
|
||
|
foreach (Variable variable in variables)
|
||
|
{
|
||
|
variable.OnReset();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public virtual bool IsCommandSupported(CommandInfoAttribute commandInfo)
|
||
|
{
|
||
|
foreach (string key in hideCommands)
|
||
|
{
|
||
|
// Match on category or command name (case insensitive)
|
||
|
if (String.Compare(commandInfo.Category, key, StringComparison.OrdinalIgnoreCase) == 0 ||
|
||
|
String.Compare(commandInfo.CommandName, key, StringComparison.OrdinalIgnoreCase) == 0)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public virtual bool HasExecutingBlocks()
|
||
|
{
|
||
9 years ago
|
var blocks = GetComponents<IBlock>();
|
||
9 years ago
|
foreach (IBlock block in blocks)
|
||
9 years ago
|
{
|
||
|
if (block.IsExecuting())
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
9 years ago
|
public virtual List<IBlock> GetExecutingBlocks()
|
||
9 years ago
|
{
|
||
9 years ago
|
var executingBlocks = new List<IBlock>();
|
||
|
var blocks = GetComponents<IBlock>();
|
||
|
foreach (var block in blocks)
|
||
9 years ago
|
{
|
||
|
if (block.IsExecuting())
|
||
|
{
|
||
|
executingBlocks.Add(block);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return executingBlocks;
|
||
|
}
|
||
|
|
||
|
public virtual string SubstituteVariables(string input)
|
||
|
{
|
||
|
if (stringSubstituer == null)
|
||
|
{
|
||
|
stringSubstituer = new StringSubstituter();
|
||
9 years ago
|
stringSubstituer.CacheSubstitutionHandlers();
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Use the string builder from StringSubstituter for efficiency.
|
||
9 years ago
|
StringBuilder sb = stringSubstituer._StringBuilder;
|
||
9 years ago
|
sb.Length = 0;
|
||
|
sb.Append(input);
|
||
9 years ago
|
|
||
9 years ago
|
// Instantiate the regular expression object.
|
||
|
Regex r = new Regex("{\\$.*?}");
|
||
|
|
||
|
bool changed = false;
|
||
|
|
||
|
// Match the regular expression pattern against a text string.
|
||
|
var results = r.Matches(input);
|
||
|
foreach (Match match in results)
|
||
|
{
|
||
|
string key = match.Value.Substring(2, match.Value.Length - 3);
|
||
|
|
||
|
// Look for any matching private variables in this Flowchart first
|
||
|
foreach (Variable variable in variables)
|
||
|
{
|
||
|
if (variable == null)
|
||
|
continue;
|
||
|
|
||
9 years ago
|
if (variable.Scope == VariableScope.Private &&
|
||
|
variable.Key == key)
|
||
9 years ago
|
{
|
||
|
string value = variable.ToString();
|
||
9 years ago
|
sb.Replace(match.Value, value);
|
||
9 years ago
|
changed = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// Now do all other substitutions in the scene
|
||
|
changed |= stringSubstituer.SubstituteStrings(sb);
|
||
9 years ago
|
|
||
9 years ago
|
if (changed)
|
||
|
{
|
||
9 years ago
|
return sb.ToString();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return input;
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
#endregion
|
||
|
|
||
|
#region IStringSubstituter implementation
|
||
|
|
||
|
/// <summary>
|
||
|
/// Implementation of StringSubstituter.ISubstitutionHandler which matches any public variable in the Flowchart.
|
||
|
/// To perform full variable substitution with all substitution handlers in the scene, you should
|
||
|
/// use the SubstituteVariables() method instead.
|
||
|
/// </summary>
|
||
|
[MoonSharp.Interpreter.MoonSharpHidden]
|
||
|
public virtual bool SubstituteStrings(StringBuilder input)
|
||
|
{
|
||
|
// Instantiate the regular expression object.
|
||
|
Regex r = new Regex("{\\$.*?}");
|
||
|
|
||
|
bool modified = false;
|
||
|
|
||
|
// Match the regular expression pattern against a text string.
|
||
|
var results = r.Matches(input.ToString());
|
||
|
foreach (Match match in results)
|
||
|
{
|
||
|
string key = match.Value.Substring(2, match.Value.Length - 3);
|
||
|
|
||
|
// Look for any matching public variables in this Flowchart
|
||
|
foreach (Variable variable in variables)
|
||
|
{
|
||
|
if (variable == null)
|
||
|
continue;
|
||
|
|
||
|
if (variable.Scope == VariableScope.Public &&
|
||
|
variable.Key == key)
|
||
|
{
|
||
|
string value = variable.ToString();
|
||
|
input.Replace(match.Value, value);
|
||
|
|
||
|
modified = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return modified;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
9 years ago
|
}
|
||
11 years ago
|
}
|