first commit
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OTGIntegrated.Dialogue
|
||||
{
|
||||
public sealed class DialogueUI : MonoBehaviour
|
||||
{
|
||||
public GameObject panelRoot;
|
||||
public Text speakerText;
|
||||
public Text lineText;
|
||||
public Transform responseRoot;
|
||||
public Button closeButton;
|
||||
public Font uiFont;
|
||||
|
||||
private readonly List<GameObject> spawnedResponses = new List<GameObject>();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
ConfigureResponseLayout();
|
||||
|
||||
if (closeButton != null)
|
||||
{
|
||||
closeButton.onClick.RemoveAllListeners();
|
||||
closeButton.onClick.AddListener(() => FindObjectOfType<DialogueManager>()?.EndDialogue());
|
||||
}
|
||||
|
||||
Hide();
|
||||
}
|
||||
|
||||
public void Show(string speaker, string line, IReadOnlyList<DialogueChoice> choices)
|
||||
{
|
||||
if (panelRoot != null)
|
||||
{
|
||||
panelRoot.SetActive(true);
|
||||
}
|
||||
|
||||
if (speakerText != null)
|
||||
{
|
||||
speakerText.text = speaker;
|
||||
}
|
||||
|
||||
if (lineText != null)
|
||||
{
|
||||
lineText.text = line;
|
||||
}
|
||||
|
||||
ClearResponses();
|
||||
if (choices == null || responseRoot == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < choices.Count; i++)
|
||||
{
|
||||
CreateChoiceButton(choices[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
ClearResponses();
|
||||
if (panelRoot != null)
|
||||
{
|
||||
panelRoot.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateChoiceButton(DialogueChoice choice)
|
||||
{
|
||||
if (choice == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject root = new GameObject("DialogueChoice", typeof(RectTransform), typeof(Image), typeof(Button));
|
||||
root.transform.SetParent(responseRoot, false);
|
||||
spawnedResponses.Add(root);
|
||||
root.GetComponent<RectTransform>().sizeDelta = new Vector2(0f, 42f);
|
||||
LayoutElement layoutElement = root.AddComponent<LayoutElement>();
|
||||
layoutElement.preferredHeight = 42f;
|
||||
layoutElement.minHeight = 42f;
|
||||
layoutElement.flexibleWidth = 1f;
|
||||
root.GetComponent<Image>().color = new Color(0.11f, 0.14f, 0.18f, 0.94f);
|
||||
|
||||
Text label = CreateText(root.transform, "Text", choice.Text, 16, TextAnchor.MiddleLeft);
|
||||
label.rectTransform.anchorMin = Vector2.zero;
|
||||
label.rectTransform.anchorMax = Vector2.one;
|
||||
label.rectTransform.offsetMin = new Vector2(14f, 0f);
|
||||
label.rectTransform.offsetMax = new Vector2(-8f, 0f);
|
||||
|
||||
Button button = root.GetComponent<Button>();
|
||||
button.onClick.AddListener(() => choice.Callback?.Invoke());
|
||||
}
|
||||
|
||||
private void ConfigureResponseLayout()
|
||||
{
|
||||
if (responseRoot == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
VerticalLayoutGroup layout = responseRoot.GetComponent<VerticalLayoutGroup>();
|
||||
if (layout == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
layout.childControlWidth = true;
|
||||
layout.childForceExpandWidth = true;
|
||||
layout.childControlHeight = false;
|
||||
layout.childForceExpandHeight = false;
|
||||
}
|
||||
|
||||
private Text CreateText(Transform parent, string name, string value, int fontSize, TextAnchor anchor)
|
||||
{
|
||||
GameObject textObject = new GameObject(name, typeof(RectTransform), typeof(Text));
|
||||
textObject.transform.SetParent(parent, false);
|
||||
Text text = textObject.GetComponent<Text>();
|
||||
text.font = uiFont != null ? uiFont : Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||||
text.fontSize = fontSize;
|
||||
text.alignment = anchor;
|
||||
text.color = new Color(0.94f, 0.93f, 0.88f, 1f);
|
||||
text.text = value;
|
||||
return text;
|
||||
}
|
||||
|
||||
private void ClearResponses()
|
||||
{
|
||||
for (int i = 0; i < spawnedResponses.Count; i++)
|
||||
{
|
||||
if (spawnedResponses[i] != null)
|
||||
{
|
||||
Destroy(spawnedResponses[i]);
|
||||
}
|
||||
}
|
||||
|
||||
spawnedResponses.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user