Skip to content

Commit e31313f

Browse files
committed
[重构](UIManager): 重构和抽象UI管理器到基类
1 parent 7feba92 commit e31313f

13 files changed

Lines changed: 964 additions & 0 deletions

Runtime/BaseUIManager.Close.cs

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
//------------------------------------------------------------
2+
// Game Framework
3+
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
4+
// Homepage: https://gameframework.cn/
5+
// Feedback: mailto:ellan@gameframework.cn
6+
//------------------------------------------------------------
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using GameFrameX.Runtime;
11+
12+
namespace GameFrameX.UI.Runtime
13+
{
14+
/// <summary>
15+
/// 界面管理器。
16+
/// </summary>
17+
public partial class BaseUIManager
18+
{
19+
protected EventHandler<CloseUIFormCompleteEventArgs> m_CloseUIFormCompleteEventHandler;
20+
21+
/// <summary>
22+
/// 关闭界面完成事件。
23+
/// </summary>
24+
public event EventHandler<CloseUIFormCompleteEventArgs> CloseUIFormComplete
25+
{
26+
add { m_CloseUIFormCompleteEventHandler += value; }
27+
remove { m_CloseUIFormCompleteEventHandler -= value; }
28+
}
29+
30+
/// <summary>
31+
/// 回收界面实例对象。
32+
/// </summary>
33+
/// <param name="uiForm"></param>
34+
/// <param name="isDispose">是否销毁释放</param>
35+
protected abstract void RecycleUIForm(IUIForm uiForm, bool isDispose = false);
36+
37+
/// <summary>
38+
/// 关闭界面。
39+
/// </summary>
40+
/// <param name="serialId">要关闭界面的序列编号。</param>
41+
/// <param name="isNowRecycle">是否立即回收界面,默认是否</param>
42+
public void CloseUIForm(int serialId, bool isNowRecycle = false)
43+
{
44+
CloseUIForm(serialId, null, isNowRecycle);
45+
}
46+
47+
/// <summary>
48+
/// 关闭界面。
49+
/// </summary>
50+
/// <param name="serialId">要关闭界面的序列编号。</param>
51+
/// <param name="userData">用户自定义数据。</param>
52+
/// <param name="isNowRecycle">是否立即回收界面,默认是否</param>
53+
public void CloseUIForm(int serialId, object userData, bool isNowRecycle = false)
54+
{
55+
if (IsLoadingUIForm(serialId))
56+
{
57+
m_UIFormsToReleaseOnLoad.Add(serialId);
58+
m_UIFormsBeingLoaded.Remove(serialId);
59+
return;
60+
}
61+
62+
IUIForm uiForm = GetUIForm(serialId);
63+
if (uiForm == null)
64+
{
65+
throw new GameFrameworkException(Utility.Text.Format("Can not find UI form '{0}'.", serialId));
66+
}
67+
68+
CloseUIForm(uiForm, userData, isNowRecycle);
69+
}
70+
71+
/// <summary>
72+
/// 关闭界面。
73+
/// </summary>
74+
/// <param name="uiForm">要关闭的界面。</param>
75+
/// <param name="isNowRecycle">是否立即回收界面,默认是否</param>
76+
public void CloseUIForm(IUIForm uiForm, bool isNowRecycle = false)
77+
{
78+
CloseUIForm(uiForm, null, isNowRecycle);
79+
}
80+
81+
/// <summary>
82+
/// 关闭界面。
83+
/// </summary>
84+
/// <param name="userData">用户自定义数据。</param>
85+
/// <param name="isNowRecycle">是否立即回收界面,默认是否</param>
86+
/// <typeparam name="T"></typeparam>
87+
public void CloseUIForm<T>(object userData, bool isNowRecycle = false) where T : IUIForm
88+
{
89+
var fullName = typeof(T).FullName;
90+
IUIForm[] uiForms = GetAllLoadedUIForms();
91+
foreach (IUIForm uiForm in uiForms)
92+
{
93+
if (uiForm.FullName != fullName)
94+
{
95+
continue;
96+
}
97+
98+
if (!HasUIFormFullName(uiForm.FullName))
99+
{
100+
continue;
101+
}
102+
103+
CloseUIForm(uiForm, userData, isNowRecycle);
104+
break;
105+
}
106+
}
107+
108+
/// <summary>
109+
/// 关闭界面。
110+
/// </summary>
111+
/// <param name="uiForm">要关闭的界面。</param>
112+
/// <param name="userData">用户自定义数据。</param>
113+
/// <param name="isNowRecycle">是否立即回收界面,默认是否</param>
114+
public void CloseUIForm(IUIForm uiForm, object userData, bool isNowRecycle = false)
115+
{
116+
GameFrameworkGuard.NotNull(uiForm, nameof(uiForm));
117+
GameFrameworkGuard.NotNull(uiForm.UIGroup, nameof(uiForm.UIGroup));
118+
UIGroup uiGroup = (UIGroup)uiForm.UIGroup;
119+
var serialId = uiForm.SerialId;
120+
uiGroup.RemoveUIForm(uiForm);
121+
uiForm.OnClose(m_IsShutdown, userData);
122+
uiGroup.Refresh();
123+
if (IsLoadingUIForm(serialId))
124+
{
125+
m_UIFormsToReleaseOnLoad.Add(serialId);
126+
m_UIFormsBeingLoaded.Remove(serialId);
127+
}
128+
129+
bool isRecycled = false;
130+
if (isNowRecycle && !uiForm.IsDisableRecycling)
131+
{
132+
isRecycled = true;
133+
RecycleUIForm(uiForm, true);
134+
}
135+
136+
if (uiForm.IsDisableRecycling == false && isRecycled == false)
137+
{
138+
m_RecycleQueue.Enqueue(uiForm);
139+
}
140+
141+
if (m_CloseUIFormCompleteEventHandler != null)
142+
{
143+
CloseUIFormCompleteEventArgs closeUIFormCompleteEventArgs = CloseUIFormCompleteEventArgs.Create(uiForm.SerialId, uiForm.UIFormAssetName, uiGroup, userData);
144+
m_CloseUIFormCompleteEventHandler(this, closeUIFormCompleteEventArgs);
145+
}
146+
}
147+
148+
/// <summary>
149+
/// 关闭所有已加载的界面。
150+
/// </summary>
151+
public void CloseAllLoadedUIForms()
152+
{
153+
CloseAllLoadedUIForms(null);
154+
}
155+
156+
/// <summary>
157+
/// 关闭所有已加载的界面。
158+
/// </summary>
159+
/// <param name="userData">用户自定义数据。</param>
160+
public void CloseAllLoadedUIForms(object userData)
161+
{
162+
IUIForm[] uiForms = GetAllLoadedUIForms();
163+
foreach (IUIForm uiForm in uiForms)
164+
{
165+
if (!HasUIForm(uiForm.SerialId))
166+
{
167+
continue;
168+
}
169+
170+
CloseUIForm(uiForm, userData);
171+
}
172+
}
173+
174+
/// <summary>
175+
/// 关闭所有正在加载的界面。
176+
/// </summary>
177+
public void CloseAllLoadingUIForms()
178+
{
179+
foreach (KeyValuePair<int, string> uiFormBeingLoaded in m_UIFormsBeingLoaded)
180+
{
181+
m_UIFormsToReleaseOnLoad.Add(uiFormBeingLoaded.Key);
182+
}
183+
184+
m_UIFormsBeingLoaded.Clear();
185+
}
186+
}
187+
}

Runtime/BaseUIManager.Close.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)