Skip to content

possibility to add numbers to the options displayed text & select opt… #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions Runtime/Views/OptionsListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Yarn Spinner is licensed to you under the terms found in the file LICENSE.md.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yarn.Markup;

#if USE_TMP
using TMPro;
#else
Expand All @@ -26,6 +28,8 @@ public class OptionsListView : DialogueViewBase

[SerializeField] bool showUnavailableOptions = false;

[SerializeField] private bool addNumbersToOptions;

[Header("Last Line Components")]
[SerializeField] TextMeshProUGUI lastLineText;
[SerializeField] GameObject lastLineContainer;
Expand Down Expand Up @@ -74,6 +78,7 @@ public override void RunOptions(DialogueOption[] dialogueOptions, Action<int> on
// Set up all of the option views
int optionViewsCreated = 0;

int optionDisplayNumber = 1;
for (int i = 0; i < dialogueOptions.Length; i++)
{
var optionView = optionViews[i];
Expand All @@ -88,6 +93,16 @@ public override void RunOptions(DialogueOption[] dialogueOptions, Action<int> on
optionView.gameObject.SetActive(true);

optionView.palette = this.palette;

if (addNumbersToOptions)
{
option.Line.Text = new MarkupParseResult
{
Attributes = option.Line.Text.Attributes,
Text = $"{optionDisplayNumber}.\t{option.Line.Text.Text}"
};
}

optionView.Option = option;

// The first available option is selected by default
Expand All @@ -96,6 +111,11 @@ public override void RunOptions(DialogueOption[] dialogueOptions, Action<int> on
optionView.Select();
}

if (option.IsAvailable == true)
{
optionDisplayNumber++;
}

optionViewsCreated += 1;
}

Expand Down Expand Up @@ -187,7 +207,7 @@ IEnumerator OptionViewWasSelectedInternal(DialogueOption selectedOption)
/// If options are still shown dismisses them.
/// </remarks>
public override void DialogueComplete()
{
{
// do we still have any options being shown?
if (canvasGroup.alpha > 0)
{
Expand Down Expand Up @@ -230,12 +250,46 @@ private void Relayout()
{
UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(layout.GetComponent<RectTransform>());
}

// Perform the second pass of re-layout. This will update the outer vertical group's positioning of the individual elements.
foreach (var layout in layouts)
{
UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(layout.GetComponent<RectTransform>());
}
}

/// <summary>
/// Call, when the option should be selected without the button being clicked.
/// This could be when the player can use the 1-9 keys on the keyboard to select an option.
/// </summary>
/// <param name="number">First option has number 1 and so on.</param>
public void SelectOption(int number)
{
if (optionViews.Count < number)
return;

int optionDisplayNumber = 0;
foreach (var option in optionViews)
{
if (!option.isActiveAndEnabled)
continue;

optionDisplayNumber++;

if (optionDisplayNumber == number)
option.InvokeOptionSelected();
}
}

/// <summary>
/// Call, when the last option should be selected without the button being clicked.
/// An example would be a "Cancel" key (on keyboard or controller), that will always select the last option.
/// </summary>
public void SelectLastOption()
{
var optionView = optionViews.FindLast(o => o.isActiveAndEnabled);
if (optionView != null)
optionView.InvokeOptionSelected();
}
}
}