Пример #1
0
 void ElementClickedHandler(BaseListElement <T> element)
 {
     if (OnElementClicked != null)
     {
         OnElementClicked(element);
     }
 }
Пример #2
0
        protected override void Awake()
        {
            base.Awake();

            _rectTransform = transform as RectTransform;
            _parentCanvas  = GetComponentInParent <Canvas>();

            if (_listComponent != null)
            {
                _listComponent.OnElementClicked += SelectElement;
            }

            if (_selectedElementPrefab != null)
            {
                GameObject go = Instantiate(_selectedElementPrefab) as GameObject;
                _selectedElement = go.GetComponent <BaseListElement <T> >();
                if (_selectedElement == null)
                {
                    Destroy(go);
                    Debug.LogWarning("Cannot create selected element, BaseListElement<T> component not found on prefab.");
                    return;
                }

                go.transform.SetParent(_selectedElementRect, false);

                Button button = go.GetComponent <Button>();
                if (button != null)
                {
                    button.onClick.AddListener(() => { SelectedElementClickedHandler(); });
                }

                //TODO -
                //constrain seleted element widget in rect bounds ?
            }
        }
Пример #3
0
        public void Insert(int index, T item)
        {
            BaseListElement <T> widget = CreateElement(index);

            widget.Data = item;

            _widgetsList.Insert(index, widget);
            _dataList.Insert(index, item);
        }
Пример #4
0
        /// <summary>
        /// Creates a list element's widget
        /// </summary>
        /// <param name="index">index of the element</param>
        BaseListElement <T> CreateElement(int index = -1)
        {
            if (listElementPrefab == null)
            {
                return(null);
            }

            GameObject          go        = Instantiate(listElementPrefab) as GameObject;
            BaseListElement <T> component = go.GetComponent <BaseListElement <T> >();

            if (component == null)
            {
                Destroy(go);
                Debug.LogWarning("Cannot create list element, BaseListElement<T> component not found on prefab.");
                return(null);
            }

            ScrollRect scrollrect = GetComponentInChildren <ScrollRect>();

            if (scrollrect == null)
            {
                go.transform.SetParent(transform, false);
            }
            else
            {
                go.transform.SetParent(scrollrect.content, false);
            }

            if (index >= 0)
            {
                SetElementIndex(index, go.transform as RectTransform);
            }

            Button button = go.GetComponent <Button>();

            if (button != null)
            {
                button.onClick.AddListener(() => { ElementClickedHandler(component); });
            }

            if (scrollrect != null)
            {
                if (scrollrect.content.sizeDelta.y < GetComponent <RectTransform>().sizeDelta.y)
                {
                    scrollrect.content.sizeDelta = new Vector2(GetComponentInChildren <ScrollRect>().content.sizeDelta.x, GetComponent <RectTransform>().sizeDelta.y);
                    if (scrollrect.verticalScrollbar != null)
                    {
                        scrollrect.verticalScrollbar.value = 0;
                    }
                }
            }

            InitializeElement(component);

            return(component);
        }
Пример #5
0
        /// <summary>
        /// Destroys a list element's widget at the given index.
        /// </summary>
        void RemoveElement(int index)
        {
            BaseListElement <T> widget = _widgetsList[index];

            if (widget != null)
            {
                Button button = widget.GetComponent <Button>();
                if (button != null)
                {
                    button.onClick.RemoveAllListeners();
                }

                widget.transform.SetParent(null);
                Destroy(widget.gameObject);

                _widgetsList.RemoveAt(index);
            }
        }
Пример #6
0
 /// <summary>
 /// Sets the selected element widget's data.
 /// </summary>
 void SelectElement(BaseListElement <T> element)
 {
     Value = element.Data;
 }
Пример #7
0
 /// <summary>
 /// Override to add initialization logic to the widget creation. Called only once when the widget is created. If the object is pooled, the initialization will not be called again when the Data changes.
 /// </summary>
 /// <param name="widget">A reference to the BaseListElement<T> that is being created</param>
 protected virtual void InitializeElement(BaseListElement <T> widget)
 {
 }