public void Scale(double scale, MemberUpdater source) { if (_field == null && _property == null) { return; } object newValue = source.Value; if (MemberType == typeof(float)) { RescaleFloatRange((float)scale); Set((float)newValue * (float)scale); } else if (MemberType == typeof(double)) { RescaleFloatRange((float)scale); Set((double)newValue * scale); } else if (MemberType == typeof(int)) { Set((int)Math.Round((int)(newValue) * scale)); } else if (MemberType == typeof(Vector3)) { Set((Vector3)newValue * (float)scale); } else if (MemberType == typeof(FloatCurve)) { AnimationCurve curve = (newValue as FloatCurve).Curve; AnimationCurve tmp = new AnimationCurve(); for (int i = 0; i < curve.length; i++) { Keyframe k = curve.keys[i]; k.value *= (float)scale; k.inTangent *= (float)scale; k.outTangent *= (float)scale; tmp.AddKey(k); } (Value as FloatCurve).Curve = tmp; } else if (MemberType == typeof(List <ResourceRatio>)) { List <ResourceRatio> l = (newValue as List <ResourceRatio>); //List<ResourceRatio> l2 = new List<ResourceRatio>(); for (int i = 0; i < l.Count; i++) { ResourceRatio tmp = l[i]; tmp.Ratio *= scale; (Value as List <ResourceRatio>)[i] = tmp; } } else if (MemberType == typeof(ConversionRecipe)) { ConversionRecipe l = (newValue as ConversionRecipe); ScaleResourceList(l.Inputs, scale); ScaleResourceList(l.Outputs, scale); ScaleResourceList(l.Requirements, scale); } }
/// <summary> /// Rescale the field of <paramref name="obj"/> according to the exponents of the ScaleExponents and <paramref name="factor"/>. /// </summary> /// <param name="obj">The object to rescale.</param> /// <param name="baseObj">The corresponding object in the prefab.</param> /// <param name="factor">The new scale.</param> /// <param name="part">The part the object is on.</param> private void UpdateFields(object obj, object baseObj, ScalingFactor factor, Part part) { if ((object)obj == null) { return; } /*if (obj is PartModule && obj.GetType().Name != _id) * { * Tools.LogWf("This ScaleExponent is intended for {0}, not {1}", _id, obj.GetType().Name); * return; * }*/ if (ShouldIgnore(part)) { return; } IEnumerable enumerable = obj as IEnumerable; if (enumerable != null) { UpdateEnumerable(enumerable, (IEnumerable)baseObj, factor, part); return; } foreach (KeyValuePair <string, ScalingMode> nameExponentKV in _exponents) { MemberUpdater value = MemberUpdater.Create(obj, nameExponentKV.Key); if (value == null) { continue; } MemberUpdater baseValue = nameExponentKV.Value.UseRelativeScaling ? null : MemberUpdater.Create(baseObj, nameExponentKV.Key); Rescale(value, baseValue ?? value, nameExponentKV.Key, nameExponentKV.Value, factor); } foreach (KeyValuePair <string, ScaleExponents> child in _children) { string childName = child.Key; MemberUpdater childObjField = MemberUpdater.Create(obj, childName); if (childObjField == null || child.Value == null) { continue; } MemberUpdater baseChildObjField = MemberUpdater.Create(baseObj, childName); child.Value.UpdateFields(childObjField.Value, (baseChildObjField ?? childObjField).Value, factor, part); } }
public void Scale(double scale, MemberUpdater source) { if (_field == null && _property == null) { return; } var newValue = source.Value; if (MemberType == typeof(float)) { RescaleFloatRange((float)scale); Set((float)newValue * (float)scale); } else if (MemberType == typeof(double)) { RescaleFloatRange((float)scale); Set((double)newValue * scale); } else if (MemberType == typeof(int)) { Set((int)Math.Round((int)(newValue) * scale)); } else if (MemberType == typeof(Vector3)) { Set((Vector3)newValue * (float)scale); } else if (MemberType == typeof(FloatCurve)) { var curve = (newValue as FloatCurve).Curve; var tmp = new AnimationCurve(); for (int i = 0; i < curve.length; i++) { var k = curve.keys[i]; k.value *= (float)scale; k.inTangent *= (float)scale; k.outTangent *= (float)scale; tmp.AddKey(k); } (Value as FloatCurve).Curve = tmp; } }
public void Scale(double scale, MemberUpdater source) { if (_field == null && _property == null) { return; } var newValue = source.Value; if (MemberType == typeof(float)) { RescaleFloatRange((float)scale); Set((float)newValue * (float)scale); } else if (MemberType == typeof(double)) { RescaleFloatRange((float)scale); Set((double)newValue * scale); } else if (MemberType == typeof(Vector3)) { Set((Vector3)newValue * (float)scale); } }
/// <summary> /// Rescales destination exponentValue according to its associated exponent. /// </summary> /// <param name="current">The current exponentValue.</param> /// <param name="baseValue">The unscaled exponentValue, gotten from the prefab.</param> /// <param name="name">The name of the field.</param> /// <param name="scalingMode">Information on exactly how to scale this.</param> /// <param name="factor">The rescaling factor.</param> /// <returns>The rescaled exponentValue.</returns> private static void Rescale(MemberUpdater current, MemberUpdater baseValue, string name, ScalingMode scalingMode, ScalingFactor factor) { var exponentValue = scalingMode.Exponent; var exponent = double.NaN; double[] values = null; if (exponentValue.Contains(',')) { if (factor.index == -1) { Tools.LogWf("Value list used for freescale part exponent field {0}: {1}", name, exponentValue); return; } values = Tools.ConvertString(exponentValue, new double[] { }); if (values.Length <= factor.index) { Tools.LogWf("Too few values given for {0}. Expected at least {1}, got {2}: {3}", name, factor.index + 1, values.Length, exponentValue); return; } } else if (!double.TryParse(exponentValue, out exponent)) { Tools.LogWf("Invalid exponent {0} for field {1}", exponentValue, name); } double multiplyBy = 1; if (!double.IsNaN(exponent)) multiplyBy = Math.Pow(scalingMode.UseRelativeScaling ? factor.relative.linear : factor.absolute.linear, exponent); if (current.MemberType.GetInterface("IList") != null) { var v = (IList)current.Value; var v2 = (IList)baseValue.Value; if(v == null) { Tools.LogWf("current.Value == null!"); return; } for (var i = 0; i < v.Count && i < v2.Count; ++i) { if (values != null) { v[i] = values[factor.index]; } else if (!double.IsNaN(exponent)) { if (v[i] is float) { v[i] = (float)v2[i] * multiplyBy; } else if (v[i] is double) { v[i] = (double)v2[i] * multiplyBy; } else if (v[i] is Vector3) { v[i] = (Vector3)v2[i] * (float)multiplyBy; } } } } if (values != null) { if (current.MemberType == typeof (float)) { current.Set((float)values[factor.index]); } else if (current.MemberType == typeof(float)) { current.Set(values[factor.index]); } } else if (!double.IsNaN(exponent)) { current.Scale(multiplyBy, baseValue); } }
/// <summary> /// Rescales destination exponentValue according to its associated exponent. /// </summary> /// <param name="current">The current exponentValue.</param> /// <param name="baseValue">The unscaled exponentValue, gotten from the prefab.</param> /// <param name="name">The name of the field.</param> /// <param name="scalingMode">Information on exactly how to scale this.</param> /// <param name="factor">The rescaling factor.</param> /// <returns>The rescaled exponentValue.</returns> static private void Rescale(MemberUpdater current, MemberUpdater baseValue, string name, ScalingMode scalingMode, ScalingFactor factor) { string exponentValue = scalingMode.Exponent; double exponent = double.NaN; double[] values = null; if (exponentValue.Contains(',')) { if (factor.index == -1) { Tools.LogWf("Value list used for freescale part exponent field {0}: {1}", name, exponentValue); return; } values = Tools.ConvertString(exponentValue, new double[] { }); if (values.Length <= factor.index) { Tools.LogWf("Too few values given for {0}. Expected at least {1}, got {2}: {3}", name, factor.index + 1, values.Length, exponentValue); return; } } else if (!double.TryParse(exponentValue, out exponent)) { Tools.LogWf("Invalid exponent {0} for field {1}", exponentValue, name); } double multiplyBy = 1; if (!double.IsNaN(exponent)) { multiplyBy = Math.Pow(scalingMode.UseRelativeScaling ? factor.relative.linear : factor.absolute.linear, exponent); } if (current.MemberType.GetInterface("IList") != null) { IList v = (IList)current.Value; IList v2 = (IList)baseValue.Value; if (v == null) { Tools.LogWf("current.Value == null!"); return; } for (int i = 0; i < v.Count && i < v2.Count; ++i) { if (values != null) { v[i] = values[factor.index]; } else if (!double.IsNaN(exponent) && (exponent != 0)) { if (v[i] is float) { v[i] = (float)v2[i] * multiplyBy; } else if (v[i] is double) { v[i] = (double)v2[i] * multiplyBy; } else if (v[i] is Vector3) { v[i] = (Vector3)v2[i] * (float)multiplyBy; } } } } if (values != null) { if (current.MemberType == typeof(float)) { current.Set((float)values[factor.index]); } else if (current.MemberType == typeof(float)) { current.Set(values[factor.index]); } } else if (!double.IsNaN(exponent) && (exponent != 0)) { current.Scale(multiplyBy, baseValue); } }