Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / Dialogue / Dialogue.cs
using Ink.Runtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Dialogue
{
    private GameObject dialoguePanel;
    private Story story;

    public Story Story => story;

    private GameObject DialoguePanel(float sizeX, float sizeY, Color32 color)
    {
        GameObject dialoguePanel = new GameObject("DialoguePanel");
        RectTransform rect = dialoguePanel.AddComponent<RectTransform>();
        Image img = dialoguePanel.AddComponent<Image>();
        img.color = color;
        rect.anchorMin = new Vector2(0.5f, 0f);
        rect.anchorMax = new Vector2(0.5f, 0f);
        rect.pivot = new Vector2(0.5f, 0f);
        rect.sizeDelta = new Vector2(sizeX, sizeY);
        return dialoguePanel;
    }

    public Dialogue Add(Story story)
    {
        this.story = story;
        return this;
    }

    // Builds the dialogue UI and attaches it to the parent
    public Dialogue Build(Transform parent)
    {
        if (story != null)
        {
            // Here, size is just an example. You can calculate it based on the story content
            float panelWidth = 600f; // Example width
            float panelHeight = 200f; // Example height

            // Call the method with size and color
            dialoguePanel = DialoguePanel(panelWidth, panelHeight, new Color32(50, 50, 50, 255));

            // Set parent and adjust UI positioning
            dialoguePanel.transform.SetParent(parent, false); // Keep the transform settings intact

            // Optionally add a Text component to display the story text
            Text dialogueText = dialoguePanel.AddComponent<Text>();
            dialogueText.text = story.Continue();  // Get the first line of the story
            dialogueText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
            dialogueText.color = Color.white;
            dialogueText.alignment = TextAnchor.MiddleCenter;

            return this;
        }
        return null;
    }
}