public void AddNewAnchor(Color c, int id, bool affectValues = true)
        {
            PWAnchorMultiData tmp          = new PWAnchorMultiData(c);
            PWValues          anchorValues = anchorInstance as PWValues;

            if (c == new Color(0, 0, 0, 0))
            {
                c = PWColorPalette.GetAnchorColorByType(type);
            }
            tmp.name       = (first != null) ? first.name : "";
            tmp.additional = true;
            tmp.id         = id;
            if (anchorValues != null && anchorValues.Count == multipleValueCount)
            {
                multipleValueCount++;
            }
            // Debug.Log("new anchor with id: " + id);
            //add an object to the PWValues list:
            if (affectValues && anchorValues != null)
            {
                anchorValues.Add(null);
            }

            multi.Add(tmp);
        }
Пример #2
0
        public void TextField(string prefix, Vector2 textPosition, ref string text, bool editable = false, GUIStyle textFieldStyle = null)
        {
            Rect textRect = new Rect(textPosition, Vector2.zero);
            var  e        = Event.current;

            string controlName = "textfield-" + text.GetHashCode().ToString();

            var fieldSettings = GetGUISettingData(() => {
                return(new PWGUISettings());
            });

            Vector2 nameSize = textFieldStyle.CalcSize(new GUIContent(text));

            textRect.size = nameSize;

            if (!String.IsNullOrEmpty(prefix))
            {
                Vector2 prefixSize = textFieldStyle.CalcSize(new GUIContent(prefix));
                Rect    prefixRect = textRect;

                textRect.position += new Vector2(prefixSize.x, 0);
                prefixRect.size    = prefixSize;
                GUI.Label(prefixRect, prefix);
            }

            Rect iconRect    = new Rect(textRect.position + new Vector2(nameSize.x + 10, 0), new Vector2(17, 17));
            bool editClickIn = (editable && e.type == EventType.MouseDown && e.button == 0 && iconRect.Contains(e.mousePosition));

            if (editClickIn)
            {
                fieldSettings.Invert(text);
            }

            if (editable)
            {
                GUI.color = (fieldSettings.active) ? PWColorPalette.GetColor("selected") : Color.white;
                GUI.DrawTexture(iconRect, ic_edit);
                GUI.color = Color.white;
            }

            if (fieldSettings.active == true)
            {
                Color oldCursorColor = GUI.skin.settings.cursorColor;
                GUI.skin.settings.cursorColor = Color.white;
                GUI.SetNextControlName(controlName);
                text = GUI.TextField(textRect, text, textFieldStyle);
                GUI.skin.settings.cursorColor = oldCursorColor;
                if (e.isKey && fieldSettings.active)
                {
                    if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter)
                    {
                        fieldSettings.InActive();
                        e.Use();
                    }
                    else if (e.keyCode == KeyCode.Escape)
                    {
                        text = (string)fieldSettings.InActive();
                        e.Use();
                    }
                }
            }
            else
            {
                GUI.Label(textRect, text, textFieldStyle);
            }

            bool editClickOut = (editable && e.type == EventType.MouseDown && e.button == 0 && !iconRect.Contains(e.mousePosition));

            if (editClickOut && fieldSettings.active)
            {
                fieldSettings.InActive();
                e.Use();
            }

            if (editClickIn)
            {
                GUI.FocusControl(controlName);
                var te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
                te.SelectAll();
                e.Use();
            }
        }
Пример #3
0
        public void ColorPicker(GUIContent prefix, Rect rect, ref Color color, bool displayColorPreview = true, bool previewOnIcon = false)
        {
            var   e           = Event.current;
            Rect  iconRect    = rect;
            int   icColorSize = 18;
            Color localColor  = color;

            var fieldSettings = GetGUISettingData(() => {
                PWGUISettings colorSettings = new PWGUISettings();

                colorSettings.c = (SerializableColor)localColor;

                return(colorSettings);
            });

            if (fieldSettings.active)
            {
                int colorPickerWidth  = 170;
                int colorPickerHeight = 270;

                GUI.changed = true;
                PWPopup.AddToRender(fieldSettings, "Color picker", () =>
                {
                    if (e.type == EventType.KeyDown)
                    {
                        if (e.keyCode == KeyCode.Return || e.keyCode == KeyCode.KeypadEnter)
                        {
                            fieldSettings.InActive();
                            e.Use();
                        }
                        if (e.keyCode == KeyCode.Escape)
                        {
                            fieldSettings.c = (SerializableColor)(Color)fieldSettings.InActive();
                            e.Use();
                        }
                    }

                    //draw the color picker window
                    colorPickerStyle = GUI.skin.FindStyle("ColorPicker");
                    {
                        Rect localColorPickerRect = new Rect(Vector2.zero, new Vector2(colorPickerWidth, colorPickerHeight));
                        GUILayout.Label(colorPickerTexture, GUILayout.Width(150), GUILayout.Height(150));

                        Vector2 colorPickerMousePosition = e.mousePosition - new Vector2(colorPickerStyle.padding.left + 1, colorPickerStyle.padding.top + 5);

                        if (colorPickerMousePosition.x >= 0 && colorPickerMousePosition.y >= 0 && colorPickerMousePosition.x <= 150 && colorPickerMousePosition.y <= 150)
                        {
                            if (e.type == EventType.MouseDown || e.type == EventType.MouseDrag)
                            {
                                Vector2 textureCoord        = colorPickerMousePosition * (colorPickerTexture.width / 150f);
                                textureCoord.y              = colorPickerTexture.height - textureCoord.y;
                                fieldSettings.c             = (SerializableColor)colorPickerTexture.GetPixel((int)textureCoord.x, (int)textureCoord.y);
                                fieldSettings.thumbPosition = colorPickerMousePosition + new Vector2(6, 9);
                            }
                        }

                        Rect colorPickerThumbRect = new Rect(fieldSettings.thumbPosition, new Vector2(8, 8));
                        GUI.DrawTexture(colorPickerThumbRect, colorPickerThumb);

                        byte r, g, b, a;
                        PWColorPalette.ColorToByte(fieldSettings.c, out r, out g, out b, out a);
                        EditorGUIUtility.labelWidth = 20;
                        r = (byte)EditorGUILayout.IntSlider("R", r, 0, 255);
                        g = (byte)EditorGUILayout.IntSlider("G", g, 0, 255);
                        b = (byte)EditorGUILayout.IntSlider("B", b, 0, 255);
                        a = (byte)EditorGUILayout.IntSlider("A", a, 0, 255);
                        fieldSettings.c             = (SerializableColor)PWColorPalette.ByteToColor(r, g, b, a);
                        EditorGUIUtility.labelWidth = 0;

                        EditorGUILayout.Space();

                        //hex field
                        int hex = PWColorPalette.ColorToHex(fieldSettings.c, false);                         //get color without alpha
                        EditorGUIUtility.labelWidth = 80;
                        EditorGUI.BeginChangeCheck();
                        string hexColor = EditorGUILayout.TextField("Hex color", hex.ToString("X6"));
                        if (EditorGUI.EndChangeCheck())
                        {
                            a = 255;
                        }
                        EditorGUIUtility.labelWidth = 0;
                        Regex reg = new Regex(@"[^A-F0-9 -]");
                        hexColor  = reg.Replace(hexColor, "");
                        hexColor  = hexColor.Substring(0, Mathf.Min(hexColor.Length, 6));
                        if (hexColor == "")
                        {
                            hexColor = "0";
                        }
                        hex             = int.Parse(a.ToString("X2") + hexColor, System.Globalization.NumberStyles.HexNumber);
                        fieldSettings.c = (SerializableColor)PWColorPalette.HexToColor(hex, false);

                        if (e.isMouse && localColorPickerRect.Contains(e.mousePosition))
                        {
                            e.Use();
                        }
                    }
                }, colorPickerWidth);
            }

            color = fieldSettings.c;

            //draw the icon
            Rect colorPreviewRect = iconRect;

            if (displayColorPreview)
            {
                int width = (int)rect.width;
                int colorPreviewPadding = 5;

                Vector2 prefixSize = Vector2.zero;
                if (prefix != null && !String.IsNullOrEmpty(prefix.text))
                {
                    prefixSize    = GUI.skin.label.CalcSize(prefix);
                    prefixSize.x += 5;                     //padding of 5 pixels
                    colorPreviewRect.position += new Vector2(prefixSize.x, 0);
                    Rect prefixRect = new Rect(iconRect.position, prefixSize);
                    GUI.Label(prefixRect, prefix);
                }
                colorPreviewRect.size = new Vector2(width - icColorSize - prefixSize.x - colorPreviewPadding, 16);
                iconRect.position    += new Vector2(colorPreviewRect.width + prefixSize.x + colorPreviewPadding, 0);
                iconRect.size         = new Vector2(icColorSize, icColorSize);
                EditorGUIUtility.DrawColorSwatch(colorPreviewRect, color);
            }

            //actions if clicked on/outside of the icon
            if (previewOnIcon)
            {
                GUI.color = color;
            }
            GUI.DrawTexture(iconRect, ic_color);
            GUI.color = Color.white;
            if (e.type == EventType.MouseDown && e.button == 0)
            {
                if (iconRect.Contains(e.mousePosition) || colorPreviewRect.Contains(e.mousePosition))
                {
                    fieldSettings.Active(color);
                    e.Use();
                }
                else if (fieldSettings.active)
                {
                    fieldSettings.InActive();
                    e.Use();
                }
            }
        }