529 lines
16 KiB
C#
529 lines
16 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.Video;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
using Assets.Scripts;
|
|
//using Unity.VectorGraphics;
|
|
using UnityEditor;
|
|
|
|
public class ChConversations_Controller : MonoBehaviour
|
|
{
|
|
int idCharactersConversation;
|
|
string route;
|
|
|
|
public static ChConversations_Controller instance;
|
|
|
|
#region Conversation
|
|
|
|
[System.Serializable]
|
|
public class ConversationResponse
|
|
{
|
|
public int status;
|
|
public string error;
|
|
public ConversationModel[] response;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class ConversationModel
|
|
{
|
|
public int idcharactersConversation;
|
|
public int seconds;
|
|
public float speed;
|
|
public string description;
|
|
public int idcharacters_1;
|
|
public string character_1;
|
|
public int idcharacters_2;
|
|
public string character_2;
|
|
public int idcharactersBackground;
|
|
public string background;
|
|
public string backgroundExt;
|
|
public int idcharactersFonts;
|
|
public string font;
|
|
public int fontSize;
|
|
public string fontColor;
|
|
public int idcharactersPhrases;
|
|
public int charactertalking;
|
|
public int idcharactersBaloons;
|
|
public string baloon;
|
|
public string baloonExt;
|
|
public string phrase;
|
|
public int idcharactersFeelings_1;
|
|
public string characterFeeling_1;
|
|
public int idcharactersFeelings_2;
|
|
public string characterFeeling_2;
|
|
public bool sintetizevoice;
|
|
public int idcharactersVoices;
|
|
public string voicebane;
|
|
public string voice;
|
|
public int idcharactersTransitions;
|
|
public string transition;
|
|
public string transitionExt;
|
|
public int ordem;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region FEELINGS
|
|
|
|
[System.Serializable]
|
|
public class FeelingsResponse
|
|
{
|
|
public int status;
|
|
public string error;
|
|
public List<FeelingsModel> response;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class FeelingsModel
|
|
{
|
|
public int idcharactersFeelings;
|
|
public string description;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Phrases
|
|
[System.Serializable]
|
|
public class Phrases
|
|
{
|
|
public int status;
|
|
public PhrasesResponse[] response;
|
|
}
|
|
[System.Serializable]
|
|
public class PhrasesResponse
|
|
{
|
|
public string phrase, firstCharacterFeeling, secondCharacterFeeling;
|
|
public int characterTalking;
|
|
}
|
|
#endregion
|
|
|
|
public class VideoCv
|
|
{
|
|
public string nameCaller, path;
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class ImagesCaller { public string caller; public Sprite sprite; }
|
|
|
|
public List<VideoCv> videos;
|
|
public List<ImagesCaller> images;
|
|
Text txtMensagens;
|
|
public ConversationModel[] ListaConversacao;
|
|
int FraseAtual = 0;
|
|
public List<FeelingsModel> ListaFeelings;
|
|
public AudioSource source;
|
|
WriteFile writeFile;
|
|
VideoPlayer playerFundo;
|
|
public Image playerChar1;
|
|
public Image playerChar2;
|
|
Image imgBalao;
|
|
Animator thisAnime;
|
|
string AudioExt = ".mp3";
|
|
string CharacterExt = ".png";
|
|
|
|
public Transform trClique;
|
|
|
|
public class BypassCertificate : CertificateHandler
|
|
{
|
|
protected override bool ValidateCertificate(byte[] certificateData)
|
|
{
|
|
//Simply return true no matter what
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public void SetInformations(int idcharactersConversation)
|
|
{
|
|
instance = this;
|
|
videos = new List<VideoCv>();
|
|
writeFile = new WriteFile();
|
|
idCharactersConversation = idcharactersConversation;
|
|
StartCoroutine(GetInfoCor());
|
|
}
|
|
|
|
IEnumerator GetInfoCor()
|
|
{
|
|
ApiClass api = ApiClass.instance;
|
|
route = api.route;
|
|
|
|
thisAnime = GetComponent<Animator>();
|
|
|
|
yield return StartCoroutine(GetConversationDetails());
|
|
|
|
yield return StartCoroutine(GetAllFeelings());
|
|
|
|
//yield return StartCoroutine(GetPhrasesConversation());
|
|
|
|
//yield return StartCoroutine(api.getBackgroundVideo(ListaConversacao[FraseAtual].idcharactersBackground, "fundo"));
|
|
|
|
yield return StartCoroutine(MontarResources());
|
|
|
|
ShowInfo();
|
|
}
|
|
|
|
IEnumerator AddImage(string caller, string path)
|
|
{
|
|
ImagesCaller img = new ImagesCaller();
|
|
img.caller = caller;
|
|
|
|
string url = "http://" + ReadingFile.iploc + "/zoeGamesResources/characters/" + path;
|
|
|
|
WWW www = new WWW(url);
|
|
|
|
yield return www;
|
|
|
|
img.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width,
|
|
www.texture.height), new Vector2(0, 0));
|
|
|
|
images.Add(img);
|
|
|
|
yield break;
|
|
}
|
|
|
|
VideoCv MontarVideo(string NomeArquivo, string Caminho)
|
|
{
|
|
VideoCv videoCv = new VideoCv();
|
|
videoCv.nameCaller = NomeArquivo;
|
|
videoCv.path = Caminho;
|
|
return videoCv;
|
|
}
|
|
|
|
IEnumerator MontarResources()
|
|
{
|
|
ApiClass api = ApiClass.instance;
|
|
|
|
images = new List<ImagesCaller>();
|
|
|
|
// BACKGROUND
|
|
string NomeFundo = "fundo";
|
|
string ExtFundo = ListaConversacao[0].backgroundExt;
|
|
//yield return StartCoroutine(api.DownloadResource("backgrounds/" + ListaConversacao[0].idcharactersBackground + ExtFundo, NomeFundo + ExtFundo));
|
|
videos.Add(MontarVideo(NomeFundo, Application.persistentDataPath + "/" + NomeFundo + ExtFundo));
|
|
|
|
// CHARACTERS FEELINGS
|
|
for (int i = 0; i < ListaFeelings.Count; i++)
|
|
{
|
|
string name1 = "Ch-" + ListaConversacao[0].idcharacters_1 + "_" + ListaFeelings[i].idcharactersFeelings;
|
|
string pathtolook = name1.Replace("Ch-", "") + CharacterExt;
|
|
yield return StartCoroutine(AddImage(name1, pathtolook));
|
|
|
|
string name2 = "Ch-" + ListaConversacao[1].idcharacters_2 + "_" + ListaFeelings[i].idcharactersFeelings;
|
|
string pathtolook2 = name2.Replace("Ch-", "") + CharacterExt;
|
|
yield return StartCoroutine(AddImage(name2, pathtolook2));
|
|
}
|
|
|
|
// AUDIOS
|
|
for (int i = 0; i < ListaConversacao.Length; i++)
|
|
{
|
|
string Ext = AudioExt;
|
|
string NomeArq = "FileAudio_" + i + Ext;
|
|
}
|
|
|
|
// BALOES
|
|
for (int i = 0; i < ListaConversacao.Length; i++)
|
|
{
|
|
string Ext = ListaConversacao[i].baloonExt;
|
|
string NomeArq = "FileImage_Bl-" + ListaConversacao[i].idcharactersBaloons + Ext;
|
|
}
|
|
|
|
// TRANSICOES
|
|
for (int i = 0; i < ListaConversacao.Length; i++)
|
|
{
|
|
string Ext = ListaConversacao[i].transitionExt;
|
|
string src = "transitions/" + ListaConversacao[i].idcharactersTransitions + Ext;
|
|
string name = "Ts-" + ListaConversacao[i].idcharactersTransitions;
|
|
videos.Add(MontarVideo(name, Application.persistentDataPath + "/videoFile" + name + Ext));
|
|
}
|
|
|
|
yield break;
|
|
}
|
|
|
|
void ShowInfo()
|
|
{
|
|
playerFundo = transform.Find("FundoVideo").GetComponent<VideoPlayer>();
|
|
playerFundo.url = getVideoByCaller("fundo");
|
|
playerFundo.Play();
|
|
|
|
transform.Find("Fundo").gameObject.SetActive(false);
|
|
|
|
txtMensagens = transform.Find("Balao").transform.Find("Mensagem").GetComponent<Text>();
|
|
|
|
imgBalao = transform.Find("Balao").GetComponent<Image>();
|
|
|
|
FraseAtual = 0;
|
|
|
|
int nbrChilds = transform.childCount;
|
|
GameObject imgLoad = transform.GetChild(nbrChilds - 1).gameObject;
|
|
Destroy(imgLoad);
|
|
|
|
ProximaFrase();
|
|
}
|
|
|
|
void ProximaFrase()
|
|
{
|
|
StartCoroutine(ShowPhraseActual());
|
|
}
|
|
|
|
IEnumerator ShowPhraseActual()
|
|
{
|
|
txtMensagens.text = "";
|
|
|
|
if (ListaConversacao[FraseAtual].idcharactersTransitions > 0)
|
|
{
|
|
thisAnime.SetBool("show", false);
|
|
playerFundo.url = getVideoByCaller("Ts-" + ListaConversacao[FraseAtual].idcharactersTransitions);
|
|
playerFundo.Play();
|
|
|
|
yield return new WaitForSecondsRealtime(ListaConversacao[FraseAtual].seconds);
|
|
}
|
|
else
|
|
{
|
|
thisAnime.SetBool("show", true);
|
|
if (FraseAtual > 0 && ListaConversacao[FraseAtual - 1].idcharactersTransitions > 0)
|
|
{
|
|
playerFundo.url = getVideoByCaller("fundo");
|
|
playerFundo.Play();
|
|
}
|
|
|
|
Color fontColor = strToColor(this.ListaConversacao[FraseAtual].fontColor);
|
|
fontColor.a = 1;
|
|
txtMensagens.color = fontColor;
|
|
FontManager fm = new FontManager();
|
|
txtMensagens.font = fm.GetFontFromOS(ListaConversacao[FraseAtual].font, ListaConversacao[FraseAtual].fontSize);
|
|
|
|
//yield return new WaitForSecondsRealtime(ListaConversacao[FraseAtual].seconds);
|
|
|
|
if (ListaConversacao[FraseAtual].baloonExt == ".png")
|
|
{
|
|
Texture2D texture = ReadingFile.LoadPNG(Application.persistentDataPath + "/FileImage_Bl-" + ListaConversacao[FraseAtual].idcharactersBaloons + ListaConversacao[FraseAtual].baloonExt);
|
|
imgBalao.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
|
|
}
|
|
else if (ListaConversacao[FraseAtual].baloonExt == ".svg")
|
|
{
|
|
Texture2D texture = ReadingFile.LoadPNG(Application.persistentDataPath + "/FileImage_Bl-" + ListaConversacao[FraseAtual].idcharactersBaloons + ListaConversacao[FraseAtual].baloonExt);
|
|
imgBalao.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
|
|
}
|
|
|
|
VerificaLadoBalao();
|
|
imgBalao.color = Color.white;
|
|
|
|
if (
|
|
(FraseAtual == 0 && ListaConversacao[FraseAtual].idcharactersTransitions == 0) || // primeira frase não é transição
|
|
(FraseAtual > 0 && ListaConversacao[FraseAtual].idcharactersTransitions == 0 && ListaConversacao[FraseAtual - 1].idcharactersFeelings_1 != ListaConversacao[FraseAtual].idcharactersFeelings_1)
|
|
) {
|
|
playerChar1.sprite = getSpriteByCaller("Ch-" + ListaConversacao[FraseAtual].idcharacters_1 + "_" + ListaConversacao[FraseAtual].idcharactersFeelings_1);
|
|
}
|
|
|
|
if (
|
|
(FraseAtual == 0 && ListaConversacao[FraseAtual].idcharactersTransitions == 0) || // primeira frase não é transição
|
|
(FraseAtual > 0 && ListaConversacao[FraseAtual].idcharactersTransitions == 0 && ListaConversacao[FraseAtual - 1].idcharactersFeelings_2 != ListaConversacao[FraseAtual].idcharactersFeelings_2)
|
|
)
|
|
{
|
|
playerChar2.sprite = getSpriteByCaller("Ch-" + ListaConversacao[FraseAtual].idcharacters_2 + "_" + ListaConversacao[FraseAtual].idcharactersFeelings_2);
|
|
}
|
|
|
|
StartCoroutine(ReproduzirAudio("FileAudio_" + FraseAtual + AudioExt));
|
|
|
|
//Escrever Frase
|
|
for (int i = 0; i < ListaConversacao[FraseAtual].phrase.Length; i++)
|
|
{
|
|
string phrase = ListaConversacao[FraseAtual].phrase;
|
|
phrase = phrase.Remove(i);
|
|
txtMensagens.text = phrase;
|
|
|
|
yield return new WaitForSecondsRealtime(ListaConversacao[FraseAtual].speed / 3);
|
|
}
|
|
|
|
txtMensagens.text = ListaConversacao[FraseAtual].phrase;
|
|
}
|
|
|
|
FraseAtual++;
|
|
|
|
while (Input.GetMouseButton(0))
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
while (!Input.GetMouseButton(0))
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
|
|
if (FraseAtual < ListaConversacao.Length)
|
|
{
|
|
ProximaFrase();
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(FimDialogo());
|
|
}
|
|
}
|
|
|
|
void VerificaLadoBalao()
|
|
{
|
|
if (ListaConversacao[FraseAtual].charactertalking == 1)
|
|
{
|
|
imgBalao.transform.localScale = new Vector3(-1, 1, 1);
|
|
txtMensagens.transform.localScale = new Vector3(-1, 1, 1);
|
|
|
|
trClique.transform.localScale = new Vector3(-1, 1, 1);
|
|
|
|
txtMensagens.alignment = TextAnchor.UpperRight;
|
|
}
|
|
else
|
|
{
|
|
imgBalao.transform.localScale = new Vector3(1, 1, 1);
|
|
txtMensagens.transform.localScale = new Vector3(1, 1, 1);
|
|
|
|
trClique.transform.localScale = new Vector3(1, 1, 1);
|
|
|
|
txtMensagens.alignment = TextAnchor.UpperLeft;
|
|
}
|
|
}
|
|
|
|
IEnumerator ReproduzirAudio(string NomeArquivo)
|
|
{
|
|
yield return StartCoroutine(writeFile.loadAudio(NomeArquivo));
|
|
source.clip = writeFile.audioLoaded;
|
|
source.Play();
|
|
}
|
|
|
|
Sprite getSpriteByCaller(string caller)
|
|
{
|
|
foreach (ImagesCaller item in images)
|
|
{
|
|
if (item.caller == caller)
|
|
{
|
|
Debug.Log("Sprite Reacher");
|
|
return item.sprite;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
string getVideoByCaller(string nameCaller)
|
|
{
|
|
foreach (VideoCv item in videos)
|
|
{
|
|
if (item.nameCaller == nameCaller)
|
|
{
|
|
return item.path;
|
|
}
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
IEnumerator GetConversationDetails()
|
|
{
|
|
string url = route + "/api/characters/getCharConvDetails/" + idCharactersConversation;
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
www.certificateHandler = new BypassCertificate();
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
www.SetRequestHeader("token", Uri.EscapeUriString(PlayerPrefs.GetString("token")));
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.isNetworkError || www.isHttpError)
|
|
{
|
|
Debug.LogError("Erro de Conexão");
|
|
}
|
|
else
|
|
{
|
|
string Json = www.downloadHandler.text;
|
|
ConversationResponse conversation = JsonUtility.FromJson<ConversationResponse>(Json);
|
|
|
|
Debug.Log(Json);
|
|
|
|
if (conversation.status == 200)
|
|
{
|
|
ListaConversacao = conversation.response;
|
|
}
|
|
}
|
|
}
|
|
|
|
yield break;
|
|
}
|
|
|
|
IEnumerator GetAllFeelings()
|
|
{
|
|
string url = route + "/api/characters/getAllFeelings";
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
www.certificateHandler = new BypassCertificate();
|
|
www.downloadHandler = new DownloadHandlerBuffer();
|
|
www.SetRequestHeader("token", Uri.EscapeUriString(PlayerPrefs.GetString("token")));
|
|
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.isNetworkError || www.isHttpError)
|
|
{
|
|
Debug.LogError("Erro de Conexão");
|
|
}
|
|
else
|
|
{
|
|
string Json = www.downloadHandler.text;
|
|
FeelingsResponse feelings = JsonUtility.FromJson<FeelingsResponse>(Json);
|
|
|
|
if (feelings.status == 200)
|
|
{
|
|
ListaFeelings = feelings.response;
|
|
}
|
|
}
|
|
}
|
|
|
|
yield break;
|
|
}
|
|
|
|
IEnumerator FimDialogo()
|
|
{
|
|
playerFundo.Stop();
|
|
|
|
thisAnime.enabled = true;
|
|
thisAnime.SetTrigger("end");
|
|
|
|
yield return new WaitForSecondsRealtime(1);
|
|
|
|
GeneralScript general = GeneralScript.instance;
|
|
general.LiberarAvanco(true);
|
|
|
|
RectTransform rectVideo = thisAnime.gameObject.GetComponent<RectTransform>();
|
|
Vector2 rect = rectVideo.anchoredPosition;
|
|
|
|
thisAnime.enabled = false;
|
|
yield return new WaitForEndOfFrame();
|
|
rectVideo.anchoredPosition = rect;
|
|
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
//general.Avancar();
|
|
|
|
yield break;
|
|
}
|
|
|
|
Color strToColor(string TextoCor)
|
|
{
|
|
Debug.Log(TextoCor);
|
|
try
|
|
{
|
|
float R = Convert.ToInt32(TextoCor.Split(',')[0]);
|
|
float G = Convert.ToInt32(TextoCor.Split(',')[1]);
|
|
float B = Convert.ToInt32(TextoCor.Split(',')[2]);
|
|
float A = Convert.ToInt32(TextoCor.Split(',')[3]);
|
|
return new Color(R / 255, G / 255, B / 255, A / 255);
|
|
}
|
|
catch
|
|
{
|
|
return new Color(0, 0, 0, 0);
|
|
}
|
|
}
|
|
|
|
}
|