using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Game.UI
{
public enum DepthLimit :int
{
Min = 100,
Top = 5000,
Max = 10000,
}
public enum DepthType
{
Fix=0,
Normal=1,
Top=2,
}
public class UIMgr : Singleton<UIMgr>
{
public List<Window> mWindows = new List<Window>();
Int64 MaxOpenOrder = 0;
bool mNeedRefresh = false;
string mPreOpenUIName = "";
object mPreOpenUIParam = new object();
Window mNormalTopWindow;
//Window mNewNormalTopWindow;
public Window normalTopWindow
{
get
{
return mNormalTopWindow;
}
}
public Window Find( string name )
{
foreach( var win in mWindows )
{
if (win.name == name)
return win;
}
return null;
}
public T FindView<T>() where T : View
{
foreach (var win in mWindows)
{
var t = win.view as T;
if (t != null)
return t;
}
return null;
}
public T FindView<T>(string name) where T : View
{
foreach (var win in mWindows)
{
if (win.name == name)
return win.view as T;
}
return null;
}
public IEnumerable<Window> Finds(string name)
{
foreach (var win in mWindows)
{
if (win.name == name)
yield return win;
}
}
public void GoToHomeLandMain()
{
List<Window> mClose = new List<Window>();
foreach (Window item in mWindows)
{
if (!item.name.Equals("HomelandMain"))
{
mClose.Add(item);
}
}
foreach(Window item in mClose)
{
item.Close();
}
}
public void ProcessGroup( int group )
{
if (group < 0) return;
List<Window> closeView = null;
foreach (var win in mWindows)
{
if (win.res.group == group)
{
if (closeView == null)
closeView = new List<Window>();
closeView.Add(win);
}
}
if (closeView == null)
return;
foreach (Window _view in closeView)
{
Close(_view);
}
}
public long ProductOrder()
{
return MaxOpenOrder++;
}
public View Open( string name , object val = null)
{
var viewItem = TableMgr.singleton.ui[name];
if (viewItem == null)
{
MyLog.LogError("Open not find name[{0}],val:{1}", name, val);
return null;
}
if( viewItem.canOpenMulty <= 0 )
{
var oldView = Find(name);
if ( oldView != null)
{
//Close(oldView);
//MyLog.Log("Open [{0}] param:{1} , will close old view ", name, val);
return oldView.view;
}
}
ProcessGroup(viewItem.group);
Window win = new Window();
win.Create(this , viewItem, val);
win.Open();
mWindows.Add( win );
mWindows.Sort((a, b) => (int)(a.openOrder - b.openOrder));
//if(mNormalTopWindow==null)
//{
// SetNormalTopWindow(win);
//}
//else
//{
// //if (win.depthType == DepthType.Top)
// //{
// // SetTopWindow(win);
// //}
// if (win.depthType == DepthType.Normal)
// {
// SetNormalTopWindow(win);
// }
//}
Refresh();
return win.view;
}
public View OpenViewByName(UIItem viewItem)
{
Window win = new Window();
win.Create(this, viewItem,null);
win.Open();
mWindows.Add(win);
mWindows.Sort((a, b) => (int)(a.openOrder - b.openOrder));
Refresh();
return win.view;
}
private void SetNormalTopWindow(Window wind)
{
if (mNormalTopWindow == wind) return;
if (wind == null)
{
if (mNormalTopWindow != null && mNormalTopWindow.view != null)
{
mNormalTopWindow.view.LoseTopView();
mNormalTopWindow = null;
}
return;
}
//if (mNormalTopWindow != wind)
//{
if (wind.depthType != DepthType.Normal)
{
return;
}
//窗口不是最上层界面触发的事件
if (mNormalTopWindow != null && mNormalTopWindow.view != null)
{
mNormalTopWindow.view.LoseTopView();
}
mNormalTopWindow = wind;
//窗口变成最上层界面触发的事件
if (mNormalTopWindow != null && mNormalTopWindow.view != null)
{
mNormalTopWindow.view.BecomeTopView();
}
//}
}
public void Refresh()
{
mNeedRefresh = true;
}
void UpdateWindow( Window win , ref int depth )
{
View view = win.view;
if (view != null)
{
if (view.panel != null)
view.panel.sortingOrder = depth++;
//foreach (var subPanel in view.subPanels)
//{
// subPanel.depth = depth++;
//}
}
foreach( var subWin in win.childs )
{
UpdateWindow(subWin, ref depth);
}
}
public void UpdateRefresh()
{
if (!mNeedRefresh)
return;
int normal = (int)DepthLimit.Min;
int top = (int)DepthLimit.Top;
Window _topview = null;
int top_depth = -1;
foreach (var win in mWindows)
{
//if (win.parant != null)
//{
// MyLog.LogError("sub window [{0}] can't open by mgr!");
// continue;
//}
if (win.depthType == DepthType.Normal)
{
UpdateWindow(win, ref normal);
if (normal > top_depth)
{
_topview = win;
top_depth = normal;
}
}
else if (win.depthType == DepthType.Top)
{
//GameObject obj;
UpdateWindow(win, ref top);
//if (top > top_depth)
//{
// _topview = win;
// top_depth = top;
//}
}
else
{
View view = win.view;
if (view && view.panel != null)
view.panel.sortingOrder = win.res.fixDepth;
//if (win.res.fixDepth > top_depth)
//{
// _topview = win;
// top_depth = win.res.fixDepth;
//}
}
}
SetNormalTopWindow(_topview);
mNeedRefresh = false;
}
public void Update( FrameTimer ft )
{
UpdateRefresh();
for (int i = mWindows.Count - 1; i >= 0; i--)
{
var win = mWindows[i];
win.Update(ft);
}
}
public void Close( Window win )
{
if (win != null)
{
win.Close();
//mWindows.Remove(win);
MainLogic.singleton.AddRun(() => mWindows.Remove(win));
}
//Refresh();
}
public void Close(string name)
{
var win = Find(name);
if (win != null)
win.Close();
}
public void _RemoveWindow(Window win)
{
Refresh();
if(MainLogic.singleton!=null)
MainLogic.singleton.AddRun(() => mWindows.Remove(win));
}
public void BoradCast(string methodName, object parameter = null)
{
//if (parameter == null)
//{
// UIRoot.Broadcast(methodName);
//}
//else
//{
// UIRoot.Broadcast(methodName, parameter);
//}
//foreach( var win in mWindows )
for ( int i = mWindows.Count - 1; i >= 0 ; i-- )
{
var win = mWindows[i];
win.OnEvent(methodName, parameter);
}
}
public void SetPreOpenUIName(string name, object param = null)
{
mPreOpenUIName = name;
mPreOpenUIParam = param;
}
public void OpenPreOpenUI()
{
if (string.IsNullOrEmpty(mPreOpenUIName))
return;
Open(mPreOpenUIName, mPreOpenUIParam);
mPreOpenUIName = "";
}
}
}