private GUIComponent CreateVector3Field(ISerializableEntity entity, SerializableProperty property, Vector3 value, int yPos, GUIComponent parent) { var label = new GUITextBlock(new Rectangle(0, yPos, 0, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, parent, false, GUI.SmallFont); label.ToolTip = property.GetAttribute <Editable>().ToolTip; for (int i = 0; i < 3; i++) { new GUITextBlock(new Rectangle(140 + i * 70, yPos, 100, 18), vectorComponentLabels[i], "", Alignment.TopLeft, Alignment.CenterLeft, parent, false, GUI.SmallFont); GUINumberInput numberInput = new GUINumberInput(new Rectangle(160 + i * 70, yPos, 45, 18), "", GUINumberInput.NumberType.Float, Alignment.Left, parent); numberInput.Font = GUI.SmallFont; if (i == 0) { numberInput.FloatValue = value.X; } else if (i == 1) { numberInput.FloatValue = value.Y; } else if (i == 2) { numberInput.FloatValue = value.Z; } int comp = i; numberInput.OnValueChanged += (numInput) => { Vector3 newVal = (Vector3)property.GetValue(); if (comp == 0) { newVal.X = numInput.FloatValue; } else if (comp == 1) { newVal.Y = numInput.FloatValue; } else { newVal.Z = numInput.FloatValue; } if (property.TrySetValue(newVal)) { TrySendNetworkUpdate(entity, property); } }; } return(label); }
// TODO: refactor and add tests private bool Matches(ISerializableEntity target, SerializableProperty property) { object propertyValue = property.GetValue(target); if (propertyValue == null) { DebugConsole.ThrowError("Couldn't compare " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" - property.GetValue() returns null!"); return(false); } Type type = propertyValue.GetType(); float?floatProperty = null; if (type == typeof(float) || type == typeof(int)) { floatProperty = (float)propertyValue; } switch (Operator) { case OperatorType.Equals: if (type == typeof(bool)) { return(((bool)propertyValue) == (AttributeValue == "true")); } else if (FloatValue == null) { return(propertyValue.ToString().Equals(AttributeValue)); } else { return(propertyValue.Equals(FloatValue)); } case OperatorType.NotEquals: if (type == typeof(bool)) { return(((bool)propertyValue) != (AttributeValue == "true")); } else if (FloatValue == null) { return(!propertyValue.ToString().Equals(AttributeValue)); } else { return(!propertyValue.Equals(FloatValue)); } case OperatorType.GreaterThan: if (FloatValue == null) { DebugConsole.ThrowError("Couldn't compare " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } else if (floatProperty > FloatValue) { return(true); } break; case OperatorType.LessThan: if (FloatValue == null) { DebugConsole.ThrowError("Couldn't compare " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } else if (floatProperty < FloatValue) { return(true); } break; case OperatorType.GreaterThanEquals: if (FloatValue == null) { DebugConsole.ThrowError("Couldn't compare " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } else if (floatProperty >= FloatValue) { return(true); } break; case OperatorType.LessThanEquals: if (FloatValue == null) { DebugConsole.ThrowError("Couldn't compare " + AttributeValue.ToString() + " (" + AttributeValue.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } else if (floatProperty <= FloatValue) { return(true); } break; } return(false); }
private bool Matches(SerializableProperty property) { object propertyValue = property.GetValue(); if (propertyValue == null) { DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" - property.GetValue() returns null!"); return(false); } Type type = propertyValue.GetType(); float?floatValue = null; float?floatProperty = null; if (type == typeof(float) || type == typeof(int)) { float parsedFloat; if (Single.TryParse(Value, NumberStyles.Float, CultureInfo.InvariantCulture, out parsedFloat)) { floatValue = parsedFloat; } floatProperty = (float)propertyValue; } switch (Operator) { case OperatorType.Equals: if (floatValue == null) { return(property.GetValue().Equals(floatValue)); } else { return(property.GetValue().Equals(Value)); } case OperatorType.NotEquals: if (floatValue == null) { return(!property.GetValue().Equals(floatValue)); } else { return(!property.GetValue().Equals(Value)); } case OperatorType.GreaterThan: if (floatValue == null) { DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } else if (floatProperty > floatValue) { return(true); } break; case OperatorType.LessThan: if (floatValue == null) { DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } else if (floatProperty < floatValue) { return(true); } break; case OperatorType.GreaterThanEquals: if (floatValue == null) { DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } else if (floatProperty >= floatValue) { return(true); } break; case OperatorType.LessThanEquals: if (floatValue == null) { DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "\" (" + type + ")! " + "Make sure the type of the value set in the config files matches the type of the property."); } else if (floatProperty <= floatValue) { return(true); } break; } return(false); }
private GUIComponent CreateColorField(ISerializableEntity entity, SerializableProperty property, Color value, int yPos, GUIComponent parent) { var label = new GUITextBlock(new Rectangle(0, yPos, 0, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, parent, false, GUI.SmallFont); label.ToolTip = property.GetAttribute <Editable>().ToolTip; var colorBoxBack = new GUIFrame(new Rectangle(110 - 1, yPos - 1, 25 + 2, 18 + 2), Color.Black, Alignment.TopLeft, null, parent); var colorBox = new GUIFrame(new Rectangle(110, yPos, 25, 18), value, Alignment.TopLeft, null, parent); for (int i = 0; i < 4; i++) { new GUITextBlock(new Rectangle(140 + i * 70, yPos, 100, 18), colorComponentLabels[i], "", Alignment.TopLeft, Alignment.CenterLeft, parent, false, GUI.SmallFont); GUINumberInput numberInput = new GUINumberInput(new Rectangle(160 + i * 70, yPos, 45, 18), "", GUINumberInput.NumberType.Int, Alignment.Left, parent); numberInput.MinValueInt = 0; numberInput.MaxValueInt = 255; if (i == 0) { numberInput.IntValue = value.R; } else if (i == 1) { numberInput.IntValue = value.G; } else if (i == 2) { numberInput.IntValue = value.B; } else { numberInput.IntValue = value.A; } numberInput.Font = GUI.SmallFont; int comp = i; numberInput.OnValueChanged += (numInput) => { Color newVal = (Color)property.GetValue(); if (comp == 0) { newVal.R = (byte)(numInput.IntValue); } else if (comp == 1) { newVal.G = (byte)(numInput.IntValue); } else if (comp == 2) { newVal.B = (byte)(numInput.IntValue); } else { newVal.A = (byte)(numInput.IntValue); } if (property.TrySetValue(newVal)) { TrySendNetworkUpdate(entity, property); colorBox.Color = newVal; } }; } return(label); }