public static StyleComplexSelector AddSelector(
            this StyleSheet styleSheet, string complexSelectorStr, string undoMessage = null)
        {
            // Undo/Redo
            if (string.IsNullOrEmpty(undoMessage))
            {
                undoMessage = "New UI Style Selector";
            }
            Undo.RegisterCompleteObjectUndo(styleSheet, undoMessage);

            // Remove extra whitespace.
            var selectorSplit = complexSelectorStr.Split(' ');

            complexSelectorStr = String.Join(" ", selectorSplit);

            // Create rule.
            var rule = new StyleRule {
                line = -1
            };

            rule.properties = new StyleProperty[0];

            // Create selector.
            var complexSelector = new StyleComplexSelector();

            complexSelector.rule = rule;
            var initResult = StyleComplexSelectorExtensions.InitializeSelector(complexSelector, complexSelectorStr);

            if (!initResult)
            {
                return(null);
            }

            // Add rule to StyleSheet.
            var rulesList = styleSheet.rules.ToList();

            rulesList.Add(rule);
            styleSheet.rules          = rulesList.ToArray();
            complexSelector.ruleIndex = styleSheet.rules.Length - 1;

            // Add complex selector to list in stylesheet.
            var complexSelectorsList = styleSheet.complexSelectors.ToList();

            complexSelectorsList.Add(complexSelector);
            styleSheet.complexSelectors = complexSelectorsList.ToArray();

            return(complexSelector);
        }
Exemple #2
0
        public static bool TryCreateSelector(string complexSelectorStr, out StyleComplexSelector selector)
        {
            // Remove extra whitespace.
            var selectorSplit = complexSelectorStr.Split(' ');

            complexSelectorStr = string.Join(" ", selectorSplit);

            // Create rule.
            var rule = new StyleRule
            {
                line       = -1,
                properties = new StyleProperty[0]
            };

            // Create selector.
            selector = new StyleComplexSelector
            {
                rule = rule
            };
            var initResult = StyleComplexSelectorExtensions.InitializeSelector(selector, complexSelectorStr);

            return(initResult);
        }