/// <summary> /// Displays a single-option popup. /// </summary> public static T PopupSingle ( Rect position, IEnumerable <DropdownOption <T> > options, T selectedValue, DropdownOption <T> noneOption, GUIContent label = null, GUIStyle style = null ) { var selectedOption = options.FirstOrDefault(o => Equal(o.value, selectedValue)); if (selectedOption == null) { selectedOption = new DropdownOption <T>(selectedValue); } return(PopupSingle ( position, options, selectedOption, noneOption, label, style )); }
/// <summary> /// Displays a single-option dropdown. /// </summary> public static void DropdownSingle ( Vector2 position, SingleCallback callback, IEnumerable <DropdownOption <T> > options, DropdownOption <T> selectedOption, DropdownOption <T> noneOption ) { var hasMultipleDifferentValues = EditorGUI.showMixedValue; ICollection <DropdownOption <T> > optionsCache = null; bool hasOptions; if (options != null) { optionsCache = options.CacheToCollection(); hasOptions = optionsCache.Count > 0; } else { hasOptions = false; } GenericMenu menu = new GenericMenu(); GenericMenu.MenuFunction2 menuCallback = (o) => { callback((T)o); }; if (noneOption != null) { bool on = !hasMultipleDifferentValues && (selectedOption == null || Equal(selectedOption.value, noneOption.value)); menu.AddItem(new GUIContent(noneOption.label), on, menuCallback, noneOption.value); } if (noneOption != null && hasOptions) { menu.AddSeparator(string.Empty); } if (hasOptions) { foreach (var option in optionsCache) { bool on = !hasMultipleDifferentValues && (selectedOption != null && Equal(selectedOption.value, option.value)); menu.AddItem(new GUIContent(option.label), on, menuCallback, option.value); } } menu.DropDown(new Rect(position, Vector2.zero)); }
/// <summary> /// Displays a single-option popup with a custom selected option label. /// </summary> public static T PopupSingle ( Rect position, IEnumerable <DropdownOption <T> > options, DropdownOption <T> selectedOption, DropdownOption <T> noneOption, GUIContent label = null, GUIStyle style = null ) { return(PopupSingle ( position, () => options, selectedOption, noneOption, label, style )); }
/// <summary> /// Displays a single-option popup where the option list is only built when the dropdown is shown. /// Useful when building the options list is very demanding (e.g. reflection). /// </summary> public static T PopupSingle ( Rect position, GetOptionsCallback getOptions, T selectedValue, DropdownOption <T> noneOption, GUIContent label = null, GUIStyle style = null ) { var selectedOption = new DropdownOption <T>(selectedValue); return(PopupSingle ( position, getOptions, selectedOption, noneOption, label, style )); }
/// <summary> /// Displays a single-option popup where the option list is only built when the dropdown is shown. /// Useful when building the options list is very demanding (e.g. reflection). /// </summary> public static T PopupSingle ( Rect position, GetOptionsCallback getOptions, DropdownOption <T> selectedOption, DropdownOption <T> noneOption, GUIContent label = null, GUIStyle style = null ) { var hasMultipleDifferentValues = EditorGUI.showMixedValue; // Determine the label text if no override is specified if (label == null) { string text; if (hasMultipleDifferentValues) { text = "\u2014"; // Em Dash } else if (selectedOption == null) { if (noneOption != null) { text = noneOption.label; } else { text = string.Empty; } } else { text = selectedOption.label; } label = new GUIContent(text); } // Apply the popup style is no override is specified if (style == null) { style = EditorStyles.popup; } // Render a button and get its control ID var popupClicked = GUI.Button(position, label, style); var popupControlID = GetLastControlID(); if (popupClicked) { // Cancel button click GUI.changed = false; // Assign the active control ID activePopupControlID = popupControlID; // Display the dropdown DropdownSingle ( new Vector2(position.xMin, position.yMax), (value) => { activeSingleDropdownValue = value; activeDropdownChanged = true; }, getOptions(), selectedOption, noneOption ); } if (popupControlID == activePopupControlID && activeDropdownChanged) { // Selected option changed // TODO: Use EditorWindow.SendEvent like EditorGUI.PopupCallbackInfo does. // Otherwise, there seems to be a 1-frame delay in update. GUI.changed = true; activePopupControlID = -1; activeDropdownChanged = false; return(activeSingleDropdownValue); } else if (selectedOption == null) { // Selected option is null if (noneOption != null) { return(noneOption.value); } else { return(default(T)); } } else { return(selectedOption.value); } }