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; PolyEditor.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(); PolyEditor.DoRepaint(); Repaint(); } break; } GUILayout.Space(EditorGUIUtility.standardVerticalSpacing); serializedObject.ApplyModifiedProperties(); m_Drag.Update(e); }
public override void OnInspectorGUI() { z_GUI.PushGUISkin(z_GUI.PolybrushSkin); Event e = Event.current; serializedObject.Update(); Color current = currentProperty.colorValue; z_GUI.PushUnitySkin(); EditorGUI.BeginChangeCheck(); current = EditorGUILayout.ColorField(current); if (EditorGUI.EndChangeCheck()) { SetCurrent(current); } z_GUI.PopGUISkin(); int swatchSize = 22; int viewWidth = (int)EditorGUIUtility.currentViewWidth - 12; int swatchesPerRow = viewWidth / (swatchSize + 4); swatchSize += (viewWidth % (swatchSize + 4)) / swatchesPerRow; GUILayout.BeginHorizontal(); int mouseOverIndex = DRAG_OVER_NULL; int index = 0; int arraySize = colorsProperty.arraySize; int arraySizeWithAdd = colorsProperty.arraySize + 1; for (int i = 0; i < arraySizeWithAdd; i++) { bool isColorSwatch = i < arraySize; bool isActiveDrag = drag.status == DragState.Status.Dragging && drag.destinationIndex == i && i != arraySizeWithAdd - 1; SerializedProperty swatch = isColorSwatch ? 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 (drag.status != DragState.Status.Dragging || i != drag.sourceIndex) { GUILayout.Label("", "ColorSwatch", GUILayout.MinWidth(swatchSize), GUILayout.MaxWidth(swatchSize), GUILayout.MinHeight(swatchSize), GUILayout.MaxHeight(swatchSize)); swatchRect = GUILayoutUtility.GetLastRect(); index = IncrementIndex(index, swatchesPerRow); } } else { if (drag.status != DragState.Status.Dragging) { GUI.backgroundColor = current; if (GUILayout.Button(gc_AddColorSwatch, "ColorSwatch", GUILayout.MinWidth(swatchSize), GUILayout.MaxWidth(swatchSize), GUILayout.MinHeight(swatchSize), GUILayout.MaxHeight(swatchSize))) { colorsProperty.arraySize++; SerializedProperty added = colorsProperty.GetArrayElementAtIndex(colorsProperty.arraySize - 1); added.colorValue = current; } } else { GUILayout.FlexibleSpace(); GUILayout.Label(z_IconUtility.GetIcon("Icon/Trashcan")); } swatchRect = GUILayoutUtility.GetLastRect(); index = IncrementIndex(index, swatchesPerRow); } GUI.backgroundColor = Color.white; if (swatchRect.Contains(e.mousePosition)) { if (drag.status == DragState.Status.Dragging) { mouseOverIndex = i >= drag.destinationIndex ? i + 1 : i; } else { mouseOverIndex = i; } if (i == arraySize) { mouseOverIndex = DRAG_OVER_TRASH; } if (e.type == EventType.MouseDrag) { if (drag.status == DragState.Status.Ready && isColorSwatch) { e.Use(); drag.Init(mouseOverIndex, colorsProperty.GetArrayElementAtIndex(mouseOverIndex), swatchRect.position - e.mousePosition); } else if (drag.status == DragState.Status.Dragging) { drag.destinationIndex = mouseOverIndex; } } else if (e.type == EventType.MouseUp && drag.status != DragState.Status.Dragging && isColorSwatch) { if (onSelectIndex != null) { SetCurrent(swatch.colorValue); } } } } GUILayout.EndHorizontal(); // 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 && drag.status == DragState.Status.Dragging && mouseOverIndex == DRAG_OVER_NULL && drag.destinationIndex == DRAG_OVER_TRASH) { drag.destinationIndex = arraySize; } bool dragIsOverTrash = drag.destinationIndex == DRAG_OVER_TRASH; if (drag.status == DragState.Status.Dragging && drag.swatch != null) { Rect r = new Rect(e.mousePosition.x + drag.offset.x, e.mousePosition.y + drag.offset.y, swatchSize, swatchSize); GUI.backgroundColor = drag.swatch.colorValue; GUI.Label(r, "", dragIsOverTrash ? "ColorSwatchGhost" : "ColorSwatch"); GUI.backgroundColor = Color.white; z_Editor.DoRepaint(); Repaint(); } switch (e.type) { case EventType.MouseUp: { if (drag.status == DragState.Status.Dragging) { if (drag.destinationIndex != DRAG_OVER_NULL) { if (dragIsOverTrash) { colorsProperty.DeleteArrayElementAtIndex(drag.sourceIndex); } else { colorsProperty.MoveArrayElement(drag.sourceIndex, drag.destinationIndex > drag.sourceIndex ? drag.destinationIndex - 1 : drag.destinationIndex); } } } drag.Reset(); z_Editor.DoRepaint(); Repaint(); } break; } serializedObject.ApplyModifiedProperties(); drag.Update(e); z_GUI.PopGUISkin(); }