4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Game.UI
{
    public class Window
    {
        string mName;
        UIItem mRes;
        DepthType mDepthType = DepthType.Normal;
        Int64 mOpenOrder;
        string mResource;
        Window mParant;
        Transform mTransParent;
        //int mGroup = -1;
        //int mFixDepth = -1;
        UIMgr mgr;
        View mView;
        object mParam;
        //UIMgr mMgr;
        bool mHasOpen = false;
        List<Window> mChilds = new List<Window>();

        public string name { get { return mName; } }
        public DepthType depthType { get { return mDepthType; } }
        public UIItem res { get { return mRes; } }
        public Int64 openOrder { get { return mOpenOrder; } }
        //public int group { get { return mGroup; } }
        //public int fixDepth { get { return mFixDepth; } }
        public View view { get { return mView; } }
        public Window parant { get { return mParant; } }
        public bool hasChild { get { return mChilds.Count > 0; } }
        public bool hasOpen { get { return mHasOpen; } }
        public IEnumerable<Window> childs { get { return mChilds; } }

        public object param { get { return mParam; } }

        //void AddChild(Window ui)
        //{
        //    if (ui.mParam != null)
        //    {
        //        MyLog.LogError( "windows [{0}] has add Chlid  " , ui.name );
        //    }
        //    ui.mParam = this;
        //    mChilds.Add(ui);
        //}
        Window _AddChild(string _Prefab, Transform transParent, object val)
        {
            var win = new Window();
            win.mName = _Prefab;
            win.mResource = _Prefab;
            win.mOpenOrder = -1;
            //win.mGroup = -1;
            win.mDepthType = DepthType.Normal;
            //win.mFixDepth = -1;
            win.mParam = val;
            win.mParant = this;
            win.mTransParent = transParent;
            win.mgr = mgr;
            mChilds.Add(win);
            return win;
        }
        public Window AddChild(string _Prefab)
        {
            return _AddChild(_Prefab, null, null);
        }
        public Window AddChild(string _Prefab , object val)
        {
            return _AddChild(_Prefab, null, val);
        }
        public Window AddChild(string _Prefab, Transform transParent, object val )
        {
            return _AddChild(_Prefab, transParent, val);
        }

        public void RemoveChild( Window win )
        {
            if (win != null)
            {
                win.Close();
            }
            mChilds.Remove(win);
        }
        public virtual bool Create(UIMgr _mgr, UIItem _res, object val)
        {
            mRes = _res;
            mgr = _mgr;
            mName = _res.name;
            //mGroup = _res.group;
            mOpenOrder = mgr.ProductOrder();
            mDepthType = (DepthType)_res.depthType;
            //mFixDepth = _res.fixDepth;
            mResource = _res.resource;

            if (res.changeSceneDestroy <= 0)
                mTransParent = GameMain.singleton.transform;

            mParam = val;
            return true;
        }

        public void OnEvent(string sevent, object obj)
        {
            if (mView!=null)
            {
                mView.OnEvent(sevent, obj);
            }
        }

        public bool isTop
        {
            get
            {
                return name == UIMgr.singleton.normalTopWindow.name;
            }
        }

        public virtual View Open(object par)
        {
            mParam = par;
            return Open();
        }

        public virtual View Open( )
        {
            //mRes = _res;
            if (mView != null)
                return mView;

            Transform _parent = mTransParent;
            if (_parent == null)
            {
                //if(BaseUIMgr.singleton!= null)
                //    _parent = BaseUIMgr.singleton.uiRoot;
            }
            //if (!mRootWindow)
            //{
            //    if (mParant != null && mParant.view != null)
            //        _parent = mParant.view.transform;
            //    else
            //        MyLog.LogError("{0} mParant != null && mParant.view error!", name);
            //}
            var obj = ResourceMgr.singleton.AddGameInstanceAsSubObject(mResource, _parent);
            if (obj == null)
                return mView;
            mView = obj.GetComponent<View>();
            if (mView == null)
            {
                MyLog.Log("Open [{0}] error! param:{1} ", name, mParam);
                return mView;
            }
            mView.Create(this, mParam);
            mgr.Refresh();
            mHasOpen = true;

            //GuideMgr.singleton.StartGuideEvent(GuideStartType.Open_UI, name);

            return mView;
        }

        public void Update(FrameTimer ft)
        {
            if( view )
            {
                view.UpdateView(ft);
            }
        }

        public virtual void Close()
        {
            Destroy();
        }
        void Destroy()
        {
            mHasOpen = false;
            if(mgr != null)
                mgr._RemoveWindow(this);
            foreach (Window win in childs)
            {
                win.Destroy();
            }
            mChilds.Clear();
            if (mView != null)
            {
                mView.Destroy();
                //MyLog.Log("release mView {0}", mView);
                if (ResourceMgr.singleton != null)
                    ResourceMgr.singleton.DeleteInstance(mView.gameObject);
                mView = null;
            }
        }
    }
}

posted @ 2017-08-03 12:01  rexzhao  阅读(120)  评论(0编辑  收藏  举报