Esempio n. 1
0
        /// <summary>
        /// Resets positions of an list item according to its index
        /// </summary>
        /// <param name="item">item to be reset</param>
        private void ResetItemPosition(ListItemBase item)
        {
            float position = item.Index * (itemSize + spacing) + itemSize / 2;

            var rectTransform = item.gameObject.GetComponent <RectTransform>();

            switch (scrollOrientation)
            {
            case ScrollOrientation.HORIZONTAL:
                rectTransform.anchorMin        = new Vector2(0, 0);
                rectTransform.anchorMax        = new Vector2(0, 1);
                rectTransform.anchoredPosition = new Vector2(position, 0);
                rectTransform.offsetMin        = new Vector2(rectTransform.offsetMin.x, 0);
                rectTransform.offsetMax        = new Vector2(rectTransform.offsetMax.x, 0);
                break;

            case ScrollOrientation.VERTICAL:
                rectTransform.anchorMin        = new Vector2(0, 1);
                rectTransform.anchorMax        = new Vector2(1, 1);
                rectTransform.anchoredPosition = new Vector2(0, -position);
                rectTransform.offsetMin        = new Vector2(0, rectTransform.offsetMin.y);
                rectTransform.offsetMax        = new Vector2(0, rectTransform.offsetMax.y);
                break;
            }
        }
Esempio n. 2
0
        private void RecycleItem(ScrollDirection direction)
        {
            ListItemBase firstItem = itemsList[0];
            ListItemBase lastItem  = itemsList[itemsList.Count - 1];

            float targetPosition = (itemSize + spacing);

            switch (direction)
            {
            case ScrollDirection.NEXT:

                switch (scrollOrientation)
                {
                case ScrollOrientation.HORIZONTAL:
                    firstItem.Position =
                        new Vector2(lastItem.Position.x + targetPosition, firstItem.Position.y);
                    break;

                case ScrollOrientation.VERTICAL:
                    firstItem.Position =
                        new Vector2(firstItem.Position.x, lastItem.Position.y - targetPosition);
                    break;
                }

                firstItem.Index = lastItemIndex;
                firstItem.transform.SetAsLastSibling();

                itemsList.RemoveAt(0);
                itemsList.Add(firstItem);

                ItemLoaded(firstItem);
                break;

            case ScrollDirection.PREVIOUS:

                switch (scrollOrientation)
                {
                case ScrollOrientation.HORIZONTAL:
                    lastItem.Position = new Vector2(firstItem.Position.x - targetPosition, lastItem.Position.y);
                    break;

                case ScrollOrientation.VERTICAL:
                    lastItem.Position = new Vector2(lastItem.Position.x, firstItem.Position.y + targetPosition);
                    break;
                }

                lastItem.Index = lastItemIndex - itemsList.Count;
                lastItem.transform.SetAsFirstSibling();

                itemsList.RemoveAt(itemsList.Count - 1);
                itemsList.Insert(0, lastItem);

                ItemLoaded(lastItem);
                break;
            }

            Canvas.ForceUpdateCanvases();
        }
Esempio n. 3
0
        protected void HandleOnItemLoadedHandler(ListItemBase item)
        {
            if (item == selectedItem)
            {
                selectedItem.Select(selectedIndex == selectedItem.Index);
            }

            item.OnLoad(Data[item.Index]);
        }
Esempio n. 4
0
        public void ItemSelected(ListItemBase item, bool clear = false)
        {
            if (OnItemSelected != null)
            {
                OnItemSelected(item);

                if (clear)
                {
                    OnItemSelected = null;
                }
            }
        }
Esempio n. 5
0
        protected void HandleOnItemSelectedHandler(ListItemBase item)
        {
            if (selectedItem != null)
            {
                selectedItem.Select(false);
            }

            selectedItem = (FriendListItem)item;
            selectedItem.Select(true);

            selectedIndex = selectedItem.Index;

            listener.OnContactSelected(selectedIndex);
        }
Esempio n. 6
0
        private ListItemBase CreateNewItem(ListItemBase prefab, int index)
        {
            GameObject instance = Instantiate(prefab.gameObject, Vector3.zero, Quaternion.identity);

            instance.transform.SetParent(content.transform);
            instance.transform.localScale = Vector3.one;
            instance.SetActive(true);

            var item = instance.GetComponent <ListItemBase>();

            item.Index = index;

            ResetItemPosition(item);

            return(item);
        }
Esempio n. 7
0
        public void InsertItem(int index, ListItemBase listItemPrefab)
        {
            if (itemsTotal < 2 * itemsVisible)
            {
                ListItemBase item = CreateNewItem(listItemPrefab, index);
                item.OnSelected = HandleOnSelectedHandler;

                itemsList.Insert(index, item);
                ReindexItems(index);

                ItemLoaded(item);
            }

            itemsTotal++;
            lastItemIndex = itemsList.Count - 1;
            itemsToRecycleAfter++;

            ResizeContentToFitItems();
        }
Esempio n. 8
0
        public void AddItem(ListItemBase listItemPrefab)
        {
            if (itemsTotal < 2 * itemsVisible)
            {
                int          i    = itemsList.Count;
                ListItemBase item = CreateNewItem(listItemPrefab, i);
                item.OnSelected = HandleOnSelectedHandler;

                itemsList.Add(item);

                ItemLoaded(item);
            }

            itemsTotal++;
            lastItemIndex = itemsList.Count - 1;
            itemsToRecycleAfter++;

            ResizeContentToFitItems();
        }
Esempio n. 9
0
 void HandleOnSelectedHandler(ListItemBase item)
 {
     ItemSelected(item);
 }
Esempio n. 10
0
        public void Create(int items, ListItemBase listItemPrefab)
        {
            switch (scrollOrientation)
            {
            case ScrollOrientation.HORIZONTAL:
                scrollRect.vertical   = false;
                scrollRect.horizontal = true;

                content.anchorMin = new Vector2(0, 0);
                content.anchorMax = new Vector2(0, 1);

                if (fitItemToViewport)
                {
                    listItemPrefab.Size = new Vector2(viewport.rect.width, listItemPrefab.Size.y);
                }

                itemSize = listItemPrefab.Size.x;

                content.sizeDelta = new Vector2(itemSize * items + spacing * (items - 1), 0);
                break;

            case ScrollOrientation.VERTICAL:
                scrollRect.vertical   = true;
                scrollRect.horizontal = false;

                content.anchorMin = new Vector2(0, 1);
                content.anchorMax = new Vector2(1, 1);

                if (fitItemToViewport)
                {
                    listItemPrefab.Size = new Vector2(listItemPrefab.Size.x, viewport.rect.height);
                }

                itemSize = listItemPrefab.Size.y;

                content.sizeDelta = new Vector2(0, itemSize * items + spacing * (items - 1));
                break;
            }

            if (centerOnItem)
            {
                scrollRect.inertia = false;
            }


            itemsVisible = Mathf.CeilToInt(GetViewportSize() / itemSize);

            int itemsToInstantiate = itemsVisible;

            if (itemsVisible == 1)
            {
                itemsToInstantiate = 5;
            }
            else if (itemsToInstantiate < items)
            {
                itemsToInstantiate *= 2;
            }

            if (itemsToInstantiate > items)
            {
                itemsToInstantiate = items;
            }

            itemsList = new List <ListItemBase>();

            for (int i = 0; i < itemsToInstantiate; i++)
            {
                ListItemBase item = CreateNewItem(listItemPrefab, i);
                item.OnSelected = HandleOnSelectedHandler;
                item.Index      = i;

                itemsList.Add(item);

                ItemLoaded(item);
            }

            itemsTotal = items;

            lastItemIndex = itemsList.Count - 1;

            itemsToRecycleAfter = itemsList.Count - itemsVisible;


            scrollRect.onValueChanged.AddListener(position =>
            {
                if (!centerOnItem)
                {
                    Recycle();
                }
            });
        }