ScreenManager.cs 8.55 KB
using UnityEngine;
using UnityEngine.UI;
using LAViD.VLibras.UI;

public class ScreenManager : MonoBehaviour {

	public static string LOCK_ID = "ScreenManager";

	public GenericPlayerManager playerManager;
	public RandomAnimations randomAnimations;

	public GameObject infoScreen;
	public GameObject translateScreen;
	public GameObject dictScreen;
	public GameObject tutorialScreen;
	public GameObject makersScreen;

	public SwitchImageAttribute textButtonImage;
	public SwitchImageAttribute pauseButtonImage;
	public SwitchImageAttribute dictButtonImage;
	public SwitchImageAttribute infoButtonImage;

	public GameObject loadingSnippet;
	public GameObject connectionErrorDialog;
	public Text connectionErrorText;
	public GameObject pauseMenu;
	public GameObject repeatLayer;
    public GameObject exportContainer;
    public GameObject exportLayer;
    public GameObject shareLayer;
    public GameObject progressLayer;
    public GameObject downloadLayer;
    public GameObject downloadProgressLayer;
    public GameObject closeLayer;
    private bool onLockExport = false;
  
    

	public GameObject textButton;
	public GameObject pauseButton;

	public BoxCollider avatarCollider;

	public RegionSelector regionSelector;
	public SlidingHidder settingsPanel;
	public Fadder regionPanel;
	public Fadder shadow;

  

	private bool exit = false;

	public void Update()
	{
		if (Input.GetKeyDown(KeyCode.Escape)) 
		{
			if (infoScreen.activeSelf)
			{
				infoScreen.SetActive(false);
				infoButtonImage.switchAttribute(false);
			}
			else if (translateScreen.activeSelf)
			{
				translateScreen.SetActive(false);
				textButtonImage.switchAttribute(false);
			}
			else if (dictScreen.activeSelf)
			{
				dictScreen.SetActive(false);
				dictButtonImage.switchAttribute(false);
			}
			else if (tutorialScreen.activeSelf)
			{
				tutorialScreen.SetActive(false);
			}
			else if (tutorialScreen.activeSelf)
			{
				makersScreen.SetActive(false);
			}
			else if (regionPanel.isVisible())
			{
				closeRegionPanel(true);
			}
			else if (settingsPanel.isVisible())
			{
				closeSettingsPanel();
			}
			else
			{
				if (exit) Application.Quit();

				exit = true;
			}
		}
	}

	public bool hasActiveScreen()
	{
		return 		this.infoScreen.activeSelf
				||	this.translateScreen.activeSelf
				||	this.dictScreen.activeSelf
				||	this.tutorialScreen.activeSelf
				||	this.makersScreen.activeSelf;
	}

	public void hideScreen()
	{
		setAvatarColliderState(true);
		randomAnimations.unlockFor(LOCK_ID);

		if (infoScreen.activeSelf)
		{
			infoScreen.SetActive(false);
			infoButtonImage.switchAttribute(false);
		}

		if (translateScreen.activeSelf)
		{
			translateScreen.SetActive(false);
			textButtonImage.switchAttribute(false);
		}

		if (dictScreen.activeSelf)
		{
			dictScreen.SetActive(false);
			dictButtonImage.switchAttribute(false);
		}

		if (tutorialScreen.activeSelf)
		{
			tutorialScreen.SetActive(false);
		}

		if (makersScreen.activeSelf)
		{
			makersScreen.SetActive(false);
		}
	}

	private void pause()
	{
		playerManager.setPauseState(true);
		setPauseMenuState(true);
	}

	public void switchScreen(GameObject screen)
	{
		bool active = screen.activeSelf;

		hideScreen();

		if (active) return;

		screen.SetActive(true);

		if (playerManager.isPlayingIntervalAnimation())
			playerManager.stopAll();

		else if (playerManager.isPlaying())
			pause();

		setAvatarColliderState(false);
		randomAnimations.lockFor(LOCK_ID);

		if (screen == infoScreen)
			infoButtonImage.switchAttribute(true);

		if (screen == translateScreen)
			textButtonImage.switchAttribute(true);

		if (screen == dictScreen)
			dictButtonImage.switchAttribute(true);

		exit = false;
	}

	public void switchScreen(string name) {
		switchScreen(getScreenByName(name));
	}

	public GameObject getScreenByName(string name)
	{
		switch (name)
		{
			case "translate": return this.translateScreen;
			case "dict": return this.dictScreen;
			case "info": return this.infoScreen;
			case "tutorial": return this.tutorialScreen;
			case "makers": return this.makersScreen;
		}

		return null;
	}

	public void openSettingsPanel()
	{
		settingsPanel.Animate(true);
		setPanelOpen(true);

		if (playerManager.isPlaying())
			pause();
	}

	public void openRegionPanel()
	{
		settingsPanel.Animate(false);
		regionPanel.Animate(true);
		setPanelOpen(true);
	}

	public void openInfoScreen()
	{
		closeSettingsPanel();
		switchScreen(infoScreen);
	}

	public void closeSettingsPanel()
	{
		settingsPanel.Animate(false);
		setPanelOpen(false);
	}

	public void closeRegionPanel(bool restoreActiveItem)
	{
		regionPanel.Animate(false);
		setPanelOpen(false);

		if (restoreActiveItem)
			regionSelector.ReselectActiveItem();
	}

	public void onPanelOutClick()
	{
		if (regionPanel.isVisible())
			closeRegionPanel(true);

		else if (settingsPanel.isVisible())
			closeSettingsPanel();

		else setPanelOpen(false);
	}

	private void setPanelOpen(bool open)
	{
		shadow.Animate(open);
		setAvatarColliderState( ! open);
	}

	public void setPauseMenuState(bool active)
	{
		this.pauseMenu.SetActive(active);
		setAvatarColliderState( ! active);
	}

	public void setLoadingSnippetState(bool active)
	{
		this.loadingSnippet.SetActive(active);

		if (active && this.pauseMenu.activeSelf)
			this.pauseMenu.SetActive(false);
	}

	public void showConnectionErrorDialog()
	{
		this.connectionErrorDialog.SetActive(true);
	}

	public void showConnectionErrorDialog(PlayerManager.ERROR_STATUS_MESSAGE msg)//int error_code
	{
		this.connectionErrorDialog.SetActive(true);
		this.connectionErrorText.text = PlayerManager.get_connection_status_message(msg);
	}

	public void setAvatarColliderState(bool active) {
		this.avatarCollider.enabled = active;
	}

	public void setRepeatLayerState(bool active) {
		this.repeatLayer.SetActive(active);
	}

    public void setExportContainerState(bool active)
    {
        this.exportContainer.SetActive(active);
    }

    public void setExportLayerState(bool active)
        
    {
        
        this.exportLayer.SetActive(active);
    }

    public void setProgressLayerState(bool active)
    {
        
        this.progressLayer.SetActive(active);
    }

    public void setDownloadLayerState(bool active)

    {
        
        this.downloadLayer.SetActive(active);
    }

    public void setDownloadProgressLayerState(bool active)
    {
        this.downloadProgressLayer.SetActive(active);
        this.closeLayer.SetActive(active);
    }

    public void setShareLayerState(bool active)
    {

        this.shareLayer.SetActive(active);
    }

    public void setTranslateButtonActive(bool active)
	{
		this.textButton.SetActive(active);
		this.pauseButton.SetActive( ! active);
	}

    
	public void changeStates(bool playing, bool paused, bool repeatable)
	{
		setTranslateButtonActive( ! playing);
		setPauseMenuState(playing && paused);
		setRepeatLayerState( ! playing && repeatable);
        if(!onLockExport)
            changeExportStates(ExportLayers.ExportLayer.Export_Layer, !playing && repeatable);
        

		this.pauseButtonImage.switchAttribute(playing && paused);
	}

    public void changeExportStates(ExportLayers.ExportLayer layers, bool show_Layer)
    {
        switch (layers)
        {
            case ExportLayers.ExportLayer.Export_Layer:
                onLockExport = false;
                setExportContainerState(show_Layer);
                setExportLayerState(show_Layer);
                break;
            case ExportLayers.ExportLayer.Progress_Layer:
                onLockExport = true;
                setExportContainerState(show_Layer);
                setProgressLayerState(show_Layer);
                break;
            case ExportLayers.ExportLayer.Download_Layer:
                onLockExport = true;
                setExportContainerState(show_Layer);
                setDownloadLayerState(show_Layer);
                break;
            case ExportLayers.ExportLayer.Progress_Download_Layer:
                onLockExport = true;
                setExportContainerState(show_Layer);
                setDownloadProgressLayerState(show_Layer);

                break;
            case ExportLayers.ExportLayer.Share_Layer:
                onLockExport = true; 
                setExportContainerState(show_Layer);
                setShareLayerState(show_Layer);
                break;
            default:
                onLockExport = false;
                setExportContainerState(false);
                setExportLayerState(false);
                setProgressLayerState(false);
                setDownloadLayerState(false);
                setShareLayerState(false);
                break;              
                

            
        }
        
        

    }

}