Example #1
0
    public void                                            MoveItemFromTo(Int32 currentIndex, Int32 newIndex)
    {
        if (currentIndex < 0 || newIndex < 0 || currentIndex == newIndex)
        {
            return;
        }

        if (ShowDebug)
        {
            Debug.Log(string.Format("Move from: {0} to: {1}", currentIndex, newIndex));
        }

        var item = _viewItems[currentIndex];

        if (item == null)
        {
            return;
        }

        ItemsSource.RemoveAt(currentIndex);
        ItemsSource.Insert(newIndex, item);

        PreFrameActions.Enqueue(() => { }); // Dummy to trigger item update
    }
Example #2
0
    public ArrayGUI(String name, Func <IList <T> > array, Boolean readOnly = false, Boolean allowDuplicates = false, Action <Rect, T, Boolean> drawElementAction = null)
    {
        _title           = name;
        _itemsSource     = array;
        _readonly        = readOnly;
        _allowDuplicates = allowDuplicates;

        if (!typeof(Object).IsAssignableFrom(typeof(T)) && drawElementAction == null)
        {
            throw new Exception(string.Format(
                                    "Trying to create ArrayGUI with type: {0}, " +
                                    "however type is not based on UnityEngine.Object and no drawElementAction is specified. " +
                                    "Either use a UnityEngine.Object derived type or provide a custom drawElementAction",
                                    typeof(T).Name));
        }

        _drawElement = ((rect, item, dragging, index) =>
        {
            if (Event.current.type == EventType.Repaint)
            {
                _styles.elementBackground.Draw(rect, false, false, false, false);
                var handleRect = new Rect(rect.x + 5f, rect.y + 7f, 10f, rect.height - 14f);
                _styles.draggingHandle.Draw(handleRect, false, false, false, false);
            }

            var middleRect = new Rect(rect.x + 20f, rect.y + 2f, rect.width - 48f, rect.height - 4);
            if (drawElementAction == null)
            {
                var previousO = item;
                var temp = EditorGUI.ObjectField(middleRect, item as Object, typeof(T), true);
                var o = temp == null ? default(T) : CastAs <T>(temp);
                var hasObject = o != null;
                if (hasObject)
                {
                    var isChangeObject = !o.Equals(previousO);
                    //var isNewObject = previousO == null && o != null;

                    //if (hasObject && isNewObject)
                    //{
                    //	PreFrameActions.Enqueue(() => {});
                    //	ItemsSource.Add( o );
                    //}

                    if (hasObject && isChangeObject)
                    {
                        PreFrameActions.Enqueue(() => {});
                        ItemsSource[index] = o;
                    }


                    if (ShowDebug)
                    {
                        Debug.Log(string.Format("HasObject: {0}, IsChangeObject: {1}", hasObject, isChangeObject));
                    }
                }
            }
            else
            {
                drawElementAction.Invoke(middleRect, item, dragging);
            }

            if (item != null)
            {
                var removeRect = new Rect(rect.x + rect.width - 24f, rect.y + 2f, 24f, rect.height - 4);
                if (GUI.Button(removeRect, _styles.iconToolbarMinus, _styles.removeButton))
                {
                    var si = index;
                    PreFrameActions.Enqueue(() => Remove(si));
                }
            }
        });
    }