private void addImage(int step = -1)
 {
     try
     {
         string name    = imageNameText.Text;
         string imgPath = imageFileText.Text;
         if (!File.Exists(imgPath))
         {
             MessageBox.Show("File does not exist");
             return;
         }
         if (name == "")
         {
             MessageBox.Show("Invalid name");
             return;
         }
         Bitmap image = new Bitmap(imgPath);
         objTemplate.AddImage(name, image, step);
         if (step < 0)
         {
             UpdateImageListView();
             ClearImageUI();
         }
     }
     catch (Exception ie)
     {
         MessageBox.Show(ie.ToString());
     }
 }
示例#2
0
        public void Load()
        {
            try
            {
                Clear();
                Byte[] mainBuffer = File.ReadAllBytes(this.path);
                int    counter    = 0;
                //Number of objects
                int numObjs = traverseToInt(mainBuffer, ref counter);
                for (int i = 0; i < numObjs; i++)
                {
                    //Object name
                    string         objName     = traverseToString(mainBuffer, ref counter, 20);
                    ObjectTemplate curTemplate = new ObjectTemplate(objName);
                    //Visible
                    curTemplate.visibility = traverseToBool(mainBuffer, ref counter);
                    //Soft
                    curTemplate.isSoft = traverseToBool(mainBuffer, ref counter);
                    //Number of properties
                    int numProps = traverseToInt(mainBuffer, ref counter);
                    for (int j = 0; j < numProps; j++)
                    {
                        //Property name
                        string propertyName = traverseToString(mainBuffer, ref counter, 20);
                        //Property type
                        string propertyType = traverseToString(mainBuffer, ref counter, 20);
                        //Property size
                        int size = traverseToInt(mainBuffer, ref counter);
                        //Property value
                        byte[] value = traverseToByte(mainBuffer, ref counter, size);

                        curTemplate.AddProperty(propertyName, propertyType, value);
                    }
                    //Number of images
                    int numImages = traverseToInt(mainBuffer, ref counter);
                    for (int k = 0; k < numImages; k++)
                    {
                        int numSteps = traverseToInt(mainBuffer, ref counter);
                        for (int p = 0; p < numSteps; p++)
                        {
                            //Image name
                            string imageName = traverseToString(mainBuffer, ref counter, 20);
                            //Image size
                            int imageSize = traverseToInt(mainBuffer, ref counter);
                            //Image value
                            Bitmap image = traverseToBitmap(mainBuffer, ref counter, imageSize);
                            //# of Collision Vectors
                            int         nVect   = traverseToInt(mainBuffer, ref counter);
                            List <Line> vectors = new List <Line>();
                            for (int m = 0; m < nVect; m++)
                            {
                                int  x1   = traverseToInt(mainBuffer, ref counter);
                                int  y1   = traverseToInt(mainBuffer, ref counter);
                                int  x2   = traverseToInt(mainBuffer, ref counter);
                                int  y2   = traverseToInt(mainBuffer, ref counter);
                                Line line = new Line(x1, y1, x2, y2);
                                vectors.Add(line);
                            }
                            curTemplate.AddImage(imageName, image, vectors);
                        }
                    }
                    objectTemplates.Add(objName, curTemplate);
                }
                //Instance count
                int numInst = traverseToInt(mainBuffer, ref counter);
                for (int i = 0; i < numInst; i++)
                {
                    //Instance name
                    string         instName = traverseToString(mainBuffer, ref counter, 20);
                    ObjectInstance instance = new ObjectInstance(objectTemplates[instName]);
                    //X
                    instance.x = traverseToFloat(mainBuffer, ref counter);
                    //Y
                    instance.y = traverseToFloat(mainBuffer, ref counter);
                    foreach (Property curProperty in instance.instance.properties.Values)
                    {
                        //Size
                        int propSize = traverseToInt(mainBuffer, ref counter);
                        //Value
                        curProperty.value = traverseToByte(mainBuffer, ref counter, propSize);
                        instance.instance.AddProperty(curProperty.name, curProperty.type, curProperty.value);
                    }
                    objectInstances.Add(instance);
                }
                //Wall Instance count
                int numWallInst = traverseToInt(mainBuffer, ref counter);
                for (int i = 0; i < numWallInst; i++)
                {
                    //Instance name
                    string     instName = traverseToString(mainBuffer, ref counter, 20);
                    ObjectWall instance = new ObjectWall(objectTemplates[instName]);
                    //X
                    instance.x = traverseToFloat(mainBuffer, ref counter);
                    //Y
                    instance.y = traverseToFloat(mainBuffer, ref counter);
                    //W
                    instance.w = traverseToFloat(mainBuffer, ref counter);
                    //H
                    instance.w = traverseToFloat(mainBuffer, ref counter);
                    foreach (Property curProperty in instance.instance.properties.Values)
                    {
                        //Size
                        int propSize = traverseToInt(mainBuffer, ref counter);
                        //Value
                        curProperty.value = traverseToByte(mainBuffer, ref counter, propSize);
                        instance.instance.AddProperty(curProperty.name, curProperty.type, curProperty.value);
                    }
                    objectInstances.Add(instance);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Warrning: Not all error file loaded");
            }
        }