/// <summary>Sets the value which is of a known given type.</summary> /// <param name="text">The value to set, e.g. "auto" or "14.9".</param> /// <param name="type">The type of the value, e.g. percentage.</param> public void Set(string valueText, ValueType type) { Type = type; switch (Type) { case ValueType.Null: Text = ""; break; case ValueType.Color: valueText = valueText.Replace("#", ""); float r = 0f; float g = 0f; float b = 0f; float a = 1f; if (valueText.Contains("rgba") || valueText.Contains("rgb")) { valueText = valueText.Replace(")", "").Replace("rgba(", "").Replace("rgb(", ""); string[] pieces = valueText.Split(','); // How many pieces? int count = pieces.Length; float.TryParse(pieces[0], out r); if (count > 1) { float.TryParse(pieces[1], out g); } if (count > 2) { float.TryParse(pieces[2], out b); } if (count > 3) { float.TryParse(pieces[3], out a); } } else { if (valueText.Contains(" ")) { string[] pieces = valueText.Split(' '); valueText = pieces[0]; } GetColour(valueText, out r, out g, out b, out a); } SetInnerValues(4); Value value = InnerValues[0]; value.Type = ValueType.Single; value.Single = r; value = InnerValues[1]; value.Type = ValueType.Single; value.Single = g; value = InnerValues[2]; value.Type = ValueType.Single; value.Single = b; value = InnerValues[3]; value.Type = ValueType.Single; value.Single = a; break; case ValueType.Pixels: if (string.IsNullOrEmpty(valueText)) { PX = 0; } else { if (valueText.EndsWith("fpx")) { // Fixed pixel value. Resolution independent value. // Attempt to parse it out: int.TryParse(valueText.Replace("fpx", ""), out PX); } else { string trimmed = valueText.Replace("px", ""); if (int.TryParse(trimmed, out PX)) { // Apply resolution: PX = (int)(PX * ScreenInfo.ResolutionScale); } else { // It's possibly a decimal. float decimalValue; if (float.TryParse(trimmed, out decimalValue)) { // It was a decimal. // Apply resolution: PX = (int)(decimalValue * ScreenInfo.ResolutionScale); } } } } break; case ValueType.Point: // 2D point, e.g. overflow. SetInnerValues(valueText, 2); break; case ValueType.Rectangle: // Rectangle set of 4 values. SetInnerValues(valueText, 4); break; case ValueType.Em: if (string.IsNullOrEmpty(valueText)) { Single = 1f; } else { float.TryParse(valueText.Replace("em", ""), out Single); } break; case ValueType.Radians: if (string.IsNullOrEmpty(valueText)) { Single = 0f; } else { float.TryParse(valueText.Replace("rad", ""), out Single); } break; case ValueType.Percentage: if (string.IsNullOrEmpty(valueText)) { Single = 0f; } else { float.TryParse(valueText.Replace("%", ""), out Single); Single /= 100f; } break; case ValueType.Single: if (string.IsNullOrEmpty(valueText)) { Single = 0f; } else { float.TryParse(valueText, out Single); } break; case ValueType.Degrees: if (string.IsNullOrEmpty(valueText)) { Single = 0f; } else { float.TryParse(valueText.Replace("deg", ""), out Single); } break; case ValueType.Boolean: Boolean = (valueText == "1" || valueText == "true" || valueText == "on"); break; case ValueType.Calc: Calculation = new Calculation(valueText.Substring(5, valueText.Length - 5 - 1)); break; default: Text = valueText; break; } }
/// <summary>Sets the value which is of a known given type.</summary> /// <param name="text">The value to set, e.g. "auto" or "14.9".</param> /// <param name="type">The type of the value, e.g. percentage.</param> public void Set(string valueText,ValueType type){ Type=type; switch(Type){ case ValueType.Null: Text=""; break; case ValueType.Color: valueText=valueText.Replace("#",""); float r=0f; float g=0f; float b=0f; float a=1f; if(valueText.Contains("rgba") || valueText.Contains("rgb")){ valueText=valueText.Replace(")","").Replace("rgba(","").Replace("rgb(",""); string[] pieces=valueText.Split(','); // How many pieces? int count=pieces.Length; float.TryParse(pieces[0],out r); if(count>1){ float.TryParse(pieces[1],out g); } if(count>2){ float.TryParse(pieces[2],out b); } if(count>3){ float.TryParse(pieces[3],out a); } }else{ if(valueText.Contains(" ")){ string[] pieces=valueText.Split(' '); valueText=pieces[0]; } GetColour(valueText,out r,out g,out b,out a); } SetInnerValues(4); Value value=InnerValues[0]; value.Type=ValueType.Single; value.Single=r; value=InnerValues[1]; value.Type=ValueType.Single; value.Single=g; value=InnerValues[2]; value.Type=ValueType.Single; value.Single=b; value=InnerValues[3]; value.Type=ValueType.Single; value.Single=a; break; case ValueType.Pixels: if(string.IsNullOrEmpty(valueText)){ PX=0; }else{ if(valueText.EndsWith("fpx")){ // Fixed pixel value. Resolution independent value. // Attempt to parse it out: int.TryParse(valueText.Replace("fpx",""),out PX); }else{ string trimmed=valueText.Replace("px",""); if(int.TryParse(trimmed,out PX)){ // Apply resolution: PX=(int)(PX*ScreenInfo.ResolutionScale); }else{ // It's possibly a decimal. float decimalValue; if(float.TryParse(trimmed,out decimalValue)){ // It was a decimal. // Apply resolution: PX=(int)(decimalValue * ScreenInfo.ResolutionScale); } } } } break; case ValueType.Point: // 2D point, e.g. overflow. SetInnerValues(valueText,2); break; case ValueType.Rectangle: // Rectangle set of 4 values. SetInnerValues(valueText,4); break; case ValueType.Em: if(string.IsNullOrEmpty(valueText)){ Single=1f; }else{ float.TryParse(valueText.Replace("em",""),out Single); } break; case ValueType.Radians: if(string.IsNullOrEmpty(valueText)){ Single=0f; }else{ float.TryParse(valueText.Replace("rad",""),out Single); } break; case ValueType.Percentage: if(string.IsNullOrEmpty(valueText)){ Single=0f; }else{ float.TryParse(valueText.Replace("%",""),out Single); Single/=100f; } break; case ValueType.Single: if(string.IsNullOrEmpty(valueText)){ Single=0f; }else{ float.TryParse(valueText,out Single); } break; case ValueType.Degrees: if(string.IsNullOrEmpty(valueText)){ Single=0f; }else{ float.TryParse(valueText.Replace("deg",""),out Single); } break; case ValueType.Boolean: Boolean=(valueText=="1" || valueText=="true" || valueText=="on"); break; case ValueType.Calc: Calculation=new Calculation(valueText.Substring(5,valueText.Length-5-1)); break; default: Text=valueText; break; } }