Example #1
0
        public bool Contains(SearchItem item)
        {
            if (m_TemporaryUnordered)
            {
                if (m_UnorderedItems.Contains(item))
                {
                    return(true);
                }
            }

            foreach (var itemsByPriority in m_Data)
            {
                foreach (var itemsByScore in itemsByPriority.Value)
                {
                    if (itemsByScore.Value.ContainsValue(item))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        private void OnItemSelected(SearchItem selectedItem, bool canceled)
        {
            if (selectedItem == null || canceled)
            {
                return;
            }

            var provider     = selectedItem.provider;
            var selectAction = provider.actions.FirstOrDefault(a => a.Id == "select");

            if (selectAction != null && selectAction.handler != null)
            {
                selectAction.handler(selectedItem, context);
            }
            else if (selectAction != null && selectAction.execute != null)
            {
                selectAction.execute(context, new SearchItem[] { selectedItem });
            }
            else
            {
                selectedItem.provider?.trackSelection?.Invoke(selectedItem, context);
            }
        }
Example #3
0
        private ISet <SearchItem> SelectObject(SearchItem item, string objectTypeName, string objectPropertyName)
        {
            var results = new HashSet <SearchItem>();

            if (objectTypeName == null || objectPropertyName == null)
            {
                return(results);
            }

            if (!s_TypeTable.TryGetValue(objectTypeName ?? "", out var objectType))
            {
                objectType = TypeCache.GetTypesDerivedFrom(typeof(UnityEngine.Object)).FirstOrDefault(t => t.Name == objectTypeName);
                s_TypeTable[objectTypeName] = objectType;
            }

            if (objectType == null)
            {
                return(results);
            }

            if (typeof(Component).IsAssignableFrom(objectType))
            {
                return(SelectComponent(item, objectTypeName, objectPropertyName));
            }

            var assetObject = item.provider?.toObject(item, objectType);

            if (!assetObject)
            {
                return(results);
            }

            SelectProperty(assetObject, objectPropertyName, results);

            return(results);
        }
Example #4
0
 public void Add(SearchItem item)
 {
     AddItems(new [] { item });
 }
Example #5
0
 private void ExecuteAction(SearchAction action, SearchItem item, SearchContext context)
 {
     SearchService.LastSearch = context.searchBoxText;
     action.handler(item, context);
     CloseSearchWindow();
 }
Example #6
0
        private void DrawGridItem(int index, SearchItem item, Rect itemRect, bool canHover, ICollection <int> selection, Event evt)
        {
            var backgroundRect = new Rect(itemRect.x + 1, itemRect.y + 1, itemRect.width - 2, itemRect.height - 2);
            var itemContent    = canHover ? new GUIContent("", item.GetDescription(context, true)) : GUIContent.none;

            if (selection.Contains(index))
            {
                GUI.Label(backgroundRect, itemContent, Styles.selectedGridItemBackground);
            }
            else if (canHover)
            {
                GUI.Label(backgroundRect, itemContent, itemRect.Contains(evt.mousePosition) ? Styles.itemGridBackground2 : Styles.itemGridBackground1);
            }

            Texture2D thumbnail          = null;
            var       shouldFetchPreview = SearchSettings.fetchPreview && itemSize > 64;

            if (SearchSettings.fetchPreview && itemSize > 64)
            {
                thumbnail          = item.preview;
                shouldFetchPreview = !thumbnail && item.provider.fetchPreview != null;
                if (shouldFetchPreview)
                {
                    var previewSize = new Vector2(itemSize, itemSize);
                    thumbnail = item.provider.fetchPreview(item, context, previewSize, FetchPreviewOptions.Preview2D | FetchPreviewOptions.Normal);
                    if (thumbnail)
                    {
                        item.preview = thumbnail;
                    }
                }
            }

            if (!thumbnail)
            {
                thumbnail = item.thumbnail;
                if (!thumbnail && item.provider.fetchThumbnail != null)
                {
                    thumbnail = item.provider.fetchThumbnail(item, context);
                    if (thumbnail && !shouldFetchPreview)
                    {
                        item.thumbnail = thumbnail;
                    }
                }
            }

            if (thumbnail)
            {
                var thumbnailRect = new Rect(itemRect.x + itemPadding, itemRect.y + itemPadding, itemSize, itemSize);
                var dw            = thumbnailRect.width - thumbnail.width;
                var dh            = thumbnailRect.height - thumbnail.height;
                if (dw > 0 || dh > 0)
                {
                    var scaledWidth  = Mathf.Min(thumbnailRect.width, thumbnail.width);
                    var scaledHeight = Mathf.Min(thumbnailRect.height, thumbnail.height);
                    thumbnailRect = new Rect(
                        thumbnailRect.center.x - scaledWidth / 2f,
                        thumbnailRect.center.y - scaledHeight / 2f,
                        scaledWidth, scaledHeight);
                }
                GUI.DrawTexture(thumbnailRect, thumbnail, ScaleMode.ScaleToFit, true, 0f, Color.white, 0f, 4f);
            }

            var labelRect = new Rect(
                itemRect.x + itemPadding, itemRect.yMax - itemLabelHeight - itemPadding,
                itemRect.width - itemPadding * 2f, itemLabelHeight - itemPadding);
            var maxCharLength = Utils.GetNumCharactersThatFitWithinWidth(Styles.itemLabelGrid, item.GetLabel(context, true), itemRect.width * 2f);
            var itemLabel     = item.GetLabel(context);

            if (itemLabel.Length > maxCharLength)
            {
                maxCharLength = Math.Max(0, maxCharLength - 3);
                itemLabel     = Utils.StripHTML(itemLabel);
                itemLabel     = itemLabel.Substring(0, maxCharLength / 2) + "\u2026" + itemLabel.Substring(itemLabel.Length - maxCharLength / 2);
            }
            GUI.Label(labelRect, itemLabel, Styles.itemLabelGrid);
        }
Example #7
0
 public bool Remove(SearchItem item)
 {
     return(m_UnorderedItems.Remove(item));
 }
Example #8
0
 public void Add(SearchItem item)
 {
     throw new NotSupportedException();
 }
Example #9
0
 public bool Remove(SearchItem item)
 {
     throw new NotSupportedException();
 }
Example #10
0
        private void SelectAssetPath(ISet <SearchItem> results, UnityEngine.Object obj, SearchItem source)
        {
            var assetPath = AssetDatabase.GetAssetPath(obj);

            if (source == null && String.IsNullOrEmpty(assetPath))
            {
                return;
            }
            results.Add(MapItem(source, assetPath));
        }
Example #11
0
        private static void DrawDescription(SearchContext context, SearchItem item)
        {
            var description = SearchContent.FormatDescription(item, context, 2048);

            GUILayout.Label(description, Styles.previewDescription);
        }
Example #12
0
 private void SelectValue(ISet <SearchItem> results, object value, SearchItem source)
 {
     results.Add(MapItem(source, value));
 }
Example #13
0
 private void SelectVector <T>(ISet <SearchItem> results, T value, SearchItem source) where T : struct
 {
     results.Add(MapItem(source, Convert.ToString(value).Replace("(", "").Replace(")", "").Replace(" ", "")));
 }
Example #14
0
        private void SelectProperty(UnityEngine.Object obj, string objectPropertyName, ISet <SearchItem> results, SearchItem source)
        {
            using (var so = new SerializedObject(obj))
            {
                var property = so.FindProperty(objectPropertyName);
                if (property == null || property.isArray)
                {
                    return;
                }
                switch (property.propertyType)
                {
                case SerializedPropertyType.Integer: SelectValue(results, property.intValue, source); break;

                case SerializedPropertyType.Enum: SelectValue(results, property.enumNames[property.enumValueIndex], source); break;

                case SerializedPropertyType.Boolean: SelectValue(results, property.boolValue.ToString().ToLowerInvariant(), source); break;

                case SerializedPropertyType.String: SelectValue(results, property.stringValue, source); break;

                case SerializedPropertyType.Float: SelectValue(results, property.floatValue, source); break;

                case SerializedPropertyType.FixedBufferSize: SelectValue(results, property.fixedBufferSize, source); break;

                case SerializedPropertyType.Color: SelectValue(results, ColorUtility.ToHtmlStringRGB(property.colorValue), source); break;

                case SerializedPropertyType.AnimationCurve: SelectValue(results, property.animationCurveValue, source); break;

                case SerializedPropertyType.Vector2: SelectVector(results, property.vector2Value, source); break;

                case SerializedPropertyType.Vector3: SelectVector(results, property.vector3Value, source); break;

                case SerializedPropertyType.Vector4: SelectVector(results, property.vector4Value, source); break;

                case SerializedPropertyType.Rect: SelectVector(results, property.rectValue, source); break;

                case SerializedPropertyType.Bounds: SelectVector(results, property.boundsValue, source); break;

                case SerializedPropertyType.Quaternion: SelectVector(results, property.quaternionValue, source); break;

                case SerializedPropertyType.Vector2Int: SelectVector(results, property.vector2IntValue, source); break;

                case SerializedPropertyType.Vector3Int: SelectVector(results, property.vector3IntValue, source); break;

                case SerializedPropertyType.RectInt: SelectVector(results, property.rectIntValue, source); break;

                case SerializedPropertyType.BoundsInt: SelectVector(results, property.boundsIntValue, source); break;

                case SerializedPropertyType.ManagedReference: SelectValue(results, property.managedReferenceFullTypename, source); break;

                case SerializedPropertyType.ObjectReference: SelectAssetPath(results, property.objectReferenceValue, source); break;

                case SerializedPropertyType.ExposedReference: SelectAssetPath(results, property.exposedReferenceValue, source); break;

                case SerializedPropertyType.Generic:
                    break;

                case SerializedPropertyType.Gradient:
                case SerializedPropertyType.Character:
                case SerializedPropertyType.LayerMask:
                case SerializedPropertyType.ArraySize:
                default:
                    Debug.LogWarning($"Cannot select {property.propertyType} {objectPropertyName} with {id}");
                    break;
                }
            }
        }
Example #15
0
 public void Add(SearchItem item)
 {
     m_UnorderedItems.Add(item);
 }
Example #16
0
 public bool Contains(SearchItem item)
 {
     return(m_Items.Contains(item));
 }
Example #17
0
 public bool Contains(SearchItem item)
 {
     return(m_UnorderedItems.Contains(item));
 }
Example #18
0
        private void DrawItem(SearchItem item, Rect itemRect, int itemIndex, ICollection <int> selection)
        {
            bool hasActionDropdown = searchView.selectCallback == null && searchView.selection.Count <= 1 && item.provider.actions.Count > 1;

            if (Event.current.type == EventType.Repaint)
            {
                bool isItemSelected = selection.Contains(itemIndex);

                // Draw item background
                var bgStyle = itemIndex % 2 == 0 ? Styles.itemBackground1 : Styles.itemBackground2;
                if (isItemSelected)
                {
                    bgStyle = Styles.selectedItemBackground;
                }
                bgStyle.Draw(itemRect, itemRect.Contains(Event.current.mousePosition), false, false, false);

                if (compactView)
                {
                    item.options |= SearchItemOptions.Compacted;
                }
                else
                {
                    item.options &= ~SearchItemOptions.Compacted;
                }

                // Draw thumbnail
                var thumbnailRect = DrawListThumbnail(item, itemRect);

                // Draw label
                var maxWidth = itemRect.width
                               - (hasActionDropdown ? Styles.actionButtonSize : 0)
                               - (compactView ? Styles.itemPreviewSize / 2.0f : Styles.itemPreviewSize)
                               - Styles.descriptionPadding;
                var labelStyle = isItemSelected ?
                                 (compactView ? Styles.selectedItemLabelCompact : Styles.selectedItemLabel) :
                                 (compactView ? Styles.itemLabelCompact : Styles.itemLabel);
                var labelRect = new Rect(
                    thumbnailRect.xMax + labelStyle.margin.left, itemRect.y + labelStyle.margin.top,
                    maxWidth - labelStyle.margin.right, labelStyle.lineHeight);

                if (!compactView)
                {
                    var label = item.provider.fetchLabel(item, context);
                    GUI.Label(labelRect, label, labelStyle);
                    labelRect.y = labelRect.yMax + labelStyle.margin.bottom;

                    // Draw description
                    var labelContent = SearchContent.FormatDescription(item, context, maxWidth);
                    labelStyle   = isItemSelected ? Styles.selectedItemDescription : Styles.itemDescription;
                    labelRect.y += labelStyle.margin.top;
                    GUI.Label(labelRect, labelContent, labelStyle);
                }
                else
                {
                    // Draw label
                    var labelContent = SearchContent.FormatDescription(item, context, maxWidth);
                    GUI.Label(labelRect, labelContent, labelStyle);
                }
            }

            // Draw action dropdown
            if (hasActionDropdown)
            {
                var buttonRect = new Rect(itemRect.xMax - Styles.actionButton.fixedWidth - Styles.actionButton.margin.right, itemRect.y, Styles.actionButton.fixedWidth, Styles.actionButton.fixedHeight);
                buttonRect.y += (itemRect.height - Styles.actionButton.fixedHeight) / 2f;
                bool actionHover = buttonRect.Contains(Event.current.mousePosition);
                GUI.Label(buttonRect, Styles.moreActionsContent, actionHover ? Styles.actionButtonHovered : Styles.actionButton);
                UnityEditor.EditorGUIUtility.AddCursorRect(buttonRect, UnityEditor.MouseCursor.Link);
                if (Event.current.type == EventType.MouseDown && actionHover)
                {
                    var contextRect = new Rect(Event.current.mousePosition, new Vector2(1, 1));
                    searchView.ShowItemContextualMenu(item, context, contextRect);
                    GUIUtility.ExitGUI();
                }
            }
        }
Example #19
0
        public static GUIContent FormatDescription(SearchItem item, SearchContext context, float availableSpace, bool useColor = true)
        {
            var desc = item.GetDescription(context);

            if (String.IsNullOrEmpty(desc))
            {
                return(Styles.emptyContent);
            }
            var content = Take(desc);

            if (item.descriptionFormat == SearchItemDescriptionFormat.None || Event.current.type != EventType.Repaint)
            {
                return(content);
            }

            var truncatedDesc = desc;
            var truncated     = false;

            if (useColor)
            {
                if (item.descriptionFormat.HasFlag(SearchItemDescriptionFormat.Ellipsis))
                {
                    int maxCharLength = Utils.GetNumCharactersThatFitWithinWidth(Styles.itemDescription, truncatedDesc + "...", availableSpace);
                    if (maxCharLength < 0)
                    {
                        maxCharLength = truncatedDesc.Length;
                    }
                    truncated = desc.Length > maxCharLength;
                    if (truncated)
                    {
                        if (item.descriptionFormat.HasFlag(SearchItemDescriptionFormat.RightToLeft))
                        {
                            truncatedDesc = "..." + desc.Replace("<b>", "").Replace("</b>", "");
                            truncatedDesc = truncatedDesc.Substring(Math.Max(0, truncatedDesc.Length - maxCharLength));
                        }
                        else
                        {
                            truncatedDesc = desc.Substring(0, Math.Min(maxCharLength, desc.Length)) + "...";
                        }
                    }
                }

                if (item.descriptionFormat.HasFlag(SearchItemDescriptionFormat.Highlight))
                {
                    var parts = context.searchQuery.Split('*', ' ', '.').Where(p => p.Length > 2);
                    foreach (var p in parts)
                    {
                        truncatedDesc = Regex.Replace(truncatedDesc, Regex.Escape(p), string.Format(Styles.highlightedTextColorFormat, "$0"), RegexOptions.IgnoreCase);
                    }
                }
                else if (item.descriptionFormat.HasFlag(SearchItemDescriptionFormat.FuzzyHighlight))
                {
                    long score   = 1;
                    var  matches = new List <int>();
                    var  sq      = Utils.CleanString(context.searchQuery.ToLowerInvariant());
                    if (FuzzySearch.FuzzyMatch(sq, Utils.CleanString(truncatedDesc), ref score, matches))
                    {
                        truncatedDesc = RichTextFormatter.FormatSuggestionTitle(truncatedDesc, matches);
                    }
                }
            }

            content.text = truncatedDesc;
            if (truncated)
            {
                content.tooltip = Utils.StripHTML(desc);
            }

            return(content);
        }
 public void ShowItemContextualMenu(SearchItem item, SearchContext context, Rect contextualActionPosition)
 {
     // Nothing to do.
 }