public object PropertyEditorEnum(controlProperty property, Enum value) { if (property.canSet == false) { Editor.Label(property.info.Name, value); return(null); } var enumHasFlagAttribute = value.GetType().GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0; var enumOptions = Enum.GetNames(value.GetType()); if (enumHasFlagAttribute) { // TODO: not gonna work with 'None' flag. // https://forum.unity3d.com/threads/editorguilayout-enummaskfield-doesnt-use-enums-values.233332/ var eeVal = Editor.MaskField(property.info.Name, Convert.ToInt32(value), enumOptions); if (eeVal.Changed) { property.info.SetValue(target, eeVal.Value, null); } } else { var eeVal = Editor.EnumField(property.info.Name, (Enum)value); if (eeVal.Changed) { property.info.SetValue(target, eeVal.Value, null); } } return(null); }
public object PropertyEditorControl(controlProperty property, Control value) { if (Editor.Button(property.info.Name, value.GetType().Name)) { return(value); } return(null); }
public object PropertyEditorColor(controlProperty property, Color value) { if (property.canSet == false) { Editor.Label(property.info.Name, value); return(null); } Editor.ColorField(property.info.Name, value, c => property.info.SetValue(target, c, null)); return(null); }
public objectEditor(object o, string objName) { obj = o; name = objName; var objType = obj.GetType(); var pList = objType.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); pList.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal)); props = new List <controlProperty>(); for (int i = 0; i < pList.Count; i++) { var p = pList[i]; if (p.DeclaringType == typeof(Delegate)) { continue; } if (p.Name == "Item") { continue; // this[] will throw an exception. } var cp = new controlProperty() { info = p, }; props.Add(cp); } fields = objType.GetFields(BindingFlags.Public | BindingFlags.Instance).ToList(); fields.Sort((x, y) => x.Name.CompareTo(y.Name)); methods = objType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public).ToList(); methods.Sort((x, y) => String.Compare(x.Name, y.Name, StringComparison.Ordinal)); for (int i = 0; i < methods.Count; i++) { var m = methods[i]; if (m.GetParameters().Length == 0 && m.ReturnType == typeof(void)) { continue; } methods.RemoveAt(i); i--; } }
public object PropertyEditorString(controlProperty property, string value) { if (property.canSet == false) { Editor.Label(property.info.Name, value); return(null); } var esVal = Editor.TextField(property.info.Name, value); if (esVal.Changed) { property.info.SetValue(target, esVal.Value, null); } return(null); }
public object PropertyEditorInt32(controlProperty property, int value) { if (property.canSet == false) { Editor.Label(property.info.Name, value); return(null); } var eiVal = Editor.IntField(property.info.Name, value); if (eiVal.Changed) { property.info.SetValue(target, eiVal.Value[0], null); } return(null); }
public object PropertyEditorBool(controlProperty property, bool value) { if (property.canSet == false) { Editor.Label(property.info.Name, value); return(null); } var ebVal = Editor.BooleanField(property.info.Name, value); if (ebVal.Changed) { property.info.SetValue(target, ebVal.Value, null); } return(null); }
public object PropertyEditorEnumerable(controlProperty property, IEnumerable value) { Editor.BeginVertical(); property.expanded = Editor.Foldout(property.info.Name, property.expanded); if (property.expanded) { var arrayIndex = 0; foreach (var item in value) { var control = item as Control; if (control != null) { if (Editor.Button(" " + arrayIndex, control.ToString())) { return(control); } } else { if (arrayIndex >= property.arrayEditors.Count) { var itemText = "null"; if (item != null) { itemText = item.ToString(); } var aEditor = new objectEditor(item, arrayIndex + " (" + itemText + ")"); aEditor.backgroundRGB = MathHelper.Clamp(backgroundRGB - 25, 128, 255); property.arrayEditors.Add(aEditor); } property.arrayEditors[arrayIndex].Draw(); } arrayIndex++; } } Editor.EndVertical(); return(null); }
public object PropertyEditorUInt16(controlProperty property, ushort value) { if (property.canSet == false) { Editor.Label(property.info.Name, value); return(null); } var eiVal = Editor.IntField(property.info.Name, value); if (eiVal.Changed) { var newValue = (ushort)MathHelper.Clamp(eiVal.Value[0], ushort.MinValue, ushort.MaxValue); property.info.SetValue(target, newValue, null); } return(null); }
public object PropertyEditorUInt64(controlProperty property, ulong value) { if (property.canSet == false) { Editor.Label(property.info.Name, value); return(null); } var eiVal = Editor.TextField(property.info.Name, value.ToString()); if (eiVal.Changed) { ulong newValue; ulong.TryParse(eiVal.Value, out newValue); property.info.SetValue(target, newValue, null); } return(null); }
public object PropertyEditorSingle(controlProperty property, float value) { if (property.canSet == false) { Editor.Label(property.info.Name, value); return(null); } var eiVal = Editor.TextField(property.info.Name, value.ToString(CultureInfo.InvariantCulture)); if (eiVal.Changed) { float newValue; float.TryParse(eiVal.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out newValue); property.info.SetValue(target, newValue, null); } return(null); }
public object Draw(controlProperty property) { if (property.info.CanRead == false) { return(null); } var value = property.info.GetValue(target, null); if (value == null) { Editor.Label(property.info.Name, "(" + property.info.PropertyType.Name + ") null"); return(null); } // Array & List. if (value is string == false && (value.GetType().IsArray || value is IEnumerable)) { return(PropertyEditorEnumerable(property, (IEnumerable)value)); } // Base editors. if (value is bool) { return(PropertyEditorBool(property, (bool)value)); } if (value is byte) { return(PropertyEditorUInt8(property, (byte)value)); } if (value is decimal) { return(PropertyEditorDecimal(property, (decimal)value)); } if (value is double) { return(PropertyEditorDouble(property, (double)value)); } if (value is float) { return(PropertyEditorSingle(property, (float)value)); } if (value is int) { return(PropertyEditorInt32(property, (int)value)); } if (value is long) { return(PropertyEditorInt64(property, (long)value)); } if (value is sbyte) { return(PropertyEditorInt8(property, (sbyte)value)); } if (value is short) { return(PropertyEditorInt16(property, (short)value)); } if (value is uint) { return(PropertyEditorUInt32(property, (uint)value)); } if (value is ulong) { return(PropertyEditorUInt64(property, (ulong)value)); } if (value is ushort) { return(PropertyEditorUInt16(property, (ushort)value)); } if (value is string) { return(PropertyEditorString(property, (string)value)); } if (value is Color) { return(PropertyEditorColor(property, (Color)value)); } if (value is Control) { return(PropertyEditorControl(property, (Control)value)); } if (value is Enum) { return(PropertyEditorEnum(property, (Enum)value)); } // Complex types. if (property.editor == null) { property.editor = new objectEditor(value, property.info.Name); property.editor.backgroundRGB = MathHelper.Clamp(backgroundRGB - 25, 128, 255); } return(property.editor.Draw()); }
public object Draw(controlProperty p) { object controlToSet = null; if (p.info.CanRead == false) { return(null); } var val = p.info.GetValue(obj, null); Type type = null; if (val != null) { type = val.GetType(); } else { Editor.Label(p.info.Name, "null"); return(null); } // Array & List. if (val is string == false) { if (type.IsArray || val is IEnumerable) { Editor.BeginVertical(); p.expanded = Editor.Foldout(p.info.Name, p.expanded); if (p.expanded) { var vEnum = val as IEnumerable; var arrayIndex = 0; foreach (var e in vEnum) { var ec = e as Control; if (ec != null) { if (Editor.Button(ec.ToString())) { controlToSet = ec; } } else { if (arrayIndex >= p.arrayEditors.Count) { var aEditor = new objectEditor(e, arrayIndex.ToString()); aEditor.rgb = rgb - 25; if (aEditor.rgb < 128) { aEditor.rgb = 128; } p.arrayEditors.Add(aEditor); } p.arrayEditors[arrayIndex].Draw(); } arrayIndex++; } } Editor.EndVertical(); return(controlToSet); } } // If there is no Set() method then skip. var canSet = true; var pSetMethod = p.info.GetSetMethod(true); if (pSetMethod == null || pSetMethod.IsPrivate) { canSet = false; } // Other editors. if (val is bool) { if (canSet == false) { Editor.Label(p.info.Name, val); return(null); } var bVal = (bool)val; var ebVal = Editor.BooleanField(p.info.Name, bVal); if (ebVal.Changed) { p.info.SetValue(obj, ebVal.Value, null); } } else if (val is Control) { var cVal = val as Control; if (Editor.Button(p.info.Name, cVal.GetType().Name)) { controlToSet = cVal; } } else if (val is Color) { if (canSet == false) { Editor.Label(p.info.Name, val); return(null); } var colorVal = (Color)val; Editor.ColorField(p.info.Name, colorVal, c => p.info.SetValue(obj, c, null)); } else if (val is string) { if (canSet == false) { Editor.Label(p.info.Name, val); return(null); } var stringtVal = (string)val; var esVal = Editor.TextField(p.info.Name, stringtVal); if (esVal.Changed) { p.info.SetValue(obj, esVal.Value, null); } } else if (val is int) { if (canSet == false) { Editor.Label(p.info.Name, val); return(null); } var eiVal = Editor.IntField(p.info.Name, (int)val); if (eiVal.Changed) { p.info.SetValue(obj, eiVal.Value[0], null); } } else if (val is byte || val is sbyte || val is short || val is ushort || val is uint || val is long || val is ulong || val is float || val is double) { if (canSet == false) { Editor.Label(p.info.Name, val); return(null); } // TODO: editors for common types (like for int ^up there). Editor.Label(p.info.Name, val); } else if (val is Enum) { if (canSet == false) { Editor.Label(p.info.Name, val); return(null); } var enumHasFlagAttribute = val.GetType().GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0; var enumOptions = Enum.GetNames(val.GetType()); if (enumHasFlagAttribute) { // TODO: not gonna work with 'None' flag. // https://forum.unity3d.com/threads/editorguilayout-enummaskfield-doesnt-use-enums-values.233332/ var eeVal = Editor.MaskField(p.info.Name, Convert.ToInt32(val), enumOptions); if (eeVal.Changed) { p.info.SetValue(obj, eeVal.Value, null); } } else { var eeVal = Editor.EnumField(p.info.Name, (Enum)val); if (eeVal.Changed) { p.info.SetValue(obj, eeVal.Value, null); } } } else if (val != null) { if (p.editor == null) { p.editor = new objectEditor(val, p.info.Name); p.editor.rgb = rgb - 25; if (p.editor.rgb < 128) { p.editor.rgb = 128; } } p.editor.Draw(); } return(controlToSet); }