1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using FairyGUI;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using XGame.Framework.UI;
- namespace XGame.Framework.FGUI
- {
- /// <summary>
- /// FGUI相关的操作都在IUIViewModel的实现类里
- /// </summary>
- public abstract class FguiViewModel : IUIViewModel, IUIVMInternal, IDisposable
- {
- public GComponent Panel { get; private set; }
- public Transform Tr => Panel.displayObject.gameObject.transform;
- private HashSet<INestedInternal> _nestedViews;
- void IUIVMInternal.Init(IUIPanel uiPanel, IUIViewAdapter adapter)
- {
- Init((uiPanel as FguiPanel).Panel, adapter);
- }
- void IUIVMInternal.Init(IUINested nested, IUIViewAdapter adapter)
- {
- Init((nested as FguiNested).Component, 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(GComponent panel, IUIViewAdapter adapter)
- {
- Panel = panel;
- BindComponents(panel, adapter);
- CustomBind(panel, adapter);
- }
- protected abstract void BindComponents(GComponent panel, IUIViewAdapter adapter);
- /// <summary>
- /// 自定义绑定,有需要的重载
- /// </summary>
- /// <param name="panel"></param>
- protected virtual void CustomBind(GComponent panel, 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);
- }
- }
- /// <summary>
- /// 结束ui的所有tween
- /// </summary>
- public void KillTweens(bool complete = false)
- {
- GTween.Kill(Panel, complete);
- }
- void IDisposable.Dispose()
- {
- if (_nestedViews != null)
- {
- foreach(var nestedView in _nestedViews)
- {
- (nestedView as IDisposable)?.Dispose();
- }
- _nestedViews.Clear();
- }
- }
- }
- }
|