Exemple #1
0
        internal static bool DrawColorPaletteColorPicker(object key, ColorPalette colorPalette, ref Color color, bool drawAlpha, bool stretchPalette, float width = 20, float height = 20, float margin = 0)
        {
            bool result = false;

            var rect = AllEditorGUI.BeginHorizontalAutoScrollBox(key, GUILayoutOptions.ExpandWidth(true).ExpandHeight(false));

            {
                if (stretchPalette)
                {
                    rect.width -= margin * colorPalette.Colors.Count - margin;
                    width       = Mathf.Max(width, rect.width / colorPalette.Colors.Count);
                }

                bool  isMouseDown = Event.current.type == EventType.MouseDown;
                var   innerRect   = GUILayoutUtility.GetRect((width + margin) * colorPalette.Colors.Count, height, GUIStyle.none);
                float spacing     = width + margin;
                var   cellRect    = innerRect;
                cellRect.width = width;

                for (int i = 0; i < colorPalette.Colors.Count; i++)
                {
                    cellRect.x = spacing * i;

                    if (drawAlpha)
                    {
                        EditorGUIUtility.DrawColorSwatch(cellRect, colorPalette.Colors[i]);
                    }
                    else
                    {
                        var c = colorPalette.Colors[i];
                        c.a = 1;
                        AllEditorGUI.DrawSolidRect(cellRect, c);
                    }

                    if (isMouseDown && cellRect.Contains(Event.current.mousePosition))
                    {
                        color       = colorPalette.Colors[i];
                        result      = true;
                        GUI.changed = true;
                        Event.current.Use();
                    }
                }
            }
            AllEditorGUI.EndHorizontalAutoScrollBox();
            return(result);
        }