using FairyGUI;
using System;
using System.Collections.Generic;
using UnityEngine;
using XGame.Framework.UI;
namespace XGame.Framework.FGUI
{
///
/// FGUI相关的操作都在IUIViewModel的实现类里
///
public abstract class FguiViewModel : IUIViewModel, IUIVMInternal, IDisposable
{
public GComponent Panel { get; private set; }
public Transform Tr => Panel.displayObject.gameObject.transform;
private HashSet _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);
///
/// 自定义绑定,有需要的重载
///
///
protected virtual void CustomBind(GComponent panel, IUIViewAdapter adapter)
{
}
public void AddChild(INestedView nestedView)
{
if (nestedView is INestedInternal temp)
{
_nestedViews ??= new HashSet();
_nestedViews.Add(temp);
}
}
public void RemoveChild(INestedView nestedView)
{
if (_nestedViews == null) return;
if (nestedView is INestedInternal temp)
{
_nestedViews.Remove(temp);
}
}
///
/// 结束ui的所有tween
///
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();
}
}
}
}