Esempio n. 1
0
        public override void UpdateStyles(Selector selector, DictionaryDelta delta)
        {
            var components = GetComponentsMatchingSelector(selector);

            delta.Process();

            foreach (Component component in components)
            {
                // 1. for removals, clear the style
                foreach (string removal in delta.Removals.Keys)
                {
                    //Debug.Log("Removing -> " + removal);
                    component.ClearStyle(removal);
                }
                // 2. for additions, set the style
                foreach (KeyValuePair <string, object> addition in delta.Additions)
                {
                    //Debug.Log("Adding -> " + addition + ", " + prop.Value);
                    component.SetStyle(addition.Key, addition.Value);                     // ?? StyleDeclaration.UNDEFINED); // StyleDeclaration.UNDEFINED 20131122
                }
                // 3. for updates, set the style
                foreach (KeyValuePair <string, object> update in delta.Updates)
                {
                    //Debug.Log("Setting -> " + update.Name + ", " + update.Value);
                    component.SetStyle(update.Key, update.Value);
                }
            }

            /* Signalize to GUI Editor that it should render the overlay for each component */
            if (null != _selectorSignal)
            {
                _selectorSignal.Emit(components);
            }
        }
        public override void UpdateStyles(Selector selector, DictionaryDelta delta)
        {
            // nothing yet
            //Debug.Log("UpdateStyles: " + delta);
            var components = GetComponentsMatchingSelector(selector);

            delta.Process();

            foreach (Component component in components)
            {
                // TODO: find out which property changed and its value
                //var changedProp = _modifiedPropertyName;
                //component.SetStyle("paddingLeft", 30);

                // 1. for removals, clear the style
                foreach (string removal in delta.Removals.Keys)
                {
                    // TODO: set the default value (if exists)
                    // (default values should be implemented via the attribute)
                    //Debug.Log("Removing -> " + removal);
                    MemberWrapper wrapper = new MemberWrapper(component.GetType(), removal);

                    object value      = null;
                    var    attributes = CoreReflector.GetMemberAttributes <StyleAttribute>(wrapper.MemberInfo);
                    if (attributes.Count > 0)
                    {
                        value = attributes[0].GetDefault();
                    }

                    wrapper.SetValue(component, value);
                }
                // 2. for additions, set the style
                foreach (KeyValuePair <string, object> addition in delta.Additions)
                {
                    MemberWrapper wrapper = new MemberWrapper(component.GetType(), addition.Key);
                    wrapper.SetValue(component, addition.Value);
                }

                // 3. for updates, set the style
                foreach (KeyValuePair <string, object> update in delta.Updates)
                {
                    MemberWrapper wrapper = new MemberWrapper(component.GetType(), update.Key);
                    wrapper.SetValue(component, update.Value);
                }

                UnityComponentStylingGizmo.Show(components);
            }
        }
		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;
			}
		}
Esempio n. 4
0
 public abstract void UpdateStyles(Selector selector, DictionaryDelta delta);
Esempio n. 5
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);
            }
        }