public virtual void OnGUI() { searchQuery = InventoryEditorStyles.SearchBar(searchQuery, this, isSearching); if (focusOnInput) { EditorGUI.FocusTextInControl("SearchField"); focusOnInput = false; } //EditorGUILayout.BeginHorizontal(); //searchQuery = EditorGUILayout.TextField(searchQuery); //EditorGUILayout.EndHorizontal(); //EditorGUILayout.BeginHorizontal(); //EditorGUILayout.LabelField("ID", GUILayout.Width(20)); //searchID = EditorGUILayout.Toggle(searchID); //EditorGUILayout.LabelField("Name", GUILayout.Width(40)); //searchName = EditorGUILayout.Toggle(searchName); //EditorGUILayout.LabelField("Description", GUILayout.Width(60)); //searchDesc = EditorGUILayout.Toggle(searchDesc); //EditorGUILayout.LabelField("Category", GUILayout.Width(50)); //searchCategory = EditorGUILayout.Toggle(searchCategory); //EditorGUILayout.LabelField("Rarity", GUILayout.Width(50)); //searchRarity = EditorGUILayout.Toggle(searchRarity); //EditorGUILayout.LabelField("Type", GUILayout.Width(35)); //searchType = EditorGUILayout.Toggle(searchType); //EditorGUILayout.EndHorizontal(); ShowInfoBox(); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); int resultCount = 0; T selectedObject = null; foreach (var obj in objects) { EditorGUILayout.BeginHorizontal(); if (isSearching) { string search = searchQuery.ToLower(); if (MatchesSearch(obj, search)) { if (resultCount == selectionIndex) { GUI.color = Color.green; selectedObject = obj; } DrawObjectButton(obj); resultCount++; } } else { if (resultCount == selectionIndex) { GUI.color = Color.green; selectedObject = obj; } DrawObjectButton(obj); resultCount++; } GUI.color = Color.white; EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndScrollView(); if (Event.current.isKey) { selectionIndex = Mathf.Clamp(selectionIndex, 0, resultCount - 1); // Keyboard movement if (Event.current.keyCode == KeyCode.DownArrow) { selectionIndex++; Repaint(); } else if (Event.current.keyCode == KeyCode.UpArrow) { selectionIndex--; Repaint(); } // When pressing enter, the selected item if (Event.current.keyCode == KeyCode.Return) { if (resultCount > 0 && selectedObject != null) { NotifyPickedObject(selectedObject); } else { EditorGUI.FocusTextInControl("SearchField"); Repaint(); } } } }
/// <summary> /// Draw the list, where item can be selected to edit /// </summary> protected virtual void DrawSidebar() { EditorGUILayout.BeginVertical(InventoryEditorStyles.boxStyle, GUILayout.Width(375.0f)); searchQuery = InventoryEditorStyles.SearchBar(searchQuery, window, isSearching); //if (isFirstRepaintCycle) //{ // EditorGUI.FocusTextInControl("SearchField"); // isFirstRepaintCycle = false; //} GUI.color = createColor; GUI.enabled = canCreateItems; if (GUILayout.Button("Create " + singleName, (GUIStyle)"LargeButton")) { CreateNewItem(); } GUI.color = Color.white; GUI.enabled = true; scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); // BEGIN ROW EditorGUILayout.BeginHorizontal(); if (isSearching) { GUILayout.Label(searchResultCount + " " + pluralName + " (search result)"); // , InventoryEditorStyles.titleStyle } else { GUILayout.Label(crudList.Count + " " + pluralName); } EditorGUILayout.EndHorizontal(); // END ROW int x = 0; searchResultCount = 0; int searchResultIndexItem = -1; bool nullInList = false; foreach (var item in crudList) { if (item == null) { x++; nullInList = true; continue; } if (item.Equals(selectedItem)) { GUI.color = Color.green; } if (isSearching) { if (MatchesSearch(item, searchQuery)) { searchResultCount++; DrawSidebarRow(item, x); searchResultIndexItem = x; } } else { DrawSidebarRow(item, x); } GUI.color = Color.white; x++; } if (nullInList) { // Cleanup list var l = new List <T>(crudList.ToArray()); l.RemoveAll(o => o == null); crudList = l; } // Edit item if only 1 search result if (searchResultIndexItem != -1 && searchResultCount == 1) { EditItem(crudList[searchResultIndexItem]); } if (searchResultCount == 0 && isSearching) { selectedItem = null; window.Repaint(); } EditorGUILayout.EndScrollView(); EditorGUILayout.EndHorizontal(); }