コード例 #1
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);
        }
コード例 #2
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);
        }
コード例 #3
0
        public Task ExecuteUserActionAsync()
        {
            // Get selected items from ShapeList
            List <PaintBase> selected = ShapeList.Where(pb => pb.Selected).ToList();

            // Only group if 2 or more items are selected
            if (selected.Count > 1)
            {
                // Add undo entry
                UndoStack.Push(ShapeList.DeepCopy());
                RedoStack.Clear();

                // Create new group
                PaintGroup newGroup = new PaintGroup()
                {
                    Selected = true
                };

                // Add selected items to the group
                // Remove selected items from the canvas itself
                foreach (PaintBase paintBase in selected)
                {
                    ShapeList.Remove(paintBase);
                    paintBase.Selected = false;

                    newGroup.Add(paintBase);
                }

                ShapeList.Add(newGroup);

                _page.Draw();
                _page.UpdateList();
            }

            return(Task.CompletedTask);
        }