Browse Source

Refactored MenuDialog to use IMenuDialog interface

master
Christopher 8 years ago
parent
commit
d9209ebc2f
  1. 4
      Assets/Fungus/Narrative/Scripts/Commands/Menu.cs
  2. 2
      Assets/Fungus/Narrative/Scripts/Commands/MenuTimer.cs
  3. 2
      Assets/Fungus/Narrative/Scripts/DialogInput.cs
  4. 43
      Assets/Fungus/Narrative/Scripts/IMenuDialog.cs
  5. 12
      Assets/Fungus/Narrative/Scripts/IMenuDialog.cs.meta
  6. 117
      Assets/Fungus/Narrative/Scripts/MenuDialog.cs

4
Assets/Fungus/Narrative/Scripts/Commands/Menu.cs

@ -47,10 +47,10 @@ namespace Fungus
if (!hideOption) if (!hideOption)
{ {
MenuDialog menuDialog = MenuDialog.GetMenuDialog(); IMenuDialog menuDialog = MenuDialog.GetMenuDialog();
if (menuDialog != null) if (menuDialog != null)
{ {
menuDialog.gameObject.SetActive(true); menuDialog.SetActive(true);
Flowchart flowchart = GetFlowchart(); Flowchart flowchart = GetFlowchart();
string displayText = flowchart.SubstituteVariables(text); string displayText = flowchart.SubstituteVariables(text);

2
Assets/Fungus/Narrative/Scripts/Commands/MenuTimer.cs

@ -26,7 +26,7 @@ namespace Fungus
public override void OnEnter() public override void OnEnter()
{ {
MenuDialog menuDialog = MenuDialog.GetMenuDialog(); IMenuDialog menuDialog = MenuDialog.GetMenuDialog();
if (menuDialog != null && if (menuDialog != null &&
targetBlock != null) targetBlock != null)

2
Assets/Fungus/Narrative/Scripts/DialogInput.cs

@ -140,7 +140,7 @@ namespace Fungus
{ {
// Ignore input events if a Menu is being displayed // Ignore input events if a Menu is being displayed
if (MenuDialog.activeMenuDialog != null && if (MenuDialog.activeMenuDialog != null &&
MenuDialog.activeMenuDialog.gameObject.activeInHierarchy && MenuDialog.activeMenuDialog.IsActive() &&
MenuDialog.activeMenuDialog.DisplayedOptionsCount > 0) MenuDialog.activeMenuDialog.DisplayedOptionsCount > 0)
{ {
dialogClickedFlag = false; dialogClickedFlag = false;

43
Assets/Fungus/Narrative/Scripts/IMenuDialog.cs

@ -0,0 +1,43 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
/// <summary>
/// Presents multiple choice buttons to the players.
/// </summary>
public interface IMenuDialog
{
/// <summary>
/// Sets the active state of the Menu Dialog gameobject.
/// </summary>
void SetActive(bool state);
/// <summary>
/// Adds the option to the list of displayed options.
/// Will cause the Menu dialog to become visible if it is not already visible.
/// </summary>
/// <returns><c>true</c>, if the option was added successfully.</returns>
/// <param name="text">The option text to display on the button.</param>
/// <param name="interactable">If false, the option is displayed but is not selectable.</param>
/// <param name="targetBlock">Block to execute when the option is selected.</param>
bool AddOption(string text, bool interactable, Block targetBlock);
/// <summary>
/// Show a timer during which the player can select an option.
/// </summary>
/// <param name="duration">The duration during which the player can select an option.</param>
/// <param name="targetBlock">Block to execute if the player does not select an option in time.</param>
void ShowTimer(float duration, Block targetBlock);
/// <summary>
/// Returns true if the Menu Dialog is currently displayed.
/// </summary>
bool IsActive();
/// <summary>
/// Returns the number of currently displayed options.
/// </summary>
int DisplayedOptionsCount { get; }
}
}

12
Assets/Fungus/Narrative/Scripts/IMenuDialog.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 88b3741e7161f40d3b6166170b69c55e
timeCreated: 1473425656
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

117
Assets/Fungus/Narrative/Scripts/MenuDialog.cs

@ -9,13 +9,10 @@ using System.Linq;
namespace Fungus namespace Fungus
{ {
/// <summary> public class MenuDialog : MonoBehaviour, IMenuDialog
/// Presents multiple choice buttons to the players.
/// </summary>
public class MenuDialog : MonoBehaviour
{ {
// Currently active Menu Dialog used to display Menu options // Currently active Menu Dialog used to display Menu options
public static MenuDialog activeMenuDialog; public static IMenuDialog activeMenuDialog;
[Tooltip("Automatically select the first interactable button when the menu is shown.")] [Tooltip("Automatically select the first interactable button when the menu is shown.")]
[SerializeField] protected bool autoSelectFirstButton = false; [SerializeField] protected bool autoSelectFirstButton = false;
@ -26,12 +23,12 @@ namespace Fungus
protected Slider cachedSlider; protected Slider cachedSlider;
public virtual Slider CachedSlider { get { return cachedSlider; } } public virtual Slider CachedSlider { get { return cachedSlider; } }
public static MenuDialog GetMenuDialog() public static IMenuDialog GetMenuDialog()
{ {
if (activeMenuDialog == null) if (activeMenuDialog == null)
{ {
// Use first Menu Dialog found in the scene (if any) // Use first Menu Dialog found in the scene (if any)
MenuDialog md = GameObject.FindObjectOfType<MenuDialog>(); IMenuDialog md = GameObject.FindObjectOfType<MenuDialog>();
if (md != null) if (md != null)
{ {
activeMenuDialog = md; activeMenuDialog = md;
@ -46,7 +43,7 @@ namespace Fungus
GameObject go = Instantiate(prefab) as GameObject; GameObject go = Instantiate(prefab) as GameObject;
go.SetActive(false); go.SetActive(false);
go.name = "MenuDialog"; go.name = "MenuDialog";
activeMenuDialog = go.GetComponent<MenuDialog>(); activeMenuDialog = go.GetComponent<IMenuDialog>();
} }
} }
} }
@ -101,6 +98,52 @@ namespace Fungus
} }
} }
public virtual void HideSayDialog()
{
ISayDialog sayDialog = SayDialog.GetSayDialog();
if (sayDialog != null)
{
sayDialog.FadeWhenDone = true;
}
}
protected virtual IEnumerator WaitForTimeout(float timeoutDuration, Block targetBlock)
{
float elapsedTime = 0;
Slider timeoutSlider = GetComponentInChildren<Slider>();
while (elapsedTime < timeoutDuration)
{
if (timeoutSlider != null)
{
float t = 1f - elapsedTime / timeoutDuration;
timeoutSlider.value = t;
}
elapsedTime += Time.deltaTime;
yield return null;
}
Clear();
gameObject.SetActive(false);
HideSayDialog();
if (targetBlock != null)
{
targetBlock.StartExecution();
}
}
#region IMenuDialog implementation
public virtual void SetActive(bool state)
{
gameObject.SetActive(state);
}
public virtual bool AddOption(string text, bool interactable, Block targetBlock) public virtual bool AddOption(string text, bool interactable, Block targetBlock)
{ {
bool addedOption = false; bool addedOption = false;
@ -156,30 +199,6 @@ namespace Fungus
return addedOption; return addedOption;
} }
public virtual int DisplayedOptionsCount
{
get {
int count = 0;
foreach (Button button in cachedButtons)
{
if (button.gameObject.activeSelf)
{
count++;
}
}
return count;
}
}
public virtual void HideSayDialog()
{
ISayDialog sayDialog = SayDialog.GetSayDialog();
if (sayDialog != null)
{
sayDialog.FadeWhenDone = true;
}
}
public virtual void ShowTimer(float duration, Block targetBlock) public virtual void ShowTimer(float duration, Block targetBlock)
{ {
if (cachedSlider != null) if (cachedSlider != null)
@ -191,34 +210,26 @@ namespace Fungus
} }
} }
protected virtual IEnumerator WaitForTimeout(float timeoutDuration, Block targetBlock) public virtual bool IsActive()
{ {
float elapsedTime = 0; return gameObject.activeInHierarchy;
}
Slider timeoutSlider = GetComponentInChildren<Slider>();
while (elapsedTime < timeoutDuration) public virtual int DisplayedOptionsCount
{ {
if (timeoutSlider != null) get {
int count = 0;
foreach (Button button in cachedButtons)
{ {
float t = 1f - elapsedTime / timeoutDuration; if (button.gameObject.activeSelf)
timeoutSlider.value = t; {
count++;
} }
elapsedTime += Time.deltaTime;
yield return null;
} }
return count;
Clear();
gameObject.SetActive(false);
HideSayDialog();
if (targetBlock != null)
{
targetBlock.StartExecution();
} }
} }
#endregion
} }
} }

Loading…
Cancel
Save