From 30cf50b55e666a84663c63981d814a78db540e82 Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Wed, 24 May 2023 15:32:09 -0500 Subject: [PATCH] Make Color and TextStyle serializable --- .../Iris/Debug/Data/InterpreterEntry.cs | 2 +- UIX/Microsoft/Iris/Drawing/Color.cs | 22 ++++++++++---- UIX/Microsoft/Iris/Drawing/TextStyle.cs | 30 ++++++++++++++++++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/UIX/Microsoft/Iris/Debug/Data/InterpreterEntry.cs b/UIX/Microsoft/Iris/Debug/Data/InterpreterEntry.cs index ac6d4f9..7456ecc 100644 --- a/UIX/Microsoft/Iris/Debug/Data/InterpreterEntry.cs +++ b/UIX/Microsoft/Iris/Debug/Data/InterpreterEntry.cs @@ -62,6 +62,6 @@ public class OpCodeArgument : ISerializable info.AddValue(nameof(Name), Name); info.AddValue(nameof(Type), Type.FullName); info.AddValue(nameof(CanSerializeValue), CanSerializeValue); - info.AddValue(nameof(Value), CanSerializeValue ? Value : Value.ToString()); + info.AddValue(nameof(Value), CanSerializeValue ? Value : Value?.ToString()); } } diff --git a/UIX/Microsoft/Iris/Drawing/Color.cs b/UIX/Microsoft/Iris/Drawing/Color.cs index 22e30af..9a11b65 100644 --- a/UIX/Microsoft/Iris/Drawing/Color.cs +++ b/UIX/Microsoft/Iris/Drawing/Color.cs @@ -6,11 +6,13 @@ using Microsoft.Iris.Render; using System; +using System.Runtime.Serialization; using System.Text; namespace Microsoft.Iris.Drawing { - public struct Color + [Serializable] + public struct Color : ISerializable { private const int ARGBAlphaShift = 24; private const int ARGBRedShift = 16; @@ -28,6 +30,11 @@ namespace Microsoft.Iris.Drawing internal Color(uint value) => this.value = value; + internal Color(SerializationInfo info, StreamingContext context) + { + value = info.GetUInt32(nameof(Value)); + } + public byte R { get => (byte)(Value >> 16 & byte.MaxValue); @@ -192,7 +199,7 @@ namespace Microsoft.Iris.Drawing internal int ToArgb() => (int)Value; - internal ColorF RenderConvert() => new ColorF(A, R, G, B); + internal ColorF RenderConvert() => new(A, R, G, B); public override string ToString() { @@ -218,10 +225,15 @@ namespace Microsoft.Iris.Drawing public override int GetHashCode() => value.GetHashCode(); - internal static Color Transparent => new Color(0U); + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue(nameof(Value), Value); + } - internal static Color Black => new Color(4278190080U); + internal static Color Transparent => new(0U); - internal static Color White => new Color(uint.MaxValue); + internal static Color Black => new(0xFF00_0000U); + + internal static Color White => new(uint.MaxValue); } } diff --git a/UIX/Microsoft/Iris/Drawing/TextStyle.cs b/UIX/Microsoft/Iris/Drawing/TextStyle.cs index f24100b..81e53bd 100644 --- a/UIX/Microsoft/Iris/Drawing/TextStyle.cs +++ b/UIX/Microsoft/Iris/Drawing/TextStyle.cs @@ -6,11 +6,13 @@ using System; using System.Collections.Specialized; +using System.Runtime.Serialization; using System.Text; namespace Microsoft.Iris.Drawing { - internal class TextStyle + [Serializable] + internal class TextStyle : ISerializable { private BitVector32 _flags; private string _fontFace; @@ -21,6 +23,18 @@ namespace Microsoft.Iris.Drawing private Color _textColor; private bool _fragment; + public TextStyle() { } + + protected TextStyle(SerializationInfo info, StreamingContext context) + { + FontFace = info.GetString(nameof(FontFace)); + FontSize = info.GetSingle(nameof(FontSize)); + LineSpacing = info.GetSingle(nameof(LineSpacing)); + Color = (Color)info.GetValue(nameof(Color), typeof(Color)); + + _flags = new(info.GetInt32("flags")); + } + public bool IsInitialized() => _flags.Data != 0; public string FontFace @@ -201,6 +215,20 @@ namespace Microsoft.Iris.Drawing return stringBuilder.ToString(); } + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + if (_flags[1]) + info.AddValue(nameof(FontFace), FontFace); + if (_flags[2]) + info.AddValue(nameof(FontSize), FontSize); + if (_flags[32]) + info.AddValue(nameof(LineSpacing), LineSpacing); + if (_flags[64]) + info.AddValue(nameof(Color), Color); + + info.AddValue("flags", _flags.Data); + } + [Flags] internal enum SetFlags {