using System;
using XGame.Framework.Quality;
using XGame.Framework.Time;
namespace XGame.Framework.UI
{
///
/// 每个UIView一个UIContext实例
///
public sealed class UIContext : IUIViewAdapter, IDisposable
{
public IQualityModule Quality { get; private set; }
private ITimeModule _timeModule;
private ITimeProxy _timeProxy;
public ITimeProxy Time => _timeProxy ?? (_timeProxy = new TimeProxy(_timeModule));
public IUIModule UI { get; private set; }
///
/// 资源加载模块,需要时才创建
///
private IUIAssetModule _asset;
///
/// 资源加载模块
///
public IUIAssetModule Asset => _asset ?? (_asset = (UI as IUIAssetModuleCreator).Create(this));
public UIContext(IQualityModule qualityModule, ITimeModule timeModule, IUIModule uiModule)
{
Quality = qualityModule;
_timeModule = timeModule;
UI = uiModule;
}
public UIContext Clone()
{
return new UIContext(Quality, _timeModule, UI);
}
///
/// 关闭UI
///
///
public void ClosePanel(bool isDestroy = false)
{
UI.Close(_uikey, isDestroy);
}
///
/// 取消定时器、清理资源加载监听等
///
public void Clear()
{
_timeProxy?.CancelAll();
_asset?.StopAllAsyncs();
}
void IDisposable.Dispose()
{
Quality = null;
_timeModule = null;
UI = null;
_timeProxy?.CancelAll();
_timeProxy = null;
(_asset as IDisposable)?.Dispose();
_asset = null;
//_ctrlGroup = null;
_uikey = null;
}
///
/// Context对应的UIKey
/// 业务不要赋值
///
private UIKey _uikey;
public UIKey Key => _uikey;
UIKey IUIViewAdapter.Key { set => _uikey = value; }
//private IUIControllerGroup _ctrlGroup;
//IUIControllerGroup IUIViewAdapter.CtrlGroup { get => _ctrlGroup; set => _ctrlGroup = value; }
TNestedView IUIViewAdapter.CreateNested(IUINested nested, bool isStatic)
{
var nestedView = Activator.CreateInstance();
(nestedView as INestedInternal).Init(this, nested, isStatic);
return nestedView;
}
INestedView IUIViewAdapter.CreateNested(Type nestedViewType, IUINested nested, bool isStatic)
{
var nestedView = Activator.CreateInstance(nestedViewType) as INestedInternal;
if (nestedView == null)
{
Log.Error($"CreateNested 类型错误. Type:{nestedViewType} isStatic:{isStatic}");
}
else
{
nestedView.Init(this, nested, isStatic);
}
return nestedView;
}
}
public interface IUIContextSetter
{
UIContext Context { set; }
}
}