using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.IO; public class MenuController : MonoBehaviour { public static MenuController instance; public static Sprite loadsprite; #region Colors [System.Serializable] public class ColorGroup { public Color clara; public Color escura; public Color contrastColor; } public List colorsGroup; public int indexColor; public ColorGroup GetColor() { if (colorsGroup == null || colorsGroup.Count == 0) { Debug.LogError( "A lista colorsGroup não foi configurada no MenuController." ); return null; } if (indexColor < 0 || indexColor >= colorsGroup.Count) { indexColor = 0; } ColorGroup colorReturn = colorsGroup[indexColor]; indexColor++; if (indexColor >= colorsGroup.Count) { indexColor = 0; } return colorReturn; } #endregion private void Awake() { instance = this; } private Animator canvasAnime; private bool canChange = true; public int nbrsClasses; public GameObject prefabClass; private APIMenu apiMenu; public List classes = new List(); [SerializeField] public List classInfos = new List(); public List allowedCourses = new List(); public List coursesName = new List(); public List allowedClasses = new List(); public Text nomeCurso; private int actualCourse; private GameObject scrollObj; private Transform alligner; private List classPrefabObjs = new List(); public bool listedCourses = false; private bool cancelLoad = false; private Coroutine routineLoading = null; private bool changingCourse = false; private List QuantidadeAulas = new List(); private void Start() { loadsprite = Resources.Load("Load"); GameObject txtClassObject = GameObject.Find("txtClass"); if (txtClassObject != null) { nomeCurso = txtClassObject.GetComponent(); } else { Debug.LogError( "GameObject 'txtClass' não encontrado." ); } GameObject controlAnimeObject = GameObject.Find("ControlAnime"); if (controlAnimeObject != null) { canvasAnime = controlAnimeObject.GetComponent(); } else { Debug.LogError( "GameObject 'ControlAnime' não encontrado." ); } apiMenu = GetComponent(); if (apiMenu == null) { Debug.LogError( "O componente APIMenu não foi encontrado em " + gameObject.name ); } GameObject contentAulasObject = GameObject.Find("Content Aulas"); if (contentAulasObject != null) { alligner = contentAulasObject.transform; } else { Debug.LogError( "GameObject 'Content Aulas' não encontrado." ); } StartCoroutine(CheckNDeleteFiles()); StartCoroutine(secondStart()); } private IEnumerator CheckNDeleteFiles() { string[] files; try { files = Directory.GetFiles( Application.persistentDataPath ); } catch (System.Exception exception) { Debug.LogError( "Erro ao listar arquivos em persistentDataPath: " + exception.Message ); yield break; } foreach (string file in files) { try { File.Delete(file); } catch (System.Exception exception) { Debug.LogWarning( "Não foi possível apagar o arquivo '" + file + "': " + exception.Message ); } yield return new WaitForEndOfFrame(); } } private IEnumerator secondStart() { GameObject objleft = GameObject.Find("Left"); GameObject objRight = GameObject.Find("Right"); if (objleft != null) { objleft.SetActive(false); } if (objRight != null) { objRight.SetActive(false); } yield return new WaitForEndOfFrame(); while ( ReadingFile.instance == null || !ReadingFile.instance.canGo ) { yield return new WaitForEndOfFrame(); } if (apiMenu == null) { Debug.LogError( "APIMenu está nulo. Não é possível carregar as aulas." ); yield break; } yield return StartCoroutine( apiMenu.LogarAluno() ); yield return StartCoroutine( apiMenu.listCourses( ReadingFile.idmatricula, ReadingFile.banco ) ); while (!listedCourses) { yield return new WaitForFixedUpdate(); } for ( int i = 0; i < allowedCourses.Count; i++ ) { if ( allowedCourses[i] == "41" || allowedCourses[i] == "42" ) { Debug.Log( "ID Curso: " + allowedCourses[i] ); yield return StartCoroutine( apiMenu.listClasses( allowedCourses[i], "99" ) ); } else { if (i >= allowedClasses.Count) { Debug.LogError( "Não existe allowedClasses para o índice " + i ); continue; } yield return StartCoroutine( apiMenu.listClasses( allowedCourses[i], allowedClasses[i].ToString() ) ); } apiMenu.FinishedLoading(); } classInfos.Clear(); QuantidadeAulas.Clear(); for ( int courseIndex = 0; courseIndex < allowedCourses.Count; courseIndex++ ) { GlobalClasses.ClassInfo courseClass = new GlobalClasses.ClassInfo(); courseClass.idCurso = allowedCourses[courseIndex]; courseClass.nomeCurso = courseIndex < coursesName.Count ? coursesName[courseIndex] : string.Empty; courseClass.infos = new List(); int totalClasses = 0; if ( classes != null && courseIndex < classes.Count && classes[courseIndex] != null && classes[courseIndex].response != null ) { totalClasses = classes[courseIndex].response.Count; } for ( int classIndex = 0; classIndex < totalClasses; classIndex++ ) { var response = classes[courseIndex] .response[classIndex]; if ( courseClass.idCurso != response.idcourses.ToString() ) { continue; } GlobalClasses.ClassDetailedInfo info = new GlobalClasses.ClassDetailedInfo(); info.description = response.description; info.disabled = response.disabled; info.idclasses = response.idclasses; info.idcourses = response.idcourses; info.name = response.name; info.ordem = response.ordem; info.viewed = response.viewed; info.sprites = response.sprites; info.demo = response.demo; courseClass.infos.Add(info); } classInfos.Add(courseClass); QuantidadeAulas.Add( courseClass.infos.Count ); yield return new WaitForEndOfFrame(); } classes = null; if (classInfos.Count > 0) { Debug.Log( classInfos[ classInfos.Count - 1 ].idCurso ); } yield return new WaitForEndOfFrame(); if (allowedCourses.Count == 0) { if (nomeCurso != null) { nomeCurso.text = "No courses available!"; } foreach ( GameObject item in classPrefabObjs ) { if (item != null) { Destroy(item); } } classPrefabObjs.Clear(); } else { if ( classInfos.Count == 0 || QuantidadeAulas.Count == 0 ) { Debug.LogError( "Nenhuma informação de curso foi carregada." ); yield break; } StartCoroutine( PreencherCursos(0) ); if (nomeCurso != null) { nomeCurso.text = classInfos[0].nomeCurso; } actualCourse = 0; coursesName = null; scrollObj = GameObject.Find("Scroll"); } yield return new WaitForEndOfFrame(); if (allowedCourses.Count > 1) { if (objleft != null) { objleft.SetActive(true); } if (objRight != null) { objRight.SetActive(true); } } yield return new WaitForEndOfFrame(); if (canvasAnime != null) { canvasAnime.SetBool( "start", true ); } if ( classInfos.Count > 0 && classInfos[0].infos != null ) { Debug.Log( classInfos[0].infos.Count ); } } private IEnumerator waitToClickAgain() { canChange = false; yield return new WaitForSecondsRealtime( 0.1f ); canChange = true; } public void EnterClass( GlobalClasses.ClassDetailedInfo ClassTogo) { if (ClassTogo == null) { Debug.LogError( "ClassTogo está nulo." ); return; } PlayerPrefs.SetString( "NomeAula", ClassTogo.name ); PlayerPrefs.SetString( "Desc", ClassTogo.description ); PlayerPrefs.SetInt( "idClass", ClassTogo.idclasses ); SceneManager.LoadSceneAsync( 1, LoadSceneMode.Single ); } private IEnumerator ExcludeExistingVideos( int idClass) { string path = Application.persistentDataPath + "/"; DirectoryInfo directory = new DirectoryInfo(path); if (directory.Exists) { FileInfo[] files = directory.GetFiles("*.*"); foreach (FileInfo file in files) { try { File.Delete(file.FullName); } catch (System.Exception exception) { Debug.LogWarning( "Não foi possível apagar '" + file.FullName + "': " + exception.Message ); } } } yield return new WaitForEndOfFrame(); PlayerPrefs.SetInt( "idClass", idClass ); SceneManager.LoadSceneAsync( 1, LoadSceneMode.Single ); } public void Scroll() { if (!canChange || canvasAnime == null) { return; } bool active = canvasAnime.GetBool( "downScroll" ); canvasAnime.SetBool( "downScroll", !active ); StartCoroutine( waitToClickAgain() ); } public void ChangeCourse(int add) { StartCoroutine( changeCourse(add) ); } private IEnumerator changeCourse( int add) { if (changingCourse) { yield break; } if ( classInfos == null || classInfos.Count == 0 ) { yield break; } changingCourse = true; int newcourse = actualCourse + add; if ( newcourse <= classInfos.Count - 1 && newcourse >= 0 ) { if (nomeCurso != null) { nomeCurso.text = classInfos[newcourse].nomeCurso; } } else { if (newcourse < 0) { newcourse = classInfos.Count - 1; } else { newcourse = 0; } if (nomeCurso != null) { nomeCurso.text = classInfos[newcourse].nomeCurso; } } yield return new WaitForEndOfFrame(); foreach ( GameObject item in classPrefabObjs ) { if (item != null) { Destroy(item); } } classPrefabObjs.Clear(); if (routineLoading != null) { StopCoroutine(routineLoading); routineLoading = null; } routineLoading = StartCoroutine( PreencherCursos(newcourse) ); yield return routineLoading; yield return new WaitForEndOfFrame(); actualCourse = newcourse; changingCourse = false; } public IEnumerator PreencherCursos( int courseIndex) { if ( classInfos == null || QuantidadeAulas == null || courseIndex < 0 || courseIndex >= classInfos.Count || courseIndex >= QuantidadeAulas.Count ) { Debug.LogError( "Índice de curso inválido: " + courseIndex ); routineLoading = null; yield break; } if (prefabClass == null) { Debug.LogError( "prefabClass não foi configurado." ); routineLoading = null; yield break; } if (alligner == null) { Debug.LogError( "alligner está nulo." ); routineLoading = null; yield break; } int quantity = QuantidadeAulas[courseIndex] < 20 ? 20 : QuantidadeAulas[courseIndex]; for ( int i = 0; i < quantity; i++ ) { GameObject createdObject = Instantiate( prefabClass, alligner ); classPrefabObjs.Add( createdObject ); ClassControl classControl = createdObject .GetComponent(); if (classControl != null) { classControl.SetInformation( classInfos[courseIndex], i ); } else { Debug.LogError( "O prefab de aula não possui ClassControl.", createdObject ); } } routineLoading = null; yield return new WaitForEndOfFrame(); } public void ExitApplication() { Debug.Log("Sair"); Application.Quit(); } }