コード例 #1
0
        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;
            }
        }
コード例 #2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            _nameStr  = property.FindPropertyRelative("Name").stringValue;
            _propType = GetType(property);

            Type type = null;
            SerializedProperty prop = GetSlot(property, ref type);

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

            bool mediaQueryPasses = false;

            //var oldColor = GUI.backgroundColor;
            if (isScanning)
            {
                var value = SerializedPropertyHelper.Read(property);
                //Debug.Log("value: " + value);
                mediaQueryPasses = MediaQueryManager.Instance.EvaluateQuery(_nameStr, value);
                //GUI.backgroundColor = mediaQueryPasses ? Color.green : Color.red;
            }

            /*_name = new GUIContent(" " + _nameStr, mediaQueryPasses ?
             *  TextureCache.Instance.MediaQueryPass : TextureCache.Instance.MediaQueryFail);*/

            _name = new GUIContent(" " + _nameStr);

            /* draw icon */
            GUI.Label(position, mediaQueryPasses ? TextureCache.Instance.MediaQueryPass : TextureCache.Instance.MediaQueryFail);

            /* move a bit to the right */
            position.x     += 16;
            position.width -= 16;
            position.height = 16; // height for the rest

            try
            {
                if (null != type)
                {
                    // 1. Object reference
                    prop.objectReferenceValue = EditorGUI.ObjectField(position, _name, prop.objectReferenceValue, type, false);
                }
                else
                {
                    if (_propType.IsEnum)
                    {
                        prop.stringValue = EnumHelper.Popup(position, _name, _propType, prop.stringValue);
                    }
                    else
                    {
                        // 2. Normal values
                        EditorGUI.PropertyField(position, prop, _name); //, new GUIContent(label));
                    }
                }
            }
            catch (Exception)
            {
                // NOTE: Silent fail!
                GUI.Label(position, new GUIContent(" " + _nameStr + " ???", TextureCache.Instance.MediaQuery));
            }

            /*if (isScanning)
             * {
             *  GUI.backgroundColor = oldColor;
             * }*/
        }