Skip to content
Snippets Groups Projects
DisplayScrolls.cs 3.04 KiB
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Networking;

public class DisplayScrolls : MonoBehaviour
{
    public Scroll[] scrolls;
    public GameObject[] ScrollButtons;
    public GameObject ScrollPrefab;
    public GameObject DetailScreen;



    public int x_Start;
    public int y_Start;
    public int X_Pacece_Between_Items;
    public int y_Pacece_Between_Items;
    public int number_of_Column;
   

    // Update is called once per frame
    void Update()
    {

    }


    public Vector3 GetPosition(int i)
    {
        return new Vector3(x_Start+ (X_Pacece_Between_Items * (i % number_of_Column)), y_Start + (-y_Pacece_Between_Items * (i / number_of_Column)), 0f);
    }

    [System.Serializable]
    class ScrollArrayWrapper{
        public Scroll[] Scrolls;
    };

    // Start is called before the first frame update
    void Start()
    {
        var rect = GetComponent<RectTransform>();
        x_Start = (int)(rect.rect.x + X_Pacece_Between_Items * .5f);
        y_Start = (int)(-rect.rect.y - y_Pacece_Between_Items * .5f);//);
        number_of_Column =Mathf.Max(1,(int)(rect.rect.width / ScrollPrefab.GetComponent<RectTransform>().rect.width) - 1);
        //get Scrolls from Backend;

        //string path = "Mock-Scrolls.json";
        //string jsonString = File.ReadAllText(path);
        //buildScrollSelection(jsonString);
        StartCoroutine(getScrollsfromServer());
        
    }

    IEnumerator getScrollsfromServer() {
        UnityWebRequest request = UnityWebRequest.Get("localhost:8081/scroll/list");
        yield return request.Send();
        if (request.isNetworkError || request.isHttpError)
        {
            Debug.LogError(request.error);
        }
        else
        {
            string jsonString = request.downloadHandler.text;
            buildScrollSelection(jsonString);
        }
    }