private static UITextField AddTextfield <T>(this UIHelperBase group, IOptionsWrapper <T> options, string text, string propertyName, TextfieldAttribute attr) { var property = typeof(T).GetProperty(propertyName); var initialValue = Convert.ToString(property.GetValue(options.GetOptions(), null)); return((UITextField)group.AddTextfield(text, initialValue, s => { }, s => { object value; if (property.PropertyType == typeof(int)) { value = Convert.ToInt32(s); } else if (property.PropertyType == typeof(short)) { value = Convert.ToInt16(s); } else if (property.PropertyType == typeof(double)) { value = Convert.ToDouble(s); } else if (property.PropertyType == typeof(float)) { value = Convert.ToSingle(s); } else { value = s; //TODO: more types } property.SetValue(options.GetOptions(), value, null); options.SaveOptions(); attr.Action <string>().Invoke(s); })); }
private static UIDropDown AddDynamicDropdown <T>(this UIHelperBase group, IOptionsWrapper <T> options, string text, string propertyName, DynamicDropDownAttribute attr, Func <string, string> translator = null) { var property = typeof(T).GetProperty(propertyName); var defaultCode = (string)property.GetValue(options.GetOptions(), null); int defaultSelection; var items = attr.GetItems(translator); var keys = items.Select(i => i.Code).ToArray(); var dictionary = items.ToDictionary(kvp => kvp.Code, kvp => kvp.Description); try { defaultSelection = Array.IndexOf(keys, defaultCode); } catch { defaultSelection = -1; } if (defaultSelection == -1) { defaultSelection = 0; property.SetValue(options.GetOptions(), keys.First(), null); } return((UIDropDown)group.AddDropdown(text, keys.Select(key => dictionary[key]).ToArray(), defaultSelection, sel => { var code = keys[sel]; property.SetValue(options.GetOptions(), code, null); options.SaveOptions(); attr.Action <string>().Invoke(code); })); }
private static UICheckBox AddCheckbox <T>(this UIHelperBase group, IOptionsWrapper <T> options, string text, string propertyName, CheckboxAttribute attr) { var property = typeof(T).GetProperty(propertyName); return((UICheckBox)group.AddCheckbox(text, (bool)property.GetValue(options.GetOptions(), null), b => { property.SetValue(options.GetOptions(), b, null); options.SaveOptions(); attr.Action <bool>().Invoke(b); })); }
private static UIDropDown AddEnumDropdown <T>(this UIHelperBase group, IOptionsWrapper <T> options, string text, string propertyName, EnumDropDownAttribute attr, Func <string, string> translator = null) { var property = typeof(T).GetProperty(propertyName); var defaultCode = (int)property.GetValue(options.GetOptions(), null); int defaultSelection; var items = attr.GetItems(translator); try { defaultSelection = items.First(kvp => kvp.Code == defaultCode).Code; } catch { defaultSelection = 0; property.SetValue(options.GetOptions(), items.First().Code, null); } return((UIDropDown)group.AddDropdown(text, items.Select(kvp => kvp.Description).ToArray(), defaultSelection, sel => { var code = items[sel].Code; property.SetValue(options.GetOptions(), code, null); options.SaveOptions(); attr.Action <int>().Invoke(code); })); }
private static UISlider AddSlider <T>(this UIHelperBase group, IOptionsWrapper <T> options, string text, string propertyName, SliderAttribute attr) { var property = typeof(T).GetProperty(propertyName); UILabel valueLabel = null; var helper = group as UIHelper; if (helper != null) { var type = typeof(UIHelper).GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance); if (type != null) { var panel = type.GetValue(helper) as UIComponent; valueLabel = panel?.AddUIComponent <UILabel>(); } } float finalValue; var value = property.GetValue(options.GetOptions(), null); if (value is float) { finalValue = (float)value; } else if (value is byte) { finalValue = (byte)value; } else if (value is int) { finalValue = (int)value; } else { throw new Exception("Unsupported numeric type for slider!"); } var slider = (UISlider)group.AddSlider(text, attr.Min, attr.Max, attr.Step, Mathf.Clamp(finalValue, attr.Min, attr.Max), f => { if (value is float) { property.SetValue(options.GetOptions(), f, null); } else if (value is byte) { property.SetValue(options.GetOptions(), (byte)Math.Round(f, MidpointRounding.AwayFromZero), null); } else if (value is int) { property.SetValue(options.GetOptions(), (int)Math.Round(f, MidpointRounding.AwayFromZero), null); } options.SaveOptions(); attr.Action <float>().Invoke(f); if (valueLabel != null) { valueLabel.text = f.ToString(CultureInfo.InvariantCulture); } }); var nameLabel = slider.parent.Find <UILabel>("Label"); if (nameLabel != null) { nameLabel.width = nameLabel.textScale * nameLabel.font.size * nameLabel.text.Length; } if (valueLabel == null) { return(slider); } valueLabel.AlignTo(slider, UIAlignAnchor.TopLeft); valueLabel.relativePosition = new Vector3(240, 0, 0); valueLabel.text = value.ToString(); return(slider); }