ZoeRobotIngles/Assets/Scripts/ControllerMenu/OpenAp.cs

592 lines
17 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class OpenAp : MonoBehaviour
{
public static bool cameraDonDestroy;
Text txt;
Animator controlAnimator;
Image ImgWifi;
Sprite sprWithWifi, sprNoWifi;
Text hour;
RectTransform mask1, mask1child, mask2, mask2child;
public GameObject logInOut;
public static float volumeSystem;
private Sprite spriteNoSong, spriteWithSong;
private Image ImageSong;
public Color afterColor;
//private int nbrIn = 1;
private Vector2 mousedown, mouseUp; private bool changed = false;
public bool aluno = false;
public Sprite batEmptycharg, batEmptyNot, batFullCharg, batFullNot;
public Image imgFullBattery, imgLowBattery, imgBattery;
public Color corBaixaBateria;
string sceneLoading;
public static bool faceOpened;
public GameObject particles;
private void Awake()
{
if (!GameObject.Find("MainMenu Camera"))
{
GameObject createCamera = Instantiate(Resources.Load<GameObject>("MainMenu Camera"));
createCamera.name = "MainMenu Camera";
DontDestroyOnLoad(createCamera);
cameraDonDestroy = true;
}
else
{
cameraDonDestroy = false;
}
StartCoroutine(AdjustMenu());
if (PlayerPrefs.GetInt("aluno") == 1 || GameObject.Find("Canvas - Login") == null)
{
GameObject.Find("Canvas").GetComponent<SupportAnimeCall>().EndAnimeLoad();
Transform trBackground = GameObject.Find("Canvas - Background").transform.Find("All").Find("Background");
Destroy(trBackground.Find("Black").gameObject);
Destroy(trBackground.Find("Stars1").gameObject);
Destroy(trBackground.Find("Stars4").gameObject);
}
}
IEnumerator AdjustMenu()
{
Canvas canvasMenu = GameObject.Find("Canvas - Background").GetComponent<Canvas>();
canvasMenu.renderMode = RenderMode.ScreenSpaceCamera;
canvasMenu.worldCamera = GameObject.Find("MainMenu Camera").GetComponent<Camera>();
while (canvasMenu.transform.childCount == 0)
{
yield return new WaitForEndOfFrame();
}
RectTransform rectAll = canvasMenu.transform.Find("All").GetComponent<RectTransform>();
rectAll.localScale = new Vector3(1, 1, 1);
rectAll.anchorMax = new Vector2(1, 1);
rectAll.anchorMin = new Vector2(0, 0);
rectAll.anchoredPosition = new Vector2(1, 1);
//particles = Instantiate(Resources.Load<GameObject>("MainMenu/ParticlesStars"), GameObject.Find("Controller").transform);
}
// Start is called before the first frame update
void Start()
{
Screen.sleepTimeout = SleepTimeout.SystemSetting;
controlAnimator = GameObject.Find("Canvas").GetComponent<Animator>();
txt = GameObject.Find("Status").GetComponent<Text>();
logInOut = GameObject.Find("LogOutScreen");
StartCoroutine(secondAwake());
}
void DontMakeOpening() {
controlAnimator.SetTrigger("DontMakeOp");
GameObject opObjects = GameObject.Find("FirstBackGround");
Destroy(opObjects);
Mask mask = GameObject.Find("All").GetComponent<Mask>();
mask.enabled = false;
}
#region inicialization
IEnumerator checkFace() {
InputField inputMat = GameObject.Find("InputUsu").GetComponent<InputField>();
inputMat.interactable = false;
TakePhoto takePhoto = GameObject.Find("PicTake").GetComponent<TakePhoto>();
while (true)
{
yield return StartCoroutine(takePhoto.takePhoto());
yield return new WaitForEndOfFrame();
if (takePhoto.response)
{
}
else
{
yield return new WaitForSeconds(.5f);
}
}
}
IEnumerator secondAwake()
{
GameObject all = GameObject.Find("All");
WriteFile writeFile = GetComponent<WriteFile>();
if (!aluno)
{
writeFile.checkFile();
}
GameObject zoesleep = GameObject.Find("ZoeSleep");
if (zoesleep != null) { zoesleep.SetActive(false); }
if (aluno)
{
differencesForStudentRobot();
}
logInOut.SetActive(false);
controlAnimator.SetBool("Open", true);
//yield return new WaitForSecondsRealtime(8f);
ImgWifi = GameObject.Find("WifiLogo").GetComponent<Image>();
sprWithWifi = Resources.Load<Sprite>("Sprites/WithWifi");
sprNoWifi = Resources.Load<Sprite>("Sprites/NoWifi");
StartCoroutine(CheckInternetConnection());
yield return new WaitForEndOfFrame();
StartCoroutine(checkHour());
yield return new WaitForEndOfFrame();
StartCoroutine(checkBattery());
StartCoroutine(checkCharging());
yield return new WaitForEndOfFrame();
spriteNoSong = Resources.Load<Sprite>("Sprites/SemSom");
spriteWithSong = Resources.Load<Sprite>("Sprites/Som");
ImageSong = GameObject.Find("Sound").GetComponent<Image>();
GameObject takePic = GameObject.Find("TakePic");
Destroy(takePic);
string cena = PlayerPrefs.GetString("Cena");
if (!string.IsNullOrEmpty(cena))
{
PlayerPrefs.SetString("Cena", "");
if (cena == "conversation")
{
ChangeToConversation();
}
}
yield break;
}
void differencesForStudentRobot() {
//List<string> itensToDestroy = new List<string>();
controlAnimator.SetLayerWeight(4, 0);
string[] itensToDestroy = { "MakeSound", "LogOut" };
foreach (string item in itensToDestroy)
{
GameObject obj = GameObject.Find(item);
obj.SetActive(false);
}
ChangePositionNNameObj("Wifi (1)", "SettingsAluno", new Vector2(-165, 0));
ChangePositionNNameObj("Button OpenApp", "EnterCv", new Vector2(+165, 0));
ChangePositionNNameObj("Status", "settingsText", new Vector2(-165, -135));
GameObject oldstatus = GameObject.Find("settingsText");
GameObject newtext = Instantiate(oldstatus, oldstatus.transform.parent);
ChangePositionNNameObj(newtext.name, "cvText", new Vector2(+165, -135));
Text cvText = newtext.GetComponent<Text>();
cvText.text = "Conversation";
}
void ChangePositionNNameObj(string objName, string newname, Vector2 pos) {
GameObject obj = GameObject.Find(objName);
obj.name = newname;
controlAnimator.Rebind();
obj.transform.localPosition = pos;
obj.transform.localScale = new Vector2(1, 1);
}
public void ShowUpdateScreen() {
GameObject updateScreen = Instantiate(Resources.Load<GameObject>("UpdateScreen/UpdateScreen"));
updateScreen.transform.parent = GameObject.Find("Canvas").GetComponent<Transform>();
updateScreen.transform.localScale = new Vector3(1, 1, 1);
RectTransform rect = updateScreen.GetComponent<RectTransform>();
rect.anchoredPosition = new Vector2(0, 0);
rect.offsetMax = new Vector2(0, 0);
rect.offsetMin = new Vector2(0, 0);
//rect.anchorMin = new Vector2(0, 0);
}
IEnumerator CheckInternetConnection() {
if (Application.internetReachability == NetworkReachability.NotReachable)
{
ImgWifi.sprite = sprNoWifi;
controlAnimator.SetBool("NoWifi", true);
yield return new WaitForSecondsRealtime(5);
controlAnimator.SetBool("NoWifi", false);
}
else
{
ImgWifi.sprite = sprWithWifi;
}
yield break;
}
IEnumerator checkHour() {
hour = GameObject.Find("Hour").GetComponent<Text>();
while (true)
{
string txt = DateTime.Now.ToShortTimeString();
hour.text = txt;
yield return new WaitForSecondsRealtime(60);
}
}
IEnumerator checkCharging() {
bool isLoad = true;
while (true)
{
if (SystemInfo.batteryStatus == BatteryStatus.Charging)
{
if (!isLoad)
{
imgFullBattery.sprite = batFullCharg;
imgLowBattery.sprite = batEmptycharg;
isLoad = true;
}
}
else
{
if (isLoad)
{
imgFullBattery.sprite = batFullNot;
imgLowBattery.sprite = batEmptyNot;
isLoad = false;
}
}
yield return new WaitForSecondsRealtime(1);
}
}
IEnumerator checkBattery() {
while (true)
{
float bat = SystemInfo.batteryLevel;
bat = bat * 100;
bat = bat / 4;
bat -= 25;
bat = -bat;
mask1 = GameObject.Find("MaskB").GetComponent<RectTransform>();
mask1child = GameObject.Find("ImgFullB").GetComponent<RectTransform>();
mask2 = GameObject.Find("MaskB2").GetComponent<RectTransform>();
mask2child = GameObject.Find("ImgEmptyB").GetComponent<RectTransform>();
mask1.anchoredPosition = new Vector2(0, -bat);
mask1child.anchoredPosition = new Vector2(0, bat);
//Debug.Log(bat);
float newbat = 25 - bat;
//Debug.Log(newbat);
mask2.anchoredPosition = new Vector2(0, newbat);
mask2child.anchoredPosition = new Vector2(0, -newbat);
if (SystemInfo.batteryLevel < 0.15f)
{
imgBattery.color = corBaixaBateria;
imgLowBattery.color = corBaixaBateria;
imgFullBattery.color = corBaixaBateria;
}
else
{
imgBattery.color = Color.white;
imgFullBattery.color = Color.white;
imgLowBattery.color = Color.white;
}
yield return new WaitForSecondsRealtime(120);
}
}
#endregion
public void WakeUpZoe() {
//controlAnimator.SetBool("ZoeFace", true);
SetAnimeBool("ZoeFace");
StartCoroutine(waitToDestroyZoe());
}
IEnumerator waitToDestroyZoe() {
yield return new WaitForSecondsRealtime(2);
GameObject obj = GameObject.Find("ZoeSleep");
obj.SetActive(false);
}
public void OpenSettings() {
ChangeToSettingsScene();
}
public void OpenGamification()
{
ChangeToGamification();
}
public void GoToEdit()
{
ChangeToEditScene();
}
void ChangeToEditScene()
{
AsyncOperation obj = SceneManager.LoadSceneAsync("Transition", LoadSceneMode.Additive);
obj.completed += (asyncOperation) =>
{
//Do stuff here with returned value asyncOperation
GameObject.Find("Canvas - Transition").GetComponent<Transition_Controller>().StartLoadingScene("EditZoe");
StartCoroutine(WaitToDeactivateScene());
};
}
void ChangeToSettingsScene() {
AsyncOperation obj = SceneManager.LoadSceneAsync("Transition", LoadSceneMode.Additive);
obj.completed += (asyncOperation) =>
{
//Do stuff here with returned value asyncOperation
GameObject.Find("Canvas - Transition").GetComponent<Transition_Controller>().StartLoadingScene("Config");
StartCoroutine(WaitToDeactivateScene());
};
}
void ChangeToGamification() {
AsyncOperation obj = SceneManager.LoadSceneAsync("Transition", LoadSceneMode.Additive);
obj.completed += (asyncOperation) =>
{
//Do stuff here with returned value asyncOperation
GameObject.Find("Canvas - Transition").GetComponent<Transition_Controller>().StartLoadingScene("Gaming");
StartCoroutine(WaitToDeactivateScene());
};
}
IEnumerator WaitToDeactivateScene()
{
yield return new WaitForSecondsRealtime(.6f);
SceneManager.UnloadSceneAsync("SampleScene", UnloadSceneOptions.UnloadAllEmbeddedSceneObjects);
}
public void OpenClass() {
StartCoroutine(openAppwait("com.myrobot.AppClass"));
}
public void VolumeChange(Slider volumeset) {
volumeSystem = volumeset.value;
if (volumeSystem == 0)
{
ImageSong.sprite = spriteNoSong;
}
else
{
ImageSong.sprite = spriteWithSong;
}
}
#region anime Global Functions
public void SetAnimeBool(string name) {
controlAnimator.SetBool(name, true);
}
public void SetAnimeBoolFalse(string name)
{
controlAnimator.SetBool(name, false);
}
public void SetAnimeBoolInverse(string name)
{
bool value = controlAnimator.GetBool(name);
controlAnimator.SetBool(name, !value);
}
#endregion
public void openApp()
{
ChangeToConversation();
}
public void ChangeToConversation() {
AsyncOperation obj = SceneManager.LoadSceneAsync("Transition", LoadSceneMode.Additive);
obj.completed += (asyncOperation) =>
{
//Do stuff here with returned value asyncOperation
GameObject.Find("Canvas - Transition").GetComponent<Transition_Controller>().StartLoadingScene("FaceConversation");
StartCoroutine(WaitToDeactivateScene());
};
}
IEnumerator openAppwait(string bundleidinsert)
{
yield return new WaitForSecondsRealtime(0.75f);
bool fail = false;
string bundleId = bundleidinsert;
string activityName = "com.unity3d.player.UnityPlayerActivity"; // Nome da Activity principal do Unity
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
try
{
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent");
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
AndroidJavaObject componentName = new AndroidJavaObject("android.content.ComponentName", bundleId, activityName);
intent.Call<AndroidJavaObject>("setComponent", componentName);
intent.Call<AndroidJavaObject>("addFlags", intentClass.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK"));
ca.Call("startActivity", intent);
Debug.Log("Abrindo app: " + bundleId + "/" + activityName);
}
catch (System.Exception e)
{
Debug.LogError("Erro ao abrir app: " + e.Message);
fail = true;
}
up.Dispose();
ca.Dispose();
}
IEnumerator openAppwait2(string bundleidinsert) {
yield return new WaitForSecondsRealtime(0.75f);
bool fail = false;
//string bundleId = "com.yongyida.robot.sdkdemo"; // your target bundle id
//string bundleId = "com.myrobot.myrobotclass";
string bundleId = bundleidinsert;
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject launchIntent = null;
try
{
launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", bundleId);
}
catch (System.Exception e)
{
fail = true;
}
if (fail)
{ //open app in store
//Application.OpenURL("https://google.com");
}
else
{ //open the app
ca.Call("startActivity", launchIntent);
}
up.Dispose();
ca.Dispose();
packageManager.Dispose();
launchIntent.Dispose();
}
public void CloseAplication() {
Application.Quit();
}
#region LogOut
public void ShowScreenLogOut() {
logInOut.SetActive(true);
}
public void HideScreenLogOut() {
logInOut.SetActive(false);
}
public void LogOutSession() {
GameObject all = GameObject.Find("All");
Image image = all.GetComponent<Image>();
Mask mask = all.GetComponent<Mask>();
image.enabled = true;
mask.enabled = true;
PlayerPrefs.SetInt("Logado", 0);
SetAnimeBool("logOut");
StartCoroutine(LogOutSessionCoroutine());
}
IEnumerator LogOutSessionCoroutine() {
yield return new WaitForSecondsRealtime(2.5f);
GameObject thisCamera = GameObject.Find("MainMenu Camera");
AsyncOperation loadScene = SceneManager.LoadSceneAsync("LogOut");
loadScene.completed += (asyncOperation) =>
{
Destroy(thisCamera);
cameraDonDestroy = false;
};
//SceneManager.UnloadSceneAsync("SampleScene");
}
#endregion
void OnApplicationPause(bool pauseStatus)
{
WriteFile write = GetComponent<WriteFile>();
if (!write.checkFile())
{
LogOutSession();
}
}
}