internal void Update(Event e)
            {
                if (e.type == EventType.Layout)
                {
                    m_IsBetweenRepaint = true;
                }
                else if (e.type == EventType.Repaint)
                {
                    m_IsBetweenRepaint = false;
                }
                else if (e.type == EventType.Ignore && status == Status.Dragging)
                {
                    Reset();
                }

                if (!m_WantsUpdate || m_IsBetweenRepaint)
                {
                    return;
                }

                m_WantsUpdate = false;

                m_Status           = m_QueuedStatus;
                m_SourceIndex      = m_QueuedSourceIndex;
                m_DestinationIndex = m_QueuedDestinationIndex;

                PolybrushEditor.DoRepaint();
            }
Beispiel #2
0
        /// <summary>
        /// Draw everything concerning a single Prefab Palette in the Polybrush Window
        /// </summary>
        /// <param name="thumbSize">size of the preview textures</param>
        internal void OnInspectorGUI_Internal(int thumbSize)
        {
            PolyGUILayout.Label(m_GCCurrentPaletteLabel);

            serializedObject.Update();
            int  count        = prefabs != null ? prefabs.arraySize : 0;
            Rect dropDownZone = EditorGUILayout.BeginVertical(paletteStyle);

            dropDownZone.width = EditorGUIUtility.currentViewWidth;
            Rect backGroundRect = new Rect(dropDownZone);

            if (count != 0)
            {
                const int pad  = 4;
                int       size = thumbSize + pad;
                backGroundRect.x += 8;
                backGroundRect.y += 4;
                // The backgroundRect is currently as wide as the current view.
                // Adjust it to take the size of the vertical scrollbar and padding into account.
                backGroundRect.width -= (20 + (int)GUI.skin.verticalScrollbar.fixedWidth);
                // size variable will not take into account the padding to the right of all the thumbnails,
                // therefore it needs to be substracted from the width.
                int container_width = ((int)Mathf.Floor(backGroundRect.width) - (pad + 1));
                int columns         = (int)Mathf.Floor(container_width / size);
                int rows            = count / columns + (count % columns == 0 ? 0 : 1);
                if (rows < 1)
                {
                    rows = 1;
                }

                backGroundRect.height = 8 + rows * thumbSize + (rows - 1) * pad;
                EditorGUI.DrawRect(backGroundRect, EditorGUIUtility.isProSkin ? PolyGUI.k_BoxBackgroundDark : PolyGUI.k_BoxBackgroundLight);

                int currentIndex = 0;
                for (int i = 0; i < rows; i++)
                {
                    var horizontalRect = EditorGUILayout.BeginHorizontal();
                    for (int j = 0; j < columns; j++)
                    {
                        GUILayout.Space(pad);
                        var prefab          = prefabs.GetArrayElementAtIndex(currentIndex);
                        var previewRectXPos = pad + j * size + horizontalRect.x;
                        DrawPrefabPreview(prefab, currentIndex, thumbSize, previewRectXPos, horizontalRect.y);
                        currentIndex++;
                        if (currentIndex >= count)
                        {
                            break;
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                    GUILayout.Space(pad);
                }

                EditorGUILayout.EndVertical();

                if (selected.Count > 0)
                {
                    EditorGUILayout.LabelField(m_GCPlacementSettingsLabel);
                    GUILayout.Space(pad);
                }

                EditorGUILayout.BeginVertical();

                foreach (var i in selected)
                {
                    DrawSinglePrefabPlacementSettings(prefabs.GetArrayElementAtIndex(i), i);
                }

                /// Little Hack to get the Rect for dropping new prefabs
                Rect endDropDownZone = EditorGUILayout.BeginVertical();
                dropDownZone.height = endDropDownZone.y - dropDownZone.y;
                EditorGUILayout.EndVertical();
            }
            else
            {
                dropDownZone.height = thumbSize;
                var r = EditorGUILayout.BeginVertical(GUILayout.Height(thumbSize + 4));
                EditorGUI.DrawRect(r, EditorGUIUtility.isProSkin ? PolyGUI.k_BoxBackgroundDark : PolyGUI.k_BoxBackgroundLight);
                GUILayout.FlexibleSpace();
                GUILayout.Label("Drag Prefabs Here!", EditorStyles.centeredGreyMiniLabel);
                GUILayout.FlexibleSpace();
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndVertical();

            Event e = Event.current;

            if (dropDownZone.Contains(e.mousePosition) &&
                (e.type == EventType.DragUpdated || e.type == EventType.DragPerform) && DragAndDrop.objectReferences.Length > 0)
            {
                if (PolyEditorUtility.ContainsPrefabAssets(DragAndDrop.objectReferences))
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                }
                else
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
                }

                if (e.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();

                    IEnumerable <GameObject> dragAndDropReferences = DragAndDrop.objectReferences
                                                                     .Where(x => x is GameObject && PolyEditorUtility.IsPrefabAsset(x)).Cast <GameObject>();

                    foreach (GameObject go in dragAndDropReferences)
                    {
                        prefabs.InsertArrayElementAtIndex(prefabs.arraySize);
                        SerializedProperty last       = prefabs.GetArrayElementAtIndex(prefabs.arraySize - 1);
                        SerializedProperty gameObject = last.FindPropertyRelative("gameObject");
                        gameObject.objectReferenceValue = go;
                        PlacementSettings.PopulateSerializedProperty(last.FindPropertyRelative("settings"));
                        last.FindPropertyRelative("name").stringValue = go.name;
                    }
                }
            }

            if (e.type == EventType.KeyUp)
            {
                if (IsDeleteKey(e) && !GUI.GetNameOfFocusedControl().Contains("cancelbackspace"))
                {
                    PrefabPalette t = target as PrefabPalette;
                    t.RemoveRange(selected.ToArray());

                    selected.Clear();

                    if (onSelectionChanged != null)
                    {
                        onSelectionChanged(null);
                    }

                    PolybrushEditor.DoRepaint();
                }
            }

            serializedObject.ApplyModifiedProperties();
            redrawCounter += 1;
        }
        public override void OnInspectorGUI()
        {
            if (s_StyleForColorSwatch == null)
            {
                GenerateStaticGUIStyle();
            }

            Event e = Event.current;

            serializedObject.Update();

            Color current = m_CurrentProperty.colorValue;

            EditorGUI.BeginChangeCheck();
            current = EditorGUILayout.ColorField(current);
            if (EditorGUI.EndChangeCheck())
            {
                SetCurrent(current);
            }

            int swatchSize     = 12;
            int viewWidth      = (int)EditorGUIUtility.currentViewWidth - 12;
            int swatchesPerRow = viewWidth / swatchSize;

            swatchSize += (viewWidth % swatchSize) / swatchesPerRow;

            int mouseOverIndex   = k_DragOverNull;
            int index            = 0;
            int arraySize        = m_ColorsProperty.arraySize;
            int arraySizeWithAdd = m_ColorsProperty.arraySize + 1;

            using (new GUILayout.HorizontalScope())
            {
                for (int i = 0; i < arraySizeWithAdd; i++)
                {
                    bool isColorSwatch        = i < arraySize;
                    bool isActiveDrag         = m_Drag.status == DragState.Status.Dragging && m_Drag.destinationIndex == i && i != arraySizeWithAdd - 1;
                    SerializedProperty swatch = isColorSwatch ? m_ColorsProperty.GetArrayElementAtIndex(i) : null;
                    Rect swatchRect           = new Rect(-1f, -1f, 0f, 0f);

                    if (isActiveDrag)
                    {
                        GUILayout.Space(swatchSize + 4);
                        index = IncrementIndex(index, swatchesPerRow);
                    }

                    if (isColorSwatch)
                    {
                        GUI.backgroundColor = swatch.colorValue;

                        if (m_Drag.status != DragState.Status.Dragging || i != m_Drag.sourceIndex)
                        {
                            GUILayout.Label("", s_StyleForColorSwatch,
                                            GUILayout.MinWidth(swatchSize),
                                            GUILayout.MaxWidth(swatchSize),
                                            GUILayout.MinHeight(swatchSize),
                                            GUILayout.MaxHeight(swatchSize));;

                            swatchRect = GUILayoutUtility.GetLastRect();
                            index      = IncrementIndex(index, swatchesPerRow);
                        }
                    }
                    else
                    {
                        if (m_Drag.status != DragState.Status.Dragging)
                        {
                            GUI.backgroundColor = current;

                            EditorGUIUtility.SetIconSize(new Vector2(10, 11));

                            if (GUILayout.Button(m_GCAddColorSwatch, s_StyleForColorSwatch,
                                                 GUILayout.MinWidth(swatchSize),
                                                 GUILayout.MaxWidth(swatchSize),
                                                 GUILayout.Height(swatchSize),
                                                 GUILayout.MaxHeight(swatchSize)))
                            {
                                m_ColorsProperty.arraySize++;
                                SerializedProperty added = m_ColorsProperty.GetArrayElementAtIndex(m_ColorsProperty.arraySize - 1);
                                added.colorValue = current;
                            }
                        }
                        else
                        {
                            GUILayout.FlexibleSpace();
                            GUILayout.Label(m_GCRemoveSwatch);
                        }

                        swatchRect = GUILayoutUtility.GetLastRect();
                        index      = IncrementIndex(index, swatchesPerRow);
                    }

                    GUI.backgroundColor = Color.white;

                    if (swatchRect.Contains(e.mousePosition))
                    {
                        if (m_Drag.status == DragState.Status.Dragging)
                        {
                            mouseOverIndex = i >= m_Drag.destinationIndex ? i + 1 : i;
                        }
                        else
                        {
                            mouseOverIndex = i;
                        }

                        if (i == arraySize)
                        {
                            mouseOverIndex = k_DragOverTrash;
                        }

                        if (e.type == EventType.MouseDrag)
                        {
                            if (m_Drag.status == DragState.Status.Ready && isColorSwatch)
                            {
                                e.Use();
                                m_Drag.Init(mouseOverIndex, m_ColorsProperty.GetArrayElementAtIndex(mouseOverIndex), swatchRect.position - e.mousePosition);
                            }
                            else if (m_Drag.status == DragState.Status.Dragging)
                            {
                                m_Drag.destinationIndex = mouseOverIndex;
                            }
                        }
                        else if (e.type == EventType.MouseUp && m_Drag.status != DragState.Status.Dragging && isColorSwatch)
                        {
                            if (onSelectIndex != null)
                            {
                                SetCurrent(swatch.colorValue);
                            }
                        }
                    }
                }
            }

            // If drag was previously over the trash bin but has moved, reset the index to be over the last array entry
            // instead.
            if (e.type == EventType.MouseDrag &&
                m_Drag.status == DragState.Status.Dragging &&
                mouseOverIndex == k_DragOverNull &&
                m_Drag.destinationIndex == k_DragOverTrash)
            {
                m_Drag.destinationIndex = arraySize;
            }

            bool dragIsOverTrash = m_Drag.destinationIndex == k_DragOverTrash;

            if (m_Drag.status == DragState.Status.Dragging && m_Drag.swatch != null)
            {
                Rect r = new Rect(e.mousePosition.x + m_Drag.offset.x, e.mousePosition.y + m_Drag.offset.y, swatchSize, swatchSize);
                GUI.backgroundColor = m_Drag.swatch.colorValue;
                GUI.Label(r, "", s_StyleForColorSwatch);
                GUI.backgroundColor = Color.white;

                PolybrushEditor.DoRepaint();
                Repaint();
            }

            switch (e.type)
            {
            case EventType.MouseUp:
            {
                if (m_Drag.status == DragState.Status.Dragging)
                {
                    if (m_Drag.destinationIndex != k_DragOverNull)
                    {
                        if (dragIsOverTrash)
                        {
                            m_ColorsProperty.DeleteArrayElementAtIndex(m_Drag.sourceIndex);
                        }
                        else
                        {
                            m_ColorsProperty.MoveArrayElement(m_Drag.sourceIndex, m_Drag.destinationIndex > m_Drag.sourceIndex ? m_Drag.destinationIndex - 1 : m_Drag.destinationIndex);
                        }
                    }
                }

                m_Drag.Reset();

                PolybrushEditor.DoRepaint();
                Repaint();
            }
            break;
            }
            GUILayout.Space(EditorGUIUtility.standardVerticalSpacing);

            serializedObject.ApplyModifiedProperties();
            m_Drag.Update(e);
        }