Ejemplo n.º 1
0
        /// <summary>
        /// Not yet documented.
        /// </summary>
        protected override void DrawPropertyLayout(IPropertyValueEntry <Color> entry, ColorPaletteAttribute attribute, GUIContent label)
        {
            AllEditorGUI.BeginIndentedHorizontal();
            {
                var hideLabel = label == null;
                if (hideLabel == false)
                {
                    GUILayout.Label(label, GUILayoutOptions.Width(EditorGUIUtility.labelWidth - 4).ExpandWidth(false));
                }
                else
                {
                    GUILayout.Space(5);
                }

                PropertyContext <PaletteContext> contextBuffer;
                if (entry.Context.Get(this, "ColorPalette", out contextBuffer))
                {
                    contextBuffer.Value = new PaletteContext()
                    {
                        PaletteIndex = 0,
                        CurrentName  = attribute.PaletteName,
                        ShowAlpha    = attribute.ShowAlpha,
                        Names        = ColorPaletteManager.Instance.ColorPalettes.Select(x => x.Name).ToArray(),
                    };

                    // Get persistent context value
                    if (attribute.PaletteName == null)
                    {
                        contextBuffer.Value.PersistentName = entry.Context.GetPersistent <string>(this, "ColorPaletteName", null);
                        var list = contextBuffer.Value.Names.ToList();
                        contextBuffer.Value.CurrentName = contextBuffer.Value.PersistentName.Value;

                        if (contextBuffer.Value.CurrentName != null && list.Contains(contextBuffer.Value.CurrentName))
                        {
                            contextBuffer.Value.PaletteIndex = list.IndexOf(contextBuffer.Value.CurrentName);
                        }
                    }
                    else
                    {
                        contextBuffer.Value.NameGetter = new StringMemberHelper(entry.ParentType, attribute.PaletteName);
                    }
                }

                var context = contextBuffer.Value;

                //var colorPaletDropDown = entry.Context.Get(this, "colorPalette", 0);
                //var currentName = entry.Context.Get(this, "currentName", attribute.PaletteName);
                //var showAlpha = entry.Context.Get(this, "showAlpha", attribute.ShowAlpha);
                //var names = ColorPaletteManager.Instance.GetColorPaletteNames();

                ColorPalette colorPalette;
                var          rect = EditorGUILayout.BeginHorizontal();
                {
                    rect.x    -= 3;
                    rect.width = 25;

                    entry.SmartValue = AllEditorGUI.DrawColorField(rect, entry.SmartValue, false, context.ShowAlpha);
                    bool openInEditorShown = false;
                    GUILayout.Space(28);
                    AllEditorGUI.BeginInlineBox();
                    {
                        if (attribute.PaletteName == null || ColorPaletteManager.Instance.ShowPaletteName)
                        {
                            AllEditorGUI.BeginBoxHeader();
                            {
                                if (attribute.PaletteName == null)
                                {
                                    var newValue = EditorGUILayout.Popup(context.PaletteIndex, context.Names, GUILayoutOptions.ExpandWidth(true));
                                    if (context.PaletteIndex != newValue)
                                    {
                                        context.PaletteIndex         = newValue;
                                        context.CurrentName          = context.Names[newValue];
                                        context.PersistentName.Value = context.CurrentName;
                                        GUIHelper.RemoveFocusControl();
                                    }
                                }
                                else
                                {
                                    GUILayout.Label(context.CurrentName);
                                    GUILayout.FlexibleSpace();
                                }
                                openInEditorShown = true;
                                if (AllEditorGUI.IconButton(EditorIcons.SettingsCog))
                                {
                                    ColorPaletteManager.Instance.OpenInEditor();
                                }
                            }
                            AllEditorGUI.EndBoxHeader();
                        }

                        if (attribute.PaletteName == null)
                        {
                            colorPalette = ColorPaletteManager.Instance.ColorPalettes.FirstOrDefault(x => x.Name == context.Names[context.PaletteIndex]);
                        }
                        else
                        {
                            colorPalette = ColorPaletteManager.Instance.ColorPalettes.FirstOrDefault(x => x.Name == context.NameGetter.GetString(entry));
                        }

                        if (colorPalette == null)
                        {
                            GUILayout.BeginHorizontal();
                            {
                                if (attribute.PaletteName != null)
                                {
                                    if (GUILayout.Button("Create color palette: " + context.NameGetter.GetString(entry)))
                                    {
                                        ColorPaletteManager.Instance.ColorPalettes.Add(new ColorPalette()
                                        {
                                            Name = context.NameGetter.GetString(entry)
                                        });
                                        ColorPaletteManager.Instance.OpenInEditor();
                                    }
                                }
                            }
                            GUILayout.EndHorizontal();
                        }
                        else
                        {
                            context.CurrentName = colorPalette.Name;
                            context.ShowAlpha   = attribute.ShowAlpha && colorPalette.ShowAlpha;
                            if (openInEditorShown == false)
                            {
                                GUILayout.BeginHorizontal();
                            }
                            var color   = entry.SmartValue;
                            var stretch = ColorPaletteManager.Instance.StretchPalette;
                            var size    = ColorPaletteManager.Instance.SwatchSize;
                            var margin  = ColorPaletteManager.Instance.SwatchSpacing;
                            if (DrawColorPaletteColorPicker(entry, colorPalette, ref color, colorPalette.ShowAlpha, stretch, size, 20, margin))
                            {
                                entry.SmartValue = color;
                                //entry.ApplyChanges();
                            }
                            if (openInEditorShown == false)
                            {
                                GUILayout.Space(4);
                                if (AllEditorGUI.IconButton(EditorIcons.SettingsCog))
                                {
                                    ColorPaletteManager.Instance.OpenInEditor();
                                }
                                GUILayout.EndHorizontal();
                            }
                        }
                    }
                    AllEditorGUI.EndInlineBox();
                }
                EditorGUILayout.EndHorizontal();
            }

            AllEditorGUI.EndIndentedHorizontal();
        }