Files
Studio/Assets/TMPFolder/Modal/UIModal.cs
2025-05-21 17:54:01 +09:00

227 lines
7.6 KiB
C#

using Studio.UVC.UI;
using System;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Studio.UI.Modal
{
public class UIModalEventArgs : EventArgs
{
public readonly object Data;
public UIModalEventArgs(object data)
{
Data = data;
}
}
[RequireComponent(typeof(CanvasGroup))]
public class UIModal : UnityEngine.MonoBehaviour
{
public static readonly string PrefabPath = "Prefabs/Modal/UIModal";
protected string title = "";
protected UIModalContentHolder contentHolder;
protected IUIModalContent content;
protected UVCButton UIButtonOk;
protected UVCButton UIButtonCancel;
protected UVCButton UIButtonClose;
protected TextMeshProUGUI titleText;
protected CanvasGroup canvasGroup;
protected bool animatting = false;
protected float target = 0;
protected float duration = 0.25f;
protected float alpha = 1;
protected bool useKeyboard = false;
public EventHandler<UIModalEventArgs> OnOk { get; set; }
public EventHandler<UIModalEventArgs> OnClose { get; set; }
public virtual void Init(string title, string contentPrefabPath, object contentData, string okButtonText = "저장하기", string cancelButtonText="취소")
{
this.title = title;
canvasGroup = GetComponent<CanvasGroup>();
var btns = GetComponentsInChildren<UVCButton>();
UIButtonOk = btns.FirstOrDefault<UVCButton>(x => x.name.Equals(nameof(UIButtonOk),StringComparison.OrdinalIgnoreCase));
UIButtonClose = btns.FirstOrDefault<UVCButton>(x => x.name.Equals(nameof(UIButtonClose), StringComparison.OrdinalIgnoreCase));
UIButtonCancel = btns.FirstOrDefault<UVCButton>(x => x.name.Equals(nameof(UIButtonCancel), StringComparison.OrdinalIgnoreCase));
var txts = GetComponentsInChildren<TextMeshProUGUI>();
contentHolder = GetComponentInChildren<UIModalContentHolder>();
content = CreateConent(contentPrefabPath, contentHolder.transform);
content.SetInfo(contentData);
RectTransform rt = transform.Find("Modal").GetComponent<RectTransform>();
rt.sizeDelta = content.GetSize() + new Vector2(0, 48 + 96);//48은 타이틀 높이, 96은 버튼 높이
if (UIButtonOk != null)
{
UIButtonOk.Init();
UIButtonOk.SetText(okButtonText);
UIButtonOk.onClickButton.AddListener(() =>
{
if (content != null)
{
if (content.Ok())
{
OnOk?.Invoke(this, new UIModalEventArgs(content.GetOkResult()));
Close();
}
}
});
}
if (UIButtonCancel != null)
{
UIButtonCancel.SetText(cancelButtonText);
UIButtonCancel.onClickButton.AddListener(() =>
{
if (content != null && content.Close())
{
OnClose?.Invoke(this, new UIModalEventArgs(content.GetCloseResult()));
Close();
}
});
}
if (UIButtonClose != null)
{
UIButtonClose.onClickButton.AddListener(() =>
{
if (content != null && content.Close())
{
OnClose?.Invoke(this, new UIModalEventArgs(content.GetCloseResult()));
Close();
}
});
}
if (txts != null)
{
titleText = txts.FirstOrDefault(x=>x.name.Equals(nameof(titleText),StringComparison.OrdinalIgnoreCase));
titleText.text = this.title;
}
canvasGroup.alpha = 0;
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
}
protected virtual IUIModalContent CreateConent(string contentPrefabPath, Transform parent)
{
GameObject prefab = Resources.Load(contentPrefabPath, typeof(GameObject)) as GameObject;
GameObject go = UnityEngine.Object.Instantiate(prefab);
IUIModalContent content = go.GetComponent<IUIModalContent>();
go.transform.SetParent(parent, false);
return content;
}
public void Open()
{
canvasGroup.interactable = true;
canvasGroup.blocksRaycasts = true;
target = 1;
alpha = canvasGroup.alpha;
animatting = true;
}
public void Close()
{
target = 0;
alpha = canvasGroup.alpha;
animatting = true;
}
void Update()
{
if (animatting)
{
if (duration < 0)
{
duration = 0.001f;
}
alpha = Mathf.MoveTowards(alpha, target, (1 / duration) * Time.deltaTime);
canvasGroup.alpha = alpha;
if (alpha == target)
{
if (target == 0)
{
Destroy(gameObject);
}
animatting = false;
}
}
else
{
if (alpha == 1 && target == 1 && useKeyboard)
{
if (Input.GetKeyDown(KeyCode.Escape))
{
UIButtonClose.UVCClickButton.OnPointerDown(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
}
else if (Input.GetKeyUp(KeyCode.Escape))
{
if (content != null && content.Close())
{
UIButtonClose.UVCClickButton.OnPointerUp(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
OnClose?.Invoke(this, new UIModalEventArgs(content.GetCloseResult()));
Close();
}
}
else if (Input.GetKeyDown(KeyCode.Space))
{
UIButtonOk.UVCClickButton.OnPointerDown(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
}
else if (Input.GetKeyUp(KeyCode.Space))
{
UIButtonOk.UVCClickButton.OnPointerUp(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
if (content != null && content.Ok())
{
OnOk?.Invoke(this, new UIModalEventArgs(content.GetOkResult()));
Close();
}
}
}
}
}
protected virtual void OnDestroy()
{
if (UIButtonOk != null)
{
UIButtonOk.onClickButton.RemoveAllListeners();
}
if (UIButtonCancel != null)
{
UIButtonCancel.onClickButton.RemoveAllListeners();
}
if (UIButtonClose != null)
{
UIButtonClose.onClickButton.RemoveAllListeners();
}
OnOk = null;
OnClose = null;
}
}
}