Created
January 5, 2024 18:11
-
-
Save Denchyaknow/4b96e299f8290eeba6c424f394853bd1 to your computer and use it in GitHub Desktop.
A HowTo on making a nice little popup selector for a list of markdown files in a given directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void DrawMainPageSelector() | |
{ | |
bool isMainPageSet = !string.IsNullOrEmpty(setMainPage); | |
var mainPageTitle = !isMainPageSet ? "NONE" : Path.GetFileName(setMainPage.ToUpper()); | |
//Display a Popup selector with default of non if exisitng value is not in the list | |
using (var mainPageScope = new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) | |
{ | |
GUILayout.Label(string.Format("Main Page Setting\nCurrent: {0}", isMainPageSet?setMainPage.ToUpper():"NONE"), EditorStyles.centeredGreyMiniLabel); | |
using (var selectionScope = new EditorGUILayout.HorizontalScope(EditorStyles.toolbar)) | |
{ | |
EditorGUILayout.PrefixLabel("Main Page ", EditorStyles.toolbar); | |
int selection = 0; | |
var existing = mainPageCandidates.Where((x) => x == setMainPage).FirstOrDefault(); | |
selection = mainPageCandidates.IndexOf(existing); | |
if(selection < 0) | |
selection = 0; | |
Color gCol = GUI.color; | |
GUI.color = isMainPageSet ? Color.green : Color.yellow; | |
var nextSel = EditorGUILayout.Popup(selection, mainPageCandidates.ToArray()); | |
GUI.color = gCol; | |
if (selection != nextSel) | |
{ | |
if(nextSel == 0) | |
setMainPage = string.Empty; | |
else | |
setMainPage = mainPageCandidates[nextSel]; | |
selection = nextSel; | |
} | |
if(mainPageCandidates.Count < 0 && !string.IsNullOrEmpty(inputDirectory) || GUILayout.Button("Refresh", EditorStyles.toolbarButton)) | |
{ | |
RefreshMainPageCandidates(); | |
} | |
} | |
} | |
} | |
private void RefreshMainPageCandidates() | |
{ | |
if (string.IsNullOrEmpty(inputDirectory)) | |
return; | |
mainPageCandidates.Clear(); | |
mainPageCandidates = GetMarkDownFiles(inputDirectory); | |
mainPageCandidates.Insert(0, "NONE"); | |
} | |
private List<string> GetMarkDownFiles(string path) | |
{ | |
var files = new List<string>(); | |
var directories = Directory.GetDirectories(path); | |
foreach (var directory in directories) | |
{ | |
files.AddRange(GetMarkDownFiles(directory)); | |
} | |
var filePaths = Directory.GetFiles(path); | |
foreach (var filePath in filePaths) | |
{ | |
if (filePath.EndsWith(".md")) | |
{ | |
files.Add(filePath); | |
} | |
} | |
return files; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment