Exemplo n.º 1
0
        private Task Render_List()
        {
            return(new Task()
            {
                Mission = "Render_List",
                Action = (seekTo, currentTask, nextTask, delivery) =>
                {
                    if (Material.Component is UIScrollable)
                    {
                        Material.Component.Type = UIType.LIST;

                        UIScrollable list = Material.Component as UIScrollable;
                        UIIdleList idle = list.Idle as UIIdleList;

                        list.Rect.anchoredPosition = new Vector2(idle.Left - idle.Right, idle.Bottom - idle.Top);
                        list.Rect.sizeDelta = new Vector2(idle.Width, idle.Height);

                        list.Background = list.Object.AddComponent <RawImage>();
                        list.Background.texture = Media.texSolid;
                        list.Background.color = idle.BackgroundColor;

                        list.Container = new UIContainer();
                        list.Container.Object = new GameObject(list.Name + "Container");
                        list.Container.Object.transform.parent = list.Object.transform;

                        list.Container.Rect = list.Container.Object.AddComponent <RectTransform>();
                        list.Container.Rect.anchorMin = new Vector2(0.5f, 1);
                        list.Container.Rect.anchorMax = new Vector2(0.5f, 1);
                        list.Container.Rect.pivot = new Vector2(0.5f, 1);
                        list.Container.Rect.sizeDelta = new Vector2(0, 0);
                        list.Container.Rect.anchoredPosition = new Vector2(0, 0);

                        UIScroll scroll = list.Object.AddComponent <UIScroll>();
                        scroll.vertical = true;
                        scroll.horizontal = false;
                        scroll.movementType = UIScroll.MovementType.Elastic;
                        scroll.content = list.Container.Rect;
                        scroll.elasticity = 0.1f;
                        scroll.decelerationRate = 0.005f;
                        scroll.scrollSensitivity = 0.0001f;
                        scroll.scrollFactor = 2f;

                        scroll.gameObject.AddComponent <Mask>();

                        new EventRegistrar()
                        {
                            Material = new EventRegistrarMaterial()
                            {
                                Listener = new EventListener()
                                {
                                    Owner = list.Name,
                                    Name = list.Name + "OnScrollUp",
                                    Enabled = true,
                                    Event = () =>
                                    {
                                        return (list.Container.Rect.anchoredPosition.y > list.ScrollUp);
                                    },
                                    OnFired = (listener) =>
                                    {
                                        list.ScrollingUp = true;
                                        list.WasScrollingUp = true;
                                        list.WasScrollingDown = false;

                                        if (!list.ScrollUpCalled)
                                        {
                                            list.ScrollUpCalled = true;

                                            if (list.OnScrollUp != null)
                                            {
                                                list.OnScrollUp(list);
                                            }
                                        }

                                        if (list.OnScrollingUp != null)
                                        {
                                            list.OnScrollingUp(list);
                                        }
                                    },
                                    OnMiss = (listener) =>
                                    {
                                        list.ScrollingUp = false;
                                        list.ScrollUpCalled = false;

                                        if (list.OnNoScrollingUp != null)
                                        {
                                            list.OnNoScrollingUp(list);
                                        }
                                    },
                                    OnEither = (listener) =>
                                    {
                                        list.ScrollUp = list.Container.Rect.anchoredPosition.y;
                                    }
                                }
                            }
                        }
                        .Register();

                        new EventRegistrar()
                        {
                            Material = new EventRegistrarMaterial()
                            {
                                Listener = new EventListener()
                                {
                                    Owner = list.Name,
                                    Name = list.Name + "OnScrollDown",
                                    Enabled = true,
                                    Event = () =>
                                    {
                                        return (list.Container.Rect.anchoredPosition.y < list.ScrollDown);
                                    },
                                    OnFired = (listener) =>
                                    {
                                        list.ScrollingDown = true;
                                        list.WasScrollingDown = true;
                                        list.WasScrollingUp = false;

                                        if (!list.ScrollDownCalled)
                                        {
                                            list.ScrollDownCalled = true;

                                            if (list.OnScrollDown != null)
                                            {
                                                list.OnScrollDown(list);
                                            }
                                        }

                                        if (list.OnScrollingDown != null)
                                        {
                                            list.OnScrollingDown(list);
                                        }
                                    },
                                    OnMiss = (listener) =>
                                    {
                                        list.ScrollingDown = false;
                                        list.ScrollDownCalled = false;

                                        if (list.OnNoScrollingDown != null)
                                        {
                                            list.OnNoScrollingDown(list);
                                        }
                                    },
                                    OnEither = (listener) =>
                                    {
                                        list.ScrollDown = list.Container.Rect.anchoredPosition.y;
                                    }
                                }
                            }
                        }
                        .Register();

                        if (list.OnRefresh != null)
                        {
                            new EventRegistrar()
                            {
                                Material = new EventRegistrarMaterial()
                                {
                                    OnTouchListener = new OnTouchListener()
                                    {
                                        Owner = list.Name,
                                        Target = list.Name,
                                        Enabled = true,
                                        Released = true,
                                        OnRelease = (go) =>
                                        {
                                            if (list.Container.Rect.anchoredPosition.y < -Screen.height * 0.15f)
                                            {
                                                if (!list.Refreshing)
                                                {
                                                    list.Refreshing = true;

                                                    (Variables.UI["txtTopRight"] as UIText).Element.text = "Refreshing...";

                                                    new Suspender()
                                                    {
                                                        Suspension = 1f,
                                                        OnFinish = () =>
                                                        {
                                                            if (list.OnRefresh != null)
                                                            {
                                                                list.OnRefresh(list);
                                                            }
                                                        }
                                                    }
                                                    .Suspend();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            .Register();
                        }
                    }

                    nextTask(null);
                }
            });
        }
Exemplo n.º 2
0
        protected override void Job()
        {
            UIList <ItemType> list = Variables.UI[Material.List] as UIList <ItemType>;
            UIComponent       item;

            if (list.Items == null)
            {
                list.Items = new List <UIComponent>();
            }
            else
            {
                foreach (UIComponent component in list.Items)
                {
                    component.Destroy();
                }

                list.Items = new List <UIComponent>();
            }

            UIIdleList idle = list.Idle as UIIdleList;

            Action <int> renderItem = null;

            renderItem = (i) =>
            {
                if (i != idle.MaxItems && i < list.Data.Count)
                {
                    item      = list.OnPopulate(i, list.Data[i]);
                    item.Name = list.Name + "Item_" + i;
                    list.Items.Add(item);

                    new UIRenderer()
                    {
                        Material = new UIRendererMaterial()
                        {
                            Parent    = list.Container.Object,
                            Component = item
                        },
                        OnFinish = () => { renderItem(i + 1); }
                    }
                    .Render();
                }
                else
                {
                    float offset = 0, height = 0, prevHeight = 0, totalHeight = 0;

                    for (int j = 0; j < list.Items.Count; j++)
                    {
                        height = list.Items[j].Idle.Height + list.Items[j].Idle.Padding;

                        if (j == 0)
                        {
                            prevHeight = 0;
                            offset     = 0;
                        }
                        else
                        {
                            prevHeight = list.Items[j - 1].Rect.sizeDelta.y;
                            offset     = list.Items[j - 1].Rect.anchoredPosition.y - prevHeight;
                        }

                        list.Items[j].Rect.sizeDelta        = new Vector2(list.Items[j].Rect.sizeDelta.x, height);
                        list.Items[j].Rect.anchoredPosition = new Vector2(0, offset + list.Items[j].Idle.Bottom - list.Items[j].Idle.Top);
                    }

                    totalHeight = -offset + height + Screen.height * 0.25f;

                    list.Container.Rect.sizeDelta        = new Vector2(Screen.width, totalHeight);
                    list.Container.Rect.anchoredPosition = new Vector2(0, -list.Items[0].Rect.anchoredPosition.y - Screen.height * 0.01f);

                    if (OnFinish != null)
                    {
                        OnFinish();
                    }
                }
            };

            bool fromCache = false;

            string url = Material.ListUrl;

            if (Material.ListFields != null)
            {
                foreach (string key in Material.ListFields.Keys)
                {
                    url += "/" + Material.ListFields[key];
                }
            }

            if (Material.FromCache)
            {
                fromCache = Cache.Data.ToList().Exists(c => c.Key == url);
            }

            if (fromCache)
            {
                string json = Cache.Data[url];

                list.Data = Material.OnConversion(json);

                if (list.Data.Count != 0)
                {
                    renderItem(0);
                }
                else
                {
                    if (OnFinish != null)
                    {
                        OnFinish();
                    }
                }
            }
            else
            {
                Dictionary <string, string> fields = new Dictionary <string, string>();

                fields["appInstanceKey"] = Variables.App_Instance_Key;

                if (Material.ListFields != null)
                {
                    foreach (string key in Material.ListFields.Keys)
                    {
                        fields[key] = Material.ListFields[key];
                    }
                }

                new HttpPostRequestSender()
                {
                    Material = new HttpPostRequestSenderMaterial()
                    {
                        Url    = Material.ListUrl,
                        Fields = fields
                    },
                    OnSuccess = (www) =>
                    {
                        Cache.Data[url] = www.text;

                        list.Data = Material.OnConversion(www.text);

                        if (list.Data.Count != 0)
                        {
                            renderItem(0);
                        }
                        else
                        {
                            if (OnFinish != null)
                            {
                                OnFinish();
                            }
                        }
                    }
                }
                .Send();
            }

            if (list.OnEventInitialization != null)
            {
                foreach (EventListener listener in list.OnEventInitialization(list))
                {
                    if (!Events.Listeners.Exists(l => l.Name == listener.Name))
                    {
                        Events.Listeners.Add(listener);
                    }
                }
            }

            if (list.OnTouchInitialization != null)
            {
                foreach (OnTouchListener listener in list.OnTouchInitialization(list))
                {
                    if (!Events.OnTouch_Listeners.Exists(l => l.Target == listener.Target))
                    {
                        Events.OnTouch_Listeners.Add(listener);
                    }
                }
            }
        }