Example #1
0
        public void UpdateStyles(Selector selector, DictionaryDelta delta)
        {
#if DEBUG
            if (DebugMode)
            {
                Debug.Log(string.Format("##### UpdateStyles [{0}] #####", selector));
            }
#endif
            // ReSharper disable once UnusedVariable
            /* Important */
            var connector = StylingOverlayConnector.Instance;

            // process styles - live
            if (EditorSettings.LiveStyling)
            {
                Gui.ProcessStyles();
            }

            if (!Application.isPlaying && ProcessEditModeChanges ||
                Application.isPlaying && ProcessPlayModeChanges)
            {
                Traverser.UpdateStyles(selector, delta);
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            position.height = StyleDeclarationInsetRenderer.HeaderHeight;

            SerializedProperty module = property.FindPropertyRelative("Module");

            _insetRenderer.Error = string.IsNullOrEmpty(module.stringValue) ? "Style module not defined" : null;

            SerializedProperty type      = property.FindPropertyRelative("Type");
            SerializedProperty classname = property.FindPropertyRelative("Class");
            SerializedProperty id        = property.FindPropertyRelative("Id");

            var selectorString = StyleSelector.BuildString(type.stringValue, classname.stringValue, id.stringValue);

            var title = selectorString;

            SerializedProperty mediaQueries = property.FindPropertyRelative("MediaQueries");
            int size = mediaQueries.arraySize;

            if (size > 0)
            {
                /*var mkList = new System.Collections.Generic.List<string>();
                 * for (int i = 0; i < size; i++)
                 * {
                 *      var query = mediaQueries.GetArrayElementAtIndex(i).FindPropertyRelative("Name").stringValue;
                 *      mkList.Add(query);
                 * }*/
                //title += string.Format(" @media {0}", string.Join(", ", mkList.ToArray()));
                title += string.Format(" @media");
            }

            var isScanning = EditorSettings.LiveStyling && Application.isPlaying;

            var passed = 0 == size || !isScanning;

            if (!passed)
            {
                // let's assume it will pass
                passed = true;

                // loop thgough each query
                for (int i = 0; i < size; i++)
                {
                    var query = mediaQueries.GetArrayElementAtIndex(i);
                    var name  = query.FindPropertyRelative("Name").stringValue;
                    var value = SerializedPropertyHelper.Read(query); //Debug.Log("value: " + value);
                    passed = MediaQueryManager.Instance.EvaluateQuery(name, value);

                    // when a single query doesn't pass, break the loop
                    if (!passed)
                    {
                        break;
                    }
                }
                //GUI.backgroundColor = mediaQueryPasses ? Color.green : Color.red;
            }

            _insetRenderer.MediaQueriesPassed = passed;

            CurrentType = type.stringValue;

            Rect pos2 = new Rect(position.x, position.y, position.width, property.isExpanded ? GetPropertyHeight(property, label) : position.height);

            /*label = */ EditorGUI.BeginProperty(position, label, property);

            property.isExpanded = _insetRenderer.RenderStart(pos2, title, property.isExpanded);

            EditorGUI.EndProperty();

            if (!property.isExpanded)
            {
                return;
            }

            position.y += StyleDeclarationInsetRenderer.HeaderHeight;

            if (!eDrivenStyleSheetEditor.EditorLocked)
            {
                position.y += ToolbarHeight;
            }

            position.width -= BorderMetrics.Right;

            //EditorGUI.indentLevel += 1;
            position.x += BorderMetrics.Left;

            /**
             * 1. Render media queries
             * */
            var numberOfMediaQueries = RenderMediaQueries(ref position, property);

            /**
             * 2. Render properties
             * */
            var numberOfProperties = RenderProperties(position, property);

            /*if (Event.current.type == EventType.ValidateCommand)
             * {
             *      Debug.Log(Event.current.type);
             * }*/

            /*var isUndo = Event.current.type == EventType.ValidateCommand &&
             *                        Event.current.commandName == "UndoRedoPerformed";*/

            if (GUI.changed /* || isUndo*/)
            {
                eDrivenStyleSheet edss = (eDrivenStyleSheet)property.serializedObject.targetObject;
                StyleSheet        ss   = edss.StyleSheet;

                StyleDeclaration declaration = null;
                foreach (StyleDeclaration dec in ss.Declarations)
                {
                    //Debug.Log(StyleSelector.BuildString(dec.Type, dec.Class, dec.Id));
                    /* Note: this is buggy, think about how to reference it without using the selector */
                    if (StyleSelector.BuildString(dec.Type, dec.Class, dec.Id) == selectorString)
                    {
                        //Debug.Log("Found declaration: " + dec);
                        declaration = dec;
                        break;
                    }
                }

                if (null == declaration)
                {
                    return;                     // nothing found?
                }

                /**
                 * 1. Get old properties
                 * */
                DictionaryDelta propertiesDelta = new DictionaryDelta();
                propertiesDelta.SnapshotBefore(declaration.Properties);

                /**
                 * 2. Apply changes
                 * */
                var propertiesChanged = property.serializedObject.ApplyModifiedProperties();

                /**
                 * 3. Get new properties
                 * */
                propertiesDelta.SnapshotAfter(declaration.Properties);

                /**
                 * 4. Process delta
                 * */
                propertiesDelta.Process();

                /**
                 * 1. Get old media queries
                 * */
                DictionaryDelta mediaQueriesDelta = new DictionaryDelta();
                mediaQueriesDelta.SnapshotBefore(declaration.MediaQueries);

                /**
                 * 2. Apply changes
                 * */
                var mediaQueriesChanged = property.serializedObject.ApplyModifiedProperties();

                /**
                 * 3. Get new properties
                 * */
                mediaQueriesDelta.SnapshotAfter(declaration.MediaQueries);

                /**
                 * 4. Process delta
                 * */
                propertiesDelta.Process();

                var selector = Selector.BuildSelector(type.stringValue, classname.stringValue, id.stringValue);

                var propertyCountChanged   = _oldNumberOfProperties != numberOfProperties;
                var mediaQueryCountChanged = _oldNumberOfMediaQueries != numberOfMediaQueries;

                if (propertiesChanged || mediaQueriesChanged || propertyCountChanged || mediaQueryCountChanged)
                {
                    var moduleId = property.FindPropertyRelative("Module").stringValue;
                    if (string.IsNullOrEmpty(moduleId))
                    {
                        Debug.Log("Module not defined (unknown module ID)");
                    }
                    else
                    {
                        StyleModuleManager.Instance.GetModule(moduleId).UpdateStyles(selector, propertiesDelta);
                    }

                    if (propertyCountChanged)
                    {
                        _oldNumberOfProperties = numberOfProperties;
                    }
                    if (mediaQueryCountChanged)
                    {
                        _oldNumberOfMediaQueries = numberOfMediaQueries;
                    }

                    StyleSheetPropertyDrawer.ShouldProcessStyles = true;
                }

                GUI.changed = false;
            }
        }