public void Update()
        {
            int mX = InputManager.CurrentMouseState.X;
            int mY = InputManager.CurrentMouseState.Y;

            //Toggles there Hover var when mouse if over
            SelectShapes();

            if (leftClicked && !moving)
            {
                movingShape = getHovered();
                if (movingShape != null)
                {
                    moving = true;
                    //Temp shape for quick acces
                    aShape s = movingShape;


                    if (s.GetType() == typeof(ShapeComposite))
                    {
                        group       = (ShapeComposite)s;
                        movingShape = group;
                    }


                    //Setting up a temp shape to draw while moving
                    if (s.ShapeName == "rectangle")
                    {
                        drawingShape = new mRectangle(s.Width, s.Height, s.Color);
                    }
                    else if (s.ShapeName == "ellipse")
                    {
                        drawingShape = new mEllipse(s.Width, s.Height, s.Color);
                    }
                    else if (s.ShapeName == "group")
                    {
                        ShapeComposite drawingGroup = new ShapeComposite();
                        drawingGroup.Add(group.GetChildren());
                        drawingGroup.Visible = true;
                        drawingShape         = drawingGroup;
                        drawingShape.Visible = true;
                    }

                    drawingShape.X = s.X;
                    drawingShape.Y = s.Y;
                    drawingShape.Load();

                    deltaX = mX - s.X;
                    deltaY = mY - s.Y;

                    movingShape.Visible = false;
                }
            }

            if (leftClicked && moving)
            {
                drawingShape.X = mX - deltaX;
                drawingShape.Y = mY - deltaY;
            }
            else if (!leftClicked && moving)
            {
                moving = false;

                playground.ExecuteCommand(new MoveCommand(group != null ? group : movingShape, drawingShape.X, drawingShape.Y));
                if (group != null)
                {
                    group = null;
                }

                movingShape.Visible = true;
            }

            if (InputManager.IsPressed(MouseInput.LeftButton))
            {
                leftClicked = true;
            }

            if (InputManager.IsReleased(Input.MouseInput.LeftButton))
            {
                leftClicked = false;
            }
        }
Exemple #2
0
        public void Update()
        {
            if (textBox != null)
            {
                textBox.Update();
            }

            /*TODO: Move to 'UI' or/and keyboard shortcut*/
            if (InputManager.IsKeyPressed(Keys.B))
            {
                foreach (mCanvas c in playground.Canvases)
                {
                    c.ForAllShapes((aShape shape) => {
                        if (shape.Selected)
                        {
                            shape.DrawBorder = !shape.DrawBorder;
                        }
                    });
                }
            }

            if (InputManager.IsKeyDown(Keys.LeftControl) && InputManager.IsKeyPressed(Keys.G))
            {
                List <aShape>  selectedShapes = getSelectedShapes();
                ShapeComposite firstGroup     = isGroupInSelection(selectedShapes);
                if (firstGroup != null)
                {
                    selectedShapes.Remove(firstGroup);
                }
                ShapeComposite group = null;
                if (selectedShapes.Count > 1)
                {
                    group = new ShapeComposite();
                    foreach (aShape s in selectedShapes)
                    {
                        group.Add(s);
                    }
                }
                if (group != null)
                {
                    deleteShapes(selectedShapes);
                    if (firstGroup != null)
                    {
                        firstGroup.Add(group);
                    }
                    else
                    {
                        playground.AddShape(group);
                    }
                }

                Reset();
            }

            if (leftClicked && !selecting && InputManager.IsKeyDown(Keys.LeftControl))
            {
                selecting = true;

                int mX = InputManager.CurrentMouseState.X;
                int mY = InputManager.CurrentMouseState.Y;

                selectionRect.X = mX;
                selectionRect.Y = mY;
            }
            else if (leftClicked && selecting && InputManager.IsKeyDown(Keys.LeftControl))
            {
                int newWidth  = Util.Clamp(InputManager.CurrentMouseState.X - selectionRect.X, 1, playground.Width);
                int newHeight = Util.Clamp(InputManager.CurrentMouseState.Y - selectionRect.Y, 1, playground.Height);

                Console.WriteLine("Width: " + newWidth);

                selectionRect.Width  = newWidth;
                selectionRect.Height = newHeight;
                selectionRect.Load();
            }
            else if (!leftClicked && selecting && InputManager.IsKeyDown(Keys.LeftControl))
            {
                selecting = false;

                foreach (mCanvas c in playground.Canvases)
                {
                    c.ForAllShapes((aShape iShape) => {
                        if (selectionRect.Intersects(iShape))
                        {
                            iShape.Selected = true;
                        }
                    });
                }

                if (selectionRect.Width > 0)
                {
                    selectionRect.Width  = 1;
                    selectionRect.Height = 1;
                    selectionRect.Load();
                }
            }
            else
            {
                foreach (mCanvas c in playground.Canvases)
                {
                    c.ForAllShapes(SelectShape);
                }
            }


            if (!typing)
            {
                //For adding text
                if (InputManager.IsKeyPressed(Keys.Up))
                {
                    typing = true;
                    AddText(TextPos.Top);
                }
                else if (InputManager.IsKeyPressed(Keys.Left))
                {
                    typing = true;
                    AddText(TextPos.Left);
                }
                else if (InputManager.IsKeyPressed(Keys.Down))
                {
                    typing = true;
                    AddText(TextPos.Bottom);
                }
                else if (InputManager.IsKeyPressed(Keys.Right))
                {
                    typing = true;
                    AddText(TextPos.Right);
                }
            }

            if (!typing && (InputManager.IsKeyPressed(Keys.Delete) || InputManager.IsKeyPressed(Keys.Back)))
            {
                deleteShapes();
            }

            if (InputManager.IsPressed(MouseInput.LeftButton))
            {
                leftClicked = true;
            }

            if (InputManager.IsReleased(Input.MouseInput.LeftButton))
            {
                leftClicked = false;
            }
        }
        public static List <aShape> Deserialize(string path)
        {
            List <aShape> shapes = new List <aShape>();

            Regex rxGroup = new Regex(@"(\t*)(group)\s(?<groupNumber>\d+)(\n\r|\n|\r)(?<shapes>(((\t+)(?<type>ornament)\s(Top|Botton|Left|Right)\s(\')(?<text>.*)(\')|(\t+)(?<isTextShape>t\s)*(?<type>rectangle|ellipse)\s(?<X>\d+)\s(?<Y>\d+)\s(?<width>\d+)\s(?<height>\d+))(\n\r|\n|\r))*)",
                                      RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex rxTextShape = new Regex(@"(ornament)\s(?<textpos>Top|Botton|Left|Right)\s(\')(?<text>.*)(\')(\n\r|\n|\r)(t\s)(?<shape>rectangle|ellipse)\s(?<X>\d+)\s(?<Y>\d+)\s(?<width>\d+)\s(?<height>\d+)",
                                          RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Regex rxShape = new Regex(@"(?<isGrouped>(\t*))(?<isTextShape>t\s)*(?<shape>rectangle|ellipse)\s(?<X>\d+)\s(?<Y>\d+)\s(?<width>\d+)\s(?<height>\d+)",
                                      RegexOptions.Compiled | RegexOptions.IgnoreCase);

            if (!File.Exists(path))
            {
                throw new System.ArgumentException("Filepath does not exist!");
            }

            string shapeStrings = File.ReadAllText(path);

            MatchCollection matchesGroups = rxGroup.Matches(shapeStrings);

            ShapeComposite group      = new ShapeComposite();
            ShapeComposite prevGroup  = null;
            ShapeComposite underGroup = null;

            foreach (Match m in matchesGroups)
            {
                GroupCollection groups = m.Groups;

                string gr = groups["shapes"].Value;

                int.TryParse(groups["groupNumber"].Value, out int groupNumber);
                if (groupNumber > 0)
                {
                    ShapeComposite newGroup = new ShapeComposite();
                    List <aShape>  temp     = deserializeGroupShapes(gr);
                    foreach (aShape s in temp)
                    {
                        newGroup.Add(s);
                    }
                    if (underGroup != null)
                    {
                        underGroup.Add(newGroup);
                        underGroup = newGroup;
                    }
                    else
                    {
                        prevGroup.Add(newGroup);
                        underGroup = newGroup;
                    }
                }
                else
                {
                    if (prevGroup != null)
                    {
                        shapes.Add(prevGroup);
                        prevGroup = null;
                    }
                    if (group == null)
                    {
                        group = new ShapeComposite();
                    }
                    List <aShape> temp = deserializeGroupShapes(gr);
                    foreach (aShape s in temp)
                    {
                        group.Add(s);
                    }
                    prevGroup = group;
                    group     = null;
                }
            }
            if (prevGroup != null)
            {
                shapes.Add(prevGroup);
                prevGroup = null;
            }
            if (group != null)
            {
                shapes.Add(group);
            }

            MatchCollection matchesTextShapes = rxTextShape.Matches(shapeStrings);

            foreach (Match m in matchesTextShapes)
            {
                GroupCollection groups = m.Groups;

                aShape temp = null;

                TextPos textPos = StringToTextpos(groups["textpos"].Value);
                string  text    = groups["text"].Value;

                //Parse strings to int
                int.TryParse(groups["X"].Value, out int x);
                int.TryParse(groups["Y"].Value, out int y);
                int.TryParse(groups["width"].Value, out int width);
                int.TryParse(groups["height"].Value, out int height);

                if (groups["shape"].Value == "rectangle")
                {
                    temp = new mRectangle(width, height, Color.Red);
                }
                else if (groups["shape"].Value == "ellipse")
                {
                    temp = new mEllipse(width, height, Color.Red);
                }

                temp.X = x;
                temp.Y = y;

                ShapeTextDecorator decTemp = new ShapeTextDecorator(temp);
                decTemp.AddText(text, textPos);

                if (temp != null)
                {
                    temp.Load();
                    shapes.Add(decTemp);
                }
                else
                {
                    return(shapes);
                }
            }

            MatchCollection matchesShapes = rxShape.Matches(shapeStrings);

            foreach (Match m in matchesShapes)
            {
                GroupCollection groups = m.Groups;

                //Check on group and text
                if (groups["isTextShape"].Value != string.Empty || groups["isGrouped"].Value != string.Empty)
                {
                    continue;
                }

                aShape temp = null;

                //Parse strings to int
                int.TryParse(groups["X"].Value, out int x);
                int.TryParse(groups["Y"].Value, out int y);
                int.TryParse(groups["width"].Value, out int width);
                int.TryParse(groups["height"].Value, out int height);

                if (groups["shape"].Value == "rectangle")
                {
                    temp = new mRectangle(width, height, Color.Red);
                }
                else if (groups["shape"].Value == "ellipse")
                {
                    temp = new mEllipse(width, height, Color.Red);
                }

                temp.X = x;
                temp.Y = y;

                if (temp != null)
                {
                    temp.Load();
                    shapes.Add(temp);
                }
                else
                {
                    return(shapes);
                }
            }

            return(shapes);
        }