using EasyTalk.Controller;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace EasyTalk.Display
{
///
/// Extends upon the DialoguePanel class to create an abstract base implementation of a display for displaying dialogue options to a player.
///
public abstract class OptionDisplay : DialoguePanel
{
///
/// A list of images used to create the option display panel.
///
[Tooltip("The images used for the option display panel.")]
[NonReorderable]
[SerializeField]
protected List images;
///
/// Listeners for option display events which are triggered whenever the options are displayed or the selected option changes.
///
[Tooltip("A set of Option Display Listeners which listen for the list of options to be set or the selected option to change on an option display.")]
[NonReorderable]
[SerializeField]
protected List optionDisplayListeners;
///
/// An event which is triggered when the presented options are updated.
///
[SerializeField]
[HideInInspector]
public UnityEvent onOptionsSet = new UnityEvent();
///
/// An event which is triggered when the player selects (highlights) an option.
///
[SerializeField]
[HideInInspector]
public UnityEvent onOptionSelected = new UnityEvent();
///
/// An event which is triggered when the player changes the selection from one option to another.
///
[SerializeField]
[HideInInspector]
public UnityEvent onOptionChanged = new UnityEvent();
///
/// Initializes the display and hides it immediately.
///
private void Awake()
{
Init();
HideImmediately();
}
///
public override void Init()
{
base.Init();
onShowComplete.AddListener(ReadyOptions);
}
///
/// Make options available and ready for player interactions, reset the display for each option, and highlight the default/selected option.
///
protected virtual void ReadyOptions() { }
///
/// Setup the display to show the options specified.
///
/// The dialogue options the display needs to present.
public abstract void SetOptions(List options);
///
/// The list of images used to create the option display panel.
///
public List Images
{
get { return images; }
}
///
/// Returns a List containing all of the options buttons of the option display.
///
///
public abstract List GetOptionButtons();
///
/// Selects the next option (relative to the currently selected one).
///
/// Returns true if an option was selected; false otherwise.
public virtual bool SelectNextOption() { return false; }
///
/// Selects the previous option (relative to the currently selected one).
///
/// Returns true if an option was selected; false otherwise.
public virtual bool SelectPreviousOption() { return false; }
///
/// Selected the option corresponding to the specified direction and returns the option index of it.
/// This is typically used for selecting options using a joystick, d-pad, or arrow keys.
///
/// The direction to get an option for.
/// Returns true if an option was selected; false otherwise.
public virtual bool SelectOptionInDirection(Vector2 direction) { return false; }
///
/// Returns the index of the option currently selected.
///
/// The index of the option currently selected.
public abstract int GetSelectedOption();
///
/// This method should be called whenever an option is finalized as the choice the player wants to make. It will call the callback methods
/// assigned to the onOptionChosen delegate.
///
protected void OptionChosen()
{
if (onOptionChosen != null)
{
onOptionChosen.Invoke();
}
}
///
/// Activates the buttons used for selecting options.
///
public abstract void ActivateButtons();
///
/// Deactivates the buttons used for selecting options.
///
public abstract void DeactivateButtons();
///
/// Calls the OnOptionsSet() method on all Option Display Listeners registered with the Option Display.
///
/// The List of Dialogue Options set on the Option Display.
protected virtual void OnOptionsSet(List options)
{
if(onOptionsSet != null) { onOptionsSet.Invoke(); }
foreach(OptionDisplayListener listener in optionDisplayListeners)
{
listener.OnOptionsSet(options);
}
}
///
/// Calls the OnOptionSelected() method on all Option Display Listeners registered with the Option Display.
///
/// The Dialogue Option to pass to the OnOptionSelected() method of each listener.
protected virtual void OptionSelected(DialogueOption option)
{
if(onOptionSelected != null) { onOptionSelected.Invoke(); }
foreach (OptionDisplayListener listener in optionDisplayListeners)
{
listener.OnOptionSelected(option);
}
}
///
/// Calls the OnOptionChanged() method on all Option Display Listeners registered with the Option Display.
///
/// The previously selected Dialogue Option.
/// The newly selected Dialogue Option.
protected virtual void OptionChanged(DialogueOption oldOption, DialogueOption newOption)
{
if(onOptionChanged != null) { onOptionChanged.Invoke(); }
foreach (OptionDisplayListener listener in optionDisplayListeners)
{
listener.OnOptionChanged(oldOption, newOption);
}
}
///
/// Translates the text of each Dialogue Button in the option display based on the current language set on EasyTalkGameState.Instance.Language.
///
/// The Dialogue Controller currently being used.
public virtual void TranslateOptions(DialogueController controller)
{
if (controller != null)
{
List optionButtons = GetOptionButtons();
foreach (DialogueButton button in optionButtons)
{
DialogueOption option = ((DialogueOption)button.Value);
if (option != null)
{
string text = option.PreTranslationText;
text = controller.GetNodeHandler().Translate(text);
if(button.StandardText != null)
{
button.StandardText.text = text;
}
#if TEXTMESHPRO_INSTALLED
if (button.TMPText != null)
{
button.TMPText.text = text;
}
#endif
}
}
}
}
}
}