84 lines
2.6 KiB
C#
84 lines
2.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Firebase.Extensions;
|
|
using Firebase.Storage;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UI;
|
|
|
|
public class PropositionFrame : MonoBehaviour
|
|
{
|
|
public RawImage picture;
|
|
public GameObject voterStickerPrefab;
|
|
public TextMeshProUGUI playerName;
|
|
public Transform playerGrid;
|
|
|
|
private List<Player> currentVoters = new List<Player>();
|
|
private Proposition proposition;
|
|
|
|
public void Initialize(Proposition _proposition)
|
|
{
|
|
Debug.Log($"Initializing {_proposition.owner}'s proposition", this);
|
|
proposition = _proposition;
|
|
playerName.text = proposition.owner.name;
|
|
|
|
if (!string.IsNullOrEmpty(proposition.photoUrl))
|
|
DisplayPicture(proposition.photoUrl);
|
|
}
|
|
|
|
private void DisplayPicture(string _url)
|
|
{
|
|
Debug.Log($"Downloading {_url}");
|
|
|
|
StorageReference imageRef = FirebaseStorage.DefaultInstance.GetReferenceFromUrl(_url);
|
|
imageRef.GetDownloadUrlAsync().ContinueWithOnMainThread(task =>
|
|
{
|
|
if (task.IsFaulted || task.IsCanceled)
|
|
{
|
|
Debug.LogException(task.Exception);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Download URL: " + task.Result);
|
|
StartCoroutine(DownloadImage(task.Result.AbsolutePath, _texture =>
|
|
{
|
|
Debug.Log("Set texture in the raw image");
|
|
picture.texture = _texture;
|
|
}));
|
|
}
|
|
});
|
|
}
|
|
|
|
private IEnumerator DownloadImage(string _url, Action<Texture> callback_OnTextureDownloaded)
|
|
{
|
|
UnityWebRequest request = UnityWebRequestTexture.GetTexture(_url);
|
|
yield return request.SendWebRequest();
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
{
|
|
Debug.LogError(request.error);
|
|
}
|
|
else
|
|
{
|
|
callback_OnTextureDownloaded?.Invoke(((DownloadHandlerTexture)request.downloadHandler).texture);
|
|
}
|
|
}
|
|
|
|
public void UpdateVoters(List<Player> _newVoters)
|
|
{
|
|
Debug.Log($"There are some new voters for {proposition.owner}'s proposition", this);
|
|
foreach (Player p in _newVoters)
|
|
{
|
|
if (!currentVoters.Contains(p))
|
|
{
|
|
currentVoters.Add(p);
|
|
VoterSticker sticker = Instantiate(voterStickerPrefab, playerGrid).GetComponent<VoterSticker>();
|
|
sticker.playerNameLabel.text = p.name;
|
|
Debug.Log($"{p.name} has just voted for {proposition.owner.name}'s proposition.");
|
|
}
|
|
}
|
|
}
|
|
}
|