Example #1
0
        public void Push <TWidget>(string widgetName, UIMessage message, Action <int> onCreated)
            where TWidget : Widget
        {
            var id = GetId();

            GetInstance <TWidget>(widgetName, id, message, instance =>
            {
                var parent = LayerLookup[instance.Layer];
                instance.transform.SetParent(parent.transform, false);
//                instance.transform.SetAsFirstSibling();

                if (instance.Layer == UILayer.Popup)
                {
                    Popups.Add(instance.Id);
                }

                if (instance.Layer == UILayer.Fixed)
                {
                    Fixes.Add(instance.Id);
                }

                if (StackedWindows.Count > 0)
                {
                    var prevWidget = StackedWindows.Peek();
                    RunCoroutine(prevWidget.OnFreeze(), () =>
                    {
                        prevWidget.TriggerOnFreezeEvent();

                        // Window will overlay previous windows.
                        if (instance.Layer == UILayer.Window && WindowsInDisplay.Contains(prevWidget.Id))
                        {
                            WindowsInDisplay.Remove(prevWidget.Id);
                        }
                    });
                    RunCoroutine(instance.OnShow(), () =>
                    {
                        instance.TriggerOnShowEvent();
                        onCreated?.Invoke(id);

                        StackedWindows.Push(instance);
                        WidgetLookup.Add(id, instance);
                        WindowsInDisplay.Add(id);
                    });
                }
                else
                {
                    RunCoroutine(instance.OnShow(), () =>
                    {
                        instance.TriggerOnShowEvent();
                        onCreated?.Invoke(id);

                        StackedWindows.Push(instance);
                        WidgetLookup.Add(id, instance);
                        WindowsInDisplay.Add(id);
                    });
                }
            });
        }
Example #2
0
        private void GetInstance <T>(string widgetPath, int assignedId, UIMessage message, Action <T> onCreated)
            where T : Widget
        {
            var resolveType = typeof(T);

            if (PoolingWidgets.ContainsKey(widgetPath))
            {
                var pool = PoolingWidgets[widgetPath];
                if (pool.Count > 0)
                {
                    var instance = pool.Pop();

                    if (instance.Controller != null)
                    {
                        try
                        {
                            instance.Controller?.SetControllerInfo(instance, this, message);
                            instance.Controller?.Initialize();
                        }
                        catch (Exception ex) { Debug.LogException(ex); }
                    }

                    onCreated.Invoke(instance as T);
                    return;
                }
            }

            var useSpecifiedFactory = false;

            if (!FactoryLookup.TryGetValue(resolveType, out var factory))
            {
                while (factory == null && resolveType.BaseType != null)
                {
                    resolveType = resolveType.BaseType;
                    FactoryLookup.TryGetValue(resolveType, out factory);
                }

                if (factory == null)
                {
                    Debug.LogError($"Widget factory not found for type: {typeof(T)}, no fallback.");
                    return;
                }
            }
            else
            {
                useSpecifiedFactory = true;
            }

            // fallback
            if (useSpecifiedFactory)
            {
                var specifiedFactory = factory as IWidgetFactory <T>;
                specifiedFactory?.CreateInstance(this, widgetPath, assignedId, message, onCreated);
            }
            else
            {
                factory.CreateInstance(this, widgetPath, assignedId, message,
                                       widgetCreated =>
                {
                    var genericWidget = widgetCreated as T;
                    if (genericWidget == null)
                    {
                        Debug.LogWarningFormat("Can not convert [{0}] to type: {1}", widgetCreated, typeof(T));
                    }

                    onCreated.Invoke(genericWidget);
                });
            }
        }
Example #3
0
 public void Push <TWidget>(string widgetName, UIMessage message) where TWidget : Widget
 {
     Push <TWidget>(widgetName, message, null);
 }
Example #4
0
 public void Push <TWidget>(UIMessage message, Action <int> onCreated) where TWidget : Widget
 {
     Push <TWidget>(null, UIMessage.Empty, null);
 }
Example #5
0
 public void Push <TWidget>(UIMessage message) where TWidget : Widget
 {
     Push <TWidget>(null, message, null);
 }
Example #6
0
 public void Push(string widgetName, UIMessage message, Action <int> onCreated)
 {
     Push <Widget>(widgetName, message, onCreated);
 }
Example #7
0
 public void Push(string widgetName, UIMessage message)
 {
     Push <Widget>(widgetName, message, null);
 }
Example #8
0
        public void CreateInstance(IUIManager manager, string widgetPath, int assignedId, UIMessage message,
                                   Action <Widget> onCreated)
        {
            if (!_caches.ContainsKey(widgetPath))
            {
                for (var i = 0; i < _databases.Count; i++)
                {
                    var database = _databases[i];
                    if (!database.Value.ContainsKey(widgetPath))
                    {
                        continue;
                    }

                    var result = database.Value[widgetPath];
                    _caches.Add(widgetPath, result);
                }
            }


            if (!_caches.ContainsKey(widgetPath))

            {
                Debug.LogError($"Can't found widget@{widgetPath}");
                return;
            }

            var widgetPrefab = _caches[widgetPath];

            if (widgetPrefab == null)
            {
                for (var i = 0; i < _databases.Count; i++)
                {
                    var database = _databases[i];
                    if (!database.Value.ContainsKey(widgetPath))
                    {
                        continue;
                    }

                    Debug.LogError(
                        $"Can't instantiate widget[{widgetPath}], it reference a [NULL] prefab in database[{database.name}].");
                }

                return;
            }

            var instance = Object.Instantiate(widgetPrefab);

            instance.SetManagerInfo(assignedId, widgetPath, manager);
            var instanceType = instance.GetType();

            if (_controllerRef.ContainsKey(instanceType))
            {
                var controllerType     = _controllerRef[instanceType];
                var controllerInstance =
                    Activator.CreateInstance(controllerType) as IWidgetController;

                if (controllerInstance == null)
                {
                    Debug.LogError($"Create [{controllerType}] instance failed.");
                    return;
                }

                _container.Inject(controllerInstance);
                try
                {
                    controllerInstance.SetControllerInfo(instance, manager, message);
                    controllerInstance.Initialize();
                }
                catch (Exception e) { Debug.LogException(e); }

                // cache reference
                instance.Controller = controllerInstance;
            }

            onCreated?.Invoke(instance);
        }