Ejemplo n.º 1
0
        public static StyleValueType[] GetCompatibleStyleValueTypes(VariableEditingHandler handler)
        {
            var field = handler.inspector.styleFields.FindStylePropertyInfo(handler.styleName);

            if (field == null)
            {
                return new[] { StyleValueType.Invalid }
            }
            ;

            var val     = field.GetValue(handler.inspector.currentVisualElement.computedStyle, null);
            var valType = val == null ? typeof(object) : val.GetType();

            if (BuilderInspectorStyleFields.IsComputedStyleFloat(val) ||
                BuilderInspectorStyleFields.IsComputedStyleInt(val) ||
                BuilderInspectorStyleFields.IsComputedStyleLength(val) ||
                BuilderInspectorStyleFields.IsComputedStyleList <TimeValue>(val))
            {
                return(new[] { StyleValueType.Float, StyleValueType.Dimension });
            }

            if (BuilderInspectorStyleFields.IsComputedStyleColor(val))
            {
                return(new[] { StyleValueType.Color });
            }

            if (BuilderInspectorStyleFields.IsComputedStyleFont(val, handler.styleName))
            {
                return(new[] { StyleValueType.AssetReference, StyleValueType.ResourcePath });
            }

            if (BuilderInspectorStyleFields.IsComputedStyleBackground(val))
            {
                return(new[] { StyleValueType.ScalableImage, StyleValueType.AssetReference, StyleValueType.ResourcePath });
            }

            if (BuilderInspectorStyleFields.IsComputedStyleCursor(val) ||
                BuilderInspectorStyleFields.IsComputedStyleList <StylePropertyName>(val))
            {
                return(new[] { StyleValueType.Enum, StyleValueType.ScalableImage, StyleValueType.AssetReference, StyleValueType.ResourcePath });
            }

            if (BuilderInspectorStyleFields.IsComputedStyleEnum(val, valType) ||
                BuilderInspectorStyleFields.IsComputedStyleList <EasingFunction>(val))
            {
                return(new[] { StyleValueType.Enum });
            }

            return(new[] { StyleValueType.Invalid });
        }

        bool Matcher(string filter, VariableInfo data)
        {
            var text = data.name;

            return(string.IsNullOrEmpty(text) ? false : text.Contains(filter));
        }
        public void RefreshField()
        {
            if (m_Row == null || m_Inspector == null)
            {
                return;
            }

            var cShartStyleName = BuilderInspectorStyleFields.ConvertUssStyleNameToCSharpStyleName(styleName);
            var property        = BuilderInspectorStyleFields.GetStyleProperty(m_Inspector.currentRule, cShartStyleName);

            // Disable completion on text field-based property fields when editing inline styles
            if (m_CompleterOnTarget != null)
            {
                m_CompleterOnTarget.enabled = BuilderSharedStyles.IsSelectorElement(m_Inspector.currentVisualElement);
            }

            if (property != null)
            {
                if (property.IsVariable())
                {
                    targetField.AddToClassList(BuilderConstants.InspectorLocalStyleVariableClassName);
                }
                else
                {
                    targetField.RemoveFromClassList(BuilderConstants.InspectorLocalStyleVariableClassName);
                }
            }
            else if (!string.IsNullOrEmpty(styleName))
            {
                bool found        = false;
                var  styleSheet   = m_Inspector.styleSheet;
                var  matchedRules = m_Inspector.matchingSelectors.matchedRulesExtractor.selectedElementRules;

                // Search for a variable in the best matching rule
                for (var i = matchedRules.Count - 1; i >= 0; --i)
                {
                    var matchRecord  = matchedRules.ElementAt(i).matchRecord;
                    var ruleProperty = styleSheet.FindProperty(matchRecord.complexSelector.rule, styleName);

                    if (ruleProperty != null)
                    {
                        if (ruleProperty.IsVariable())
                        {
                            targetField.AddToClassList(BuilderConstants.InspectorLocalStyleVariableClassName);
                            found = true;
                        }

                        break;
                    }
                }

                if (!found)
                {
                    targetField.RemoveFromClassList(BuilderConstants.InspectorLocalStyleVariableClassName);
                }
            }
        }
        public static string GetBoundVariableName(VariableEditingHandler handler)
        {
            var    styleName = handler.targetField.GetProperty(BuilderConstants.InspectorStylePropertyNameVEPropertyName) as string;
            var    property  = BuilderInspectorStyleFields.GetLastStyleProperty(handler.m_Inspector.currentRule, styleName);
            string varName   = null;

            // Find the name of the variable bound to the field
            if (property != null)
            {
                using (var manipulator = handler.m_Inspector.styleSheet.GetStylePropertyManipulator(
                           handler.m_Inspector.currentVisualElement, handler.m_Inspector.currentRule, property.name,
                           handler.m_Inspector.document.fileSettings.editorExtensionMode))
                {
                    var displayIndex = handler.index;
                    displayIndex = AdjustDisplayIndexForTransitions(displayIndex, manipulator);
                    if (displayIndex >= 0 && manipulator.IsVariableAtIndex(displayIndex))
                    {
                        varName = manipulator.GetVariableNameAtIndex(displayIndex);
                    }
                }
            }
            else if (!handler.editingEnabled)
            {
                var matchedRules = handler.m_Inspector.matchingSelectors.matchedRulesExtractor.selectedElementRules;

                for (var i = matchedRules.Count - 1; i >= 0; --i)
                {
                    var matchRecord  = matchedRules.ElementAt(i).matchRecord;
                    var ruleProperty = matchRecord.sheet.FindLastProperty(matchRecord.complexSelector.rule, styleName);

                    if (ruleProperty != null)
                    {
                        using (var manipulator = matchRecord.sheet.GetStylePropertyManipulator(
                                   handler.m_Inspector.currentVisualElement, matchRecord.complexSelector.rule, ruleProperty.name,
                                   handler.m_Inspector.document.fileSettings.editorExtensionMode))
                        {
                            var displayIndex = handler.index;
                            displayIndex = AdjustDisplayIndexForTransitions(displayIndex, manipulator);

                            if (displayIndex >= 0 && manipulator.IsVariableAtIndex(displayIndex))
                            {
                                varName = manipulator.GetVariableNameAtIndex(displayIndex);
                                break;
                            }
                        }
                    }
                }
            }

            return(varName);
        }
        static string GetBoundVariableName(VariableEditingHandler handler)
        {
            var    styleName = handler.targetField.GetProperty(BuilderConstants.InspectorStylePropertyNameVEPropertyName) as string;
            var    property  = BuilderInspectorStyleFields.GetStyleProperty(handler.m_Inspector.currentRule, styleName);
            string varName   = null;

            // Find the name of the variable bound to the field
            if (property != null)
            {
                if (property.IsVariable())
                {
                    varName = handler.m_Inspector.styleSheet.ReadVariable(property);
                }
            }
            else if (!handler.editingEnabled)
            {
                var matchedRules = handler.m_Inspector.matchingSelectors.matchedRulesExtractor.selectedElementRules;

                for (var i = matchedRules.Count - 1; i >= 0; --i)
                {
                    var matchRecord  = matchedRules.ElementAt(i).matchRecord;
                    var ruleProperty = matchRecord.sheet.FindProperty(matchRecord.complexSelector.rule, styleName);

                    if (ruleProperty != null)
                    {
                        if (ruleProperty.IsVariable())
                        {
                            varName = matchRecord.sheet.ReadVariable(ruleProperty);
                            break;
                        }
                    }
                }
            }

            return(varName);
        }
Ejemplo n.º 5
0
        public BuilderInspector(BuilderPaneWindow paneWindow, BuilderSelection selection)
        {
            // Yes, we give ourselves a view data key. Don't do this at home!
            viewDataKey = "unity-ui-builder-inspector";

            // Init External References
            m_Selection  = selection;
            m_PaneWindow = paneWindow;

            // Load Template
            var template = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(
                BuilderConstants.UIBuilderPackagePath + "/Inspector/BuilderInspector.uxml");

            template.CloneTree(this);

            // Get the scroll view.
            // HACK: ScrollView is not capable of remembering a scroll position for content that changes often.
            // The main issue is that we expande/collapse/display/hide different parts of the Inspector
            // all the time so initially the ScrollView is empty and it restores the scroll position to zero.
            m_ScrollView = this.Q <ScrollView>("inspector-scroll-view");
            m_ScrollView.contentContainer.RegisterCallback <GeometryChangedEvent>(OnScrollViewContentGeometryChange);
            m_ScrollView.verticalScroller.valueChanged += (newValue) =>
            {
                CacheScrollPosition(newValue, m_ScrollView.verticalScroller.highValue);
                SaveViewData();
            };

            // Load styles.
            AddToClassList(s_UssClassName);
            styleSheets.Add(AssetDatabase.LoadAssetAtPath <StyleSheet>(BuilderConstants.InspectorUssPathNoExt + ".uss"));
            if (EditorGUIUtility.isProSkin)
            {
                styleSheets.Add(AssetDatabase.LoadAssetAtPath <StyleSheet>(BuilderConstants.InspectorUssPathNoExt + "Dark.uss"));
            }
            else
            {
                styleSheets.Add(AssetDatabase.LoadAssetAtPath <StyleSheet>(BuilderConstants.InspectorUssPathNoExt + "Light.uss"));
            }

            // Matching Selectors
            m_MatchingSelectors = new BuilderInspectorMatchingSelectors(this);

            // Style Fields
            m_StyleFields = new BuilderInspectorStyleFields(this);

            // Sections
            m_Sections = new List <VisualElement>();

            // Nothing Selected Section
            m_NothingSelectedSection = this.Q <Label>("nothing-selected-label");
            m_Sections.Add(m_NothingSelectedSection);

            // Canvas Section
            m_CanvasSection = new BuilderInspectorCanvas(this);
            m_Sections.Add(m_CanvasSection.root);

            // StyleSheet Section
            m_StyleSheetSection = new BuilderInspectorStyleSheet(this);
            m_Sections.Add(m_StyleSheetSection.root);

            // Style Selector Section
            m_SelectorSection = new BuilderInspectorSelector(this);
            m_Sections.Add(m_SelectorSection.root);

            // Attributes Section
            m_AttributesSection = new BuilderInspectorAttributes(this);
            m_Sections.Add(m_AttributesSection.root);

            // Inherited Styles Section
            m_InheritedStyleSection = new BuilderInspectorInheritedStyles(this, m_MatchingSelectors);
            m_Sections.Add(m_InheritedStyleSection.root);

            // Local Styles Section
            m_LocalStylesSection = new BuilderInspectorLocalStyles(this, m_StyleFields);
            m_Sections.Add(m_LocalStylesSection.root);

            // This will take into account the current selection and then call RefreshUI().
            SelectionChanged();

            // Forward focus to the panel header.
            this.Query().Where(e => e.focusable).ForEach((e) => AddFocusable(e));
        }
        public BuilderInspectorLocalStyles(BuilderInspector inspector, BuilderInspectorStyleFields styleFields)
        {
            m_Inspector   = inspector;
            m_StyleFields = styleFields;

            m_StyleFields.updateFlexColumnGlobalState         = UpdateFlexColumnGlobalState;
            m_StyleFields.updateStyleCategoryFoldoutOverrides = UpdateStyleCategoryFoldoutOverrides;

            m_LocalStylesSection = m_Inspector.Q <PersistedFoldout>("inspector-local-styles-foldout");

            // We need to hide new Text Asset style property fields in any Unity version older than 2021.1.
#if !PACKAGE_TEXT_CORE || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 || UNITY_2020_3
            m_LocalStylesSection.Query(className: "unity-builder-font-asset-property-container").ForEach(e => e.style.display = DisplayStyle.None);
#else
            m_LocalStylesSection.Query(className: "unity-builder-no-font-asset-property-container").ForEach(e => e.style.display = DisplayStyle.None);
#endif

            var styleCategories = m_LocalStylesSection.Query <PersistedFoldout>(
                className: "unity-builder-inspector__style-category-foldout").ToList();

            foreach (var styleCategory in styleCategories)
            {
                styleCategory.Q <VisualElement>(null, PersistedFoldout.headerUssClassName)
                .AddManipulator(new ContextualMenuManipulator(StyleCategoryContextualMenu));

                var categoryStyleFields = new List <VisualElement>();
                var styleRows           = styleCategory.Query <BuilderStyleRow>().ToList();
                foreach (var styleRow in styleRows)
                {
                    var bindingPath        = styleRow.bindingPath;
                    var currentStyleFields = styleRow.Query <BindableElement>().ToList();

                    if (styleRow.ClassListContains("unity-builder-double-field-row"))
                    {
                        m_StyleFields.BindDoubleFieldRow(styleRow);
                    }

                    foreach (var styleField in currentStyleFields)
                    {
                        // Avoid fields within fields.
                        if (styleField.parent != styleRow)
                        {
                            continue;
                        }

                        if (styleField is FoldoutNumberField)
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField as FoldoutNumberField);
                        }
                        else if (styleField is FoldoutColorField)
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField as FoldoutColorField);
                        }
                        else if (!string.IsNullOrEmpty(styleField.bindingPath))
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField.bindingPath, styleField);
                        }
                        else
                        {
                            BuilderStyleRow.ReAssignTooltipToChild(styleField);
                            m_StyleFields.BindStyleField(styleRow, bindingPath, styleField);
                        }

                        categoryStyleFields.Add(styleField);
                    }
                }
                m_StyleCategories.Add(styleCategory, categoryStyleFields);
            }
        }
Ejemplo n.º 7
0
        public BuilderInspectorLocalStyles(BuilderInspector inspector, BuilderInspectorStyleFields styleFields)
        {
            m_Inspector   = inspector;
            m_StyleFields = styleFields;

            m_StyleFields.updateFlexColumnGlobalState         = UpdateFlexColumnGlobalState;
            m_StyleFields.updateStyleCategoryFoldoutOverrides = UpdateStyleCategoryFoldoutOverrides;

            m_LocalStylesSection = m_Inspector.Q <PersistedFoldout>("inspector-local-styles-foldout");

            var styleCategories = m_LocalStylesSection.Query <PersistedFoldout>(
                className: "unity-builder-inspector__style-category-foldout").ToList();

            foreach (var styleCategory in styleCategories)
            {
                styleCategory.Q <VisualElement>(null, PersistedFoldout.headerUssClassName)
                .AddManipulator(new ContextualMenuManipulator(StyleCategoryContextualMenu));

                var categoryStyleFields = new List <BindableElement>();
                var styleRows           = styleCategory.Query <BuilderStyleRow>().ToList();
                foreach (var styleRow in styleRows)
                {
                    var bindingPath        = styleRow.bindingPath;
                    var currentStyleFields = styleRow.Query <BindableElement>().ToList();

#if UNITY_2019_2
                    if (styleRow.ClassListContains(BuilderConstants.Version_2019_3_OrNewer))
                    {
                        styleRow.AddToClassList(BuilderConstants.HiddenStyleClassName);
                        continue;
                    }
#else
                    if (styleRow.ClassListContains(BuilderConstants.Version_2019_2))
                    {
                        styleRow.AddToClassList(BuilderConstants.HiddenStyleClassName);
                        continue;
                    }
#endif

                    if (styleRow.ClassListContains("unity-builder-double-field-row"))
                    {
                        m_StyleFields.BindDoubleFieldRow(styleRow);
                    }

                    foreach (var styleField in currentStyleFields)
                    {
                        // Avoid fields within fields.
                        if (styleField.parent != styleRow)
                        {
                            continue;
                        }

                        if (styleField is FoldoutNumberField)
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField as FoldoutNumberField);
                        }
                        else if (styleField is FoldoutColorField)
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField as FoldoutColorField);
                        }
                        else if (!string.IsNullOrEmpty(styleField.bindingPath))
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField.bindingPath, styleField);
                        }
                        else
                        {
                            BuilderStyleRow.ReAssignTooltipToChild(styleField);
                            m_StyleFields.BindStyleField(styleRow, bindingPath, styleField);
                        }

                        categoryStyleFields.Add(styleField);
                    }
                }
                m_StyleCategories.Add(styleCategory, categoryStyleFields);
            }
        }
        public StyleProperty GetStyleProperty()
        {
            var cSharpStyleName = BuilderInspectorStyleFields.ConvertUssStyleNameToCSharpStyleName(styleName);

            return(BuilderInspectorStyleFields.GetLastStyleProperty(m_Inspector.currentRule, cSharpStyleName));
        }
Ejemplo n.º 9
0
        public BuilderInspectorLocalStyles(BuilderInspector inspector, BuilderInspectorStyleFields styleFields)
        {
            m_Inspector   = inspector;
            m_StyleFields = styleFields;

            m_StyleFields.updateFlexColumnGlobalState         = UpdateFlexColumnGlobalState;
            m_StyleFields.updateStyleCategoryFoldoutOverrides = UpdateStyleCategoryFoldoutOverrides;

            m_LocalStylesSection = m_Inspector.Q <PersistedFoldout>("inspector-local-styles-foldout");

            var styleCategories = m_LocalStylesSection.Query <PersistedFoldout>(
                className: "unity-builder-inspector__style-category-foldout").ToList();

            foreach (var styleCategory in styleCategories)
            {
                styleCategory.Q <VisualElement>(null, PersistedFoldout.headerUssClassName)
                .AddManipulator(new ContextualMenuManipulator(StyleCategoryContextualMenu));

                var categoryStyleFields = new List <VisualElement>();
                var styleRows           = styleCategory.Query <BuilderStyleRow>().ToList();
                foreach (var styleRow in styleRows)
                {
                    var bindingPath        = styleRow.bindingPath;
                    var currentStyleFields = styleRow.Query <BindableElement>().ToList();

                    if (styleRow.ClassListContains("unity-builder-double-field-row"))
                    {
                        m_StyleFields.BindDoubleFieldRow(styleRow);
                    }

                    foreach (var styleField in currentStyleFields)
                    {
                        // Avoid fields within fields.
                        if (styleField.parent != styleRow)
                        {
                            continue;
                        }

                        if (styleField is FoldoutNumberField)
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField as FoldoutNumberField);
                        }
                        else if (styleField is FoldoutColorField)
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField as FoldoutColorField);
                        }
                        else if (styleField is TransitionsListView transitionsListView)
                        {
                            GenerateTransitionPropertiesContent();
                            m_StyleFields.BindStyleField(styleRow, transitionsListView);
                        }
                        else if (!string.IsNullOrEmpty(styleField.bindingPath))
                        {
                            m_StyleFields.BindStyleField(styleRow, styleField.bindingPath, styleField);
                        }
                        else
                        {
                            m_StyleFields.BindStyleField(styleRow, bindingPath, styleField);
                        }

                        categoryStyleFields.Add(styleField);
                    }
                }
                m_StyleCategories.Add(styleCategory, categoryStyleFields);
            }
        }
        public void RefreshField()
        {
            if (m_Row == null || m_Inspector == null)
            {
                return;
            }

            var cShartStyleName = BuilderInspectorStyleFields.ConvertUssStyleNameToCSharpStyleName(styleName);

            // Disable completion on text field-based property fields when editing inline styles
            if (m_CompleterOnTarget != null)
            {
                m_CompleterOnTarget.enabled = BuilderSharedStyles.IsSelectorElement(m_Inspector.currentVisualElement);
            }

            var property = BuilderInspectorStyleFields.GetLastStyleProperty(m_Inspector.currentRule, cShartStyleName);

            if (property != null)
            {
                using (var manipulator = m_Inspector.styleSheet.GetStylePropertyManipulator(
                           m_Inspector.currentVisualElement, m_Inspector.currentRule, property.name,
                           m_Inspector.document.fileSettings.editorExtensionMode))
                {
                    var displayIndex = index;
                    displayIndex = AdjustDisplayIndexForTransitions(displayIndex, manipulator);

                    if (displayIndex >= 0 && manipulator.IsVariableAtIndex(displayIndex))
                    {
                        targetField.AddToClassList(BuilderConstants.InspectorLocalStyleVariableClassName);
                    }
                    else
                    {
                        targetField.RemoveFromClassList(BuilderConstants.InspectorLocalStyleVariableClassName);
                    }
                }
            }
            else if (!string.IsNullOrEmpty(styleName))
            {
                bool found        = false;
                var  styleSheet   = m_Inspector.styleSheet;
                var  matchedRules = m_Inspector.matchingSelectors.matchedRulesExtractor.selectedElementRules;

                // Search for a variable in the best matching rule
                for (var i = matchedRules.Count - 1; i >= 0; --i)
                {
                    var matchRecord  = matchedRules.ElementAt(i).matchRecord;
                    var ruleProperty = styleSheet.FindLastProperty(matchRecord.complexSelector.rule, styleName);

                    if (ruleProperty != null)
                    {
                        using (var manipulator = matchRecord.sheet.GetStylePropertyManipulator(
                                   m_Inspector.currentVisualElement, matchRecord.complexSelector.rule, ruleProperty.name,
                                   m_Inspector.document.fileSettings.editorExtensionMode))
                        {
                            var displayIndex = index;
                            displayIndex = AdjustDisplayIndexForTransitions(displayIndex, manipulator);

                            if (displayIndex >= 0 && manipulator.IsVariableAtIndex(displayIndex))
                            {
                                targetField.AddToClassList(BuilderConstants.InspectorLocalStyleVariableClassName);
                                found = true;
                            }
                        }

                        break;
                    }
                }

                if (!found)
                {
                    targetField.RemoveFromClassList(BuilderConstants.InspectorLocalStyleVariableClassName);
                }
            }
        }