public void Show(VariableEditingHandler handler, VariableInfo info)
 {
     m_CurrentHandler = handler;
     m_View.SetInfo(info);
     anchoredControl = handler.variableField;
     Show();
 }
        public VariableInfoTooltip()
        {
            AddToClassList(s_UssClassName);
            m_View = new VariableInfoView();

            VisualElement warningContainer = new VisualElement();

            warningContainer.AddToClassList(s_WarningUssClassName);

            var warningIcon  = new Image();
            var warningLabel = new Label(BuilderConstants.VariableNotSupportedInInlineStyleMessage);

            warningContainer.Add(warningIcon);
            warningContainer.Add(warningLabel);

            m_View.Add(warningContainer);

            Add(m_View);

            // TODO: Will need to bring this back once we can also do the dragger at the same time.
#if false
            RegisterCallback <FocusOutEvent>(e => {
                if (m_CurrentHandler != null)
                {
                    Hide();
                    m_CurrentHandler = null;
                }
            });
#endif
            focusable = true;
        }
Esempio n. 3
0
        public VariableCompleter(VariableEditingHandler handler)
            : base(handler.variableField != null ? handler.variableField.textField : null)
        {
            m_Handler = handler;
            getFilterFromTextCallback = (text) => text != null?text.TrimStart('-') : null;

            dataSourceCallback = () =>
            {
                return(StyleVariableUtilities.GetAllAvailableVariables(handler.inspector.currentVisualElement, GetCompatibleStyleValueTypes(handler), handler.inspector.document.fileSettings.editorExtensionMode));
            };
            makeItem = () =>
            {
                var item = new VisualElement();

                item.AddToClassList(s_ItemUssClassName);
                var nameLabel       = new Label();
                var editorOnlyLabel = new Label(BuilderConstants.EditorOnlyTag);
                nameLabel.AddToClassList(s_ItemNameLabelUssClassName);
                nameLabel.name = s_ItemNameLabelName;
#if !UNITY_2019_4
                // Cannot use USS because no way to do version checks in USS.
                // This is not available in 2019.4.
                nameLabel.style.textOverflow = TextOverflow.Ellipsis;
#endif
                editorOnlyLabel.AddToClassList(s_ItemEditorOnlyLabelUssClassName);
                editorOnlyLabel.AddToClassList("unity-builder-tag-pill");
                editorOnlyLabel.name = s_ItemEditorOnlyLabelName;
                item.Add(nameLabel);
                item.Add(editorOnlyLabel);
                return(item);
            };
            bindItem = (e, i) =>
            {
                var res = results[i];

                e.Q <Label>(s_ItemNameLabelName).text = res.name;
                e.Q <Label>(s_ItemEditorOnlyLabelName).EnableInClassList(BuilderConstants.HiddenStyleClassName, !res.isEditorVar);
            };

            m_DetailsView = new VariableInfoView();
            m_DetailsView.AddToClassList(BuilderConstants.HiddenStyleClassName);
            detailsContent     = m_DetailsView;
            onSelectionChange += data =>
            {
                m_DetailsView.SetInfo(data);
                if (data != null)
                {
                    m_DetailsView.RemoveFromClassList(BuilderConstants.HiddenStyleClassName);
                }
                else
                {
                    m_DetailsView.AddToClassList(BuilderConstants.HiddenStyleClassName);
                }
            };

            matcherCallback         = Matcher;
            getTextFromDataCallback = GetVarName;
        }
        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));
        }
Esempio n. 5
0
        public VariableInfoTooltip()
        {
            AddToClassList(s_UssClassName);

            VisualElement content = new VisualElement();

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

            template.CloneTree(content);

            Add(content);

            m_VarNameLabel = this.Q <Label>("inspector-varinfo-name-label");

            m_SelectorSourceLabel = this.Q <Label>("inspector-varinfo-source");

#if false // TODO: Will need to bring this back once we can also do the dragger at the same time.
            m_EditButton = this.Q <Button>("inspector-varinfo-edit-button");

            /*
             * var shorcutLabel = this.Q<Label>("inspector-varinfo-edit-shortcut");
             * shorcutLabel.text = "(CTRL+Q)";*/
#if UNITY_2020_1_OR_NEWER
            m_EditButton.RegisterCallback <ClickEvent>(e =>
            {
                m_CurrentHandler.ShowVariableField();
            });
#else
            m_EditButton.clickable.clicked += () => {
                m_CurrentHandler.ShowVariableField();
            };
#endif
#endif // false

// TODO: Will need to bring this back once we can also do the dragger at the same time.
#if false
            RegisterCallback <FocusOutEvent>(e => {
                if (m_CurrentHandler != null)
                {
                    Hide();
                    m_CurrentHandler = null;
                }
            });
#endif

            this.RegisterCallback <GeometryChangedEvent>(e => {
                if (m_Showing)
                {
                    m_Showing = false;
                    AdjustXPosition();
                }
            });

            focusable = true;
        }
        static void OnPopupClosed(VariableInfoTooltip tooltip)
        {
            VariableEditingHandler handler = tooltip.currentHandler;

            if (!handler.isVariableFieldVisible)
            {
                handler.targetField.RemoveFromClassList(BuilderConstants.InspectorLocalStyleVariableEditingClassName);
            }
            handler.DisablePopupTemporarily();
        }
        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 void ShowPopup(VariableEditingHandler handler)
        {
            if (handler.m_Builder == null || handler.m_Inspector == null || handler.editingEnabled)
            {
                return;
            }

            string varName = GetBoundVariableName(handler);

            VariableInfo varInfo = null;

            if (!string.IsNullOrEmpty(varName))
            {
                varInfo = StyleVariableUtilities.FindVariable(handler.m_Inspector.currentVisualElement, varName, handler.inspector.document.fileSettings.editorExtensionMode);

                if (varInfo == null)
                {
                    varInfo = new VariableInfo {
                        name = varName
                    }
                }
                ;
            }

            if (handler.variableInfoTooltip == null)
            {
                handler.variableInfoTooltip = handler.m_Builder.rootVisualElement.GetProperty(BuilderConstants.ElementLinkedVariableTooltipVEPropertyName) as VariableInfoTooltip;

                if (handler.variableInfoTooltip == null)
                {
                    handler.variableInfoTooltip = new VariableInfoTooltip();
                    handler.m_Builder.rootVisualElement.SetProperty(
                        BuilderConstants.ElementLinkedVariableTooltipVEPropertyName, handler.variableInfoTooltip);
                    handler.m_Builder.rootVisualElement.Add(handler.variableInfoTooltip);
                    handler.variableInfoTooltip.onHide += () => OnPopupClosed(handler.variableInfoTooltip);
                }
            }

            handler.targetField.AddToClassList(BuilderConstants.InspectorLocalStyleVariableEditingClassName);
            if (handler.editingEnabled)
            {
                handler.targetField.RemoveFromClassList(BuilderConstants.ReadOnlyStyleClassName);
            }
            else
            {
                handler.targetField.AddToClassList(BuilderConstants.ReadOnlyStyleClassName);
            }
            handler.variableInfoTooltip.Show(handler, varInfo);
            handler.m_ScheduledShowPopup = null;
        }
        static public VariableEditingHandler GetOrCreateVarHandler(BindableElement field)
        {
            if (field == null)
            {
                return(null);
            }

            VariableEditingHandler handler = GetVarHandler(field);

            if (handler == null)
            {
                handler = new VariableEditingHandler(field);
                field.SetProperty(BuilderConstants.ElementLinkedVariableHandlerVEPropertyName, handler);
            }
            return(handler);
        }
        static void ShowPopup(VariableEditingHandler handler)
        {
            if (handler.m_Builder == null || handler.m_Inspector == null)
            {
                return;
            }

            string varName = GetBoundVariableName(handler);

            StyleSheet           varStyleSheetOrigin     = null;
            StyleComplexSelector varSourceSelectorOrigin = null;

            if (!string.IsNullOrEmpty(varName))
            {
                StyleVariableUtilities.FindVariableOrigin(handler.m_Inspector.currentVisualElement, varName, out varStyleSheetOrigin, out varSourceSelectorOrigin);
            }

            if (handler.variableInfoTooltip == null)
            {
                handler.variableInfoTooltip = handler.m_Builder.rootVisualElement.GetProperty(BuilderConstants.ElementLinkedVariableTooltipVEPropertyName) as VariableInfoTooltip;

                if (handler.variableInfoTooltip == null)
                {
                    handler.variableInfoTooltip = new VariableInfoTooltip();
                    handler.m_Builder.rootVisualElement.SetProperty(
                        BuilderConstants.ElementLinkedVariableTooltipVEPropertyName, handler.variableInfoTooltip);
                    handler.m_Builder.rootVisualElement.Add(handler.variableInfoTooltip);
                    handler.variableInfoTooltip.onHide += () => OnPopupClosed(handler.variableInfoTooltip);
                }
            }

            handler.targetField.AddToClassList(BuilderConstants.InspectorLocalStyleVariableEditingClassName);
            if (handler.editingEnabled)
            {
                handler.targetField.RemoveFromClassList(BuilderConstants.ReadOnlyStyleClassName);
            }
            else
            {
                handler.targetField.AddToClassList(BuilderConstants.ReadOnlyStyleClassName);
            }
            handler.variableInfoTooltip.Show(handler, varName, varStyleSheetOrigin, varSourceSelectorOrigin);
            handler.m_ScheduledShowPopup = null;
        }
Esempio n. 11
0
        public void Show(VariableEditingHandler handler, string variableName, StyleSheet varStyleSheetOrigin, StyleComplexSelector varSelectorOrigin)
        {
            m_CurrentHandler    = handler;
            m_VarNameLabel.text = !string.IsNullOrEmpty(variableName) ? variableName : "None";

            if (!string.IsNullOrEmpty(variableName) && varSelectorOrigin != null)
            {
                var    fullPath    = AssetDatabase.GetAssetPath(varStyleSheetOrigin);
                string displayPath = null;

                if (string.IsNullOrEmpty(fullPath))
                {
                    displayPath = varStyleSheetOrigin.name;
                }
                else
                {
                    if (fullPath == "Library/unity editor resources")
                    {
                        displayPath = varStyleSheetOrigin.name;
                    }
                    else
                    {
                        displayPath = Path.GetFileName(fullPath);
                    }
                }

                m_SelectorSourceLabel.text = $"{StyleSheetToUss.ToUssSelector(varSelectorOrigin)} ({displayPath})";
            }
            else
            {
                m_SelectorSourceLabel.text = "None";
            }

            var pos = handler.labelElement.ChangeCoordinatesTo(parent, Vector2.zero);

            pos.x      = parent.layout.width - (style.minWidth.value.value + handler.inspector.resolvedStyle.width + BuilderConstants.TooltipPreviewYOffset);
            style.left = pos.x;
            style.top  = pos.y;
            m_Showing  = true;
            Show();
            // TODO: Will need to bring this back once we can also do the dragger at the same time.
            //Focus();
        }
        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);
        }