Ejemplo n.º 1
0
    //设置Item数据和坐标, forceRefresh 为true 则强制刷新
    private void SetItemData(UIScrollViewItemBase item, int index, bool forceRefresh)
    {
        Vector2 itemPos = CalculateItemFixPosByIndex(index);

        if (index < completelyIndex)
        {
            item.transform.localPosition = new Vector3(itemPos.x, itemPos.y, 0);
            item.Show();
            if (forceRefresh || (!forceRefresh && (item.index != index)))
            {
                if (index < itemCount)
                {
                    item.SetData(index);
                }
                else
                {
                    if (fillMode)
                    {
                        item.ResetData(index);
                    }
                    else
                    {
                        item.Hide();
                    }
                }
            }
        }
        else
        {
            item.Hide();
        }
    }
Ejemplo n.º 2
0
    static int set_onSelected(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UIScrollViewItemBase obj = (UIScrollViewItemBase)o;
            UIScrollViewItemBase.OnSelectedHandler arg0 = null;
            LuaTypes funcType2 = LuaDLL.lua_type(L, 2);

            if (funcType2 != LuaTypes.LUA_TFUNCTION)
            {
                arg0 = (UIScrollViewItemBase.OnSelectedHandler)ToLua.CheckObject(L, 2, typeof(UIScrollViewItemBase.OnSelectedHandler));
            }
            else
            {
                LuaFunction func = ToLua.ToLuaFunction(L, 2);
                arg0 = DelegateFactory.CreateDelegate(typeof(UIScrollViewItemBase.OnSelectedHandler), func) as UIScrollViewItemBase.OnSelectedHandler;
            }

            obj.onSelected = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index onSelected on a nil value" : e.Message));
        }
    }
Ejemplo n.º 3
0
    public void ItemSelected(UIScrollViewItemBase item, bool clear = false)
    {
        if (onItemSelected != null)
        {
            onItemSelected(item);

            if (clear)
            {
                onItemSelected = null;
            }
        }
    }
Ejemplo n.º 4
0
 static int Selected(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         UIScrollViewItemBase obj = (UIScrollViewItemBase)ToLua.CheckObject(L, 1, typeof(UIScrollViewItemBase));
         bool arg0 = LuaDLL.luaL_checkboolean(L, 2);
         obj.Selected(arg0);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Ejemplo n.º 5
0
 static int Create(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 3);
         UIScrollViewAdapter obj = (UIScrollViewAdapter)ToLua.CheckObject(L, 1, typeof(UIScrollViewAdapter));
         int arg0 = (int)LuaDLL.luaL_checknumber(L, 2);
         UIScrollViewItemBase arg1 = (UIScrollViewItemBase)ToLua.CheckUnityObject(L, 3, typeof(UIScrollViewItemBase));
         obj.Create(arg0, arg1);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Ejemplo n.º 6
0
 static int ItemSelected(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 3);
         UIScrollViewAdapter  obj  = (UIScrollViewAdapter)ToLua.CheckObject(L, 1, typeof(UIScrollViewAdapter));
         UIScrollViewItemBase arg0 = (UIScrollViewItemBase)ToLua.CheckUnityObject(L, 2, typeof(UIScrollViewItemBase));
         bool arg1 = LuaDLL.luaL_checkboolean(L, 3);
         obj.ItemSelected(arg0, arg1);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Ejemplo n.º 7
0
    static int get_Index(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UIScrollViewItemBase obj = (UIScrollViewItemBase)o;
            int ret = obj.Index;
            LuaDLL.lua_pushinteger(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index Index on a nil value" : e.Message));
        }
    }
Ejemplo n.º 8
0
    static int get__widgetTransform(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UIScrollViewItemBase obj = (UIScrollViewItemBase)o;
            UIWidget             ret = obj._widgetTransform;
            ToLua.Push(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index _widgetTransform on a nil value" : e.Message));
        }
    }
Ejemplo n.º 9
0
    static int get_onSelected(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UIScrollViewItemBase obj = (UIScrollViewItemBase)o;
            UIScrollViewItemBase.OnSelectedHandler ret = obj.onSelected;
            ToLua.Push(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index onSelected on a nil value" : e.Message));
        }
    }
Ejemplo n.º 10
0
    static int set_Position(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UIScrollViewItemBase obj  = (UIScrollViewItemBase)o;
            UnityEngine.Vector2  arg0 = ToLua.ToVector2(L, 2);
            obj.Position = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index Position on a nil value" : e.Message));
        }
    }
Ejemplo n.º 11
0
    static int set_Index(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UIScrollViewItemBase obj = (UIScrollViewItemBase)o;
            int arg0 = (int)LuaDLL.luaL_checknumber(L, 2);
            obj.Index = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index Index on a nil value" : e.Message));
        }
    }
Ejemplo n.º 12
0
    static int set__widgetTransform(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UIScrollViewItemBase obj  = (UIScrollViewItemBase)o;
            UIWidget             arg0 = (UIWidget)ToLua.CheckUnityObject(L, 2, typeof(UIWidget));
            obj._widgetTransform = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index _widgetTransform on a nil value" : e.Message));
        }
    }
Ejemplo n.º 13
0
    static int get_Size(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UIScrollViewItemBase obj = (UIScrollViewItemBase)o;
            UnityEngine.Vector2  ret = obj.Size;
            ToLua.Push(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index Size on a nil value" : e.Message));
        }
    }
Ejemplo n.º 14
0
    /// <summary>
    /// 初始化方法
    /// 步骤:
    /// 1.AddComponent<此组件>()
    /// 2.设置EnableMultiLine属性值
    ///   例如:多行多列
    ///     EnableMultiLine = true
    ///     _spacing_row = 行间距
    ///     _spacing_line = 列间距
    /// 3.预制继承自UIScrollViewItemBase的子类
    /// 4.调用Create(列表项总数,预制体)方法
    /// </summary>
    /// <param name="items">实例化项目个数</param>
    /// <param name="listItemPrefab">实例化的模板</param>
    public void Create(int items, UIScrollViewItemBase listItemPrefab)
    {
        _itemsTotal = items;
        p_prefab    = listItemPrefab.gameObject;

        switch (_scrollOrientation)
        {
        case ScrollOrientation.HORIZONTAL:
            if (!EnableMultiLine)
            {
                _itemSize     = listItemPrefab.Size.x;
                _itemsVisible = Mathf.CeilToInt(GetViewportSize() / (_itemSize + _spacing));
            }
            else
            {
                _itemSize_ml = new Vector2(listItemPrefab.Size.x, listItemPrefab.Size.y);

                _itemsVisible_row  = Mathf.CeilToInt(_viewport.width / (_itemSize_ml.x + _spacing_line));
                _itemsVisible_line = Mathf.FloorToInt(_viewport.height / (_itemSize_ml.y + _spacing_row));
            }
            break;

        case ScrollOrientation.VERTICAL:
            if (!EnableMultiLine)
            {
                _itemSize     = listItemPrefab.Size.y;
                _itemsVisible = Mathf.CeilToInt(GetViewportSize() / (_itemSize + _spacing));
            }
            else
            {
                _itemSize_ml = new Vector2(listItemPrefab.Size.x, listItemPrefab.Size.y);
                //垂直方向时,row可显示item数量使用向下舍位取整
                _itemsVisible_row  = Mathf.FloorToInt(_viewport.width / (_itemSize_ml.x + _spacing_line));
                _itemsVisible_line = Mathf.CeilToInt(_viewport.height / (_itemSize_ml.y + _spacing_row));

                //print("_itemsVisible_row = " + _itemsVisible_row);
                //print("_itemsVisible_line = " + _itemsVisible_line);
            }
            break;
        }//end Switch

        //Debug.LogWarning("GetViewportSize() "+ GetViewportSize()+"  "+ _itemSize
        //    +"  Mathf.CeilToInt()"+ Mathf.CeilToInt(GetViewportSize() / _itemSize)
        //    );

        int itemsToInstantiate = EnableMultiLine ? _itemsVisible_row * _itemsVisible_line : _itemsVisible;

        if (itemsToInstantiate > items || (items >= itemsToInstantiate && items <= 2 * itemsToInstantiate))
        {
            itemsToInstantiate = items;

            //CHANGED: 2017-7-06 添加自动补齐行/列开关
            if (WhetherAutomaticallyFill)
            {
                if (itemsToInstantiate % _itemsVisible_row != 0)//每行项目数不满的时候,补齐行
                {
                    itemsToInstantiate += Mathf.CeilToInt((float)itemsToInstantiate / (float)_itemsVisible_row) * _itemsVisible_row - itemsToInstantiate;
                }
            }
            //print("total:"+ items);
            //print("gen:"+ itemsToInstantiate);
            //print("itemsToInstantiate / _itemsVisible_row = " + (float)(itemsToInstantiate / _itemsVisible_row));
            //print("itemsToInstantiate = " + itemsToInstantiate);
            //print("_itemsVisible_row = " + _itemsVisible_row);
            //print("Mathf.CeilToInt(itemsToInstantiate / _itemsVisible_row) = " + Mathf.CeilToInt(itemsToInstantiate / _itemsVisible_row));
        }
        else
        {
            itemsToInstantiate *= 2;
        }

        _itemsList = new List <UIScrollViewItemBase>();

        for (int i = 0; i < itemsToInstantiate; i++)
        {
            UIScrollViewItemBase item = EnableMultiLine ?
                                        CreateNewItem(i, _itemSize_ml) : CreateNewItem(i, _itemSize);
            item.onSelected = HandleOnSelectedHandler;
            item.Index      = i;

            _itemsList.Add(item);
            ItemLoaded(item);//列表项加载完成Callback
        }

        _firstItemIndex = 0;
        _lastItemIndex  = _itemsList.Count - 1;

        _itemsToRecycleAfter = EnableMultiLine ?
                               _itemsList.Count / _itemsVisible_row - _itemsVisible_line :
                               _itemsList.Count - _itemsVisible;

        //Debug.LogWarning("_itemSize = " + _itemSize);
        //Debug.LogWarning("_itemsVisible = " + _itemsVisible);
        //Debug.LogWarning("_itemsVisibles = " + _itemsVisible_row + " " + _itemsVisible_line);
        //Debug.LogWarning("itemsToInstantiate = " + itemsToInstantiate);
        //Debug.LogWarning("_itemsTotal = " + _itemsTotal);
        //Debug.LogWarning("_lastItemIndex = " + _lastItemIndex);

        //注册事件
        onListMoved += Recycle;//在列表向某方向移动了一整行后触发

        //List<EventDelegate> events_OnChange = new List<EventDelegate>();
        //events_OnChange.Add(new EventDelegate(HandleOnScrollBarValueChangedHandler));
        //_scrollBar.onChange = events_OnChange;
        isActived = true;
    }//end Create()
Ejemplo n.º 15
0
    void Awake()
    {
        mScrollView = GetComponent <UIScrollView> ();
        if (mScrollView == null)
        {
            Debug.LogError("Put UIScrollViewRecycle and UIScrollView together , please !");
            enabled = false;
            return;
        }
        mScrollView.contentPivot    = UIWidget.Pivot.TopLeft;
        mScrollView.onDragStarted   = new UIScrollView.OnDragNotification(onDragStarted);
        mScrollView.onStoppedMoving = new UIScrollView.OnDragNotification(onStoppedMoving);
        mScrollView.movement        = (UIScrollView.Movement)Enum.Parse(typeof(UIScrollView.Movement), movement.ToString());

        if (perLineCount == 0)
        {
            CalculatePerLineCount();
        }

        //check parent's pivot
        UIWidget parentWidget = transform.parent.GetComponent <UIWidget>();

        if (parentWidget != null)
        {
            if (parentWidget.pivot != UIWidget.Pivot.TopLeft)
            {
                Debug.LogError("hey man , set " + parentWidget.name + "'s pivot as TopLeft please");
            }
        }
        else
        {
            Debug.LogError("oh my god , you ScrollView looks like shit ...");
        }

        GameObject itemContainerObj = new GameObject();

        itemContainerObj.layer                   = (int)LayerEnum.UI; //Set To NGUI Layer
        itemContainer                            = itemContainerObj.AddComponent <UIWidget> ();
        itemContainer.name                       = "ItemContainer";
        itemContainer.pivot                      = UIWidget.Pivot.TopLeft;
        itemContainer.transform.parent           = this.transform;
        itemContainer.transform.localPosition    = Vector3.zero;
        itemContainer.transform.localEulerAngles = Vector3.zero;
        itemContainer.transform.localScale       = Vector3.one;

        if (templateItem != null)
        {
            if (templateItem.transform.parent == transform)
            {
                templateItem.gameObject.SetActive(false);
            }
        }
        else
        {
            Transform itemTrans = transform.Find("TemplateItem");
            if (itemTrans != null)
            {
                templateItem = itemTrans.GetComponent <UIScrollViewItemBase> ();
                if (templateItem != null)
                {
                    templateItem.gameObject.SetActive(false);
                }
                else
                {
                    Debug.LogError("TemplateItem without UIScrollViewItemBase's derived Class");
                }
            }
        }

        itemCount = itemCount;        //为了刷新
    }
Ejemplo n.º 16
0
 void HandleOnSelectedHandler(UIScrollViewItemBase item)
 {
     ItemSelected(item);
 }
Ejemplo n.º 17
0
    }//end PreviousItem()

    /// <summary>
    /// 进行回收项目操作
    /// </summary>
    /// <param name="direction">回收方向(用于判断回收头/尾部)</param>
    private void RecycleItem(ScrollDirection direction)
    {
        if (!EnableMultiLine)
        {
            #region SingleLine
            UIScrollViewItemBase firstItem = _itemsList[0];
            UIScrollViewItemBase lastItem  = _itemsList[_itemsList.Count - 1];

            float targetPosition = (_itemSize + _spacing);

            switch (direction)
            {
            case ScrollDirection.NEXT:
                //向后滚动时,将首元素移至队尾
                switch (_scrollOrientation)
                {
                case ScrollOrientation.HORIZONTAL:        //水平滚动方向,只改变x值
                    firstItem.Position = new Vector2(lastItem.Position.x + targetPosition, firstItem.Position.y);
                    break;

                case ScrollOrientation.VERTICAL:        //垂直滚动方向,只改变y值
                    /* y轴向下为负 */
                    //FIXED: 2017-07-21 修复了一个bug,该bug在单列模式下向下滚动时,列表项坐标计算错误,无法正常循环
                    firstItem.Position = new Vector2(firstItem.Position.x, lastItem.Position.y - targetPosition);
                    break;
                }
                //print(firstItem.gameObject.name + "移至队尾");
                //print("参与运算变量_lastItemIndex = " + _lastItemIndex);
                firstItem.Index = _lastItemIndex;
                //print("预加载Index = " + firstItem.Index);
                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:
                    /* y轴向上为正 */
                    //FIXED: 2017-07-21 修复了一个bug,该bug在单列模式下向上滚动时,列表项坐标计算错误,无法正常循环
                    lastItem.Position = new Vector2(lastItem.Position.x, firstItem.Position.y + targetPosition);
                    break;
                }
                //print(lastItem.gameObject.name + "移至队头");
                //print("参与运算变量_lastItemIndex = " + _lastItemIndex + " _itemsList.Count = " + _itemsList.Count);
                lastItem.Index = _lastItemIndex - _itemsList.Count;
                //print("预加载Index = " + lastItem.Index);
                lastItem.transform.SetAsFirstSibling();

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

                ItemLoaded(lastItem);
                break;
            }//end switch
            #endregion
        }
        else
        {
            UIScrollViewItemBase[] firstRowItems = new UIScrollViewItemBase[_itemsVisible_row];
            UIScrollViewItemBase[] lastItems     = new UIScrollViewItemBase[firstRowItems.Length];
            for (int i = 0; i < _itemsVisible_row; ++i)
            {
                firstRowItems[i] = _itemsList[0 + i];
                lastItems[i]     = _itemsList[_itemsList.Count - 1 - i];
            }

            float targetPosition = firstRowItems[0].Size.y + _spacing_row;

            switch (direction)
            {
            case ScrollDirection.NEXT:
                //向后滚动时,将首元素移至队尾
                switch (_scrollOrientation)
                {
                case ScrollOrientation.HORIZONTAL:        //水平滚动方向,只改变x值
                    /* TODO: 水平滚动方向逻辑处理未完成 */
                    //firstItem.Position = new Vector2(lastItem.Position.x + targetPosition, firstItem.Position.y);
                    break;

                case ScrollOrientation.VERTICAL:        //垂直滚动方向,只改变y值
                    for (int i = 0; i < firstRowItems.Length; ++i)
                    {
                        firstRowItems[i].Position = new Vector2(firstRowItems[i].Position.x, lastItems[i].Position.y - targetPosition);
                        //print(firstRowItems[i].gameObject.name + "移至队尾");
                        //print(firstRowItems[i].gameObject.name + " y轴坐标-" + targetPosition);
                    }
                    break;
                }

                for (int i = 0; i < firstRowItems.Length; ++i)
                {
                    //CHANGED: 2017-7-05 首行回收添加索引偏移 +1
                    firstRowItems[i].Index = _lastItemIndex + i + 1;
                    firstRowItems[i].transform.SetAsLastSibling();

                    _itemsList.RemoveAt(0);
                    _itemsList.Add(firstRowItems[i]);

                    ItemLoaded(firstRowItems[i]);
                }
                break;

            case ScrollDirection.PREVIOUS:
                //向前滚动时,将列表末元素移至队头
                switch (_scrollOrientation)
                {
                case ScrollOrientation.HORIZONTAL:
                    //lastItem.Position = new Vector2(firstItem.Position.x - targetPosition, lastItem.Position.y);
                    break;

                case ScrollOrientation.VERTICAL:
                    for (int i = 0; i < lastItems.Length; ++i)
                    {
                        lastItems[i].Position = new Vector2(lastItems[i].Position.x, firstRowItems[i].Position.y + targetPosition);
                        //print(lastItems[i].gameObject.name + "移至队头");
                        //print(firstRowItems[i].gameObject.name + " y轴坐标+" + targetPosition);
                    }
                    break;
                }
                for (int i = 0; i < lastItems.Length; i++)
                {
                    //FIXED: 2017-7-05 尾行回收bug修复
                    lastItems[i].Index = _firstItemIndex + _itemsVisible_row - i - 1;
                    lastItems[i].transform.SetAsFirstSibling();

                    _itemsList.RemoveAt(_itemsList.Count - 1);
                    _itemsList.Insert(0, lastItems[i]);

                    ItemLoaded(lastItems[i]);
                }
                break;
            }//end switch
        }
        _scrollView.InvalidateBounds();
    }//end RecycleItem()
Ejemplo n.º 18
0
    //private GameObject
    //更新Item缓存数量,初始化和ScrollView尺寸更新时调用
    private void UpdateItemCacheCount()
    {
        if (mScrollView == null)
        {
            Debug.LogError("mScrollView is null , check it !");
            enabled = false;
            return;
        }

        if (templateItem == null)
        {
            Debug.LogError("templateItem is null , check it !");
            return;
        }

        int xItemCount = 0;
        int yItemCount = 0;

        int mWidth  = Mathf.RoundToInt(mScrollView.panel.width);
        int mHeight = Mathf.RoundToInt(mScrollView.panel.height);

        if (perLineCount == 0)
        {
            CalculatePerLineCount();
        }

        switch (movement)
        {
        case Movement.Horizontal:
            xItemCount = CalculateCountByQuotientAndRemainder(mWidth / cellWidth, mWidth % cellWidth) + 1;
            yItemCount = perLineCount;
            break;

        case Movement.Vertical:
            xItemCount = perLineCount;
            yItemCount = CalculateCountByQuotientAndRemainder(mHeight / cellHeight, mHeight % cellHeight) + 1;
            break;

        default:
            Debug.LogError("may be you should case a new type here");
            xItemCount = 0;
            yItemCount = 0;
            break;
        }

        int maxCount = xItemCount * yItemCount;

        //非填满模式截取处理
        if (!fillMode)
        {
            maxCount = Mathf.Min(itemCount, maxCount);
        }

        if (maxCount > cacheItems.Count)
        {
            int count = maxCount - cacheItems.Count;
            for (int i = 0; i < count; i++)
            {
                GameObject           newItemObj = NGUITools.AddChild(itemContainer.gameObject, templateItem.gameObject);
                UIScrollViewItemBase temp       = newItemObj.GetComponent <UIScrollViewItemBase> ();
                temp.OnInit();

                if (onItemInstantiate != null)
                {
                    onItemInstantiate(temp);
                }

                cacheItems.Add(temp);
            }
        }
    }