This commit is contained in:
SepComet
2026-02-26 13:21:43 +08:00
parent a118803f1a
commit 4daefa17d5
1259 changed files with 290021 additions and 1 deletions
@@ -0,0 +1,9 @@
namespace GeometryTD.UI
{
public interface IUIFormController
{
int? OpenUI(object userData = null);
void CloseUI();
void BindUseCase(IUIUseCase useCase);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 63bbd9f533c214c40b43e910c9e2e096
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,6 @@
namespace GeometryTD.UI
{
public interface IUIUseCase
{
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 29f3a280b77ccf746a4a219a991717c7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+250
View File
@@ -0,0 +1,250 @@
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using GeometryTD;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public abstract class UGuiForm : UIFormLogic
{
public const int DepthFactor = 100;
private const float FadeTime = 0.3f;
private static Font _mainFont = null;
private static TMP_FontAsset _mainTMPFont = null;
private Canvas _cachedCanvas = null;
private CanvasGroup _canvasGroup = null;
private List<Canvas> _cachedCanvasContainer = new List<Canvas>();
public int OriginalDepth { get; private set; }
public int Depth => _cachedCanvas.sortingOrder;
public void Close()
{
Close(false);
}
public void Close(bool ignoreFade)
{
StopAllCoroutines();
if (ignoreFade)
{
GameEntry.UI.CloseUIForm(this);
}
else
{
StartCoroutine(CloseCo(FadeTime));
}
}
public void PlayUISound(int uiSoundId)
{
GameEntry.Sound.PlayUISound(uiSoundId);
}
public static void SetMainFont(Font mainFont)
{
if (mainFont == null)
{
Log.Error("Main font is invalid.");
return;
}
_mainFont = mainFont;
}
public static void SetMainTMPFont(TMP_FontAsset mainTMPFont)
{
if (mainTMPFont == null)
{
Log.Error("Main font is invalid.");
return;
}
_mainTMPFont = mainTMPFont;
}
#if UNITY_2017_3_OR_NEWER
protected override void OnInit(object userData)
#else
protected internal override void OnInit(object userData)
#endif
{
base.OnInit(userData);
_cachedCanvas = gameObject.GetOrAddComponent<Canvas>();
_cachedCanvas.overrideSorting = true;
OriginalDepth = _cachedCanvas.sortingOrder;
_canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
RectTransform rect = GetComponent<RectTransform>();
rect.anchorMin = Vector2.zero;
rect.anchorMax = Vector2.one;
rect.anchoredPosition = Vector2.zero;
rect.sizeDelta = Vector2.zero;
gameObject.GetOrAddComponent<GraphicRaycaster>();
if (_mainTMPFont == null)
{
Log.Warning("Main TMP font isn't set.");
}
else
{
var tmp_texts = GetComponentsInChildren<TMP_Text>(true);
foreach (var text in tmp_texts)
{
text.font = _mainTMPFont;
if (!string.IsNullOrEmpty(text.text))
{
text.text = GameEntry.Localization.GetString(text.text);
}
}
}
if (_mainFont == null)
{
Log.Warning("Main font isn't set.");
}
else
{
var texts = GetComponentsInChildren<Text>(true);
foreach (var text in texts)
{
text.font = _mainFont;
if (!string.IsNullOrEmpty(text.text))
{
text.text = GameEntry.Localization.GetString(text.text);
}
}
}
}
#if UNITY_2017_3_OR_NEWER
protected override void OnRecycle()
#else
protected internal override void OnRecycle()
#endif
{
base.OnRecycle();
}
#if UNITY_2017_3_OR_NEWER
protected override void OnOpen(object userData)
#else
protected internal override void OnOpen(object userData)
#endif
{
base.OnOpen(userData);
_canvasGroup.alpha = 0f;
StopAllCoroutines();
StartCoroutine(_canvasGroup.FadeToAlpha(1f, FadeTime));
}
#if UNITY_2017_3_OR_NEWER
protected override void OnClose(bool isShutdown, object userData)
#else
protected internal override void OnClose(bool isShutdown, object userData)
#endif
{
base.OnClose(isShutdown, userData);
}
#if UNITY_2017_3_OR_NEWER
protected override void OnPause()
#else
protected internal override void OnPause()
#endif
{
base.OnPause();
}
#if UNITY_2017_3_OR_NEWER
protected override void OnResume()
#else
protected internal override void OnResume()
#endif
{
base.OnResume();
_canvasGroup.alpha = 0f;
StopAllCoroutines();
StartCoroutine(_canvasGroup.FadeToAlpha(1f, FadeTime));
}
#if UNITY_2017_3_OR_NEWER
protected override void OnCover()
#else
protected internal override void OnCover()
#endif
{
base.OnCover();
}
#if UNITY_2017_3_OR_NEWER
protected override void OnReveal()
#else
protected internal override void OnReveal()
#endif
{
base.OnReveal();
}
#if UNITY_2017_3_OR_NEWER
protected override void OnRefocus(object userData)
#else
protected internal override void OnRefocus(object userData)
#endif
{
base.OnRefocus(userData);
}
#if UNITY_2017_3_OR_NEWER
protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
#else
protected internal override void OnUpdate(float elapseSeconds, float realElapseSeconds)
#endif
{
base.OnUpdate(elapseSeconds, realElapseSeconds);
}
#if UNITY_2017_3_OR_NEWER
protected override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
#else
protected internal override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
#endif
{
int oldDepth = Depth;
base.OnDepthChanged(uiGroupDepth, depthInUIGroup);
int deltaDepth = UGuiGroupHelper.DepthFactor * uiGroupDepth + DepthFactor * depthInUIGroup - oldDepth +
OriginalDepth;
GetComponentsInChildren(true, _cachedCanvasContainer);
for (int i = 0; i < _cachedCanvasContainer.Count; i++)
{
_cachedCanvasContainer[i].sortingOrder += deltaDepth;
}
_cachedCanvasContainer.Clear();
}
private IEnumerator CloseCo(float duration)
{
yield return _canvasGroup.FadeToAlpha(0f, duration);
GameEntry.UI.CloseUIForm(this);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 0b92f84dc56dc764a9c261fe47df5520
timeCreated: 1528026161
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,53 @@
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
/// <summary>
/// uGUI 界面组辅助器。
/// </summary>
public class UGuiGroupHelper : UIGroupHelperBase
{
public const int DepthFactor = 10000;
private int m_Depth = 0;
private Canvas m_CachedCanvas = null;
/// <summary>
/// 设置界面组深度。
/// </summary>
/// <param name="depth">界面组深度。</param>
public override void SetDepth(int depth)
{
m_Depth = depth;
m_CachedCanvas.overrideSorting = true;
m_CachedCanvas.sortingOrder = DepthFactor * depth;
}
private void Awake()
{
m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
gameObject.GetOrAddComponent<GraphicRaycaster>();
}
private void Start()
{
m_CachedCanvas.overrideSorting = true;
m_CachedCanvas.sortingOrder = DepthFactor * m_Depth;
RectTransform transform = GetComponent<RectTransform>();
transform.anchorMin = Vector2.zero;
transform.anchorMax = Vector2.one;
transform.anchoredPosition = Vector2.zero;
transform.sizeDelta = Vector2.zero;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 05a35a4a1f7dd5646a70cb1a73348780
timeCreated: 1528026161
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,6 @@
namespace GeometryTD.UI
{
public class UIContext
{
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ad938323d1c8a364baace3c6d97b6431
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,182 @@
//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------
using GameFramework.DataTable;
using GameFramework.UI;
using System.Collections;
using GeometryTD.Procedure;
using GeometryTD;
using GeometryTD.CustomUtility;
using GeometryTD.DataTable;
using GeometryTD.Definition;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public static class UIExtension
{
public static IEnumerator FadeToAlpha(this CanvasGroup canvasGroup, float alpha, float duration)
{
float time = 0f;
float originalAlpha = canvasGroup.alpha;
while (time < duration)
{
time += Time.deltaTime;
canvasGroup.alpha = Mathf.Lerp(originalAlpha, alpha, time / duration);
yield return new WaitForEndOfFrame();
}
canvasGroup.alpha = alpha;
}
public static IEnumerator SmoothValue(this Slider slider, float value, float duration)
{
float time = 0f;
float originalValue = slider.value;
while (time < duration)
{
time += Time.deltaTime;
slider.value = Mathf.Lerp(originalValue, value, time / duration);
yield return new WaitForEndOfFrame();
}
slider.value = value;
}
public static bool HasUIForm(this UIComponent uiComponent, UIFormType uiFormId, string uiGroupName = null)
{
return uiComponent.HasUIForm((int)uiFormId, uiGroupName);
}
public static bool HasUIForm(this UIComponent uiComponent, int uiFormId, string uiGroupName = null)
{
IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
if (drUIForm == null)
{
return false;
}
string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);
if (string.IsNullOrEmpty(uiGroupName))
{
return uiComponent.HasUIForm(assetName);
}
IUIGroup uiGroup = uiComponent.GetUIGroup(uiGroupName);
if (uiGroup == null)
{
return false;
}
return uiGroup.HasUIForm(assetName);
}
public static UGuiForm GetUIForm(this UIComponent uiComponent, UIFormType uiFormId, string uiGroupName = null)
{
return uiComponent.GetUIForm((int)uiFormId, uiGroupName);
}
public static UGuiForm GetUIForm(this UIComponent uiComponent, int uiFormId, string uiGroupName = null)
{
IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
if (drUIForm == null)
{
return null;
}
string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);
UIForm uiForm = null;
if (string.IsNullOrEmpty(uiGroupName))
{
uiForm = uiComponent.GetUIForm(assetName);
if (uiForm == null)
{
return null;
}
return (UGuiForm)uiForm.Logic;
}
IUIGroup uiGroup = uiComponent.GetUIGroup(uiGroupName);
if (uiGroup == null)
{
return null;
}
uiForm = (UIForm)uiGroup.GetUIForm(assetName);
if (uiForm == null)
{
return null;
}
return (UGuiForm)uiForm.Logic;
}
public static void CloseUIForm(this UIComponent uiComponent, UGuiForm uiForm)
{
uiComponent.CloseUIForm(uiForm.UIForm);
}
public static int? OpenUIForm(this UIComponent uiComponent, UIFormType uiFormId, object userData = null)
{
return uiComponent.OpenUIForm((int)uiFormId, userData);
}
public static int? OpenUIForm(this UIComponent uiComponent, int uiFormId, object userData = null)
{
IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
if (drUIForm == null)
{
Log.Warning("Can not load UI form '{0}' from data table.", uiFormId.ToString());
return null;
}
string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);
if (!drUIForm.AllowMultiInstance)
{
if (uiComponent.IsLoadingUIForm(assetName))
{
return null;
}
if (uiComponent.HasUIForm(assetName))
{
return null;
}
}
return uiComponent.OpenUIForm(assetName, drUIForm.UIGroupName, Constant.AssetPriority.UIFormAsset,
drUIForm.PauseCoveredUIForm, userData);
}
public static void OpenDialog(this UIComponent uiComponent, DialogParams dialogParams)
{
if (((ProcedureBase)GameEntry.Procedure.CurrentProcedure).UseNativeDialog)
{
OpenNativeDialog(dialogParams);
}
else
{
uiComponent.OpenUIForm(UIFormType.DialogForm, dialogParams);
}
}
private static void OpenNativeDialog(DialogParams dialogParams)
{
// TODO:这里应该弹出原生对话框,先简化实现为直接按确认按钮
if (dialogParams.OnClickConfirm != null)
{
dialogParams.OnClickConfirm(dialogParams.UserData);
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9cfc2c881ce240349901812c61f4476b
timeCreated: 1528026162
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,10 @@
namespace GeometryTD.UI
{
public abstract class UIFormControllerBase<TContext> : IUIFormController where TContext : UIContext
{
protected abstract int? OpenUIInternal(TContext context);
public abstract int? OpenUI(object userData = null);
public abstract void CloseUI();
public abstract void BindUseCase(IUIUseCase useCase);
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f14634cead1af164d8795233697f4100
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,183 @@
using GeometryTD.Definition;
using GameFramework.Event;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public abstract class UIFormControllerCommonBase<TContext, TForm> : UIFormControllerBase<TContext>
where TContext : UIContext
where TForm : UGuiForm
{
private TContext _context;
private TForm _form;
private int? _formSerialId;
private bool _pendingRefresh;
private bool _isBindEvent;
protected TContext Context => _context;
protected TForm Form => _form;
protected int? FormSerialId => _formSerialId;
protected abstract UIFormType UIFormTypeId { get; }
protected abstract void RefreshUI(TForm form, TContext context);
protected virtual void SubscribeCustomEvents()
{
}
protected virtual void UnsubscribeCustomEvents()
{
}
protected virtual void CloseLoadedFormDirect(TForm form)
{
form.Close();
}
protected void SetContext(TContext context)
{
_context = context;
}
protected void RefreshCurrentUI()
{
if (_context == null)
{
return;
}
if (_form == null)
{
_pendingRefresh = true;
return;
}
RefreshUI(_form, _context);
_pendingRefresh = false;
}
protected override int? OpenUIInternal(TContext context)
{
if (context == null)
{
Log.Warning("{0}.OpenUI() context is null.", GetType().Name);
return null;
}
_context = context;
if (_form != null && _formSerialId.HasValue && GameEntry.UI.HasUIForm(_formSerialId.Value))
{
RefreshUI(_form, _context);
return _formSerialId;
}
CloseUI();
_pendingRefresh = true;
SubscribeEvents();
_formSerialId = GameEntry.UI.OpenUIForm(UIFormTypeId, _context);
return _formSerialId;
}
public override void CloseUI()
{
_pendingRefresh = false;
UnsubscribeEvents();
if (_formSerialId.HasValue)
{
if (GameEntry.UI.HasUIForm(_formSerialId.Value))
{
GameEntry.UI.CloseUIForm(_formSerialId.Value);
}
_form = null;
_formSerialId = null;
return;
}
if (_form != null)
{
CloseLoadedFormDirect(_form);
_form = null;
}
}
private void SubscribeEvents()
{
if (_isBindEvent)
{
return;
}
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
SubscribeCustomEvents();
_isBindEvent = true;
}
private void UnsubscribeEvents()
{
if (!_isBindEvent)
{
return;
}
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OpenUIFormSuccess);
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, CloseUIFormComplete);
UnsubscribeCustomEvents();
_isBindEvent = false;
}
private void OpenUIFormSuccess(object sender, GameEventArgs e)
{
if (!(e is OpenUIFormSuccessEventArgs args))
{
return;
}
if (!_formSerialId.HasValue)
{
return;
}
if (args.UIForm == null || args.UIForm.SerialId != _formSerialId.Value || args.UserData != _context)
{
return;
}
_form = args.UIForm.Logic as TForm;
if (_form == null)
{
Log.Warning("{0} open success but form logic is invalid.", GetType().Name);
return;
}
if (_pendingRefresh)
{
RefreshCurrentUI();
}
}
private void CloseUIFormComplete(object sender, GameEventArgs e)
{
if (!(e is CloseUIFormCompleteEventArgs args))
{
return;
}
if (args.SerialId != _formSerialId)
{
return;
}
_form = null;
_formSerialId = null;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1c5e27ba12c95604a858ba7c25f8b863
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: