Exemple #1
0
    private void DestroyWindowObject(WindowContextBase context)
    {
        mWindowContextDic.Remove(context.id);

        GameObject go = GetObject(context);

        DestroyGameObject(go);

        mWindowObjectDic.Remove(context.id);

        var w = context as WindowContext;

        if (w != null)
        {
            using (var it = w.widgets.GetEnumerator())
            {
                while (it.MoveNext())
                {
                    DestroyWindow(it.Current.Value);
                }
            }
        }

        context.Clear();
    }
Exemple #2
0
 public bool IsActive(WindowContextBase context)
 {
     if (context == null)
     {
         return(false);
     }
     return(context.active && mWindowContextDic.ContainsKey(context.id));
 }
Exemple #3
0
 public GameObject GetObject(WindowContextBase context)
 {
     if (context == null)
     {
         return(null);
     }
     return(GetObject(context.id));
 }
Exemple #4
0
    private Canvas GetCanvas(WindowContextBase context)
    {
        GameObject go = GetObject(context);

        if (go != null)
        {
            return(go.GetComponent <Canvas>());
        }
        return(null);
    }
Exemple #5
0
    public override void CopyFrom(WindowContextBase context)
    {
        base.CopyFrom(context);
        WidgetContext widget = context as WidgetContext;

        if (widget != null)
        {
            defaultParam.CopyFrom(widget.defaultParam);
        }
    }
Exemple #6
0
    public void Close(string name)
    {
        WindowContextBase context = GetWindowContextBase(name);

        if (context == null)
        {
            return;
        }
        Close(context);
    }
Exemple #7
0
    private void DestroyWindow(WindowContextBase context)
    {
        if (context == null)
        {
            return;
        }
        SetActive(context, false);

        if (context.closeDestroy)
        {
            DestroyWindowObject(context);
        }
    }
Exemple #8
0
    public virtual void Close(bool self = false)
    {
        WindowContextBase windowContext = contextbase;

        if (self == false)
        {
            if (widget != null)
            {
                windowContext = widget.parent;
            }
        }

        WindowManager.Instance.Close(windowContext);
    }
Exemple #9
0
    public override void CopyFrom(WindowContextBase context)
    {
        base.CopyFrom(context);
        WindowContext windowContext = context as WindowContext;

        if (windowContext != null)
        {
            fixedOrder = windowContext.fixedOrder;
            hideOther  = windowContext.hideOther;

            if (windowContext.fixedWidgets != null)
            {
                fixedWidgets = new Dictionary <string, WidgetContext>(windowContext.fixedWidgets);
            }
        }
    }
Exemple #10
0
    public virtual void CopyFrom(WindowContextBase context)
    {
        name         = context.name;
        path         = context.path;
        closeDestroy = context.closeDestroy;
        if (context.components != null)
        {
            components = new Dictionary <Type, Dictionary <string, string> >();

            using (var it = context.components.GetEnumerator())
            {
                while (it.MoveNext())
                {
                    Dictionary <string, string> parameters = new Dictionary <string, string>(it.Current.Value);
                    components.Add(it.Current.Key, parameters);
                }
            }
        }
    }
Exemple #11
0
    private void AddComponent(GameObject go, WindowContextBase context)
    {
        if (Application.isPlaying == false)
        {
            return;
        }
        if (go == null)
        {
            return;
        }
        if (context.components != null)
        {
            using (var it = context.components.GetEnumerator())
            {
                while (it.MoveNext())
                {
                    var type = it.Current.Key;

                    WindowComponent component = go.GetComponent(type) as WindowComponent;
                    if (component == null)
                    {
                        component = go.AddComponent(type) as WindowComponent;
                    }
                    if (component != null)
                    {
                        component.contextbase = context;
                        component.parameters  = it.Current.Value;
                    }
                }
            }
        }
        var components = go.GetComponentsInChildren <WindowComponent>();

        for (int i = 0; i < components.Length; ++i)
        {
            components[i].contextbase = context;
        }
        for (int i = 0; i < components.Length; ++i)
        {
            components[i].OnInit();
        }
    }
Exemple #12
0
    public void Open(string name, Action <GameObject> callback = null, params string[] widgets)
    {
        WindowContextBase context = GetWindowContextBase(name);

        if (context == null)
        {
            return;
        }

        WidgetContext[] widgetContexts = null;
        if (widgets != null)
        {
            widgetContexts = new WidgetContext[widgets.Length];
            for (int i = 0; i < widgets.Length; ++i)
            {
                widgetContexts[i] = GetWidgetContext(widgets[i]);
            }
        }
        Open(context, callback, widgetContexts);
    }
Exemple #13
0
    public void Close(WindowContextBase context)
    {
        if (context == null)
        {
            return;
        }
        if (context.type == WindowType.Normal)
        {
            int index = mWindowStack.FindIndex((nav) => { return(nav.windowState.context == context); });
            if (index >= 0 && index < mWindowStack.Count)
            {
                WindowNav current = mWindowStack[index];

                WindowNav previous      = null;
                int       previousIndex = index + 1;
                if (previousIndex < mWindowStack.Count)
                {
                    previous = mWindowStack[previousIndex];
                }

                mWindowStack.RemoveAt(index);
                if (mWindowStack.FindIndex((nav) => { return(nav.windowState.context == context); }) < 0)
                {
                    DestroyWindow(context);
                }
                else
                {
                    SetActive(context, false);
                }
                mTempList.Clear();
                if (current != null && current.hideWindowStates != null)
                {
                    for (int i = 0; i < current.hideWindowStates.Count; ++i)
                    {
                        SetActive(current.hideWindowStates[i].context, true, current.hideWindowStates[i].widgetsActive);
                        mTempList.Add(current.hideWindowStates[i].context.id);
                    }
                }

                if (previous != null)
                {
                    if (previous.windowState.context.hideOther == false && previousIndex < mWindowStack.Count)
                    {
                        var previousPrevious = mWindowStack[previousIndex];

                        if (mTempList.Contains(previousPrevious.windowState.context.id))
                        {
                            SetActive(previousPrevious.windowState.context, true, previousPrevious.windowState.widgetsActive);
                        }
                    }
                    if (mTempList.Contains(previous.windowState.context.id) == false)
                    {
                        SetActive(previous.windowState.context, true, previous.windowState.widgetsActive);
                    }
                }
                mTempList.Clear();
            }
            else
            {
                DestroyWindow(context);
            }
        }
        else
        {
            DestroyWindow(context);
        }
    }
Exemple #14
0
    private void SetOrder(WindowContextBase context)
    {
        if (context == null)
        {
            return;
        }

        var canvas = GetCanvas(context);

        if (canvas == null)
        {
            return;
        }
        var widget = context as WidgetContext;

        if (widget != null)
        {
            var parent = GetCanvas(widget.parent);
            if (parent != null)
            {
                canvas.sortingOrder = parent.sortingOrder + widget.sortingOrderOffset;
            }
        }

        else
        {
            var window = context as WindowContext;
            //固定层级
            if (window != null && window.fixedOrder != 0)
            {
                canvas.sortingOrder = window.fixedOrder;
            }
            else
            {
                int maxOrder = int.MinValue;
                using (var it = mWindowContextDic.GetEnumerator())
                {
                    while (it.MoveNext())
                    {
                        var v = it.Current.Value as WindowContext;
                        if (v != null)
                        {
                            var c = GetCanvas(v);
                            if (c != null && v.fixedOrder == 0)
                            {
                                if (c.sortingOrder > maxOrder)
                                {
                                    maxOrder = c.sortingOrder;
                                }
                            }
                        }
                    }
                }
                if (maxOrder == int.MinValue)
                {
                    maxOrder = 0;
                }
                else
                {
                    maxOrder += mOrderAddition;
                }
                canvas.sortingOrder = maxOrder;
            }
        }
    }
Exemple #15
0
    public void Draw(Rect rect)
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label("搜索", GUILayout.Width(30), GUILayout.Height(30));
        mKey = GUILayout.TextField(mKey, GUILayout.Width(200), GUILayout.Height(30));
        if (string.IsNullOrEmpty(mKey) == false)
        {
            mKey = mKey.Trim().ToLower();

            if (GUILayout.Button("Reset", GUILayout.Width(60), GUILayout.Height(30)))
            {
                mKey = null;
            }
        }

        GUILayout.EndHorizontal();

        GUILayout.Space(10);

        mScroll = GUILayout.BeginScrollView(mScroll, false, true);
        float width = 200f;
        int   count = (int)(rect.width / width);

        DrawItems(count, contexts.GetEnumerator(), (current) =>
        {
            WindowContextBase val = current.Value;
            if (val != null)
            {
                bool visible = true;
                if (string.IsNullOrEmpty(mKey) == false)
                {
                    if (val.name.ToLower().Contains(mKey) == false)
                    {
                        visible = false;
                    }
                }
                if (visible)
                {
                    bool open              = GetObject(val);
                    GUIStyle style         = GUI.skin.button;
                    style.normal.textColor = open ? Color.yellow : Color.white;

                    if (GUILayout.Button(val.name, style, GUILayout.Width(width), GUILayout.Height(30)))
                    {
                        if (!open)
                        {
                            Open(val);
                        }
                        else
                        {
                            Close(val);
                        }
                    }
                }
            }
        });

        if (mMenuList == null)
        {
            mMenuList = new Dictionary <WindowMenuAttribute, Action>();
            var types = GetType().Assembly.GetTypes();
            foreach (var type in types)
            {
                MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

                for (int i = 0; methods != null && i < methods.Length; ++i)
                {
                    var method = methods[i];

                    // only find static functions
                    if (method.IsStatic)
                    {
                        WindowMenuAttribute attribute = method.GetCustomAttribute(typeof(WindowMenuAttribute), true) as WindowMenuAttribute;
                        if (attribute != null)
                        {
                            mMenuList.Add(attribute, (Action)Delegate.CreateDelegate(typeof(Action), method));
                        }
                    }
                }
            }
        }

        if (mMenuList.Count > 0)
        {
            GUILayout.Space(10);
            DrawItems(count, mMenuList.GetEnumerator(), (current) => {
                GUIStyle style  = GUI.skin.button;
                Action callback = current.Value;
                if (GUILayout.Button(current.Key.Title, style, GUILayout.Width(width), GUILayout.Height(30)))
                {
                    if (callback != null)
                    {
                        callback();
                    }
                }
            });
        }


        GUILayout.EndScrollView();
    }
Exemple #16
0
    public void SetActive(WindowContextBase context, bool active, Dictionary <ulong, bool> widgetsActive = null)
    {
        if (context == null)
        {
            return;
        }
        var canvas = GetCanvas(context);

        if (active)
        {
            SetOrder(context);
        }
        else
        {
            if (canvas != null)
            {
                canvas.sortingOrder = 0;
            }
        }
        GameObject go = GetObject(context);

        if (context.active != active)
        {
            context.active = active;
            if (canvas != null)
            {
                canvas.enabled = context.active;
            }

            SetComponentActive(go, active);
        }
        if (context.type == WindowType.Normal)
        {
            SetWidgetsActive(context as WindowContext, active, widgetsActive);
        }
        else
        {
            var widget = context as WidgetContext;
            if (widget != null)
            {
                var parent = GetObject(widget.parent);
                if (parent != null)
                {
                    Transform bindNode = parent.transform;
                    if (!string.IsNullOrEmpty(widget.bindNode))
                    {
                        bindNode = parent.transform.Find(widget.bindNode);
                    }

                    if (bindNode != go.transform.parent)
                    {
                        go.transform.SetParent(bindNode);
                        go.transform.localScale    = Vector3.one;
                        go.transform.localPosition = Vector3.zero;

                        RectTransform rect = go.transform as RectTransform;

                        rect.anchorMin = Vector2.zero;
                        rect.anchorMax = Vector2.one;
                        rect.offsetMin = Vector2.zero;
                        rect.offsetMax = Vector2.zero;

                        if (canvas != null)
                        {
                            canvas.overrideSorting = true;
                        }
                    }

                    var components = parent.GetComponentsInChildren <WindowComponent>();
                    for (int i = 0; i < components.Length; ++i)
                    {
                        var com = components[i];

                        if (com.contextbase != null && com.contextbase.id == widget.id)
                        {
                            continue;
                        }
                        if (active)
                        {
                            com.OnWidgetShow(widget);
                        }
                        else
                        {
                            com.OnWidgetHide(widget);
                        }
                    }
                }
            }
        }
    }
Exemple #17
0
    public void Open(WindowContextBase context, Action <GameObject> callback = null, params WidgetContext[] widgets)
    {
        if (mLoader == null)
        {
            Debug.Assert(mLoader != null, "Loader is null!!");
            callback?.Invoke(null);
            return;
        }

        if (context == null)
        {
            Debug.Assert(context != null, "Context is null");
            callback?.Invoke(null);
            return;
        }

        SetTouch(false);

        if (mWindowContextDic.ContainsKey(context.id) == false)
        {
            mWindowContextDic.Add(context.id, context);
        }


        if (widgets != null && widgets.Length > 0)
        {
            Debug.Assert(context.type == WindowType.Normal, string.Format("{0} type = {1}", context.name, context.type));
            var it = widgets.GetEnumerator();
            while (it.MoveNext())
            {
                WidgetContext widget = it.Current as WidgetContext;
                if (widget != null)
                {
                    widget.parent = context as WindowContext;
                }
            }
        }

        GameObject go = GetObject(context);

        if (go == null)
        {
            if (context.status == WindowStatus.Loading)
            {
                return;
            }
            context.status = WindowStatus.Loading;
            mLoader(context.path, (asset) =>
            {
                SetTouch(true);

                if (asset == null || context.status == WindowStatus.None)
                {
                    if (asset == null)
                    {
                        Debug.LogError("Can't load ui with path= " + context.path);
                    }
                    if (context.status == WindowStatus.None)
                    {
                        Debug.LogError("context.name=" + context.name + " status=" + context.path);
                    }
                    mWindowContextDic.Remove(context.id);
                    context.Clear();
                    return;
                }


                context.status = WindowStatus.Done;
                if (Application.isPlaying)
                {
                    go = Instantiate(asset) as GameObject;
                }
                else
                {
#if UNITY_EDITOR
                    go = UnityEditor.PrefabUtility.InstantiatePrefab(asset) as GameObject;
#else
                    return;
#endif
                }
                go.name = System.IO.Path.GetFileNameWithoutExtension(context.path);
                if (mWindowObjectDic.ContainsKey(context.id))
                {
                    DestroyGameObject(mWindowObjectDic[context.id]);
                    mWindowObjectDic[context.id] = go;
                }
                else
                {
                    mWindowObjectDic.Add(context.id, go);
                }
                AddComponent(go, context);

                go.TryGetComponent(out Canvas canvas);
                if (canvas == null)
                {
                    canvas = go.AddComponent <Canvas>();
                }
                go.TryGetComponent(out CanvasScaler scaler);

                go.transform.SetParent(transform);
                bool isScale = context.type == WindowType.Normal;
                if (isScale == false)
                {
                    var widget = context as WidgetContext;
                    if (widget.parent == null)
                    {
                        isScale = true;
                    }
                }

                if (isScale)
                {
                    if (scaler == null)
                    {
                        scaler = go.AddComponent <CanvasScaler>();
                    }

                    canvas.renderMode       = RenderMode.ScreenSpaceCamera;
                    canvas.worldCamera      = mCamera;
                    canvas.sortingLayerName = "UI";
                    canvas.overrideSorting  = false;

                    scaler.scaleFactor            = 1;
                    scaler.uiScaleMode            = CanvasScaler.ScaleMode.ScaleWithScreenSize;
                    scaler.screenMatchMode        = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
                    scaler.referenceResolution    = DesignResolution;
                    scaler.referencePixelsPerUnit = 100;
                }
                else
                {
                    if (scaler != null)
                    {
                        DestroyImmediate(scaler);
                    }
                }
                go.SetActive(true);

                Push(context as WindowContext);

                SetActive(context, true);

                callback?.Invoke(go);
            });
        }
        else
        {
            context.status = WindowStatus.Done;

            Push(context as WindowContext);

            SetActive(context, true);
            SetTouch(true);

            callback?.Invoke(go);
        }
    }