Пример #1
0
 void OnEnable()
 {
     if (swatchyColor == null)
     {
         swatchyColor = new SwatchyColor();
     }
     swatchyColor.OnColorChanged += Apply;
     swatchyColor.OnEnable();
 }
Пример #2
0
        void OnEnable()
        {
            if (sky == null)
            {
                sky = new SwatchyColor();
            }
            if (equator == null)
            {
                equator = new SwatchyColor();
            }
            if (ground == null)
            {
                ground = new SwatchyColor();
            }
            sky.OnColorChanged += Apply;
            sky.OnEnable();

            equator.OnColorChanged += Apply;
            equator.OnEnable();

            ground.OnColorChanged += Apply;
            ground.OnEnable();
        }
Пример #3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            SwatchyColor swatchyColor = (SwatchyColor)fieldInfo.GetValue(property.serializedObject.targetObject);
            Swatch       swatch       = swatchyColor.swatch;
            Color        color        = swatchyColor.color;

            if (swatchTexture == null)
            {
                swatchTexture = textureWithColor(color);
            }

            var swatchProperty     = property.FindPropertyRelative("_swatch");
            var colorIndexProperty = property.FindPropertyRelative("_colorIndex");

            position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

            var swatchSize = EditorGUIUtility.singleLineHeight;
            var keySize    = EditorGUIUtility.singleLineHeight * 1.25f;
            var spacing    = EditorGUIUtility.singleLineHeight * 0.5f;
            //var toggleSize				= EditorGUIUtility.singleLineHeight;
            var toggleSize            = 0;
            var swatchObjectPositionX = swatch == null ? position.x : position.x + swatchSize + keySize + toggleSize + spacing * 2;
            //var swatchObjectWidth = swatch == null ? position.width : position.width - swatchSize - keySize - spacing * 2;
            var swatchObjectWidth = position.width - swatchObjectPositionX + position.x;
            var swatchObjectRect  = new Rect(swatchObjectPositionX, position.y, swatchObjectWidth, EditorGUIUtility.singleLineHeight);
            var swatchRect        = new Rect(position.x, position.y, swatchSize, EditorGUIUtility.singleLineHeight);
            var colorIndexRect    = new Rect(swatchRect.position.x + swatchRect.width + spacing, position.y, keySize, EditorGUIUtility.singleLineHeight);

            EditorGUI.BeginProperty(position, label, property);


            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = 0;



            // Draw Swatch object
            EditorGUI.BeginChangeCheck();
            EditorGUI.PropertyField(swatchObjectRect, swatchProperty, GUIContent.none);
            if (EditorGUI.EndChangeCheck())
            {
                property.serializedObject.ApplyModifiedProperties();
                swatchyColor.swatch = swatchyColor._swatch;                 // hack which calls observer pattern
                UpdateActiveSwatch(swatchyColor.color);
            }

            if (swatch != null)
            {
                // Draw Color Field
                if (DrawTextureButton(swatchTexture, swatchRect))
                {
                    paletteOpen = !paletteOpen && swatch != null && swatch.colors != null && swatch.colors.Length > 0;
                }
                DrawBlackGrid(swatchRect.x, swatchRect.y, 1, 1, (int)EditorGUIUtility.singleLineHeight);

                // Draw Color index text field
                EditorGUI.BeginChangeCheck();
                EditorGUI.PropertyField(colorIndexRect, colorIndexProperty, GUIContent.none);
                if (EditorGUI.EndChangeCheck())
                {
                    property.serializedObject.ApplyModifiedProperties();
                    swatchyColor.colorIndex = colorIndexProperty.intValue;                     // hack which calls observer pattern
                    UpdateActiveSwatch(swatchyColor.color);
                }
                // Draw Toggle
                //EditorGUI.PropertyField(usingSwatchGroupToggleR, usingSwatchGroupProperty, GUIContent.none);
                //usingSwatchGroupProperty.boolValue = EditorGUI.Toggle(usingSwatchGroupToggleR, usingSwatchGroupProperty.boolValue);

                if (paletteOpen)
                {
                    if (palleteTexture == null)
                    {
                        palleteTexture = textureWithColors(swatch.colors);
                    }
                    var textureRect = new Rect(swatchRect.x, swatchRect.y + EditorGUIUtility.singleLineHeight + 3, palleteTexture.width * EditorGUIUtility.singleLineHeight, palleteTexture.height * EditorGUIUtility.singleLineHeight);
                    DrawTexture(palleteTexture, textureRect);
                    DrawBlackGrid(textureRect.x, textureRect.y, palleteTexture.width, palleteTexture.height, (int)EditorGUIUtility.singleLineHeight);

                    // listen to click
                    Event e = Event.current;
                    if (IsClickInRect(textureRect))
                    {
                        Vector2 rectClickPosition = e.mousePosition - textureRect.position;
                        int     cellXIndex        = (int)(rectClickPosition.x / EditorGUIUtility.singleLineHeight);
                        int     cellYIndex        = (int)(rectClickPosition.y / EditorGUIUtility.singleLineHeight);
                        int     colorIndex        = cellYIndex * palleteTexture.width + cellXIndex;
                        colorIndexProperty.intValue = colorIndex;
                        property.serializedObject.ApplyModifiedProperties();
                        swatchyColor.colorIndex = colorIndex;                         //  calls observer pattern
                        UpdateActiveSwatch(swatchyColor.color);
                    }
                    else if (IsClick())
                    {
                        paletteOpen = false;
                        EditorUtility.SetDirty(property.serializedObject.targetObject);                         // Repaint
                    }
                }
            }
            // Set indent back to what it was
            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }