Beispiel #1
0
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            bool hasMoved = mouseDownLocation.X != e.X || mouseDownLocation.Y != e.Y;

            if (hasSelected)
            {
                if (hasSecondSelected)
                {
                    ShapeComposite newGroup = new ShapeComposite("group");
                    newGroup.AddChild(SelectedShape);
                    newGroup.AddChild(SecondSelectedShape);

                    shapeList.Remove(SelectedShape);
                    shapeList.Remove(SecondSelectedShape);

                    SecondSelectedShape.selected = false;
                    SecondSelectedShape          = null;

                    Command groupShape = new AddShapeCommand(shapeList, newGroup);
                    history.Add(groupShape);

                    Command selectShape = new SelectShapeCommand(newGroup, this);
                    history.Add(selectShape);
                }

                if (hasMoved)
                {
                    Point   velocity  = new Point(e.Location.X - mouseDownLocation.X, e.Location.Y - mouseDownLocation.Y);
                    Command moveShape = new MoveShapeCommand(SelectedShape, velocity, shapeStartPosition);
                    history.Add(moveShape);
                }

                Invalidate();
            }
        }
        private Shape CreateShapeFromLine(string line)
        {
            string[] lineSplit = SplitCurrentLine();
            Shape    shape     = null;

            switch (lineSplit[0])
            {
            case "rectangle":
                shape = new BaseShape(lineSplit[0], RectangleStategy.Instance());
                break;

            case "ellipse":
                shape = new BaseShape(lineSplit[0], EllipseStrategy.Instance());
                break;

            case "group":
                shape = new ShapeComposite();
                break;

            case "ornament":
                shape = new ShapeDecorator();
                break;
            }

            return(shape);
        }
Beispiel #3
0
 public void Visit(ShapeComposite group)
 {
     group.Width  = width;
     group.Height = height;
     group.X      = x;
     group.Y      = y;
     group.Load();
 }
Beispiel #4
0
        public override void Visit(ShapeComposite shapeComposite)
        {
            foreach (Shape shape in shapeComposite)
            {
                shape.Accept(this);
            }

            shapeComposite.RecalculateValues();
        }
        public override void Visit(ShapeComposite shapeComposite)
        {
            WriteToFile(shapeComposite);

            depthCounter++;
            foreach (Shape s in shapeComposite)
            {
                s.Accept(this);
            }
            depthCounter--;
        }
        public override void Visit(ShapeComposite shapeComposite)
        {
            // For now we know groups are always 2 shapes big
            int childCount = 2;

            for (int i = 0; i < childCount; i++)
            {
                currentLine = streamReader.ReadLine();
                Shape shape = CreateShapeFromLine(currentLine);
                shape.Accept(this);
                shapeComposite.AddChild(shape);
            }
        }
Beispiel #7
0
        // Open existing file
        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInputOutput fileInput = new FileInputOutput(openFileDialog.OpenFile());
                fileInput.CreateList();
                List <string[]> tempList = fileInput.streamList;

                Stack <Command> commandStack = new Stack <Command>();

                foreach (string[] shape in fileInput.streamList)
                {
                    if (shape[0] == "rectangle")
                    {
                        Command addShape = new AddShapeCommand(shapeList, new RectangleShape(shape[0], Int32.Parse(shape[1]), Int32.Parse(shape[2]), Int32.Parse(shape[3]), Int32.Parse(shape[4])));
                        commandStack.Push(addShape);
                    }

                    if (shape[0] == "ellipse")
                    {
                        Command addShape = new AddShapeCommand(shapeList, new EllipseShape(shape[0], Int32.Parse(shape[1]), Int32.Parse(shape[2]), Int32.Parse(shape[3]), Int32.Parse(shape[4])));
                        commandStack.Push(addShape);
                    }

                    if (shape[0] == "group")
                    {
                        ShapeComposite newGroup = new ShapeComposite(shape[0]);
                        // TODO: pop groupsize add composite shape
                        //newGroup.AddChild(commandStack.Pop());
                    }
                }

                //foreach (string[] shape in fileInput.streamList)
                //{
                //    if (shape[0] == "rectangle")
                //    {
                //        Command addShape = new AddShapeCommand(shapeList, new RectangleShape(shape[0], Int32.Parse(shape[1]), Int32.Parse(shape[2]), Int32.Parse(shape[3]), Int32.Parse(shape[4])));
                //        history.Add(addShape);
                //    }

                //    if (shape[0] == "ellipse")
                //    {
                //        Command addShape = new AddShapeCommand(shapeList, new EllipseShape(shape[0], Int32.Parse(shape[1]), Int32.Parse(shape[2]), Int32.Parse(shape[3]), Int32.Parse(shape[4])));
                //        history.Add(addShape);
                //    }
                //}
            }
            Invalidate();
        }
Beispiel #8
0
        public void Visit(ShapeComposite shapeComposite)
        {
            Rectangle dimension = shapeComposite.dimension;

            float widthFactor  = width / dimension.Width;
            float heightFactor = height / dimension.Height;

            foreach (Shape shape in shapeComposite)
            {
                width  = Convert.ToInt32(shape.width * widthFactor);
                height = Convert.ToInt32(shape.height * heightFactor);

                shape.Accept(this);
            }
        }
Beispiel #9
0
        ShapeComposite isGroupInSelection(List <aShape> selection)
        {
            List <ShapeComposite> groups = new List <ShapeComposite>();

            foreach (aShape shape in selection)
            {
                if (shape.GetType() == typeof(ShapeComposite))
                {
                    ShapeComposite temp = (ShapeComposite)shape;
                    groups.Add(temp);
                }
            }
            if (groups.Count > 0)
            {
                return(groups[0]);
            }
            return(null);
        }
        public void Visit(ShapeComposite group)
        {
            for (int i = 0; i < groupLevel; i++)
            {
                tabPre += "\t";
            }

            output += tabPre + "group " + groupLevel + "\n";
            foreach (aShape s in group.GetChildren())
            {
                if (s.GetType() == typeof(ShapeComposite))
                {
                    groupLevel++;
                }
                output += tabPre + "\t";
                Visit(s);
            }
            groupLevel = 0;
        }
Beispiel #11
0
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            bool hasMoved = mouseDownLocation.X != e.X || mouseDownLocation.Y != e.Y;

            if (hasSelected)
            {
                if (hasSecondSelected)
                {
                    ShapeComposite newGroup = new ShapeComposite();
                    newGroup.AddChild(SelectedShape);
                    newGroup.AddChild(SecondSelectedShape);

                    MacroCommand macroCommandSelectedShape = new MacroCommand();
                    macroCommandSelectedShape.Add(new CustomCommand(
                                                      () =>
                    {
                        shapeList.Remove(SelectedShape);
                        shapeList.Remove(SecondSelectedShape);
                    },
                                                      () =>
                    {
                        shapeList.Add(newGroup.groupMembers[0]);
                        shapeList.Add(newGroup.groupMembers[1]);
                    }
                                                      ));

                    SecondSelectedShape.selected = false;
                    SecondSelectedShape          = null;

                    macroCommandSelectedShape.Add(new AddShapeCommand(shapeList, newGroup));
                    history.Add(macroCommandSelectedShape);
                }

                if (hasMoved)
                {
                    Point   velocity  = new Point(e.Location.X - mouseDownLocation.X, e.Location.Y - mouseDownLocation.Y);
                    Command moveShape = new MoveShapeCommand(SelectedShape, velocity, shapeStartPosition);
                    history.Add(moveShape);
                }
                Invalidate();
            }
        }
        public void Execute()
        {
            textShape = new ShapeTextDecorator(shape);
            if (shape.GetType() == typeof(ShapeComposite))
            {
                ShapeComposite temp = (ShapeComposite)shape;
                textShape = new ShapeTextDecorator(temp.GetChildren()[0]);
                textShape.AddText(text, textPos);
                temp.GetChildren()[temp.GetChildren().FindIndex(ind => ind.Equals(temp.GetChildren()[0]))] = textShape;

                playground.AddShape(temp);
                playground.RemoveShape(shape);
            }
            else
            {
                textShape.AddText(text, textPos);

                playground.AddShape(textShape);
                playground.RemoveShape(shape);
            }
        }
 public void Visit(aShape shape)
 {
     if (shape.GetType() == typeof(ShapeTextDecorator))
     {
         ShapeTextDecorator s = (ShapeTextDecorator)shape;
         Visit(s);
     }
     else if (shape.GetType() == typeof(ShapeComposite))
     {
         ShapeComposite s = (ShapeComposite)shape;
         Visit(s);
     }
     else if (shape.GetType() == typeof(mRectangle))
     {
         mRectangle s = (mRectangle)shape;
         Visit(s);
     }
     else if (shape.GetType() == typeof(mEllipse))
     {
         mEllipse s = (mEllipse)shape;
         Visit(s);
     }
 }
Beispiel #14
0
 public virtual void Visit(ShapeComposite shapeComposite)
 {
 }
        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;
            }
        }
 public void Visit(ShapeComposite shapeComposite)
 {
     WriteToFile(shapeComposite);
 }
Beispiel #17
0
 public void Visit(ShapeComposite group)
 {
     group.X += deltaX;
     group.Y += deltaY;
 }
Beispiel #18
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);
        }