123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using System.Collections.Generic;
- namespace XGame.Framework.UI
- {
- public abstract class UIViewModule : IUIViewModel, IUIVMInternal, IDisposable
- {
- private HashSet<INestedInternal> _nestedViews;
- void IUIVMInternal.Init(IUIPanel panel, IUIViewAdapter adapter)
- {
- Init(panel as IComponentGetter, adapter);
- }
- void IUIVMInternal.Init(IUINested nested, IUIViewAdapter adapter)
- {
- Init(nested as IComponentGetter, adapter);
- }
- void IUIVMInternal.EnableChildren(object intent)
- {
- if (_nestedViews == null)
- return;
- foreach (var nestedView in _nestedViews)
- {
- nestedView.EnableByParent(intent);
- }
- }
- void IUIVMInternal.DisableChildren()
- {
- if (_nestedViews == null)
- return;
- foreach (var nestedView in _nestedViews)
- {
- nestedView.DisableByParent();
- }
- }
- private void Init(IComponentGetter getter, IUIViewAdapter adapter)
- {
- BindComponents(getter, adapter);
- CustomBind(getter, adapter);
- }
- protected abstract void BindComponents(IComponentGetter getter, IUIViewAdapter adapter);
- /// <summary>
- /// 自定义绑定,有需要的重载
- /// </summary>
- /// <param name="getter"></param>
- protected virtual void CustomBind(IComponentGetter getter, IUIViewAdapter adapter)
- {
- }
- public void AddChild(INestedView nestedView)
- {
- if (nestedView is INestedInternal temp)
- {
- _nestedViews ??= new HashSet<INestedInternal>();
- _nestedViews.Add(temp);
- }
- }
- public void RemoveChild(INestedView nestedView)
- {
- if (_nestedViews == null) return;
- if (nestedView is INestedInternal temp)
- {
- _nestedViews.Remove(temp);
- }
- }
- void IDisposable.Dispose()
- {
- if (_nestedViews != null)
- {
- foreach (var nestedView in _nestedViews)
- {
- (nestedView as IDisposable)?.Dispose();
- }
- _nestedViews.Clear();
- }
- }
- }
- }
|