private void ShowOptions(Rect rect, IEnumerable <T> options) { if (!searchField.HasFocus() || searchString.Length == 0 || !options.Any()) { Cleanup(); } else if (popupWindow) { if (options.Any()) { popupWindow.options = options.OfType <object>().ToList(); } popupWindow.itemHeight = itemHeight; popupWindow.OnItemGUI = (index, obj) => OnSuggestionGUI(index, (T)obj); if (Event.current.type == EventType.Repaint) { popupWindow.UpdatePopupPosition(rect); } } else if (searchField.HasFocus() && options.Any()) { popupWindow = ScriptableObject.CreateInstance <PickerWindow>(); popupWindow.itemHeight = itemHeight; popupWindow.options = options.OfType <object>().ToList(); popupWindow.OnItemGUI = (index, obj) => OnSuggestionGUI(index, (T)obj); popupWindow.Show(rect); } }
private void AssetListGUI() { GUILayout.BeginHorizontal(); GUILayout.Space(10); searchMode = (SearchMode)EditorGUILayout.EnumPopup(searchMode, GUILayout.Width(80)); Rect rect = GUILayoutUtility.GetRect(position.width - 130, 25); rect.height = 20; rect.y += 3; searchText = searchField.OnGUI(rect, searchText); if (searchField.HasFocus() && Event.current.rawType == EventType.KeyUp && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)) { projecListTreeView.Filtter(searchText, searchMode); searchField.SetFocus(); } if (GUILayout.Button(createAssetIcon, GUILayout.Width(20), GUILayout.Height(17))) { string path = EditorUtility.SaveFilePanelInProject("创建", "New TimelineLite", "asset", ""); if (!string.IsNullOrEmpty(path)) { AssetDatabase.CreateAsset(ScriptableObject.CreateInstance(TimelineLiteAssetType), path); AssetDatabase.Refresh(); } } GUILayout.Space(10); GUILayout.EndHorizontal(); rect.x = 10; rect.y += 20; rect.width = position.width - 20; rect.height = position.height - rect.y - 5; projecListTreeView.OnGUI(rect); }
void VerifyIfSearchFieldIsRecentlyFocused(SearchField searchField) { if (searchField.HasFocus() != mIsSearchFieldFocused) { mIsSearchFieldFocused = !mIsSearchFieldFocused; if (mIsSearchFieldFocused) { TrackFeatureUseEvent.For( PlasticGui.Plastic.API.GetRepositorySpec(mWkInfo), TrackFeatureUseEvent.Features.ChangesetsViewChangesetsSearchBox); } } }
public static bool DelayedSearchField(SearchField searchField, ref string searchString) { // check keydown before processing the search field, because it swallows them var isEnter = Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter); var isESC = Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape; searchString = searchField.OnToolbarGUI(searchString); if (!searchField.HasFocus()) { return(false); } return(isEnter || isESC); }
protected internal virtual void DrawHeader(Rect rect) { bool canSearch = CanSearch(); bool canAddElement = CanAddElement(); Dictionary <string, Rect> rects = AdvancedRect.GetRects(rect, AdvancedRect.Orientation.Horizontal, new AdvancedRect.ExpandedGroup("SearchBar", AdvancedRect.Orientation.Horizontal, new AdvancedRect.FixedSpace(12, canSearch), new AdvancedRect.ExpandedItem("Title") ), new AdvancedRect.FixedItem("AddButton", Styles.AddButtonWidth, canAddElement) ); Styles.HeaderBackground.Draw(rect); if (canSearch) { if (ElementCount > 0) { DrawSearchBar(rects["SearchBar"]); } } if (!canSearch || (!searchBar.HasFocus() && string.IsNullOrEmpty(SearchString))) { GUI.Label(rects["Title"], Title, Styles.Header); } if (canAddElement) { if (GUI.Button(rects["AddButton"], string.Empty, Styles.AddButton)) { AddElement(); } } }
public override void OnGUI(Rect rect) { EditorGUI.BeginChangeCheck(); m_connectionTreeView.search = m_SearchField.OnGUI(new Rect(rect.x + ConnectionDropDownStyles.searchFieldPadding, rect.y + ConnectionDropDownStyles.searchFieldPadding, rect.width - (2 * ConnectionDropDownStyles.searchFieldPadding), EditorGUI.kSingleLineHeight), m_connectionTreeView.search); if (!didFocus && !m_SearchField.HasFocus()) { m_SearchField.SetFocus(); didFocus = true; } if (EditorGUI.EndChangeCheck()) { if (!string.IsNullOrEmpty(m_connectionTreeView.search)) { if (!searching) { var rows = m_connectionTreeView.GetRows(); m_expandedState.Clear(); foreach (var row in rows) { m_expandedState.Add(row.id, m_connectionTreeView.IsExpanded(row.id)); } } m_connectionTreeView.ExpandAll(); searching = true; } else { if (searching) { foreach (var b in m_expandedState) { m_connectionTreeView.SetExpanded(b.Key, b.Value); } searching = false; } } Reload(); } rect.y += EditorGUI.kSingleLineHeight + ConnectionDropDownStyles.searchFieldVerticalSpacing; if (consoleAttachToPlayerState != null) { rect.y += searchToLoggingPadding; EditorGUI.BeginChangeCheck(); GUI.Toggle(new Rect(rect.x + ConnectionDropDownStyles.searchFieldPadding, rect.y, rect.width, EditorGUI.kSingleLineHeight), consoleAttachToPlayerState.IsConnected(), ConnectionUIHelper.Content.PlayerLogging); if (EditorGUI.EndChangeCheck()) { consoleAttachToPlayerState.PlayerLoggingOptionSelected(); } rect.y += EditorGUI.kSingleLineHeight + loggingVerticalPadding; GUI.enabled = consoleAttachToPlayerState.IsConnected(); m_connectionTreeView.dropDownItems.ForEach(x => x.m_Disabled = !GUI.enabled); EditorGUI.BeginChangeCheck(); GUI.Toggle(new Rect(rect.x + ConnectionDropDownStyles.searchFieldPadding, rect.y, rect.width, EditorGUI.kSingleLineHeight), consoleAttachToPlayerState.IsLoggingFullLog(), ConnectionUIHelper.Content.FullLog); if (EditorGUI.EndChangeCheck()) { consoleAttachToPlayerState.FullLogOptionSelected(); } rect.y += EditorGUI.kSingleLineHeight + loggingVerticalPadding; } m_connectionTreeView?.OnGUI(new Rect(rect.x, rect.y, rect.width, (float)m_connectionTreeView?.totalHeight)); rect.y += (float)m_connectionTreeView?.totalHeight; GUI.enabled = true; EditorGUI.DrawDelimiterLine(new Rect(rect.x, rect.y, rect.width, 1f)); rect.y += 1f; rect.y += ConnectionDropDownStyles.troubleShootBtnPadding / 2f; if (EditorGUI.Button(rect, Content.TroubleShoot, ConnectionDropDownStyles.sConnectionTrouble)) { var version = $"{Application.unityVersionVer}.{Application.unityVersionMaj}"; Application.OpenURL($"https://docs.unity3d.com/{version}/Documentation/Manual/profiler-profiling-applications.html"); } if (Event.current.type == EventType.MouseMove) { Event.current.Use(); } }
private void OnGUI() { EditorGUILayout.BeginHorizontal(); GUILayout.Label(filePath); GUILayout.FlexibleSpace(); if (GUILayout.Button("Save", (GUIStyle)"toolbarbutton", GUILayout.Width(100))) { if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { Save(filePath); } else { string path = EditorUtility.SaveFilePanelInProject("Save", "language", "json", "", Application.dataPath); if (!string.IsNullOrEmpty(path)) { Save(path); } } } int i = EditorGUILayout.Popup(-1, Enum.GetNames(typeof(DataFormat)), (GUIStyle)"ToolbarDropDownToggle", GUILayout.Width(20)); if (i != -1) { string path = ""; switch (i) { case 0: path = EditorUtility.SaveFilePanelInProject("Save", "language", "json", "", Application.dataPath); break; case 1: path = EditorUtility.SaveFilePanelInProject("Save", "language", "csv", "", Application.dataPath); break; } if (!string.IsNullOrEmpty(path)) { Save(path); } } GUILayout.Space(10); if (GUILayout.Button("Load", (GUIStyle)"toolbarbutton", GUILayout.Width(100))) { string path = EditorUtility.OpenFilePanel("Select", Application.dataPath, "json,csv"); if (!string.IsNullOrEmpty(path)) { Load(path, out languages, out dataTable); treeView.TotalReload(); } } EditorGUILayout.EndHorizontal(); Rect rect = new Rect(lineSpace, 30, 50, lineHeight); if (GUI.Button(rect, new GUIContent("Reload", "Load from file")) && !string.IsNullOrEmpty(filePath)) { Load(filePath, out languages, out dataTable); treeView.TotalReload(); } rect.x += rect.width + lineSpace; rect.width = 300; searchText = searchField.OnGUI(rect, searchText); if (searchField.HasFocus() && Event.current.rawType == EventType.KeyUp && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter)) { treeView.Filtter(searchText, searchFlags); searchField.SetFocus(); } rect.x += 300; rect.width = 100; searchFlags = EditorGUI.MaskField(rect, searchFlags, searchTypes); rect.x += 110; rect.width = 20; GUI.Button(rect, new GUIContent("?", "Alt + A : 增加条目\nCtrl + C : 复制条目(不支持多选复制)\nCtrl + V : 粘贴条目\nCtrl + D : 粘贴选中条目(不支持多选)\n右键显示菜单")); treeView.OnGUI(new Rect(lineSpace, 50, position.width - 10, position.height - 55)); }
/// <summary>A textField to popup a matching popup, based on developers input values.</summary> /// <param name="input">string input.</param> /// <param name="source">the data of all possible values (string).</param> /// <param name="maxShownCount">the amount to display result.</param> /// <param name="levenshteinDistance"> /// value between 0f ~ 1f, /// - more then 0f will enable the fuzzy matching /// - 1f = anything thing is okay. /// - 0f = require full match to the reference /// - recommend 0.4f ~ 0.7f /// </param> /// <returns>output string.</returns> public static string TextFieldAutoComplete(SearchField searchField, Rect position, string input, string[] source, System.Func<string, string> display, int maxShownCount = 5, float levenshteinDistance = 0.5f) { if (input == null) return string.Empty; tag = m_AutoCompleteField + GUIUtility.GetControlID(FocusType.Passive); int uiDepth = GUI.depth; GUI.SetNextControlName(tag); string rst = searchField.OnGUI(position, input); if (input.Length > 0 && searchField.HasFocus()) { if (m_AutoCompleteLastInput != input || // input changed m_EditorFocusAutoComplete != tag) // another field. { // Update cache m_EditorFocusAutoComplete = tag; m_AutoCompleteLastInput = input; List<string> uniqueSrc = new List<string>(new HashSet<string>(source)); // remove duplicate int srcCnt = uniqueSrc.Count; m_CacheCheckList = new List<string>(System.Math.Min(maxShownCount, srcCnt)); // optimize memory alloc // Start with - slow for (int i = 0; i < srcCnt && m_CacheCheckList.Count < maxShownCount; i++) { if (uniqueSrc[i].ToLower().StartsWith(input.ToLower())) { m_CacheCheckList.Add(uniqueSrc[i]); uniqueSrc.RemoveAt(i); srcCnt--; i--; } } // Contains - very slow if (m_CacheCheckList.Count == 0) { for (int i = 0; i < srcCnt && m_CacheCheckList.Count < maxShownCount; i++) { if (uniqueSrc[i].ToLower().Contains(input.ToLower())) { m_CacheCheckList.Add(uniqueSrc[i]); uniqueSrc.RemoveAt(i); srcCnt--; i--; } } } // Levenshtein Distance - very very slow. if (levenshteinDistance > 0f && // only developer request input.Length > 3 && // 3 characters on input, hidden value to avoid doing too early. m_CacheCheckList.Count < maxShownCount) // have some empty space for matching. { levenshteinDistance = Mathf.Clamp01(levenshteinDistance); string keywords = input.ToLower(); for (int i = 0; i < srcCnt && m_CacheCheckList.Count < maxShownCount; i++) { int distance = LevenshteinDistance(uniqueSrc[i], keywords, caseSensitive: false); bool closeEnough = (int)(levenshteinDistance * uniqueSrc[i].Length) > distance; if (closeEnough) { m_CacheCheckList.Add(uniqueSrc[i]); uniqueSrc.RemoveAt(i); srcCnt--; i--; } } } } // Draw recommend keyward(s) if (m_CacheCheckList.Count > 0) { int cnt = m_CacheCheckList.Count; float height = cnt * EditorGUIUtility.singleLineHeight; Rect area = position; area = new Rect(area.x, area.y - height, area.width, height); GUI.depth -= 10; // GUI.BeginGroup(area); // area.position = Vector2.zero; GUI.BeginClip(area); Rect line = new Rect(0, 0, area.width, EditorGUIUtility.singleLineHeight); Event e = Event.current; //if control key is pressed if (e.type == EventType.KeyDown) { if (e.keyCode == KeyCode.Tab || e.keyCode == KeyCode.Return) return m_CacheCheckList[0]; } for (int i = 0; i < cnt; i++) { var style = new GUIStyle() { alignment = TextAnchor.MiddleCenter }; style.normal.background = TextureTools.MakeTex((int)line.width, (int)line.height, new Color(.82f, .82f, .82f)); if (GUI.Button(line, display(m_CacheCheckList[i]), style))//, EditorStyles.toolbarDropDown)) { rst = m_CacheCheckList[i]; GUI.changed = true; GUI.FocusControl(""); // force update } line.y += line.height; } GUI.EndClip(); //GUI.EndGroup(); GUI.depth += 10; } } return rst; }
public override void OnGUI(Rect fullRect) { if (m_Entries.Count == 0) { return; } int count = -1; if (m_Labels == null) { m_Labels = GetLabelNamesOrderedSelectedFirst(); } var areaRect = new Rect(fullRect.xMin + 3, fullRect.yMin + 3, fullRect.width - 6, fullRect.height - 6); GUILayout.BeginArea(areaRect); GUILayoutUtility.GetRect(areaRect.width, 1); Rect barRect = EditorGUILayout.GetControlRect(); var marginDown = GUILayoutUtility.GetRect(areaRect.width, 1); Rect plusRect = barRect; plusRect.width = plusRect.height; plusRect.x = (barRect.width - plusRect.width) + 4; if (UnityEngine.GUI.Button(plusRect, m_ManageLabelsButtonContent, m_ToolbarButtonStyle)) { EditorWindow.GetWindow <LabelWindow>(true).Intialize(m_Settings); editorWindow.Close(); } Rect searchRect = barRect; float plusOffset = plusRect.width + 2; searchRect.width = searchRect.width - plusOffset; m_SearchValue = m_SearchField.OnGUI(searchRect, m_SearchValue, m_SearchStyles[0], m_SearchStyles[1], m_SearchStyles[2]); EditorGUI.BeginDisabledGroup(true); string labelText; int searchLabelIndex = m_Labels.IndexOf(m_SearchValue); if (searchLabelIndex >= 0) { if (m_LabelCount == null) { count = m_Entries[0].labels.Contains(m_SearchValue) ? m_Entries.Count : 0; } else { m_LabelCount.TryGetValue(m_SearchValue, out count); } labelText = string.Format(count == m_Entries.Count ? k_HintSearchFoundIsEnabled : k_HintSearchFoundIsDisabled, m_SearchValue); } else { labelText = !string.IsNullOrEmpty(m_SearchValue) ? string.Format(k_HintCreateNewLabel, m_SearchValue) : k_HintIdle; } Rect hintRect = EditorGUILayout.GetControlRect(true, m_HintLabelStyle.CalcHeight(new GUIContent(labelText), fullRect.width), m_HintLabelStyle); hintRect.x -= 3; hintRect.width += 6; hintRect.y -= 3; EditorGUI.LabelField(hintRect, new GUIContent(labelText), m_HintLabelStyle); EditorGUI.EndDisabledGroup(); if (Event.current.isKey && (Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter) && m_SearchField.HasFocus()) { if (!string.IsNullOrEmpty(m_SearchValue)) { if (searchLabelIndex >= 0) { if (count != m_Entries.Count) { SetLabelForEntries(m_SearchValue, true); } else { SetLabelForEntries(m_SearchValue, false); } } else { m_Settings.AddLabel(m_SearchValue); AddressableAssetUtility.OpenAssetIfUsingVCIntegration(m_Settings); SetLabelForEntries(m_SearchValue, true); m_Labels.Insert(0, m_SearchValue); } m_ControlToFocus = m_SearchValue; UnityEngine.GUI.ScrollTo(new Rect(0, searchLabelIndex * 19, 0, 0)); m_SearchValue = ""; m_LastItemCount = -1; Event.current.Use(); GUIUtility.ExitGUI(); editorWindow.Repaint(); } } var scrollViewHeight = areaRect.height - (hintRect.y + hintRect.height + 2); m_ScrollPosition = EditorGUILayout.BeginScrollView(m_ScrollPosition, false, false); Vector2 yPositionDrawRange = new Vector2(m_ScrollPosition.y - 19, m_ScrollPosition.y + scrollViewHeight); for (int i = 0; i < m_Labels.Count; ++i) { var labelName = m_Labels[i]; if (!string.IsNullOrEmpty(m_SearchValue)) { if (labelName.IndexOf(m_SearchValue, StringComparison.OrdinalIgnoreCase) < 0) { continue; } } var toggleRect = EditorGUILayout.GetControlRect(GUILayout.Width(m_LabelToggleControlRectWidth), GUILayout.Height(m_LabelToggleControlRectHeight)); if (toggleRect.height > 1) { // only draw toggles if they are in view if (toggleRect.y < yPositionDrawRange.x || toggleRect.y > yPositionDrawRange.y) { continue; } } else { continue; } bool newState; if (m_LabelCount == null) { count = m_Entries[0].labels.Contains(labelName) ? m_Entries.Count : 0; } else { m_LabelCount.TryGetValue(labelName, out count); } bool oldState = count == m_Entries.Count; if (!(count == 0 || count == m_Entries.Count)) { EditorGUI.showMixedValue = true; } UnityEngine.GUI.SetNextControlName(labelName); newState = EditorGUI.ToggleLeft(toggleRect, new GUIContent(labelName), oldState); EditorGUI.showMixedValue = false; if (oldState != newState) { SetLabelForEntries(labelName, newState); } } EditorGUILayout.EndScrollView(); GUILayout.EndArea(); if (Event.current.type == EventType.Repaint && m_Labels != null && m_ControlToFocus != null) { UnityEngine.GUI.FocusControl(m_ControlToFocus); m_ControlToFocus = null; } }