Exemple #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
    }
Exemple #2
0
    public Boolean                                         OnInspectorGUI(Boolean isExpaned, Rect position)
    {
        if (_styles == null)
        {
            _styles = new Styles( );
        }

        position.x     += 4;
        position.width -= 4;

        var foldoutState = EditorGUI.Foldout(position, isExpaned, String.Format("{0} ({1} items) ", _title, ItemsSource.Count));

        position.width /= 2;
        position.x     += position.width;
        position.width -= 4;

        var newVal = EditorGUI.ObjectField(position, (UnityEngine.Object)null, typeof(T), true);

        if (newVal != null)
        {
            ItemsSource.Add((T)(System.Object)newVal);
            BufferItems();
        }

        if (!foldoutState || ItemsSource.Count == 0)
        {
            return(foldoutState);
        }

        #region | Change Processing |
        {
            var hasPreFrameActions = PreFrameActions != null && PreFrameActions.Count > 0;
            if (hasPreFrameActions)
            {
                foreach (var action in PreFrameActions)
                {
                    action( );
                }

                PreFrameActions.Clear( );
            }

            var hasNextFrameActions = NextFrameActions != null && NextFrameActions.Count > 0;
            var changed             = hasPreFrameActions || hasNextFrameActions;

            if (ShowDebug)
            {
                Debug.Log(String.Format("PreFrame Count: {0}", PreFrameActions.Count));
            }

            if (changed || _viewItems == null || _viewItems.Count <= 0)
            {
                BufferItems( );
            }

            if (changed)
            {
                foreach (var action in NextFrameActions)
                {
                    action( );
                }

                NextFrameActions.Clear( );
            }
        }
        #endregion

        EditorGUILayout.Space( );
        EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
        {
            GUILayout.Space(4f);
            EditorGUILayout.BeginVertical(new GUILayoutOption[0]);
            {
                EditorGUILayout.BeginVertical(_styles.boxBackground);
                {
                    //_scroll = EditorGUILayout.BeginScrollView(_scroll);
                    //{
                    var options    = new[] { GUILayout.ExpandWidth(true) };
                    var dragResult = DragGUI <T> .Draggable(GUILayoutUtility.GetRect(10f, 21 * _viewItems.Count, options), 21, (List <T>) _viewItems, _drawElement);

                    if (ShowDebug)
                    {
                        Debug.Log(String.Format("PreFrame Count END: {0}", PreFrameActions.Count));
                    }

                    if (dragResult.WasMoved)
                    {
                        var newIndex = dragResult.NewIndex.Value;
                        if (ShowDebug)
                        {
                            Debug.Log(String.Format("Trying to move from:{0} to:{1}", dragResult.PreviousIndex, newIndex));
                        }

                        MoveItemFromTo(dragResult.PreviousIndex, newIndex);
                        //SetIndexAccordingToNeighbors(newIndex);
                    }
                    //}
                    //EditorGUILayout.EndScrollView();
                }
                EditorGUILayout.EndVertical();

                //GUILayout.BeginHorizontal(_styles.toolbar, new GUILayoutOption[0]);
                //{
                //	GUILayout.FlexibleSpace();
                //	var iconToolbarPlus = _styles.iconToolbarPlus;
                //	var rect = GUILayoutUtility.GetRect(iconToolbarPlus, _styles.toolbarDropDown);

                //	if (EditorGUIExt.ButtonMouseDown(rect, _styles.iconToolbarClear, FocusType.Native, _styles.toolbarDropDown))
                //		PreFrameActions.Enqueue(() => ItemsSource.Clear());
                //}
                //GUILayout.EndHorizontal();
            }
            GUILayout.EndVertical( );
            GUILayout.Space(4f);
        }
        GUILayout.EndHorizontal( );
        //GUILayout.FlexibleSpace( );

        return(foldoutState);
    }
Exemple #3
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));
                }
            }
        });
    }