/// <summary> /// Render the Variable Name dropdown /// </summary> /// <param name="label">The label to display</param> /// <param name="currentValue">The currently selected variable value</param> /// <param name="options">The available options</param> /// <param name="variableTypeFilter">The type of variables to use</param> /// <returns>The name of the selected variable, or string.empty /// if none</returns> protected static string RenderVariableSelector(Rect rect, string label, string currentValue, VariableDetails[] options, IsValidVariableDelegate variableTypeFilter) { VariableDetails[] filtered = options.Where( (x) => variableTypeFilter(x.VariableType)).ToArray(); filtered = AddNoneOption(filtered); int selectedIndex = Array.FindIndex(filtered, (x) => x.Name.Equals(currentValue, StringComparison.OrdinalIgnoreCase)); if (selectedIndex < 0) { selectedIndex = 0; } selectedIndex = EditorGUI.Popup(rect, label, selectedIndex, filtered.Select((x) => x.Name).ToArray()); if (selectedIndex < 0 || selectedIndex >= filtered.Length) { return(""); } VariableDetails selected = filtered[selectedIndex]; if (selected == VariableDetails.NoneVariable) { return(string.Empty); } return(selected.Name); }
public void InitializeVariablesList(SceneVariableContainer variablesContainer) { IEnumerable <KeyValuePair <string, Type> > varDetails = variablesContainer.GetVariableDetails(); List <VariableDetails> details = new List <VariableDetails>(); foreach (var varDetail in varDetails) { VariableDetails detail = new VariableDetails(varDetail.Key, varDetail.Value); details.Add(detail); } VariablesList = details.ToArray(); }
/// <summary> /// Helper to make sure that the variable details array contains /// a None option /// </summary> /// <param name="variableDetails">The array to ensure contains a None option</param> /// <returns>The array with a none option</returns> private static VariableDetails[] AddNoneOption(VariableDetails[] variableDetails) { if (variableDetails == null) { variableDetails = new VariableDetails[] { VariableDetails.NoneVariable }; return(variableDetails); } int index = Array.FindIndex(variableDetails, (x) => x == VariableDetails.NoneVariable); if (index < 0) { VariableDetails[] newList = new VariableDetails[variableDetails.Length + 1]; newList[0] = VariableDetails.NoneVariable; variableDetails.CopyTo(newList, 1); return(newList); } else if (index != 0) { variableDetails.SwapValues(0, index); } return(variableDetails); }