public static object FromString(EditorPropertyType type, string value) { if (type == EditorPropertyType.String) return value; #if !(RELEASE && RELEASE_DISABLE_CHECKS) if (value == null) throw new System.ArgumentNullException("value"); if (value.Length < 1) throw new System.ArgumentException("value string was empty"); #endif switch (type) { case EditorPropertyType.Bool: { if (value == "0") value = "false"; else if (value == "1") value = "true"; return bool.Parse(value); } case EditorPropertyType.Int: return int.Parse(value); case EditorPropertyType.Float: return float.Parse(value); case EditorPropertyType.Vec3: return Vec3.Parse(value); } return null; }
public EditorProperty(string Name, string Desc, EditorPropertyType Type) : this() { name = Name; description = Desc; type = Type; }
public EditorProperty(string Name, string Desc, string DefaultValue, EditorPropertyType Type) : this() { name = Name; description = Desc; defaultValue = DefaultValue; type = Type; }
/// <summary> /// Initializes new instance of type <see cref="EditorProperty" />. /// </summary> /// <param name="name"> Name of the property. </param> /// <param name="description"> Description of the property. </param> /// <param name="defaultValue"> Default value of the property. </param> /// <param name="type"> Type of the property. </param> public EditorProperty(string name, string description, string defaultValue, EditorPropertyType type) : this() { this.Name = name; this.Description = description; this.DefaultValue = defaultValue; this.Type = type; }
public static EditorPropertyType GetEditorType(Type type, EditorPropertyType propertyType) { //If a special type is needed, do this here. switch (propertyType) { case EditorPropertyType.Object: case EditorPropertyType.Texture: case EditorPropertyType.File: case EditorPropertyType.Sound: case EditorPropertyType.Dialogue: case EditorPropertyType.Sequence: { if (type == typeof(string)) { return(propertyType); } throw new EntityException("File selector type was specified, but property was not a string."); } case EditorPropertyType.Color: { if (type == typeof(Vec3)) { return(propertyType); } throw new EntityException("Vector type was specified, but property was not a vector."); } } //OH PROGRAMMING GODS, FORGIVE ME if (type == typeof(string)) { return(EditorPropertyType.String); } if (type == typeof(int)) { return(EditorPropertyType.Int); } if (type == typeof(float) || type == typeof(double)) { return(EditorPropertyType.Float); } if (type == typeof(bool)) { return(EditorPropertyType.Bool); } if (type == typeof(Vec3)) { return(EditorPropertyType.Vec3); } throw new EntityException("Invalid property type specified."); }
public EditorProperty(string Name, string Desc, string DefaultValue, EditorPropertyType Type, EditorPropertyLimits Limits, int Flags = 0) : this(Name, Desc, DefaultValue, Type) { if (Limits.max == 0 && Limits.min == 0) { limits.max = Sandbox.UIConstants.MAX_SLIDER_VALUE; } else { limits.max = Limits.max; limits.min = Limits.min; } flags = Flags; }
/// <summary> /// Initializes new instance of type <see cref="EditorProperty" />. /// </summary> /// <param name="name"> Name of the property. </param> /// <param name="description"> Description of the property. </param> /// <param name="defaultValue"> Default value of the property. </param> /// <param name="type"> Type of the property. </param> /// <param name="limits"> Limitations applied to the property. </param> /// <param name="flags"> Flags assigned to the property. </param> public EditorProperty(string name, string description, string defaultValue, EditorPropertyType type, EditorPropertyLimits limits, int flags = 0) : this(name, description, defaultValue, type) { if (Math.Abs(limits.Max) < MathHelpers.ZeroTolerance && Math.Abs(limits.Min) < MathHelpers.ZeroTolerance) { this.Limits.Max = Sandbox.UserInterfaceConstants.MaxSliderValue; } else { this.Limits.Max = limits.Max; this.Limits.Min = limits.Min; } this.Flags = flags; }
public static object FromString(EditorPropertyType type, string value) { if (type == EditorPropertyType.String) { return(value); } #if !(RELEASE && RELEASE_DISABLE_CHECKS) if (value == null) { throw new System.ArgumentNullException("value"); } if (value.Length < 1) { throw new System.ArgumentException("value string was empty"); } #endif switch (type) { case EditorPropertyType.Bool: { if (value == "0") { value = "false"; } else if (value == "1") { value = "true"; } return(bool.Parse(value)); } case EditorPropertyType.Int: return(int.Parse(value)); case EditorPropertyType.Float: return(float.Parse(value)); case EditorPropertyType.Vec3: return(Vec3.Parse(value)); } return(null); }
internal virtual void SetPropertyValue(string propertyName, EditorPropertyType propertyType, string valueString) { #if !((RELEASE && RELEASE_DISABLE_CHECKS)) if (valueString == null) { throw new ArgumentNullException("valueString"); } if (propertyName == null) { throw new ArgumentNullException("propertyName"); } if (valueString.Length < 1 && propertyType != EditorPropertyType.String) { throw new ArgumentException("value was empty!"); } if (propertyName.Length < 1) { throw new ArgumentException("propertyName was empty!"); } #endif var value = ConvertExtension.FromString(propertyType, valueString); var member = this.GetType() .GetMember(propertyName) .FirstOrDefault(x => x.MemberType == MemberTypes.Field || x.MemberType == MemberTypes.Property); if (member == null) { throw new ArgumentException(string.Format("member {0} could not be located", propertyName)); } if (member.MemberType == MemberTypes.Property) { (member as PropertyInfo).SetValue(this, value, null); } else { (member as FieldInfo).SetValue(this, value); } this.OnPropertyChanged(member, propertyType, value); }
public static EditorPropertyType GetEditorType(Type type, EditorPropertyType propertyType) { //If a special type is needed, do this here. switch (propertyType) { case EditorPropertyType.Object: case EditorPropertyType.Texture: case EditorPropertyType.File: case EditorPropertyType.Sound: case EditorPropertyType.Dialogue: case EditorPropertyType.Sequence: { if (type == typeof(string)) return propertyType; throw new EntityException("File selector type was specified, but property was not a string."); } case EditorPropertyType.Color: { if (type == typeof(Vec3)) return propertyType; throw new EntityException("Vector type was specified, but property was not a vector."); } } //OH PROGRAMMING GODS, FORGIVE ME if (type == typeof(string)) return EditorPropertyType.String; if (type == typeof(int)) return EditorPropertyType.Int; if (type == typeof(float) || type == typeof(double)) return EditorPropertyType.Float; if (type == typeof(bool)) return EditorPropertyType.Bool; if (type == typeof(Vec3)) return EditorPropertyType.Vec3; throw new EntityException("Invalid property type specified."); }
/// <summary> /// Called when the user changes a property from within the Editor. /// </summary> /// <param name="memberInfo"></param> /// <param name="propertyType"></param> /// <param name="newValue"></param> protected virtual void OnPropertyChanged(MemberInfo memberInfo, EditorPropertyType propertyType, object newValue) { }
internal virtual void SetPropertyValue(string propertyName, EditorPropertyType propertyType, string valueString) { #if !((RELEASE && RELEASE_DISABLE_CHECKS)) if (valueString == null) throw new ArgumentNullException("valueString"); if (propertyName == null) throw new ArgumentNullException("propertyName"); if (valueString.Length < 1 && propertyType != EditorPropertyType.String) throw new ArgumentException("value was empty!"); if (propertyName.Length < 1) throw new ArgumentException("propertyName was empty!"); #endif var value = Convert.FromString(propertyType, valueString); var member = GetType().GetMember(propertyName).First(x => x.MemberType == MemberTypes.Field || x.MemberType == MemberTypes.Property); if (member == null) throw new ArgumentException(string.Format("member {0} could not be located", propertyName)); if (member.MemberType == MemberTypes.Property) (member as PropertyInfo).SetValue(this, value, null); else (member as FieldInfo).SetValue(this, value); OnPropertyChanged(member, propertyType, value); }
private static object[] GetProperties(IReflect type) { SortedList <string, List <EditorProperty> > folders = new SortedList <string, List <EditorProperty> >(); // Get all public and not so public fields and properties with EditorProperty // attribute. var members = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .Where(member => member.ContainsAttribute <EditorPropertyAttribute>()); foreach (MemberInfo member in members) { EditorPropertyAttribute attribute = member.GetAttribute <EditorPropertyAttribute>(); // Validate the type. EditorPropertyType propType = Entity.GetEditorType(member.GetAssociatedType(), attribute.Type); // Use default folder, if it is not specified. string folderName = attribute.Folder ?? "Default"; // Register new folder, if there is a need for that. if (!folders.ContainsKey(folderName)) { folders.Add(folderName, new List <EditorProperty>()); } // Add the property to the folder. folders[folderName].Add ( new EditorProperty ( attribute.Name ?? member.Name, attribute.Description, attribute.DefaultValue ?? new Func <string> ( delegate { switch (propType) { case EditorPropertyType.Bool: return("false"); case EditorPropertyType.Int: case EditorPropertyType.Float: return("0"); case EditorPropertyType.Vector3: case EditorPropertyType.Color: return("0,0,0"); default: return(""); } } ).Invoke(), propType, new EditorPropertyLimits { Min = attribute.Min, Max = attribute.Max }, attribute.Flags ) ); } // Now lets create the array of properties. List <object> properties = new List <object>(); foreach (KeyValuePair <string, List <EditorProperty> > folder in folders) { if (folder.Key == "Default") { properties.AddRange(folder.Value.Cast <object>()); } else { properties.Add(new EditorProperty(folder.Key, "", "", EditorPropertyType.FolderBegin)); properties.AddRange(folder.Value.Cast <object>()); properties.Add(new EditorProperty(folder.Key, "", "", EditorPropertyType.FolderEnd)); } } return(properties.ToArray()); }