public GGVRDataType(BinaryReader br, long?offset = null) { if (offset != null) { br.BaseStream.Seek(offset.GetValueOrDefault(), SeekOrigin.Begin); } this._address = br.BaseStream.Position; this._enabled = true; this.HasValueRange = false; if (typeof(T) == typeof(float)) { _value = (T)(object)(br.ReadSingle()); } if (typeof(T) == typeof(int)) { _value = (T)(object)(br.ReadInt32()); } if (typeof(T) == typeof(byte)) { _value = (T)(object)(br.ReadByte()); } if (typeof(T) == typeof(ColorComparable)) { _value = (T)(object)(ColorComparable.FromStream(br)); } }
public override string ToString() { if (!this.Enabled) { return("-"); } if (typeof(T) == typeof(float)) { float v = (float)(object)this._value; return(String.Format(CultureInfo.InvariantCulture, "{0:###############0.##}", v)); } if (typeof(T) == typeof(int)) { int v = (int)(object)this._value; return(v.ToString()); } if (typeof(T) == typeof(byte)) { byte b = (byte)(object)this._value; return(b.ToString()); } if (typeof(T) == typeof(ColorComparable)) { ColorComparable cc = (ColorComparable)(object)this._value; return(cc.ToString()); } return(_value.ToString()); }
public override void WriteToFile(BinaryWriter bw) { if (!this.Enabled || this.Address < 0) { return; } bw.BaseStream.Seek(this._address, SeekOrigin.Begin); if (typeof(T) == typeof(float)) { float w = (float)(object)this._value; bw.Write(w); } if (typeof(T) == typeof(int)) { int w = (int)(object)this._value; bw.Write(w); } if (typeof(T) == typeof(byte)) { byte w = (byte)(object)this._value; bw.Write(w); } if (typeof(T) == typeof(ColorComparable)) { ColorComparable cc = (ColorComparable)(object)this._value; cc.ToStream(bw); } }
public int CompareTo(object obj) { if (obj is ColorComparable) { ColorComparable cc = (ColorComparable)obj; if (this.Color == null && cc.Color == null) { return(0); } if (this.Color == null) { return(-1); } if (cc.Color == null) { return(1); } int ret = this.Color.GetHue().CompareTo(cc.Color.GetHue()); if (ret != 0) { return(ret); } ret = this.Color.GetSaturation().CompareTo(cc.Color.GetSaturation()); if (ret != 0) { return(ret); } return(this.Color.GetBrightness().CompareTo(cc.Color.GetBrightness())); } else { return(0); } }
/// <summary> /// /// </summary> /// <param name="valueId"></param> /// <param name="readValue"></param> /// <returns></returns> public T GetDefaultValue <T>(string valueId, T readValue) where T : IComparable { T res = readValue; string value = this._iniFile.Read(valueId, "Defaults", "p"); bool parsable = false; if (typeof(T) == typeof(float)) { float resFloat = (float)(object)res; if (parsable = float.TryParse(value, NumberStyles.Number, this._invariant, out resFloat)) { res = (T)(object)resFloat; } } if (typeof(T) == typeof(byte)) { byte resByte = (byte)(object)res; if (parsable = byte.TryParse(value, NumberStyles.Integer, this._invariant, out resByte)) { res = (T)(object)resByte; } } if (typeof(T) == typeof(ColorComparable)) { string[] parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (parsable = (parts.Length == 4)) { int r, g, b, a; if (int.TryParse(parts[0], out r) && int.TryParse(parts[1], out g) && int.TryParse(parts[2], out b) && int.TryParse(parts[3], out a) && r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255 && a >= 0 && a <= 255) { parsable = true; ColorComparable cc = new ColorComparable(); cc.Color = Color.FromArgb(a, r, g, b); res = (T)(object)cc; } } } if (!parsable) { this.SetDefaultValue(valueId, readValue); } return(res); }
public static ColorComparable FromStream(BinaryReader br) { ColorComparable cc = new ColorComparable(); int r, g, b, a; r = (int)Math.Round(br.ReadSingle() * 255.0f); b = (int)Math.Round(br.ReadSingle() * 255.0f); g = (int)Math.Round(br.ReadSingle() * 255.0f); a = (int)Math.Round(br.ReadSingle() * 255.0f); cc.Color = Color.FromArgb(a, r, b, g); return(cc); }
private void SetDefaultValue <T>(string valueId, T writeValue) where T : IComparable { if (typeof(T) == typeof(float)) { float wF = (float)(object)writeValue; this._iniFile.Write(valueId, wF.ToString(this._invariant), "Defaults"); } if (typeof(T) == typeof(byte)) { float wB = (byte)(object)writeValue; this._iniFile.Write(valueId, wB.ToString(), "Defaults"); } if (typeof(T) == typeof(ColorComparable) && writeValue != null) { ColorComparable cc = (ColorComparable)(object)writeValue; if (cc.Color == null) { return; } Color c = cc.Color; this._iniFile.Write(valueId, c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + "," + c.A.ToString(), "Defaults"); } }