123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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
- {
- protected GComponent Panel { get; private set; }
- public EventListener PanelEvent => Panel.onClick;
- 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();
- }
- }
- }
- }
|