3

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Game.UI
{
    public class View : UIItemWidget
    {
        //[SerializeField]
        //bool CloseReleaseResource = false;
        public event DelegateT<View> OnClose;
        bool hasClose = false;
        Window mWindow;
        Canvas mPannel;
        //List<UIPanel> mSubPanels = new List<UIPanel>();

        public Window window { get { return mWindow; } }
        public Canvas panel { get { return mPannel; } }

        public const string EventSetPos = "SetPos";
        public const string EventSetPosX = "SetPosX";
        public const string EventSetPosY = "SetPosY";

        //public IEnumerable<UIPanel> subPanels { get { return mSubPanels; } }

        //public void AddSubPanel( UIPanel panel )
        //{
        //    if (panel == null)
        //        return;

        //    mSubPanels.Add(panel);
        //}
        //public void RemoveSubPanel(UIPanel panel)
        //{
        //    mSubPanels.Remove(panel);
        //}
        void _Close()
        {
            if (!hasClose)
            {
                hasClose = true;
                if (OnClose != null)
                {
                    OnClose(this);
                }
            }
        }
        public virtual void Close()
        {
            _Close();
            if (mWindow != null)
                mWindow.Close();
        }

        public bool Create(Window win, object val)
        {
            //try
            //{
            mWindow = win;
            CreateImp(val);
            mPannel = GetComponentInChildren<Canvas>();
            return true;
            //}
            //catch(Exception e)
            //{
            //    MyLog.LogError("[{0}] create error!msg:{1}", window.name, e);
            //    return false;
            //}
        }

        public void Show(bool bShow)
        {
            gameObject.SetActive(bShow);
        }

        public virtual void UpdateView(FrameTimer ft)
        {

        }

        //[Flags]
        //public enum eEnabelFlag
        //{
        //    ContainChild = 1,
        //    ContainBox = 2,
        //}
        //static Color c_DisableColor = new Color(0.3f, 0.3f, 0.3f, 1);
        //public static EnableClass EnableUI(bool _enable, GameObject uiRoot, eEnabelFlag flag = eEnabelFlag.ContainChild | eEnabelFlag.ContainBox,List<string> list=null)
        //{
        //    EnableClass enable = new EnableClass();
        //    //UISprite[] sprites = null;
        //    //Collider[] colliders = null;
        //    if ( (int)(flag & eEnabelFlag.ContainChild) > 0 )
        //    {
        //        enable.sprites = uiRoot.GetComponentsInChildren<UISprite>();
        //        enable.textures = uiRoot.GetComponentsInChildren<UITexture>();
        //        if ((int)(flag & eEnabelFlag.ContainBox) > 0)
        //        {
        //            enable.colliders = uiRoot.GetComponentsInChildren<Collider>();
        //        }
        //    }
        //    else
        //    {
        //        enable.sprites = uiRoot.GetComponents<UISprite>();
        //        enable.textures = uiRoot.GetComponents<UITexture>();
        //        if ((int)(flag & eEnabelFlag.ContainBox) > 0)
        //        {
        //            enable.colliders = uiRoot.GetComponentsInChildren<Collider>();
        //        }
        //    }
        //    if (list != null)
        //    {
        //        List<Collider> coliList = new List<Collider>();
        //        foreach (var item in enable.colliders)
        //        {
        //            if (!list.Contains(item.name))
        //            {
        //                coliList.Add(item);
        //            }
        //        }
        //        enable.colliders = coliList.ToArray();

        //        List<UISprite> spriList = new List<UISprite>();
        //        foreach (var item in enable.sprites)
        //        {
        //            if (!list.Contains(item.name))
        //            {
        //                spriList.Add(item);
        //            }
        //        }
        //        enable.sprites = spriList.ToArray();

        //        List<UITexture> texList = new List<UITexture>();
        //        foreach (var item in enable.textures)
        //        {
        //            if (!list.Contains(item.name))
        //            {
        //                texList.Add(item);
        //            }
        //        }
        //        enable.textures = texList.ToArray();
        //    }
        //    if (enable.colliders != null)
        //    {
        //        foreach (var col in enable.colliders)
        //        {
        //            col.enabled = _enable;
        //        }
        //    }
        //    foreach (UISprite sprite in enable.sprites)
        //    {
        //        if(sprite.enabled != false)
        //        {
        //            if (!_enable)
        //            {
        //                sprite.color = new Color(0, 0, 0, sprite.color.a);
        //            }
        //            else
        //            {
        //                sprite.color = new Color(1, 1, 1, sprite.color.a);
        //            }
        //        }
        //    }
        //    if (enable.textures != null)
        //    {
        //        foreach (var sprite in enable.textures)
        //        {
        //            if (sprite.enabled != false)
        //            {
        //                if (!_enable)
        //                {
        //                    sprite.color = new Color(0, 0, 0, sprite.color.a);
        //                }
        //                else
        //                {
        //                    sprite.color = new Color(1, 1, 1, sprite.color.a);
        //                }
        //            }
        //        }
        //    }
        //    return enable;
        //}
        public virtual void BecomeTopView()
        {
            //GuideMgr.singleton.StartGuideEvent(GuideStartType.Open_UI, window.name);
        }

        public virtual void LoseTopView()
        {

        }

        public virtual void OnEvent(string sevent, object obj)
        {
            //switch (sevent)
            //{
            //    case EventSetPos:
            //        {
            //            var newPos = (Vector2)obj;
            //            var pos = transform.localPosition;
            //            pos.x = newPos.x;
            //            pos.y = newPos.y;
            //            transform.localPosition = pos;
            //        }break;
            //    case EventSetPosX:
            //        {
            //            float newPos = (float)obj;
            //            var pos = transform.localPosition;
            //            pos.x = newPos;
            //            transform.localPosition = pos;
            //        }break;
            //    case EventSetPosY:
            //        {
            //            float newPos = (float)obj;
            //            var pos = transform.localPosition;
            //            pos.y = newPos;
            //            transform.localPosition = pos;
            //        } break;
            //}
        }
        public override void Destroy()
        {
            try
            {
                base.Destroy();
                DestroyImp();
                _Close();
                return;
            }
            catch (Exception e)
            {
                if (window != null)
                    MyLog.LogWarning("[{0}] Destroy error!msg:{1}", window.name, e);
                return;
            }
        }
        protected virtual void CreateImp(object val)
        {

        }

        protected virtual void DestroyImp()
        {
        }

        protected override void OnDestroy()
        {
            if (!hasDestroy)
            {
                //if (CloseReleaseResource && GameMain.singleton)
                //    GameMain.singleton.UnloadUnusedAssets();
                if (window != null)
                {
                    MyLog.LogWarning("please close {0} view", window.name);
                }
                Close();
                //UIMgr.singleton.Close(this);
            }
            base.OnDestroy();
        }
        protected virtual void PlayAnimation(Animation ani, string name, bool forward = true)
        {
            if (ani == null)
            {
                return;
            }
            var clip = ani[name];
            if (forward)
            {
                clip.time = 0;
                clip.speed = 1;
            }
            else
            {
                clip.time = clip.length;
                clip.speed = -1;
            }
            ani.Play(name);
        }
    }
    //public class EnableClass 
    //{
    //    public UISprite[] sprites = null;
    //    public Collider[] colliders = null;
    //    public UITexture[] textures = null;

    //    public void Reset()
    //    {
    //        if(sprites != null)
    //        {
    //            foreach (UISprite item in sprites)
    //            {
    //                item.color = Color.white;
    //            }
    //        }
    //        if (colliders != null)
    //        {
    //            foreach(Collider item in colliders)
    //            {
    //                item.enabled = true;
    //            }
    //        }
    //        if (textures != null)
    //        {
    //            foreach (var item in textures)
    //            {
    //                item.color = Color.white;
    //            }
    //        }
    //    }
    //}
}

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