From e73121ea5c24c80aaf4a91b4f3b0db9dbc4adb4a Mon Sep 17 00:00:00 2001 From: desktop-maesty/steve Date: Mon, 14 Jan 2019 21:50:56 +1000 Subject: [PATCH] Command can now cache a list of referenced variables that are checked by the variablelistadapter for highlighting - Flowchart can identify add variables found via the substitute regex - Commands that use SubstituteVariables on their string data use the new caching method so they can highlight variables that will be used during substitution --- .../Fungus/Scripts/Commands/Conversation.cs | 14 ++++++++ Assets/Fungus/Scripts/Commands/DebugLog.cs | 13 +++++++ .../Fungus/Scripts/Commands/DeleteSaveKey.cs | 15 +++++++- .../Fungus/Scripts/Commands/LoadVariable.cs | 14 +++++++- Assets/Fungus/Scripts/Commands/Menu.cs | 13 +++++++ .../Fungus/Scripts/Commands/SaveVariable.cs | 14 +++++++- Assets/Fungus/Scripts/Commands/SetText.cs | 14 ++++++++ Assets/Fungus/Scripts/Commands/SetVariable.cs | 13 +++++++ Assets/Fungus/Scripts/Components/Command.cs | 34 +++++++++++++++++++ Assets/Fungus/Scripts/Components/Flowchart.cs | 17 ++++++++++ .../Scripts/Editor/VariableListAdaptor.cs | 4 +-- 11 files changed, 160 insertions(+), 5 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/Conversation.cs b/Assets/Fungus/Scripts/Commands/Conversation.cs index 42e1da4a..34410fba 100644 --- a/Assets/Fungus/Scripts/Commands/Conversation.cs +++ b/Assets/Fungus/Scripts/Commands/Conversation.cs @@ -64,5 +64,19 @@ namespace Fungus } #endregion + + + #region Editor caches +#if UNITY_EDITOR + protected override void RefreshVariableCache() + { + base.RefreshVariableCache(); + + var f = GetFlowchart(); + + f.DetermineSubstituteVariables(conversationText, referencedVariables); + } +#endif + #endregion Editor caches } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Commands/DebugLog.cs b/Assets/Fungus/Scripts/Commands/DebugLog.cs index fce1c374..35285992 100644 --- a/Assets/Fungus/Scripts/Commands/DebugLog.cs +++ b/Assets/Fungus/Scripts/Commands/DebugLog.cs @@ -67,5 +67,18 @@ namespace Fungus } #endregion + + #region Editor caches +#if UNITY_EDITOR + protected override void RefreshVariableCache() + { + base.RefreshVariableCache(); + + var f = GetFlowchart(); + + f.DetermineSubstituteVariables(logMessage.Value, referencedVariables); + } +#endif + #endregion Editor caches } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Commands/DeleteSaveKey.cs b/Assets/Fungus/Scripts/Commands/DeleteSaveKey.cs index 6b7559ac..e9a6e6b5 100644 --- a/Assets/Fungus/Scripts/Commands/DeleteSaveKey.cs +++ b/Assets/Fungus/Scripts/Commands/DeleteSaveKey.cs @@ -53,5 +53,18 @@ namespace Fungus } #endregion - } + + #region Editor caches +#if UNITY_EDITOR + protected override void RefreshVariableCache() + { + base.RefreshVariableCache(); + + var f = GetFlowchart(); + + f.DetermineSubstituteVariables(key, referencedVariables); + } +#endif + #endregion Editor caches + } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Commands/LoadVariable.cs b/Assets/Fungus/Scripts/Commands/LoadVariable.cs index ecea1bab..5c074ea3 100644 --- a/Assets/Fungus/Scripts/Commands/LoadVariable.cs +++ b/Assets/Fungus/Scripts/Commands/LoadVariable.cs @@ -100,5 +100,17 @@ namespace Fungus } #endregion - } + #region Editor caches +#if UNITY_EDITOR + protected override void RefreshVariableCache() + { + base.RefreshVariableCache(); + + var f = GetFlowchart(); + + f.DetermineSubstituteVariables(key, referencedVariables); + } +#endif + #endregion Editor caches + } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Commands/Menu.cs b/Assets/Fungus/Scripts/Commands/Menu.cs index c8ac058a..6babd89c 100644 --- a/Assets/Fungus/Scripts/Commands/Menu.cs +++ b/Assets/Fungus/Scripts/Commands/Menu.cs @@ -121,5 +121,18 @@ namespace Fungus } #endregion + + #region Editor caches +#if UNITY_EDITOR + protected override void RefreshVariableCache() + { + base.RefreshVariableCache(); + + var f = GetFlowchart(); + + f.DetermineSubstituteVariables(text, referencedVariables); + } +#endif + #endregion Editor caches } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Commands/SaveVariable.cs b/Assets/Fungus/Scripts/Commands/SaveVariable.cs index 43933880..5b9f9e5e 100644 --- a/Assets/Fungus/Scripts/Commands/SaveVariable.cs +++ b/Assets/Fungus/Scripts/Commands/SaveVariable.cs @@ -104,5 +104,17 @@ namespace Fungus } #endregion - } + #region Editor caches +#if UNITY_EDITOR + protected override void RefreshVariableCache() + { + base.RefreshVariableCache(); + + var f = GetFlowchart(); + + f.DetermineSubstituteVariables(key, referencedVariables); + } +#endif + #endregion Editor caches + } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Commands/SetText.cs b/Assets/Fungus/Scripts/Commands/SetText.cs index 0f87a67b..6fdb0fe5 100644 --- a/Assets/Fungus/Scripts/Commands/SetText.cs +++ b/Assets/Fungus/Scripts/Commands/SetText.cs @@ -82,6 +82,20 @@ namespace Fungus #endregion + + #region Editor caches +#if UNITY_EDITOR + protected override void RefreshVariableCache() + { + base.RefreshVariableCache(); + + var f = GetFlowchart(); + + f.DetermineSubstituteVariables(text, referencedVariables); + } +#endif + #endregion Editor caches + #region ILocalizable implementation public virtual string GetStandardText() diff --git a/Assets/Fungus/Scripts/Commands/SetVariable.cs b/Assets/Fungus/Scripts/Commands/SetVariable.cs index 98e3f3ba..a696a3ee 100644 --- a/Assets/Fungus/Scripts/Commands/SetVariable.cs +++ b/Assets/Fungus/Scripts/Commands/SetVariable.cs @@ -319,5 +319,18 @@ namespace Fungus } #endregion + + #region Editor caches +#if UNITY_EDITOR + protected override void RefreshVariableCache() + { + base.RefreshVariableCache(); + + var f = GetFlowchart(); + + f.DetermineSubstituteVariables(stringData.Value, referencedVariables); + } +#endif + #endregion Editor caches } } diff --git a/Assets/Fungus/Scripts/Components/Command.cs b/Assets/Fungus/Scripts/Components/Command.cs index 0c63df11..6188f3ea 100644 --- a/Assets/Fungus/Scripts/Components/Command.cs +++ b/Assets/Fungus/Scripts/Components/Command.cs @@ -50,6 +50,30 @@ namespace Fungus protected string errorMessage = ""; + #region Editor caches +#if UNITY_EDITOR + // + protected List referencedVariables = new List(); + + //used by var list adapter to highlight variables + public bool IsVariableReferenced(Variable variable) + { + return referencedVariables.Contains(variable) || HasReference(variable); + } + + /// + /// Called by OnValidate + /// + /// Child classes to specialise to add variable references to referencedVariables, either directly or + /// via the use of Flowchart.DetermineSubstituteVariables + /// + protected virtual void RefreshVariableCache() + { + referencedVariables.Clear(); + } +#endif + #endregion Editor caches + #region Public members /// @@ -204,6 +228,16 @@ namespace Fungus return false; } + /// + /// Called by unity when script is loaded or its data changed by editor + /// + public virtual void OnValidate() + { +#if UNITY_EDITOR + RefreshVariableCache(); +#endif + } + /// /// Returns the summary text to display in the command inspector. /// diff --git a/Assets/Fungus/Scripts/Components/Flowchart.cs b/Assets/Fungus/Scripts/Components/Flowchart.cs index 28355e20..25b11047 100644 --- a/Assets/Fungus/Scripts/Components/Flowchart.cs +++ b/Assets/Fungus/Scripts/Components/Flowchart.cs @@ -1264,6 +1264,23 @@ namespace Fungus } } + public virtual void DetermineSubstituteVariables(string str, List vars) + { + Regex r = new Regex(Flowchart.SubstituteVariableRegexString); + + // Match the regular expression pattern against a text string. + var results = r.Matches(str); + for (int i = 0; i < results.Count; i++) + { + var match = results[i]; + var v = GetVariable(match.Value.Substring(2, match.Value.Length - 3)); + if (v != null) + { + vars.Add(v); + } + } + } + #endregion #region IStringSubstituter implementation diff --git a/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs b/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs index 3b3698cc..8dcfaf8c 100644 --- a/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs +++ b/Assets/Fungus/Scripts/Editor/VariableListAdaptor.cs @@ -178,7 +178,7 @@ namespace Fungus.EditorUtils { if (Application.isPlaying && flowchart.SelectedBlock.IsExecuting()) { - highlight = flowchart.SelectedBlock.ActiveCommand.HasReference(variable); + highlight = flowchart.SelectedBlock.ActiveCommand.IsVariableReferenced(variable); } else if (!Application.isPlaying && flowchart.SelectedCommands.Count > 0) { @@ -189,7 +189,7 @@ namespace Fungus.EditorUtils continue; } - if (selectedCommand.HasReference(variable)) + if (selectedCommand.IsVariableReferenced(variable)) { highlight = true; break;