Exemplo n.º 1
0
        private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (objectManager.SelectedObject != null && e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Point deltaMoved = new Point(e.Location.X - lastMouse.X, e.Location.Y - lastMouse.Y);

                if (objectManager.SelectedObject.SelectedPoint == -1)
                {
                    Point newPos = new Point(
                        objectManager.SelectedObject.Position.X + (int)(deltaMoved.X / zoomLevel),
                        objectManager.SelectedObject.Position.Y + (int)(deltaMoved.Y / zoomLevel));

                    // clip object to screen
                    if (newPos.X < 0)
                        newPos.X = 0;

                    if (newPos.Y < 0)
                        newPos.Y = 0;

                    // move collision points attached to this object
                    Point moved = new Point(newPos.X - objectManager.SelectedObject.Position.X, newPos.Y - objectManager.SelectedObject.Position.Y);
                    for (int i = 0; i < objectManager.SelectedObject.CollisionPoints.Count; ++i)
                    {
                        PointF curr = objectManager.SelectedObject.CollisionPoints[i];
                        objectManager.SelectedObject.CollisionPoints[i] = new PointF(curr.X + moved.X, curr.Y + moved.Y);
                    }

                    objectManager.SelectedObject.Position = newPos;

                    //extend canvas size if needed
                    FindCanvasSize();

                    //move the scrollbar based on if the object is on the edge
                    if ((objectManager.SelectedObject.Position.X + objectManager.SelectedObject.Image.Width) * zoomLevel >= splitContainer1.Panel1.Width * zoomLevel + -splitContainer1.Panel1.AutoScrollPosition.X && deltaMoved.X > 0)
                    {
                        splitContainer1.Panel1.AutoScrollPosition = new Point(-splitContainer1.Panel1.AutoScrollPosition.X + deltaMoved.X,
                                                                              -splitContainer1.Panel1.AutoScrollPosition.Y);
                    }
                    else if (objectManager.SelectedObject.Position.X <= -splitContainer1.Panel1.AutoScrollPosition.X && deltaMoved.X < 0)
                    {
                        splitContainer1.Panel1.AutoScrollPosition = new Point(-splitContainer1.Panel1.AutoScrollPosition.X + deltaMoved.X,
                                                                              -splitContainer1.Panel1.AutoScrollPosition.Y);
                    }

                    if ((objectManager.SelectedObject.Position.Y + objectManager.SelectedObject.Image.Height) * zoomLevel >= splitContainer1.Panel1.Height * zoomLevel + -splitContainer1.Panel1.AutoScrollPosition.Y && deltaMoved.Y > 0)
                    {
                        splitContainer1.Panel1.AutoScrollPosition = new Point(-splitContainer1.Panel1.AutoScrollPosition.X,
                                                                              -splitContainer1.Panel1.AutoScrollPosition.Y + deltaMoved.Y);
                    }
                    else if (objectManager.SelectedObject.Position.Y <= -splitContainer1.Panel1.AutoScrollPosition.Y && deltaMoved.Y < 0)
                    {
                        splitContainer1.Panel1.AutoScrollPosition = new Point(-splitContainer1.Panel1.AutoScrollPosition.X,
                                                                              -splitContainer1.Panel1.AutoScrollPosition.Y + deltaMoved.Y);
                    }
                }
                else
                {
                    //move the selected collision point
                    PointF newPos = new PointF(
                        objectManager.SelectedObject.CollisionPoints[objectManager.SelectedObject.SelectedPoint].X + (int)(deltaMoved.X / zoomLevel),
                        objectManager.SelectedObject.CollisionPoints[objectManager.SelectedObject.SelectedPoint].Y + (int)(deltaMoved.Y / zoomLevel));

                    //clamp the point to the edges of the screen (the hardcoded 30 is to handle not moving the point past the scrollbar)
                    if (newPos.X < 0 + -splitContainer1.Panel1.AutoScrollPosition.X)
                        newPos.X = 0 + -splitContainer1.Panel1.AutoScrollPosition.X;
                    else if ((newPos.X + (pointSize / 2)) * zoomLevel > (splitContainer1.Panel1.Width + -splitContainer1.Panel1.AutoScrollPosition.X) - 30 && deltaMoved.X > 0)
                        newPos.X = (splitContainer1.Panel1.Width + -splitContainer1.Panel1.AutoScrollPosition.X) / zoomLevel - (pointSize / 2) - (30 / zoomLevel);

                    if (newPos.Y < 0 + -splitContainer1.Panel1.AutoScrollPosition.Y)
                        newPos.Y = 0 + -splitContainer1.Panel1.AutoScrollPosition.Y;
                    else if ((newPos.Y + (pointSize / 2)) * zoomLevel > splitContainer1.Panel1.Height + -splitContainer1.Panel1.AutoScrollPosition.Y - 30 && deltaMoved.Y > 0)
                        newPos.Y = (splitContainer1.Panel1.Height - splitContainer1.Panel1.AutoScrollPosition.Y) / zoomLevel - (pointSize / 2) - (30 / zoomLevel);

                    objectManager.SelectedObject.CollisionPoints[objectManager.SelectedObject.SelectedPoint] = newPos;

                    //moving the point around so test whether the collision polygon is convex
                    isConvex = objectManager.SelectedObject.CollisionPoints.Count <= 3 ? true : IsConvex(objectManager.SelectedObject.CollisionPoints);
                }

                saved = Saved_State.NOTSAVED;
                Invalidate();
            }

            lastMouse = e.Location;
        }
Exemplo n.º 2
0
        private void splitContainer1_Panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (collisionPolygonToolStripMenuItem.Checked && e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (null != objectManager.SelectedObject)
                {
                    Size offset = Size.Empty;

                    offset.Width += splitContainer1.Panel1.AutoScrollPosition.X;
                    offset.Height += splitContainer1.Panel1.AutoScrollPosition.Y;

                    PointF loc = new PointF((int)((e.Location.X - offset.Width) / zoomLevel), (int)((e.Location.Y - offset.Height) / zoomLevel));

                    if(objectManager.SelectedObject.SelectedPoint == -1)
                    {
                        objectManager.SelectedObject.CollisionPoints.Add(loc);
                        objectManager.SelectedObject.SelectedPoint = objectManager.SelectedObject.CollisionPoints.Count - 1;
                    }
                    else
                    {
                        objectManager.SelectedObject.CollisionPoints.Insert(objectManager.SelectedObject.SelectedPoint + 1, loc);
                        objectManager.SelectedObject.SelectedPoint = objectManager.SelectedObject.SelectedPoint + 1;
                    }

                    if (objectManager.SelectedObject.CollisionPoints.Count >= 3)
                        isConvex = objectManager.SelectedObject.CollisionPoints.Count == 3 ? true : IsConvex(objectManager.SelectedObject.CollisionPoints);

                    saved = Saved_State.NOTSAVED;
                    Invalidate();
                }
            }
        }
Exemplo n.º 3
0
        private void splitContainer1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && null != objectManager.SelectedObject)
            {
                if (objectManager.SelectedObject.SelectedPoint == -1)
                {
                    if (MessageBox.Show("Are you sure you want to delete this object?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
                        == System.Windows.Forms.DialogResult.Yes)
                    {
                        objectManager.RemoveObject(objectManager.SelectedObject);
                        objectManager.SelectedObject = null;
                        saved = Saved_State.NOTSAVED;
                        Invalidate();
                        FixViewList();
                        ButtonsEnabled(false);
                    }
                }
                else
                {
                    objectManager.SelectedObject.CollisionPoints.RemoveAt(objectManager.SelectedObject.SelectedPoint);
                    objectManager.SelectedObject.SelectedPoint = -1;

                    saved = Saved_State.NOTSAVED;
                    Invalidate();
                }
            }
        }
Exemplo n.º 4
0
        private void SaveXML(string _fileName)
        {
            if (!CheckEverythingConvex())
                return;

            XmlTextWriter textWriter = new XmlTextWriter(_fileName, null);
            textWriter.Formatting = Formatting.Indented;
            textWriter.WriteStartDocument();

            textWriter.WriteStartElement("World");
            textWriter.WriteAttributeString("Version", VERSION);

            textWriter.WriteStartElement("Objects");
            textWriter.WriteAttributeString("Number_of_Objects", objectManager.ObjectList.Count.ToString());
            foreach (cObject obj in objectManager.ObjectList)
            {
                obj.SaveXML(textWriter);
            }
            textWriter.WriteEndElement();

            textWriter.WriteEndDocument();
            textWriter.Close();

            fileName = _fileName;
            saved = Saved_State.SAVED;
        }
Exemplo n.º 5
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Image Files (*.bmp, *.gif, *.jpeg, *.jpg, *.png, *.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|XML Files (*.xml)|*.xml";
            ofd.Multiselect = true;

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (ofd.FilterIndex == 1)
                {
                    foreach (string s in ofd.FileNames)
                    {
                        string relativePath = MakeRelativePath(Application.ExecutablePath, s);

                        cObject t_object = new cObject(new Bitmap(s), relativePath);

                        objectManager.AddObject(t_object);
                        saved = Saved_State.NOTSAVED;
                    }
                }
                else
                {
                    XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object
                    int objOpend = 0;
                    int wrldOpend = 0;

                    foreach (string s in ofd.FileNames)
                    {
                        xmlDoc.Load(s); // Load the XML document from the specified file
                        bool exportedObject = false;

                        XmlNode versionNode = xmlDoc.SelectSingleNode("/World");
                        if (null == versionNode)
                        {
                            versionNode = xmlDoc.SelectSingleNode("/Exported_Object");
                            if (null == versionNode)
                            {
                                MessageBox.Show("ERROR: Cannot find root \"World\" or \"Exported_Object\".", "ERROR: Bad File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            else
                                exportedObject = true;
                        }

                        if (null == versionNode.Attributes["Version"])
                        {
                            MessageBox.Show("ERROR: XML file is does not contain version number.", "ERROR: Bad Version", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        string worldVersion = versionNode.Attributes["Version"].Value;

                        if (0 != String.Compare(worldVersion, VERSION))
                        {
                            MessageBox.Show("ERROR: XML file is using a version different from the Editor.", "ERROR: Old Version", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                        if (!exportedObject)
                        {
                            XmlNode objectsNode = xmlDoc.SelectSingleNode("/World/Objects");
                            int numOfObjects = int.Parse(objectsNode.Attributes["Number_of_Objects"].Value);

                            XmlNodeList objectChildren = objectsNode.ChildNodes;
                            XmlNode objectNode;
                            for (int i = 0; i < numOfObjects; ++i)
                            {
                                objectNode = objectChildren[i];

                                if (null != objectNode.Attributes["Image_Path"])
                                {
                                    string path = objectNode.Attributes["Image_Path"].Value;
                                    path = Path.GetFullPath(path);
                                    string relativePath = MakeRelativePath(Application.ExecutablePath, path);

                                    cObject t_object = new cObject(new Bitmap(path), relativePath);

                                    t_object.LoadXML(objectNode);

                                    objectManager.AddObject(t_object);
                                    ++objOpend;
                                }
                                else
                                    MessageBox.Show("ERROR: XML file has an object with a bad image file path.", "ERROR: Bad File Path", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }

                            ++wrldOpend;
                        }
                        else
                        {
                            XmlNode objectNode = xmlDoc.SelectSingleNode("/Exported_Object/Object");

                            if (null != objectNode.Attributes["Image_Path"])
                            {
                                string path = objectNode.Attributes["Image_Path"].Value;
                                path = Path.GetFullPath(path);
                                string relativePath = MakeRelativePath(Application.ExecutablePath, path);

                                cObject t_object = new cObject(new Bitmap(path), relativePath);

                                t_object.LoadXML(objectNode);

                                objectManager.AddObject(t_object);
                                ++objOpend;

                                saved = Saved_State.NOTSAVED;
                            }
                            else
                                MessageBox.Show("ERROR: XML file has an object with a bad image file path.", "ERROR: Bad File Path", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                    }

                    if (saved == Saved_State.NEVERSAVED && wrldOpend == 1)
                        saved = Saved_State.SAVED;
                    else
                        saved = Saved_State.NOTSAVED;
                }

                FindCanvasSize();
                objectManager.SelectedObject = null;
                ButtonsEnabled(false);
                Invalidate();
            }
        }
Exemplo n.º 6
0
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saved == Saved_State.NOTSAVED || (saved == Saved_State.NEVERSAVED && objectManager.ObjectList.Count > 0))
            {
                DialogResult result = MessageBox.Show("You have unsaved changes, you will loose any unsaved progress. Would you like to continue?",
                    "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                if (result == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
            }

            objectManager.ObjectList.Clear();

            saved = Saved_State.NEVERSAVED;
            splitContainer1.Panel1.AutoScrollMinSize = new Size(0, 0);
            objectManager.SelectedObject = null;
            ButtonsEnabled(false);
            Invalidate();
        }