예제 #1
0
        public void SetupSim()
        {
            if (pLocalRoot == null)
            {
                pLocalRoot = HUB.simRoot.Duplicate();
            }
            XMLTree rn = pLocalRoot.Duplicate(); // sim node (not sim list but <Simulation>)

            // setup environment
            fps       = int.Parse(rn.children.ElementAt(0).content);
            g         = double.Parse(rn.children.ElementAt(1).content);
            gA        = double.Parse(rn.children.ElementAt(2).content);
            resetTime = int.Parse(rn.children.ElementAt(3).content);

            XMLTree o = rn.children.ElementAt(4); // object list

            // setup build ellipse
            int c = o.children.Count;

            UIObjects = new Ellipse[c];
            eObjects  = new EngineCircle[c];
            vectors   = new Vector[c];

            int i = 0;

            foreach (XMLTree n in o.children)   // foreach object in simulation, build ellipse

            {
                BuildEllipse(i,                                                                   // index
                             double.Parse(n.children.ElementAt(0).content),                       // xPos
                             double.Parse(n.children.ElementAt(1).content),                       // yPos
                             double.Parse(n.children.ElementAt(2).content),                       // radius
                             byte.Parse(n.children.ElementAt(5).children.ElementAt(0).content),   // a
                             byte.Parse(n.children.ElementAt(5).children.ElementAt(1).content),   // r
                             byte.Parse(n.children.ElementAt(5).children.ElementAt(2).content),   // g
                             byte.Parse(n.children.ElementAt(5).children.ElementAt(3).content),   // b
                             double.Parse(n.children.ElementAt(6).children.ElementAt(2).content), // mag
                             double.Parse(n.children.ElementAt(6).children.ElementAt(3).content), // angle
                             double.Parse(n.children.ElementAt(6).children.ElementAt(0).content), // xV
                             double.Parse(n.children.ElementAt(6).children.ElementAt(1).content), // yV
                             double.Parse(n.children.ElementAt(3).content),                       // mass
                             double.Parse(n.children.ElementAt(4).content));                      // e
                i++;
            }

            // UserEdits

            SimStackPanel.Children.Clear();

            if (rn.children.Count == 6)   // If simulation has UserEdits

            {
                o = rn.children.ElementAt(5);                                 // UserEdits list

                List <int[]> targetList = new List <int[]>(o.children.Count); // List of arrays that will be added to int[][] targets

                foreach (XMLTree n in o.children)                             // for each object available for edit

                {
                    int objectID; // ObjectID from xml

                    // If the ObjectID from the xml is not valid, then throw an exception
                    if ((!int.TryParse(n.children.ElementAt(0).content, out objectID)) || objectID >= eObjects.Length)
                    {
                        throw new InvalidDataException("XML format error: <objectID>" + n.children.ElementAt(0).content + "</objectID>. " + n.children.ElementAt(0).content + " is not a valid objectID.");
                    }

                    int[] tempArray = new int[n.children.Count]; // array to be added to targetList

                    tempArray[0] = objectID;

                    for (i = 1; i < n.children.Count; i++)   // goes through each target of object available for edit

                    {
                        int target; // targeted value ID of taret object from xml

                        // If the target tag's content from the xml is not valid then throw an exception
                        if ((!int.TryParse(n.children.ElementAt(i).content, out target)) || target > 8)
                        {
                            throw new InvalidDataException("XML format error: <target>" + n.children.ElementAt(i).content + "</target>. " + n.children.ElementAt(i).content + " is not a valid target.");
                        }

                        tempArray[i] = target; // add <target> value to array

                        // Setup the TextBox user will input values into
                        TextBox b = new TextBox();
                        b.Width              = 100;            // set width of box to 80 pixels
                        b.CharacterReceived += TextBoxHandler; // call handler when new character is received
                        // add objectID # to box access key, to the left of the '|' the integer is the objectID,
                        //to the right it is the target value, access key is like a name for the textbox
                        b.AccessKey += objectID + "|" + target;

                        switch (target)
                        {
                        case 0:
                            b.PlaceholderText = "x pos";
                            break;

                        case 1:
                            b.PlaceholderText = "y pos";
                            break;

                        case 2:
                            b.PlaceholderText = "radius";
                            break;

                        case 3:
                            b.PlaceholderText = "mass";
                            break;

                        case 4:
                            b.PlaceholderText = "elasticiy";
                            break;

                        case 5:
                            b.PlaceholderText = "x velocity";
                            break;

                        case 6:
                            b.PlaceholderText = "y velocity";
                            break;

                        case 7:
                            b.PlaceholderText = "velocity";
                            break;

                        case 8:
                            b.PlaceholderText = "direction";
                            break;
                        }

                        SimStackPanel.Children.Add(b);
                    }

                    targetList.Add(tempArray); // add array to list
                }

                targets = targetList.ToArray(); // targets = list
            }

            // Add a reset Button

            Button resetB = new Button();

            resetB.Content = "Reset";
            resetB.Width   = 100;
            resetB.Click  += Reset;
            SimStackPanel.Children.Add(resetB);
        }