void OnEnable()
 {
     if (swatchrColor == null)
     {
         swatchrColor = new SwatchrColor();
     }
     swatchrColor.OnColorChanged += Apply;
     swatchrColor.OnEnable();
 }
Beispiel #2
0
        void OnEnable()
        {
            if (sky == null)
            {
                sky = new SwatchrColor();
            }
            if (equator == null)
            {
                equator = new SwatchrColor();
            }
            if (ground == null)
            {
                ground = new SwatchrColor();
            }
            sky.OnColorChanged += Apply;
            sky.OnEnable();

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

            ground.OnColorChanged += Apply;
            ground.OnEnable();
        }
Beispiel #3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            SwatchrColor swatchrColor = (SwatchrColor)fieldInfo.GetValue(property.serializedObject.targetObject);
            Swatch       swatch       = swatchrColor.swatch;
            Color        color        = swatchrColor.color;

            if (swatchTexture == null)
            {
                                #if SWATCHR_VERBOSE
                Debug.LogWarning("[swatchrColorDrawer] creating swatch texture");
                                #endif
                swatchTexture = textureWithColor(color);
            }

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

            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   fullWidth         = position.width - swatchObjectPositionX + position.x;
            float swatchObjectWidth = fullWidth;
            float colorWidth        = 0.25f * fullWidth;
            if (swatch == null)
            {
                swatchObjectWidth *= 0.75f;
            }
            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);
            var colorField       = new Rect(position.x + position.width - colorWidth, position.y, colorWidth, 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();
                swatchrColor.swatch = swatchrColor._swatch;                 // hack which calls observer pattern
                UpdateActiveSwatch(swatchrColor.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();
                    swatchrColor.colorIndex = colorIndexProperty.intValue;                     // hack which calls observer pattern
                    UpdateActiveSwatch(swatchrColor.color);
                }
                // Draw Toggle
                //EditorGUI.PropertyField(usingSwatchGroupToggleR, usingSwatchGroupProperty, GUIContent.none);
                //usingSwatchGroupProperty.boolValue = EditorGUI.Toggle(usingSwatchGroupToggleR, usingSwatchGroupProperty.boolValue);

                if (paletteOpen)
                {
                    int swatchHash = swatch.cachedTexture.GetHashCode();
                    if (palleteTexture == null || palleteTextureCachedHash != swatchHash)
                    {
                                                #if SWATCHR_VERBOSE
                        Debug.LogWarning("[swatchrColorDrawer] creating pallete texture");
                                                #endif
                        palleteTexture           = textureWithColors(swatch.colors);
                        palleteTextureCachedHash = swatchHash;
                    }
                    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();
                        swatchrColor.colorIndex = colorIndex;                         //  calls observer pattern
                        UpdateActiveSwatch(swatchrColor.color);
                    }
                    else if (IsClick())
                    {
                        paletteOpen = false;
                        EditorUtility.SetDirty(property.serializedObject.targetObject);                         // Repaint
                    }
                }
            }
            else
            {
                EditorGUI.BeginChangeCheck();
                EditorGUI.PropertyField(colorField, overrideColorProperty, GUIContent.none);
                if (EditorGUI.EndChangeCheck())
                {
                    property.serializedObject.ApplyModifiedProperties();
                    swatchrColor.colorIndex = colorIndexProperty.intValue;                     // hack which calls observer pattern
                }
            }
            // Set indent back to what it was
            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }