Skip to content

Commit

Permalink
Added requested feature: Narrative Log not registering empty Say dial…
Browse files Browse the repository at this point in the history
…og entries
  • Loading branch information
CG-Tespy committed Sep 8, 2020
1 parent fa94124 commit 5cad400
Show file tree
Hide file tree
Showing 8 changed files with 469 additions and 43 deletions.
30 changes: 30 additions & 0 deletions Assets/Fungus/Scripts/Commands/ClearNarrativeLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Fungus
{
[CommandInfo("Narrative",
"Clear Narrative Log",
"Clear the Narrative Log, erasing all entries")]
public class ClearNarrativeLog : Command
{
public override void OnEnter()
{
// This should lead to the UI clearing all the entries; it should do so as a response
// to the Narrative Log clearing its own history
base.OnEnter();
var manager = FungusManager.Instance;
var narrLog = manager.NarrativeLog;

narrLog.Clear();

Continue();
}

public override Color GetButtonColor()
{
return new Color32(184, 210, 235, 255);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Fungus/Scripts/Commands/ClearNarrativeLog.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 75 additions & 10 deletions Assets/Fungus/Scripts/Components/NarrativeLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class NarrativeLogEntry
{
[SerializeField] public string name;
[SerializeField] public string text;
[SerializeField] public int index = -1;
// The index is to keep track of where in the log this entry is to be displayed. A negative index
// means it's not yet even been added to the log.
}

/// <summary>
Expand All @@ -33,29 +36,35 @@ public class NarrativeLog : MonoBehaviour
/// <summary>
/// NarrativeAdded signal. Sent when a line is added.
/// </summary>
public static event NarrativeAddedHandler OnNarrativeAdded;
public event NarrativeAddedHandler OnNarrativeAdded = delegate { };
public event NarrativeHandler OnNarrativeRemoved = delegate { };
// ^ Makes sure these events are never null. Less necessary null checks = better performance ^_^

public delegate void NarrativeHandler(NarrativeLogEntry data);
public delegate void NarrativeAddedHandler(NarrativeLogEntry data);
public static void DoNarrativeAdded(NarrativeLogEntry data)

public void DoNarrativeAdded(NarrativeLogEntry data)
{
if (OnNarrativeAdded != null)
{
OnNarrativeAdded(data);
}
}

public void DoNarrativeRemoved(NarrativeLogEntry data)
{
OnNarrativeRemoved(data);
}

/// <summary>
/// Signal sent when log history is cleared or loaded
/// </summary>
public static System.Action OnNarrativeLogClear;
public static void DoNarrativeCleared()
public System.Action OnNarrativeLogClear = delegate { }; // So we won't need any null checks
public void DoNarrativeCleared()
{
if (OnNarrativeLogClear != null)
{
OnNarrativeLogClear();
}
OnNarrativeLogClear();
}


NarrativeData history;

protected virtual void Awake()
Expand All @@ -66,14 +75,52 @@ protected virtual void Awake()

protected virtual void OnEnable()
{
ListenForNarrativeEvents();
WriterSignals.OnWriterState += OnWriterState;
}

protected virtual void ListenForNarrativeEvents()
{
OnNarrativeRemoved += RespondToNarrativeRemoval;
OnNarrativeAdded += RespondToNarrativeAdding;
}

protected virtual void RespondToNarrativeRemoval(NarrativeLogEntry entryRemoved)
{
// We may want to have this class do other responses to narrative-removal in the
// future, so it's best to have each response in its own func.
UpdateEntryIndexesAfterRemoval(entryRemoved);
}

protected virtual void UpdateEntryIndexesAfterRemoval(NarrativeLogEntry entryRemoved)
{
// The entry removed might not be the last one added, after all.
// We'll need to update the indexes of all the entries that followed
// the removed one.
for (int i = entryRemoved.index; i < history.entries.Count; i++)
{
var entry = history.entries[i];
entry.index = i;
}
}

protected virtual void RespondToNarrativeAdding(NarrativeLogEntry entryAdded)
{
// Future-proofing.
}

protected virtual void OnDisable()
{
UnlistenForNarrativeEvents();
WriterSignals.OnWriterState -= OnWriterState;
}

protected virtual void UnlistenForNarrativeEvents()
{
OnNarrativeRemoved -= RespondToNarrativeRemoval;
OnNarrativeAdded -= RespondToNarrativeAdding;
}

protected virtual void OnWriterState(Writer writer, WriterState writerState)
{
if (writerState == WriterState.End)
Expand All @@ -85,13 +132,15 @@ protected virtual void OnWriterState(Writer writer, WriterState writerState)
NarrativeLogEntry entry = new NarrativeLogEntry()
{
name = sd.NameText,
text = sd.StoryText
text = sd.StoryText,
index = history.entries.Count
};
AddLine(entry);
}
}
}


#region Public Methods

/// <summary>
Expand All @@ -103,6 +152,22 @@ public void AddLine(NarrativeLogEntry entry)
DoNarrativeAdded(entry);
}

/// <summary>
/// Removes a line of dialog from the Narrative Log. Returns whether or not
/// the line was in the log at the time this func was called.
/// </summary>
public bool RemoveLine(NarrativeLogEntry entry)
{
bool hadLineToBeginWith = history.entries.Remove(entry);

if (hadLineToBeginWith)
{
DoNarrativeRemoved(entry);
}

return hadLineToBeginWith;
}

/// <summary>
/// Clear all lines of the narrative log
/// Usually used on restart
Expand Down
95 changes: 83 additions & 12 deletions Assets/Fungus/Scripts/Components/NarrativeLogEntryUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using UnityEngine;
using NarrativeHandler = Fungus.NarrativeLog.NarrativeHandler;

namespace Fungus
{
Expand All @@ -21,19 +22,44 @@ public class NarrativeLogEntryUI : MonoBehaviour
protected List<NarrativeLogEntryDisplay> entryDisplays = new List<NarrativeLogEntryDisplay>();
protected UnityEngine.UI.ScrollRect scrollRect;

public delegate void EntryDisplayHandler(NarrativeLogEntryDisplay entryDisplay);

/// <summary>
/// For when an entry has been registered in the UI.
/// </summary>
public event EntryDisplayHandler OnEntryAdded = delegate { };

/// <summary>
/// For when an entry has been removed from the UI.
/// </summary>
public event EntryDisplayHandler OnEntryRemoved = delegate { };

protected virtual void Awake()
{
ListenForNarrativeEvents();
canvasGroup = GetComponent<CanvasGroup>();
var tmp = FungusManager.Instance.NarrativeLog;
// Make sure to update the UI when new entries are added to or
// cleared from the log.
//using the fungusmanager to ensure that the narrativeLog is inited
NarrativeLog.OnNarrativeAdded += OnNarrativeAdded;
NarrativeLog.OnNarrativeLogClear += Clear;

scrollRect = GetComponentInChildren<UnityEngine.UI.ScrollRect>();
}

protected virtual void ListenForNarrativeEvents()
{
var manager = FungusManager.Instance;

if (manager != null)
{
var narrLog = manager.NarrativeLog;
// Make sure to update the UI when new entries are added to or
// cleared from the log.
narrLog.OnNarrativeAdded += OnNarrativeAdded;
narrLog.OnNarrativeRemoved += OnNarrativeRemoved;
narrLog.OnNarrativeLogClear += Clear;
}
}

/// <summary>
/// Clears all UI entries of the Narrative Log. Note that this does not clear the underlying
/// Narrative Log itself; just the stuff being displayed.
/// </summary>
public virtual void Clear()
{
for (int i = 0; i < entryDisplays.Count; i++)
Expand Down Expand Up @@ -61,12 +87,23 @@ public virtual void Close()

protected virtual void OnNarrativeAdded(NarrativeLogEntry entryAdded)
{
// Create a display for the new entry, and have it show in the UI.
var newEntryDisplay = AddEntryToUI(entryAdded);
AlertListenersForEntryAdding(newEntryDisplay);
StartCoroutine(ForceToBottom());
}

protected virtual NarrativeLogEntryDisplay AddEntryToUI(NarrativeLogEntry entry)
{
var newEntryDisplay = Instantiate(entryDisplayPrefab);
newEntryDisplay.transform.SetParent(entryHolder, false);
newEntryDisplay.ToDisplay = entryAdded;
newEntryDisplay.ToDisplay = entry;
entryDisplays.Add(newEntryDisplay);
StartCoroutine(ForceToBottom());
return newEntryDisplay;
}

protected virtual void AlertListenersForEntryAdding(NarrativeLogEntryDisplay display)
{
OnEntryAdded(display);
}

private System.Collections.IEnumerator ForceToBottom()
Expand All @@ -75,6 +112,27 @@ private System.Collections.IEnumerator ForceToBottom()
scrollRect.verticalNormalizedPosition = 0;
}

protected virtual void OnNarrativeRemoved(NarrativeLogEntry entryRemoved)
{
// Based on the entry's index, make sure we remove exactly the right entry display from the log.
int index = entryRemoved.index;

var toRemove = entryHolder.GetChild(index);
toRemove.gameObject.SetActive(false); // Should make the entry disappear from the log

var entryDisplayScript = toRemove.GetComponent<NarrativeLogEntryDisplay>();
entryDisplays.Remove(entryDisplayScript);
AlertListenersForEntryRemoval(entryDisplayScript);


Destroy(toRemove.gameObject);
}

protected virtual void AlertListenersForEntryRemoval(NarrativeLogEntryDisplay entry)
{
OnEntryRemoved(entry);
}

protected virtual void OnLogCleared()
{
Clear();
Expand All @@ -86,8 +144,21 @@ protected virtual void OnDestroy()
// Avoid this responding to signals when being destroyed.
if (fManInst != null)
{
NarrativeLog.OnNarrativeAdded -= OnNarrativeAdded;
NarrativeLog.OnNarrativeLogClear -= Clear;
FungusManager.Instance.NarrativeLog.OnNarrativeAdded -= OnNarrativeAdded;
FungusManager.Instance.NarrativeLog.OnNarrativeLogClear -= Clear;
}
}

protected virtual void UnlistenForNarrativeEvents()
{
var manager = FungusManager.Instance;

if (manager != null)
{
var narrLog = manager.NarrativeLog;
narrLog.OnNarrativeAdded -= OnNarrativeAdded;
narrLog.OnNarrativeRemoved -= OnNarrativeRemoved;
narrLog.OnNarrativeLogClear -= Clear;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Fungus/Scripts/Components/NarrativeLogMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected virtual void OnEnable()
SaveManagerSignals.OnSavePointLoaded += OnSavePointLoaded;
SaveManagerSignals.OnSaveReset += OnSaveReset;
BlockSignals.OnBlockEnd += OnBlockEnd;
NarrativeLog.OnNarrativeAdded += OnNarrativeAdded;
FungusManager.Instance.NarrativeLog.OnNarrativeAdded += OnNarrativeAdded;
}

protected virtual void OnDisable()
Expand All @@ -92,7 +92,7 @@ protected virtual void OnDisable()
SaveManagerSignals.OnSavePointLoaded -= OnSavePointLoaded;
SaveManagerSignals.OnSaveReset -= OnSaveReset;
BlockSignals.OnBlockEnd -= OnBlockEnd;
NarrativeLog.OnNarrativeAdded -= OnNarrativeAdded;
FungusManager.Instance.NarrativeLog.OnNarrativeAdded -= OnNarrativeAdded;
}

protected virtual void OnNarrativeAdded(NarrativeLogEntry data)
Expand Down
Loading

0 comments on commit 5cad400

Please sign in to comment.