From 12fc2081f5480a5fc4e7bb79f1e0cd0e8475c936 Mon Sep 17 00:00:00 2001 From: lealeelu Date: Sat, 18 Feb 2017 14:43:20 -0500 Subject: [PATCH] Separate NarrativeLogMenu from SaveMenu --- .../Fungus/Scripts/Components/NarrativeLog.cs | 15 -- .../Scripts/Components/NarrativeLogMenu.cs | 152 ++++++++++++++++++ .../Components/NarrativeLogMenu.cs.meta | 12 ++ Assets/Fungus/Scripts/Components/SaveData.cs | 6 +- Assets/Fungus/Scripts/Components/SaveMenu.cs | 80 +-------- .../Fungus/Scripts/Signals/WriterSignals.cs | 2 +- Assets/Fungus/Scripts/Utils/SaveHistory.cs | 2 +- .../Conversation/Say History.unity | 20 ++- 8 files changed, 186 insertions(+), 103 deletions(-) create mode 100644 Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs create mode 100644 Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs.meta diff --git a/Assets/Fungus/Scripts/Components/NarrativeLog.cs b/Assets/Fungus/Scripts/Components/NarrativeLog.cs index 956887d0..1654480f 100644 --- a/Assets/Fungus/Scripts/Components/NarrativeLog.cs +++ b/Assets/Fungus/Scripts/Components/NarrativeLog.cs @@ -45,21 +45,6 @@ namespace Fungus history = new NarrativeData(); } - protected virtual void OnEnable() - { - SaveManagerSignals.OnSaveReset += OnSaveReset; - } - - protected virtual void OnDisable() - { - SaveManagerSignals.OnSaveReset -= OnSaveReset; - } - - protected virtual void OnSaveReset() - { - Clear(); - } - #region Public Methods /// diff --git a/Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs b/Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs new file mode 100644 index 00000000..7238f52b --- /dev/null +++ b/Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs @@ -0,0 +1,152 @@ +// 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) + +#if UNITY_5_3_OR_NEWER + +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.SceneManagement; + +namespace Fungus +{ + /// + /// A singleton game object which displays a simple UI for the Narrative Log. + /// + public class NarrativeLogMenu : MonoBehaviour + { + [Tooltip("The button that shows conversation history.")] + [SerializeField] protected Button narrativeLogButton; + + [Tooltip("A scrollable text field used for displaying conversation history.")] + [SerializeField] protected ScrollRect narrativeLogView; + + [Tooltip("The CanvasGroup containing the save menu buttons")] + [SerializeField] protected CanvasGroup narrativeLogMenuGroup; + + protected static bool narrativeLogActive = false; + + protected AudioSource clickAudioSource; + + protected LTDescr fadeTween; + + protected static NarrativeLogMenu instance; + + protected virtual void Awake() + { + // Only one instance of NarrativeLogMenu may exist + if (instance != null) + { + Destroy(gameObject); + return; + } + + instance = this; + + GameObject.DontDestroyOnLoad(this); + + clickAudioSource = GetComponent(); + } + + protected virtual void Start() + { + if (!narrativeLogActive) + { + narrativeLogMenuGroup.alpha = 0f; + } + + //Clear up the lorem ipsum + UpdateNarrativeLogText(); + + } + + protected virtual void OnEnable() + { + WriterSignals.OnWriterState += OnWriterState; + SaveManagerSignals.OnSavePointLoaded += OnSavePointLoaded; + SaveManagerSignals.OnSaveReset += OnSaveReset; + } + + protected virtual void OnDisable() + { + WriterSignals.OnWriterState -= OnWriterState; + SaveManagerSignals.OnSavePointLoaded -= OnSavePointLoaded; + SaveManagerSignals.OnSaveReset -= OnSaveReset; + } + + protected virtual void OnWriterState(Writer writer, WriterState writerState) + { + if (writerState == WriterState.End || writerState == WriterState.Start) + { + UpdateNarrativeLogText(); + } + } + + protected virtual void OnSavePointLoaded(string savePointKey) + { + UpdateNarrativeLogText(); + } + + protected virtual void OnSaveReset() + { + FungusManager.Instance.NarrativeLog.Clear(); + UpdateNarrativeLogText(); + } + + protected void UpdateNarrativeLogText() + { + if (narrativeLogView.enabled) + { + Debug.Log("Update NarrativeLog"); + var historyText = narrativeLogView.GetComponentInChildren(); + if (historyText != null) + { + historyText.text = FungusManager.Instance.NarrativeLog.GetPrettyHistory(); + } + } + } + + protected void PlayClickSound() + { + if (clickAudioSource != null) + { + clickAudioSource.Play(); + } + } + + #region Public methods + + public virtual void ToggleNarrativeLogView() + { + if (fadeTween != null) + { + LeanTween.cancel(fadeTween.id, true); + fadeTween = null; + } + + if (narrativeLogActive) + { + // Switch menu off + LeanTween.value(narrativeLogMenuGroup.gameObject, narrativeLogMenuGroup.alpha, 0f, 0.2f).setOnUpdate((t) => { + narrativeLogMenuGroup.alpha = t; + }).setOnComplete(() => { + narrativeLogMenuGroup.alpha = 0f; + }); + } + else + { + // Switch menu on + LeanTween.value(narrativeLogMenuGroup.gameObject, narrativeLogMenuGroup.alpha, 1f, 0.2f).setOnUpdate((t) => { + narrativeLogMenuGroup.alpha = t; + }).setOnComplete(() => { + narrativeLogMenuGroup.alpha = 1f; + }); + } + + narrativeLogActive = !narrativeLogActive; + } + + #endregion + } +} + +#endif \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs.meta b/Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs.meta new file mode 100644 index 00000000..92f231ae --- /dev/null +++ b/Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 76befdada4ed8754db75aeb0b0d42976 +timeCreated: 1487446345 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/Components/SaveData.cs b/Assets/Fungus/Scripts/Components/SaveData.cs index 00f1c382..3a6fdc05 100644 --- a/Assets/Fungus/Scripts/Components/SaveData.cs +++ b/Assets/Fungus/Scripts/Components/SaveData.cs @@ -17,7 +17,7 @@ namespace Fungus { protected const string FlowchartDataKey = "FlowchartData"; - protected const string NarrativeLogKey = "HistoryData"; + protected const string NarrativeLogKey = "NarrativeLogData"; [Tooltip("A list of Flowchart objects whose variables will be encoded in the save data. Boolean, Integer, Float and String variables are supported.")] [SerializeField] protected List flowcharts = new List(); @@ -37,8 +37,8 @@ namespace Fungus var saveDataItem = SaveDataItem.Create(FlowchartDataKey, JsonUtility.ToJson(flowchartData)); saveDataItems.Add(saveDataItem); - var historyDataItem = SaveDataItem.Create(NarrativeLogKey, FungusManager.Instance.NarrativeLog.GetJsonHistory()); - saveDataItems.Add(historyDataItem); + var narrativeLogItem = SaveDataItem.Create(NarrativeLogKey, FungusManager.Instance.NarrativeLog.GetJsonHistory()); + saveDataItems.Add(narrativeLogItem); } } diff --git a/Assets/Fungus/Scripts/Components/SaveMenu.cs b/Assets/Fungus/Scripts/Components/SaveMenu.cs index 04f9e45e..1c13daeb 100644 --- a/Assets/Fungus/Scripts/Components/SaveMenu.cs +++ b/Assets/Fungus/Scripts/Components/SaveMenu.cs @@ -47,22 +47,11 @@ namespace Fungus [Tooltip("The button which restarts the game.")] [SerializeField] protected Button restartButton; - [Tooltip("The button that shows conversation history.")] - [SerializeField] protected Button narrativeLogButton; - - [Tooltip("A scrollable text field used for displaying conversation history.")] - [SerializeField] protected ScrollRect narrativeLogView; - - [Tooltip("The CanvasGroup containing the save menu buttons")] - [SerializeField] protected CanvasGroup narrativeLogMenuGroup; - [Tooltip("A scrollable text field used for debugging the save data. The text field should be disabled in normal use.")] [SerializeField] protected ScrollRect debugView; protected static bool saveMenuActive = false; - protected static bool narrativeLogActive = false; - protected AudioSource clickAudioSource; protected LTDescr fadeTween; @@ -92,14 +81,6 @@ namespace Fungus saveMenuGroup.alpha = 0f; } - if (!narrativeLogActive) - { - narrativeLogMenuGroup.alpha = 0f; - } - - //Clear up the lorem ipsum - UpdateNarrativeLogText(); - var saveManager = FungusManager.Instance.SaveManager; // Make a note of the current scene. This will be used when restarting the game. @@ -167,13 +148,11 @@ namespace Fungus protected virtual void OnEnable() { SaveManagerSignals.OnSavePointAdded += OnSavePointAdded; - WriterSignals.OnWriterState += OnWriterState; } protected virtual void OnDisable() { SaveManagerSignals.OnSavePointAdded -= OnSavePointAdded; - WriterSignals.OnWriterState -= OnWriterState; } protected virtual void OnSavePointAdded(string savePointKey, string savePointDescription) @@ -187,27 +166,6 @@ namespace Fungus } } - protected virtual void OnWriterState(Writer writer, WriterState writerState) - { - if (writerState == WriterState.End || writerState == WriterState.Start) - { - UpdateNarrativeLogText(); - } - } - - protected void UpdateNarrativeLogText() - { - if (narrativeLogView.enabled) - { - Debug.Log("Update NarrativeLog"); - var historyText = narrativeLogView.GetComponentInChildren(); - if (historyText != null) - { - historyText.text = FungusManager.Instance.NarrativeLog.GetPrettyHistory(); - } - } - } - protected void PlayClickSound() { if (clickAudioSource != null) @@ -284,7 +242,6 @@ namespace Fungus saveManager.Load(saveDataKey); } - UpdateNarrativeLogText(); } /// @@ -300,7 +257,6 @@ namespace Fungus saveManager.Rewind(); } - UpdateNarrativeLogText(); } /// @@ -334,48 +290,14 @@ namespace Fungus // Reset the Save History for a new game saveManager.ClearHistory(); - // Update and clear narrative log - SaveManagerSignals.DoSaveReset(); - UpdateNarrativeLogText(); - if (restartDeletesSave) { saveManager.Delete(saveDataKey); } - + SaveManagerSignals.DoSaveReset(); SceneManager.LoadScene(saveManager.StartScene); } - public virtual void ToggleNarrativeLogView() - { - if (fadeTween != null) - { - LeanTween.cancel(fadeTween.id, true); - fadeTween = null; - } - - if (narrativeLogActive) - { - // Switch menu off - LeanTween.value(narrativeLogMenuGroup.gameObject, narrativeLogMenuGroup.alpha, 0f, 0.2f).setOnUpdate((t) => { - narrativeLogMenuGroup.alpha = t; - }).setOnComplete(() => { - narrativeLogMenuGroup.alpha = 0f; - }); - } - else - { - // Switch menu on - LeanTween.value(narrativeLogMenuGroup.gameObject, narrativeLogMenuGroup.alpha, 1f, 0.2f).setOnUpdate((t) => { - narrativeLogMenuGroup.alpha = t; - }).setOnComplete(() => { - narrativeLogMenuGroup.alpha = 1f; - }); - } - - narrativeLogActive = !narrativeLogActive; - } - #endregion } } diff --git a/Assets/Fungus/Scripts/Signals/WriterSignals.cs b/Assets/Fungus/Scripts/Signals/WriterSignals.cs index 8c8419c5..86b59c76 100644 --- a/Assets/Fungus/Scripts/Signals/WriterSignals.cs +++ b/Assets/Fungus/Scripts/Signals/WriterSignals.cs @@ -4,7 +4,7 @@ namespace Fungus { /// - /// Writer event signalling system. + /// Writer event signaling system. /// You can use this to be notified about various events in the writing process. /// public static class WriterSignals diff --git a/Assets/Fungus/Scripts/Utils/SaveHistory.cs b/Assets/Fungus/Scripts/Utils/SaveHistory.cs index a39189bd..4e237686 100644 --- a/Assets/Fungus/Scripts/Utils/SaveHistory.cs +++ b/Assets/Fungus/Scripts/Utils/SaveHistory.cs @@ -53,7 +53,7 @@ namespace Fungus /// /// Rewinds to the previous Save Point in the Save History. - /// The latest Save Point is moved to a seperate list of rewound save points. + /// The latest Save Point is moved to a separate list of rewound save points. /// public void Rewind() { diff --git a/Assets/FungusExamples/Conversation/Say History.unity b/Assets/FungusExamples/Conversation/Say History.unity index 4c2adfe3..8ebb4f42 100644 --- a/Assets/FungusExamples/Conversation/Say History.unity +++ b/Assets/FungusExamples/Conversation/Say History.unity @@ -2984,6 +2984,7 @@ GameObject: - component: {fileID: 1155851441} - component: {fileID: 1155851440} - component: {fileID: 1155851446} + - component: {fileID: 1155851447} m_Layer: 5 m_Name: SaveMenu m_TagString: Untagged @@ -3154,9 +3155,6 @@ MonoBehaviour: rewindButton: {fileID: 1752978368} forwardButton: {fileID: 1747792014} restartButton: {fileID: 854933977} - narrativeLogButton: {fileID: 1334503914} - narrativeLogView: {fileID: 236109459} - narrativeLogMenuGroup: {fileID: 236109455} debugView: {fileID: 1797755878} --- !u!224 &1155851445 RectTransform: @@ -3202,6 +3200,20 @@ MonoBehaviour: m_ChildForceExpandHeight: 0 m_ChildControlWidth: 0 m_ChildControlHeight: 0 +--- !u!114 &1155851447 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1155851439} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 76befdada4ed8754db75aeb0b0d42976, type: 3} + m_Name: + m_EditorClassIdentifier: + narrativeLogButton: {fileID: 1334503914} + narrativeLogView: {fileID: 236109459} + narrativeLogMenuGroup: {fileID: 236109455} --- !u!1 &1173948434 GameObject: m_ObjectHideFlags: 0 @@ -3594,7 +3606,7 @@ MonoBehaviour: m_OnClick: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 1155851444} + - m_Target: {fileID: 1155851447} m_MethodName: ToggleNarrativeLogView m_Mode: 1 m_Arguments: