//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using System.Collections.Generic; namespace GameFramework.UI { internal sealed partial class UIManager : GameFrameworkModule, IUIManager { /// /// 界面组。 /// private sealed partial class UIGroup : IUIGroup { private readonly string m_Name; private int m_Depth; private bool m_Pause; private readonly IUIGroupHelper m_UIGroupHelper; private readonly GameFrameworkLinkedList m_UIFormInfos; private LinkedListNode m_CachedNode; /// /// 初始化界面组的新实例。 /// /// 界面组名称。 /// 界面组深度。 /// 界面组辅助器。 public UIGroup(string name, int depth, IUIGroupHelper uiGroupHelper) { if (string.IsNullOrEmpty(name)) { throw new GameFrameworkException("UI group name is invalid."); } if (uiGroupHelper == null) { throw new GameFrameworkException("UI group helper is invalid."); } m_Name = name; m_Pause = false; m_UIGroupHelper = uiGroupHelper; m_UIFormInfos = new GameFrameworkLinkedList(); m_CachedNode = null; Depth = depth; } /// /// 获取界面组名称。 /// public string Name { get { return m_Name; } } /// /// 获取或设置界面组深度。 /// public int Depth { get { return m_Depth; } set { if (m_Depth == value) { return; } m_Depth = value; m_UIGroupHelper.SetDepth(m_Depth); Refresh(); } } /// /// 获取或设置界面组是否暂停。 /// public bool Pause { get { return m_Pause; } set { if (m_Pause == value) { return; } m_Pause = value; Refresh(); } } /// /// 获取界面组中界面数量。 /// public int UIFormCount { get { return m_UIFormInfos.Count; } } /// /// 获取当前界面。 /// public IUIForm CurrentUIForm { get { return m_UIFormInfos.First != null ? m_UIFormInfos.First.Value.UIForm : null; } } /// /// 获取界面组辅助器。 /// public IUIGroupHelper Helper { get { return m_UIGroupHelper; } } /// /// 界面组轮询。 /// /// 逻辑流逝时间,以秒为单位。 /// 真实流逝时间,以秒为单位。 public void Update(float elapseSeconds, float realElapseSeconds) { LinkedListNode current = m_UIFormInfos.First; while (current != null) { if (current.Value.Paused) { break; } m_CachedNode = current.Next; current.Value.UIForm.OnUpdate(elapseSeconds, realElapseSeconds); current = m_CachedNode; m_CachedNode = null; } } /// /// 界面组中是否存在界面。 /// /// 界面序列编号。 /// 界面组中是否存在界面。 public bool HasUIForm(int serialId) { foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { if (uiFormInfo.UIForm.SerialId == serialId) { return true; } } return false; } /// /// 界面组中是否存在界面。 /// /// 界面资源名称。 /// 界面组中是否存在界面。 public bool HasUIForm(string uiFormAssetName) { if (string.IsNullOrEmpty(uiFormAssetName)) { throw new GameFrameworkException("UI form asset name is invalid."); } foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName) { return true; } } return false; } /// /// 从界面组中获取界面。 /// /// 界面序列编号。 /// 要获取的界面。 public IUIForm GetUIForm(int serialId) { foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { if (uiFormInfo.UIForm.SerialId == serialId) { return uiFormInfo.UIForm; } } return null; } /// /// 从界面组中获取界面。 /// /// 界面资源名称。 /// 要获取的界面。 public IUIForm GetUIForm(string uiFormAssetName) { if (string.IsNullOrEmpty(uiFormAssetName)) { throw new GameFrameworkException("UI form asset name is invalid."); } foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName) { return uiFormInfo.UIForm; } } return null; } /// /// 从界面组中获取界面。 /// /// 界面资源名称。 /// 要获取的界面。 public IUIForm[] GetUIForms(string uiFormAssetName) { if (string.IsNullOrEmpty(uiFormAssetName)) { throw new GameFrameworkException("UI form asset name is invalid."); } List results = new List(); foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName) { results.Add(uiFormInfo.UIForm); } } return results.ToArray(); } /// /// 从界面组中获取界面。 /// /// 界面资源名称。 /// 要获取的界面。 public void GetUIForms(string uiFormAssetName, List results) { if (string.IsNullOrEmpty(uiFormAssetName)) { throw new GameFrameworkException("UI form asset name is invalid."); } if (results == null) { throw new GameFrameworkException("Results is invalid."); } results.Clear(); foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName) { results.Add(uiFormInfo.UIForm); } } } /// /// 从界面组中获取所有界面。 /// /// 界面组中的所有界面。 public IUIForm[] GetAllUIForms() { List results = new List(); foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { results.Add(uiFormInfo.UIForm); } return results.ToArray(); } /// /// 从界面组中获取所有界面。 /// /// 界面组中的所有界面。 public void GetAllUIForms(List results) { if (results == null) { throw new GameFrameworkException("Results is invalid."); } results.Clear(); foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { results.Add(uiFormInfo.UIForm); } } /// /// 往界面组增加界面。 /// /// 要增加的界面。 public void AddUIForm(IUIForm uiForm) { m_UIFormInfos.AddFirst(UIFormInfo.Create(uiForm)); } /// /// 从界面组移除界面。 /// /// 要移除的界面。 public void RemoveUIForm(IUIForm uiForm) { UIFormInfo uiFormInfo = GetUIFormInfo(uiForm); if (uiFormInfo == null) { throw new GameFrameworkException(Utility.Text.Format("Can not find UI form info for serial id '{0}', UI form asset name is '{1}'.", uiForm.SerialId, uiForm.UIFormAssetName)); } if (!uiFormInfo.Covered) { uiFormInfo.Covered = true; uiForm.OnCover(); } if (!uiFormInfo.Paused) { uiFormInfo.Paused = true; uiForm.OnPause(); } if (m_CachedNode != null && m_CachedNode.Value.UIForm == uiForm) { m_CachedNode = m_CachedNode.Next; } if (!m_UIFormInfos.Remove(uiFormInfo)) { throw new GameFrameworkException(Utility.Text.Format("UI group '{0}' not exists specified UI form '[{1}]{2}'.", m_Name, uiForm.SerialId, uiForm.UIFormAssetName)); } ReferencePool.Release(uiFormInfo); } /// /// 激活界面。 /// /// 要激活的界面。 /// 用户自定义数据。 public void RefocusUIForm(IUIForm uiForm, object userData) { UIFormInfo uiFormInfo = GetUIFormInfo(uiForm); if (uiFormInfo == null) { throw new GameFrameworkException("Can not find UI form info."); } m_UIFormInfos.Remove(uiFormInfo); m_UIFormInfos.AddFirst(uiFormInfo); } /// /// 刷新界面组。 /// public void Refresh() { LinkedListNode current = m_UIFormInfos.First; bool pause = m_Pause; bool cover = false; int depth = UIFormCount; while (current != null && current.Value != null) { LinkedListNode next = current.Next; current.Value.UIForm.OnDepthChanged(Depth, depth--); if (current.Value == null) { return; } if (pause) { if (!current.Value.Covered) { current.Value.Covered = true; current.Value.UIForm.OnCover(); if (current.Value == null) { return; } } if (!current.Value.Paused) { current.Value.Paused = true; current.Value.UIForm.OnPause(); if (current.Value == null) { return; } } } else { if (current.Value.Paused) { current.Value.Paused = false; current.Value.UIForm.OnResume(); if (current.Value == null) { return; } } if (current.Value.UIForm.PauseCoveredUIForm) { pause = true; } if (cover) { if (!current.Value.Covered) { current.Value.Covered = true; current.Value.UIForm.OnCover(); if (current.Value == null) { return; } } } else { if (current.Value.Covered) { current.Value.Covered = false; current.Value.UIForm.OnReveal(); if (current.Value == null) { return; } } cover = true; } } current = next; } } internal void InternalGetUIForms(string uiFormAssetName, List results) { foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { if (uiFormInfo.UIForm.UIFormAssetName == uiFormAssetName) { results.Add(uiFormInfo.UIForm); } } } internal void InternalGetAllUIForms(List results) { foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { results.Add(uiFormInfo.UIForm); } } private UIFormInfo GetUIFormInfo(IUIForm uiForm) { if (uiForm == null) { throw new GameFrameworkException("UI form is invalid."); } foreach (UIFormInfo uiFormInfo in m_UIFormInfos) { if (uiFormInfo.UIForm == uiForm) { return uiFormInfo; } } return null; } } } }