Esempio n. 1
0
        private void HandleDrop(Action <int, int, T> onReorder, Action <int, int, T> onBeforeReorder)
        {
            NeedRepaint = true;
            LIInfo dragRect = Drag.Info;

            dragRect.AnimateStep();
            if (dragRect.IsAnimate)
            {
                return;                 //check if drop completed
            }

            int dragIdx = Drag.Idx;
            int dropIdx = Drag.DropIdx;

            if (onBeforeReorder != null)
            {
                onBeforeReorder(dragIdx, dropIdx, (T)Drag.Target);
            }
            CacheList.Remove((T)Drag.Target);
            CacheList.Insert(dropIdx, (T)Drag.Target);
            if (onReorder != null)
            {
                onReorder(dragIdx, dropIdx, (T)Drag.Target);
            }

            Drag.DropIdx = -1;
            Drag.Target  = null;
        }
Esempio n. 2
0
 public void SetSelection(bool isSelected)
 {
     foreach (T item in CacheList)
     {
         LIInfo info = GetInfo(item);
         info.Selected = isSelected;
     }
 }
Esempio n. 3
0
 public void SetSelection(bool isSelected)
 {
     for (var i = 0; i < CacheList.Count; i++)
     {
         LIInfo info = GetInfo(i, CacheList[i]);
         info.Selected = isSelected;
     }
 }
Esempio n. 4
0
 public void InvertSelection()
 {
     foreach (T item in CacheList)
     {
         LIInfo info = GetInfo(item);
         info.Selected = !info.Selected;
     }
 }
Esempio n. 5
0
 public void InvertSelection()
 {
     for (var i = 0; i < CacheList.Count; i++)
     {
         LIInfo info = GetInfo(i, CacheList[i]);
         info.Selected = !info.Selected;
     }
 }
Esempio n. 6
0
        private int DrawCell(Func <Rect, T, int, int> onDrawCell, Rect r, T item, int i, int idx = -1)
        {
            if (item == null)
            {
                Debug.LogWarning("Item should not be null :: " + i);
            }

            LIInfo info = GetInfo(i, item);

            if (item == Drag.Target && onDrawCell != null)
            {
                if (Theme.DraggingTex != null)
                {
                    GUI.DrawTexture(r.h(info.CurrentRect.height), Theme.DraggingTex);
                }
                if (Theme.ShowIndex)
                {
                    GUI.Label(r.dx(3).w(30f), (idx + 1) + "");                  //.dy(3)
                }
            }
            else
            {
                if (info.Selected == true)
                {
                    if (Theme.SelectedTex != null)
                    {
                        GUI.DrawTexture(r.dl(1).dt(1), Theme.SelectedTex);
                    }
                }
                else if (idx % 2 == 0)                   //even
                {
                    if (Theme.EvenBgTex != null)
                    {
                        GUI.DrawTexture(r, Theme.EvenBgTex);
                    }
                }
                else                     //odd
                {
                    if (Theme.OddBgTex != null)
                    {
                        GUI.DrawTexture(r, Theme.OddBgTex);
                    }
                }
            }

            if (onDrawCell != null)
            {
                var indexW = Theme.ShowIndex ? 20 : 0;

                if (Theme.ShowIndex)
                {
                    GUI.Label(r.dx(3).w(indexW), (idx + 1) + "", GuiX.miniLabelGrayStyle);//.dy(3)
                }
                return(onDrawCell(r.dl(indexW), item, idx));
            }

            return(-1);
        }
Esempio n. 7
0
        private int DrawCell(Func <Rect, T, int, int> onDrawCell, Rect r, T item, int idx = -1)
        {
            LIInfo info = GetInfo(item);

            if (item == Drag.Target && onDrawCell != null)
            {
                if (Theme.DraggingTex != null)
                {
                    GUI.DrawTexture(r.h(info.CurrentRect.height), Theme.DraggingTex);
                }
                if (Theme.ShowIndex)
                {
                    GUI.Label(r.dx(3).dy(3).w(30f), (idx + 1) + "");
                }
            }
            else
            {
                if (info.Selected == true)
                {
                    if (Theme.SelectedTex != null)
                    {
                        GUI.DrawTexture(r.dl(1).dt(1), Theme.SelectedTex);
                    }
                }
                else if (idx % 2 == 0)                   //even
                {
                    if (Theme.EvenBgTex != null)
                    {
                        GUI.DrawTexture(r, Theme.EvenBgTex);
                    }
                }
                else                     //odd
                {
                    if (Theme.OddBgTex != null)
                    {
                        GUI.DrawTexture(r, Theme.OddBgTex);
                    }
                }
            }

            if (onDrawCell != null)
            {
                if (Theme.ShowIndex)
                {
                    GUI.Label(r.dx(3).dy(3).w(30f), (idx + 1) + "");
                }
                return(onDrawCell(r.dl(Theme.ShowIndex ? 15 : 0), item, idx));
            }

            return(-1);
        }
Esempio n. 8
0
        //---------------------------------------- PUBLIC APIs --------------------------------------------

        public LIInfo GetInfo(int idx, T item)
        {
            if (item == null)
            {
                Debug.LogWarning("item should not be null");
                return(null);
            }

            if (idx == -1)   //works if items being different
            {
                idx = CacheList.IndexOf(item);
                if (idx == -1)
                {
                    return(null);
                }
            }

            if (RectMap.ContainsKey(idx))
            {
                var li = RectMap[idx];
                if (li.data == item)
                {
                    return(li);
                }

                //invalid data found
                RectMap.Remove(idx);
            }

            LIInfo <T> info = new LIInfo <T>
            {
                data      = item,
                Selected  = Theme.Selectable ? (bool?)false : null,
                CanDrag   = Theme.Draggable,
                CanDelete = Theme.ShowDelete
            };            //.SetXY(GuiRect.x, GuiRect.y);.SetHeight(Theme.CellHeight);

            //Debug.Log("NEW :: " + idx);

            RectMap.Add(idx, info);
            return(info);
        }
Esempio n. 9
0
        //---------------------------------------- PUBLIC APIs --------------------------------------------

        public LIInfo GetInfo(T item)
        {
            if (item == null)
            {
                Debug.LogWarning("item should not be null");
                return(null);
            }

            if (RectMap.ContainsKey(item))
            {
                return(RectMap[item]);
            }

            LIInfo info = new LIInfo
            {
                Selected  = Theme.Selectable ? (bool?)false : null,
                CanDrag   = Theme.Draggable,
                CanDelete = Theme.ShowDelete
            };            //.SetXY(GuiRect.x, GuiRect.y);.SetHeight(Theme.CellHeight);

            RectMap.Add(item, info);
            return(info);
        }
Esempio n. 10
0
        /*public void CacheRect() {
         *      if (Id != null) {
         *              _c_idx = Id;
         *              _c_GuiRect = GuiRect;
         *              _c_ScrollRect = ScrollRect;
         *              _c_ScrollPosition = ScrollPosition;
         *
         *              var list = new List<int>();
         *              for (var i = 0; i < CacheList.Count; i++) {
         *                      var info = GetInfo(CacheList[i]);
         *                      if (info.Selected == true) list.Add(i);
         *              }
         *
         *              _c_SelectedIndexes = list.ToArray();
         *      }
         * }*/
        private void HandleDrag()
        {
            NeedRepaint = true;

            if (Event.current.type == EventType.mouseDrag)               //only update when mouse drag
            {
                LIInfo dragAnim = Drag.Info;

                var ch         = GuiRect.y;
                var dropIdx    = -1;
                var lastCheck  = false;
                var cnt        = 0;
                var localMouse = Event.current.mousePosition;

                foreach (T item in CacheList)
                {
                    var info = GetInfo(item);

                    if (item == Drag.Target)
                    {
                        continue;
                    }

                    var checkRect = info.CurrentRect.y(ch);
                    var check     = checkRect.Contains(localMouse);
                    if (lastCheck && !check)
                    {
                        dropIdx = cnt - 1;
                    }
                    lastCheck = check;

                    info.TweenY(ch);
                    ch += info.CurrentRect.height;
                    cnt++;
                }

                GuiRect.height = ch + dragAnim.CurrentRect.height - GuiRect.y;
                if (dropIdx == -1)
                {
                    dropIdx = CacheList.Count - (lastCheck ? 2 : 1);
                }
                Drag.DropIdx = dropIdx;

                cnt = 0;
                foreach (T t in CacheList)
                {
                    if (t == Drag.Target)
                    {
                        continue;
                    }

                    if (cnt >= dropIdx)
                    {
                        GetInfo(t).TweenDY(dragAnim.CurrentRect.height);
                        //Debug.Log(i + "===>" + cnt + "--->");
                    }
                    cnt++;
                }
            }

            //stop drag if mouse out or up
            if (Drag.IsDragging && ScrollRect.height > 0 && !ScrollRect.Contains(MousePos))
            {
                //Debug.Log("Stop Drag at : " + MousePos + ":" + GuiRect + ":" + ScrollRect);
                StopDrag(false);
            }
            if (Event.current.type == EventType.MouseUp)
            {
                StopDrag(true);
            }
        }
Esempio n. 11
0
        internal void Draw(Func <Rect, T, int, int> onDrawCell,
                           Action <int, int, T> onReorder       = null,
                           Action <T, int> OnRightClick         = null,
                           Action <int, int, T> onBeforeReorder = null,
                           Rect?drawRect = null)
        {
            NeedRepaint = false;

            const float scrollW   = 16f;
            var         rect      = drawRect ?? GetGUILayoutViewRect();
            var         hasScroll = (rect.height > 2f) && (GuiRect.height > rect.height);
            var         counter   = 0;
            var         ch        = 0f;

            if (rect.width > 1f)
            {
                rect = rect.xAdd(Theme.RectOffset);

                GuiRect.x     = rect.x;
                GuiRect.y     = rect.y;
                GuiRect.width = rect.width;
            }

            var x = GuiRect.x;
            var y = GuiRect.y;
            var w = GuiRect.width;
            var h = GuiRect.height;

            MousePos = Event.current.mousePosition;

            if (hasScroll)
            {
                ScrollRect     = GuiRect.h(rect.height);
                ScrollPosition = GUI.BeginScrollView(ScrollRect, ScrollPosition, GuiRect.dw(-scrollW), false, true);
            }

            if (Drag.IsDropping)
            {
                HandleDrop(onReorder, onBeforeReorder);
            }
            else if (Drag.IsDragging)
            {
                HandleDrag();
            }
            if (CacheList == null)
            {
                return;
            }

            for (int i = 0; i < CacheList.Count; i++)               //layout
            {
                T   item  = CacheList[i];
                var info  = GetInfo(item).SetWidth(w - (hasScroll ? scrollW : 0)).SetX(x);
                var first = info.CurrentRect.height == 0;

                if (first)
                {
                    info.SetY(y);                    //.SetHeight(Theme.CellHeight);
                }
                info.AnimateStep();

                if (!Drag.IsDragging)
                {
                    if (Drag.IsDropping && counter == Drag.DropIdx)
                    {
                        if (item != Drag.Target)
                        {
                            LIInfo animx = Drag.Info;
                            animx.TweenY(y + ch);
                            ch += animx.CurrentRect.height;
                        }
                    }

                    if (item != Drag.Target)
                    {
                        if (Event.current.type == EventType.layout)
                        {
                            info.SetY(y + ch);
                        }
                        else
                        {
                            info.TweenY(y + ch);
                        }
                    }
                }

                if (item == Drag.Target)
                {
                    continue;
                }
                int newH = DrawCell(i != Drag.DragIdx ? onDrawCell : null,
                                    info.CurrentRect,
                                    item,
                                    (Drag.Target != null && counter >= Drag.DropIdx) ? counter + 1 : counter);                                     //!(info.Selected==true)
                if (newH > 0)
                {
                    if (first)
                    {
                        info.SetHeight(newH);
                    }
                    else
                    {
                        info.TweenH(newH);
                    }
                }

                if (info.IsAnimate && !NeedRepaint)
                {
                    NeedRepaint = true;
                }

                if (!Drag.IsDragging && !Drag.IsDropping)
                {
                    if (info.CanDrag && info.CurrentRect.w(30f).h(20f).xLMB_isDown().noModifier)
                    {
                        StartDrag(i);
                    }
                    else if (info.Selected != null && info.CurrentRect.xLMB_isDown().noModifier)
                    {
                        info.Selected = !info.Selected;
                    }

                    if (OnRightClick != null && info.CurrentRect.xRMB_isDown().noModifier)
                    {
                        OnRightClick(item, i);
                    }
                }

                ch += info.CurrentRect.height;
                counter++;
            }

            if (Drag.IsDragging)
            {
                ch += Drag.Info.CurrentRect.height;
            }

            if (ch != h && ch > 0)
            {
                GuiRect.height = ch;
                NeedRepaint    = true;
            }

            if (Drag.Target != null)
            {
                LIInfo dragRect = Drag.Info;
                int    newH     = DrawCell(onDrawCell,
                                           Drag.IsDragging ? MouseDragRect : dragRect.CurrentRect,
                                           (T)Drag.Target,
                                           Drag.DropIdx);
                if (newH > 0)
                {
                    dragRect.SetHeight(newH);
                }
            }

            if (hasScroll)
            {
                GUI.EndScrollView();
            }
        }
Esempio n. 12
0
        /*public void CacheRect() {
         *      if (Id != null) {
         *              _c_idx = Id;
         *              _c_GuiRect = GuiRect;
         *              _c_ScrollRect = ScrollRect;
         *              _c_ScrollPosition = ScrollPosition;
         *
         *              var list = new List<int>();
         *              for (var i = 0; i < CacheList.Count; i++) {
         *                      var info = GetInfo(CacheList[i]);
         *                      if (info.Selected == true) list.Add(i);
         *              }
         *
         *              _c_SelectedIndexes = list.ToArray();
         *      }
         * }*/
        private void HandleDrag()
        {
            NeedRepaint = true;
            //true ||
            if (Event.current.type == EventType.mouseDrag)   //only update when mouse drag
            {
                LIInfo dragAnim = Drag.Info;

                var ch         = GuiRect.y;
                var dropIdx    = -1;
                var lastCheck  = false;
                var cnt        = 0;
                var localMouse = Event.current.mousePosition;

                for (var i = 0; i < CacheList.Count; i++)
                {
                    if (i == Drag.DragIdx)
                    {
                        continue;
                    }

                    var item = CacheList[i];
                    var info = GetInfo(i, item);

                    var checkRect = info.CurrentRect.y(ch);
                    var check     = checkRect.Contains(localMouse);
                    if (lastCheck && !check)
                    {
                        dropIdx = cnt - 1;
                    }
                    lastCheck = check;

                    info.TweenY(ch);
                    ch += info.CurrentRect.height;

                    //Debug.Log(i + "--->" + cnt + ":" + ch + ":" + check + ":"+ info.CurrentRect.height + ":" + Drag.DragIdx + ":" + dropIdx);
                    //Debug.Log(i + "--->" + cnt + ":" + ch + ":" + Drag.DragIdx + ":" + dropIdx + "::::" + info.CurrentRect.height);
                    cnt++;
                }

                //Debug.Log("================================================" + ch + ":" + dropIdx + "   " + Event.current);

                GuiRect.height = ch + dragAnim.CurrentRect.height - GuiRect.y;
                if (dropIdx == -1)
                {
                    dropIdx = CacheList.Count - (lastCheck ? 2 : 1);
                }
                Drag.DropIdx = dropIdx;

                cnt = 0;

                for (var i = 0; i < CacheList.Count; i++)
                {
                    if (i == Drag.DragIdx)
                    {
                        continue;
                    }

                    var t = CacheList[i];

                    if (cnt >= dropIdx)
                    {
                        GetInfo(i, t).TweenDY(dragAnim.CurrentRect.height);
                        //Debug.Log(i + "===>" + cnt + "--->" + GetInfo(i, t).CurrentRect);
                    }
                    cnt++;
                }
            }

            //Fix for dragging the last position, ScrollRect's size does not contains the dragging one
            var r2 = ScrollRect.dh(Drag.Info._cRect.height);

            //stop drag if mouse out or up
            if (Drag.IsDragging && ScrollRect.height > 0 && !r2.Contains(MousePos))
            {
                //Debug.Log("Stop Drag at : " + MousePos + ":" + GuiRect + ":" + r2);
                StopDrag(false);
            }

            if (Event.current.type == EventType.MouseUp)
            {
                StopDrag(true);
            }
        }
Esempio n. 13
0
        internal void Draw(Func <Rect, T, int, int> onDrawCell,
                           Action <int, int, T> onReorder = null,
                           Action <T, int, vlbGUIList <T> > OnRightClick = null,
                           Action <int, int, T> onBeforeReorder          = null,
                           Rect?drawRect = null)
        {
            NeedRepaint = false;
            const float scrollW = 16f;

            var rect      = drawRect ?? (Theme.AllowScroll ? GuiX.FlexibleSpace() : GuiX.Height(GuiRect.height));
            var hasScroll = (rect.height > 2f) && (GuiRect.height > rect.height);
            var counter   = 0;
            var ch        = 0f;

            if (!Event.current.xIsLayout() && Event.current.xIsNotUsed())              //rect.width > 1f
            {
                rect          = rect.xAdd(Theme.RectOffset);
                GuiRect.x     = rect.x;
                GuiRect.y     = rect.y;
                GuiRect.width = rect.width;

                MousePos = Event.current.mousePosition;
                //Debug.Log(Event.current + ":" + GuiRect);
            }

            var x = GuiRect.x;
            var y = GuiRect.y;
            var w = GuiRect.width;
            var h = GuiRect.height;

            if (hasScroll)
            {
                ScrollRect     = GuiRect.h(rect.height);
                ScrollPosition = GUI.BeginScrollView(ScrollRect, ScrollPosition, GuiRect.dw(-scrollW), false, true);
            }

            //Debug.Log("----> " + x + ":" + y + ":" + w + ":" + h + ":" + Event.current + ":" + hasScroll);
            //Debug.Log("--------------------------> " + Event.current);

            if (Drag.IsDropping)
            {
                HandleDrop(onReorder, onBeforeReorder);
            }
            else if (Drag.IsDragging)
            {
                HandleDrag();
            }
            if (CacheList == null)
            {
                return;
            }



            for (int i = 0; i < CacheList.Count; i++)               //layout
            //var willLog = i < 5 && CacheList.Count == 12;

            {
                T   item  = CacheList[i];
                var info  = GetInfo(i, item).SetWidth(w - (hasScroll ? scrollW : 0)).SetX(x);
                var first = info.CurrentRect.height == 0;

                /*if (RectMap[CacheList[i]] == RectMap[CacheList[0]]) {
                 *  Debug.Log("Invalid <" + i + ">" + string.IsNullOrEmpty((string)(object)item) + ":" + CacheList[0]);
                 * }*/

                //Debug.Log(i + " CHECK :: " + CacheList[0] + ":" + (RectMap[CacheList[0]] == RectMap[CacheList[i]]));


                //info._logChange = i == 0;
                //if (i == 0) Debug.Log(item.GetHashCode() + " Before :: " + info.CurrentRect);

                if (first)
                {
                    info.SetY(y);                    //.SetHeight(Theme.CellHeight);
                }
                info.AnimateStep();

                if (!Drag.IsDragging)
                {
                    if (Drag.IsDropping && counter == Drag.DropIdx)
                    {
                        if (item != Drag.Target)
                        {
                            LIInfo animx = Drag.Info;
                            animx.TweenY(y + ch);
                            ch += animx.CurrentRect.height;
                        }
                    }

                    if (item != Drag.Target)
                    {
                        if (Event.current.type == EventType.layout)
                        {
                            info.SetY(y + ch);
                        }
                        else
                        {
                            info.TweenY(y + ch);
                        }
                    }
                }

                //if (i == 0) Debug.Log(item.GetHashCode() + " After :: " + info.CurrentRect);

                if (item == Drag.Target)
                {
                    continue;
                }

                //if (i == 0) Debug.Log("Before ::" + info.CurrentRect);

                var newH = DrawCell(i != Drag.DragIdx ? onDrawCell : null,
                                    info.CurrentRect,
                                    item, i,
                                    (Drag.Target != null && counter >= Drag.DropIdx) ? counter + 1 : counter);                                     //!(info.Selected==true)

                //if (i == 0) Debug.Log("After ::" + info.CurrentRect);

                if (newH > 0)
                {
                    if (first)
                    {
                        info.SetHeight(newH);
                    }
                    else
                    {
                        info.TweenH(newH);
                    }
                }

                if (info.IsAnimate && !NeedRepaint)
                {
                    NeedRepaint = true;
                }

                if (!Drag.IsDragging && !Drag.IsDropping)
                {
                    if (info.CanDrag && info.CurrentRect.w(Theme.DragWidth).h(20f).xLMB_isDown().noModifier)
                    {
                        StartDrag(i);
                    }
                    else if (info.Selected != null && info.CurrentRect.xLMB_isDown().noModifier)
                    {
                        info.Selected = !info.Selected;
                    }

                    if (OnRightClick != null && info.CurrentRect.xRMB_isDown().noModifier)
                    {
                        OnRightClick(item, i, this);
                    }
                }


                ch += info.CurrentRect.height;
                counter++;
                //if (willLog) Debug.Log(counter + ":" + ch + "::::" + info.CurrentRect);

                //T item0 = CacheList[0];
                //var info0 = GetInfo(item0);
                //Debug.Log(i + " Last ---> " + info0.CurrentRect);
            }

            /*if (Drag.IsDragging) {
             *  //DEBUG
             *  GUI.DrawTexture(GuiRect, Color.yellow.xAlpha(0.2f).xGetTexture2D());
             *  GUI.DrawTexture(new Rect(MousePos.x, MousePos.y, 10, 10), Color.red.xAlpha(0.2f).xGetTexture2D());
             * }*/

            if (Drag.IsDragging)
            {
                ch += Drag.Info.CurrentRect.height;
            }

            if (ch != h && ch > 0)
            {
                GuiRect.height = ch;
                NeedRepaint    = true;
            }

            if (Drag.Target != null)
            {
                LIInfo dragRect = Drag.Info;
                int    newH     = DrawCell(onDrawCell,
                                           Drag.IsDragging ? MouseDragRect : dragRect.CurrentRect,
                                           (T)Drag.Target, Drag.DragIdx,
                                           Drag.DropIdx);
                if (newH > 0)
                {
                    dragRect.SetHeight(newH);
                }
            }

            if (hasScroll)
            {
                GUI.EndScrollView();
            }
        }