예제 #1
0
        /// <summary>
        /// Convert json object to PaintShape
        /// </summary>
        /// <param name="jObject">json object</param>
        /// <param name="baseProps">base properties</param>
        /// <returns>initialized paintshape</returns>
        private PaintShape GetPaintShape(JObject jObject, PaintBaseProperties baseProps)
        {
            if (jObject.GetValue("shapeType").ToString() == CircleShape.Instance.ToString() || jObject.GetValue("shapeType").ToString() == RectangleShape.Instance.ToString())
            {
                // Determine shape
                IShapeBase shape = null;
                if (jObject.GetValue("shapeType").ToString() == CircleShape.Instance.ToString())
                {
                    shape = CircleShape.Instance;
                }
                if (jObject.GetValue("shapeType").ToString() == RectangleShape.Instance.ToString())
                {
                    shape = RectangleShape.Instance;
                }

                // Create and return paintshape
                return(new PaintShape(shape)
                {
                    Height = baseProps.Height,
                    Width = baseProps.Width,
                    X = baseProps.X,
                    Y = baseProps.Y
                });
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// Deserialize paint group
        /// </summary>
        /// <param name="jObject">paintgroup JObject</param>
        /// <param name="baseProps">basic shape properties</param>
        /// <returns>Paintgroup</returns>
        private PaintGroup GetPaintGroup(JObject jObject, PaintBaseProperties baseProps)
        {
            PaintGroup group = new PaintGroup
            {
                Height = baseProps.Height,
                Width  = baseProps.Width,
                X      = baseProps.X,
                Y      = baseProps.Y
            };

            if (jObject.GetValue("children") is JArray children)
            {
                // Deserialize children
                foreach (JToken child in children)
                {
                    if (child is JObject jChild &&
                        Enum.TryParse(jChild.GetValue("type").ToString(), out PaintType type))
                    {
                        PaintBaseProperties deserBase = GetBaseProperties(jChild);

                        if (deserBase != null)
                        {
                            if (type == PaintType.Shape)
                            {
                                PaintShape shape = GetPaintShape(jChild, deserBase);
                                if (shape != null)
                                {
                                    group.Add(AddDecorators(jChild, shape));
                                }
                            }
                            else if (type == PaintType.Group)
                            {
                                // If child is a group, recursively deserialize to object
                                PaintGroup innerGroup = GetPaintGroup(jChild, deserBase);
                                if (innerGroup != null)
                                {
                                    group.Add(AddDecorators(jChild, innerGroup));
                                }
                            }
                        }
                    }
                }
            }

            return(group);
        }
예제 #3
0
        /// <summary>
        /// Convert json string into a state with objects
        /// </summary>
        /// <param name="jsonSaveString">savefile as json string</param>
        /// <returns>Canvas state</returns>
        private List <PaintBase> DeserializeJsonSave(string jsonSaveString)
        {
            List <PaintBase> newShapeList = new List <PaintBase>();

            // Savefile should contain json array at root
            JArray jArray = JArray.Parse(jsonSaveString);

            if (jArray != null)
            {
                // Iterate through items in array
                foreach (JToken jToken in jArray)
                {
                    if (jToken is JObject jObject)
                    {
                        if (Enum.TryParse(jObject.GetValue("type").ToString(), out PaintType type))
                        {
                            // Get base properties
                            PaintBaseProperties deserializedProperties = GetBaseProperties(jObject);

                            if (deserializedProperties != null)
                            {
                                if (type == PaintType.Shape)
                                {
                                    PaintShape shape = GetPaintShape(jObject, deserializedProperties);
                                    if (shape != null)
                                    {
                                        newShapeList.Add(AddDecorators(jObject, shape));
                                    }
                                }
                                else if (type == PaintType.Group)
                                {
                                    PaintGroup group = GetPaintGroup(jObject, deserializedProperties);
                                    if (group != null)
                                    {
                                        newShapeList.Add(AddDecorators(jObject, group));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(newShapeList);
        }