Scriptum/Assets/Scripts/REFACTORING/Application/Shared/Manager/UI/UIBaseManager.cs

248 lines
7.7 KiB
C#
Raw Normal View History

2022-10-22 18:04:21 +02:00
using System;
using System.Collections.Generic;
using UnityEngine;
2022-12-29 00:56:54 +01:00
using System.Threading.Tasks;
2022-10-22 18:04:21 +02:00
[Serializable]
public abstract class UIBaseManager<T> : MonoBehaviour, ManagerInterface<T>
{
2022-12-29 00:56:54 +01:00
private Animator animator;
void Start()
{
animator = GetComponent<Animator>();
}
public static UIBaseManager<T> Instance { get; protected set; }
2022-10-22 18:04:21 +02:00
[SerializeField] public GameObject DynamicPanel;
2022-10-22 18:04:21 +02:00
[SerializeField] public KeyCode keyToOpen;
/// <summary>
/// Managed objects list - most important element in module
/// </summary>
[SerializeField] protected List<T> Elements = new List<T>(); // { get; set; } - this block inspector
2022-12-21 12:19:57 +01:00
public virtual void Awake()
2022-10-22 18:04:21 +02:00
{
if (Instance == null)
{
Instance = this;
}
else
2022-10-22 18:04:21 +02:00
{
Debug.Log(gameObject);
Debug.Log(Instance);
2022-10-22 18:04:21 +02:00
Destroy(gameObject);
}
}
2022-12-29 00:56:54 +01:00
//public GameObject Chest1 = GameObject.Find("Gold Chest");
2022-10-22 18:04:21 +02:00
2022-12-29 00:56:54 +01:00
public async void Update()
2022-10-22 18:04:21 +02:00
{
if (Input.GetKeyDown(keyToOpen))
{
if (!DynamicPanel)
2022-12-29 00:56:54 +01:00
{
//condition to be near to the chest
if (ChestUIManager.Instance.CurrentChestName != null && ChestUIManager.Instance.CurrentChestName != "")
{
//delay using async Yield and triggering chests to start open animation
var end = Time.time + 0.3;
animateOpening();
while (Time.time < end)
{
await System.Threading.Tasks.Task.Yield();
}
}
this.OpenPanel();
}
else
{
this.ClosePanel();
var end2 = Time.time + 0.1;
while (Time.time < end2)
{
await System.Threading.Tasks.Task.Yield();
}
animateClosing();
end2 = Time.time + 0.3;
while (Time.time < end2)
{
await System.Threading.Tasks.Task.Yield();
}
resettriggers();
}
}
if (ChestUIManager.Instance.CurrentChestName == null || ChestUIManager.Instance.CurrentChestName == "")
{
var end2 = Time.time + 0.1;
while (Time.time < end2)
2022-10-22 18:04:21 +02:00
{
2022-12-29 00:56:54 +01:00
await System.Threading.Tasks.Task.Yield();
2022-10-22 18:04:21 +02:00
}
2022-12-29 00:56:54 +01:00
animateClosing();
end2 = Time.time + 0.3;
while (Time.time < end2)
2022-10-22 18:04:21 +02:00
{
2022-12-29 00:56:54 +01:00
await System.Threading.Tasks.Task.Yield();
2022-10-22 18:04:21 +02:00
}
2022-12-29 00:56:54 +01:00
resettriggers();
2022-10-22 18:04:21 +02:00
}
}
/// <summary>
/// Function to fetch currently opened panel asociated with this UI manager
/// </summary>
/// <param name="panelConrtoller"></param>
public void SetPanelController(GameObject panelConrtoller)
{
// ToDo change fetched types
//DynamicPanel = dynamicPanelController;
}
2022-12-29 00:56:54 +01:00
private async void animateOpening()
{
2023-01-02 00:05:47 +01:00
/* GameObject.Find("Gold Chest").GetComponent<Animator>().SetTrigger("OpenIt");
2022-12-29 00:56:54 +01:00
GameObject.Find("Prize Chest").GetComponent<Animator>().SetTrigger("OpenIt");
2023-01-02 00:05:47 +01:00
GameObject.Find("Wodden Chest").GetComponent<Animator>().SetTrigger("OpenIt");*/
2022-12-29 00:56:54 +01:00
}
private async void animateClosing()
{
2023-01-02 00:05:47 +01:00
/* GameObject.Find("Gold Chest").GetComponent<Animator>().ResetTrigger("OpenIt");
2022-12-29 00:56:54 +01:00
GameObject.Find("Prize Chest").GetComponent<Animator>().ResetTrigger("OpenIt");
GameObject.Find("Wodden Chest").GetComponent<Animator>().ResetTrigger("OpenIt");
GameObject.Find("Gold Chest").GetComponent<Animator>().SetTrigger("CloseIt");
GameObject.Find("Prize Chest").GetComponent<Animator>().SetTrigger("CloseIt");
2023-01-02 00:05:47 +01:00
GameObject.Find("Wodden Chest").GetComponent<Animator>().SetTrigger("CloseIt");*/
2022-12-29 00:56:54 +01:00
}
private async void resettriggers()
{
var end3 = Time.time + 0.0;
while (Time.time < end3)
{
await System.Threading.Tasks.Task.Yield();
}
2023-01-02 00:05:47 +01:00
/* GameObject.Find("Gold Chest").GetComponent<Animator>().ResetTrigger("CloseIt");
2022-12-29 00:56:54 +01:00
GameObject.Find("Prize Chest").GetComponent<Animator>().ResetTrigger("CloseIt");
GameObject.Find("Wodden Chest").GetComponent<Animator>().ResetTrigger("CloseIt");
GameObject.Find("Wodden Chest").GetComponent<Animator>().SetTrigger("reactivate");
GameObject.Find("Prize Chest").GetComponent<Animator>().SetTrigger("reactivate");
2023-01-02 00:05:47 +01:00
GameObject.Find("Gold Chest").GetComponent<Animator>().SetTrigger("reactivate");*/
2022-12-29 00:56:54 +01:00
}
2022-10-22 18:04:21 +02:00
public virtual bool OpenPanel()
2022-10-22 18:04:21 +02:00
{
Console.WriteLine("Panel opened");
GameObject globalGUI = GameObject.FindGameObjectWithTag("GUI");
2022-10-22 18:04:21 +02:00
if(!globalGUI)
throw new Exception("Panel could not be opened - can't find global GUI object!!");
2022-10-22 18:04:21 +02:00
GameObject uiPanelTemplate = GetTemplatePanel();
DynamicPanel = GameObject.Instantiate(uiPanelTemplate, uiPanelTemplate.transform.position, Quaternion.identity, globalGUI.transform); // 4'th arg allow set object as child
2022-10-22 18:04:21 +02:00
DynamicPanel.transform.localPosition = uiPanelTemplate.transform.position; // prevent overwritten position by... environment???
2022-10-22 18:04:21 +02:00
DynamicPanel.name = uiPanelTemplate.name;
2022-10-22 18:04:21 +02:00
SetupPanel();
return true;
2022-10-22 18:04:21 +02:00
}
public virtual bool ClosePanel()
2022-10-22 18:04:21 +02:00
{
Console.WriteLine("Panel closed");
try
{
Destroy(DynamicPanel);
return true;
} catch (Exception ex) { Debug.LogError(ex.Message); }
return true;
}
public virtual void SetupPanel()
{
if(!DynamicPanel)
throw new Exception("Panel not found - UIBaseManager don't have UI Panel instance!!");
// bind this class instance
//DynamicPanel.GetComponent<TaskPanelController>().Bind(this);
}
/// <summary>
/// Function which return panel status
/// True - if panel is opened and it's instance is handled
/// false - if panel is closed and it's instance is not handled
/// </summary>
/// <returns></returns>
public bool GetPanelStatus()
{
return DynamicPanel != null;
}
public List<T> GetList()
{
return Elements;
}
/// <summary>
/// Function to setting list of elements - invoked once at the beginning
/// Reset current content
/// </summary>
/// <param name="list"></param>
/// <exception cref="NotImplementedException"></exception>
public void SetList(List<T> list)
{
Elements = list;
if(DynamicPanel != null) // is null = is close - when we firstly init list content before open panel in SceneTaskDataManager file
UpdateList();
}
/// <summary>
/// Set of functions to synchronize local list with list from SystemManager
/// </summary>
public void Add(T model)
{
// throw new System.NotImplementedException();
Elements.Add(model);
UpdateList();
// add to local list
// and rebuild view - sync lists (this.UpdateList() ;) )
}
public abstract void UpdateList();
public void Remove()
{
UpdateList();
throw new System.NotImplementedException();
// remove from local list
// and rebuild view - sync lists (this.UpdateList() ;) )
}
/// <summary>
/// Function to find saved template of created panel instance
/// </summary>
/// <returns></returns>
protected abstract GameObject GetTemplatePanel();
}