public void AddNewMasses(Vector2 position)
        {
            //check combo box for number of masses to add
            string val = parentForm.combo_numOfMasses.Text;

            // int num = (int)parentForm.combo_numOfMasses.SelectedItem;
            if (val == "80" || val == "120" || val == "160" || val == "200" || val == "240")
            {
                //initialise mass list
                int            num    = int.Parse(val);
                List <SimMass> masses = new List <SimMass>();
                masses.Add(new SimMass(mass, position, SimObjectType.PASSIVE));
                for (int i = 0; i < num - 1; i++)
                {
                    masses.Add(new SimMass(mass, new Vector2(position.X, position.Y + (massDistance * (massTexture.Height * (masses.Count))) / 2), SimObjectType.ACTIVE));
                }
                SimString massString = new SimString(masses);
                stringLists.Add(massString);

                parentForm.stringAdded(masses);
                //add masses to simulation
                springSim.AddSimString(massString);
            }
            else
            {
                MessageBox.Show("Please enter a valid number of masses in the combo-box");
            }
        }
 private void MakeSelection(SimMass mass, SimString massString)
 {
     selectedMass          = mass;
     selectedMass.Selected = true;
     selectedString        = massString;
     lastSelectedMass      = mass;       //for use in parent form
     lastSelectedString    = massString; //for use in parent form
     parentForm.setPropertyView(selectedString);
 }
        public void AddSimString(SimString list)
        {
            simObjects.Add(list.MassList);
            //add springs and constraints
            List <Spring>           springs     = new List <Spring>();
            List <LengthConstraint> constraints = new List <LengthConstraint>();

            for (int i = 0; i < list.NumOfMasses - 1; i++)
            {
                //   constraints.Add(new LengthConstraint(length, list.MassList.ElementAt(i), list.MassList.ElementAt(i + 1)));
                springs.Add(new Spring(stiffness, damping, list.MassList.ElementAt(i), list.MassList.ElementAt(i + 1)));
            }
            springList.Add(springs);
            //constraintsList.Add(constraints);
        }
        public void RemoveString(SimString simString)
        {
            List <List <Spring> > springsToDelete = new List <List <Spring> >();

            foreach (List <Spring> springs in springSim.SpringList)
            {
                if (springs.ElementAt(0).SimObjectA == simString.MassList.ElementAt(0))
                {
                    springsToDelete.Add(springs);
                }
            }

            foreach (List <Spring> springs in springsToDelete)
            {
                springSim.SpringList.Remove(springs);
            }

            springSim.SimObjects.Remove(simString.MassList);
        }
        public void AlignLeft()
        {
            //find all selected strings
            List <SimString> strings = new List <SimString>();

            foreach (SimString massString in stringLists)
            {
                if (massString.MassList.ElementAt(0).Selected)
                {
                    strings.Add(massString);
                }
            }
            if (strings.Count != 0)
            {
                //find leftmost first mass string
                SimString leftString = strings.ElementAt(0);
                foreach (SimString massString in strings)
                {
                    if (massString.MassList.ElementAt(0).CurrPositionX < leftString.MassList.ElementAt(0).CurrPositionX)
                    {
                        leftString = massString;
                    }
                }

                //align the strings to match the topstring
                foreach (SimString massString in strings)
                {
                    if (massString != leftString)
                    {
                        float difference = massString.FirstMassLocationX - leftString.FirstMassLocationX;
                        foreach (SimMass mass in massString.MassList)
                        {
                            mass.CurrPositionX = mass.CurrPositionX - difference;
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Could not align, no full strings selected.\nPlease select strings using rectangle select and try again", "Align Error");
            }
        }
        //deselects all selected masses
        public void ClearSelection()
        {
            if (selectedMass != null)
            {
                selectedMass.Selected = false;
                selectedMass          = null;

                selectedString = null;
            }
            foreach (SimString massString in stringLists)
            {
                foreach (SimMass mass in massString.MassList)
                {
                    if (mass.Selected)
                    {
                        mass.Selected = false;
                    }
                }
            }
        }
        //any controls other than pan and zoom
        private void HandleInput()
        {
            if (MainForm.ApplicationIsActivated())
            {
                MouseState mouseState = Mouse.GetState();
                //DETECT MOUSE UP AND DOWN
                rMouseDown = false;
                rMouseUp   = false;
                lMouseDown = false;
                lMouseUp   = false;

                bool spaceDown = false;
                bool delDown   = false;
                bool dDown     = false;


                #region button up/down checks;

                if (kbState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D) &&
                    oldKbState.IsKeyUp(Microsoft.Xna.Framework.Input.Keys.D))
                {
                    dDown = true;
                }
                else
                {
                    dDown = false;
                }

                if (kbState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Space) &&
                    oldKbState.IsKeyUp(Microsoft.Xna.Framework.Input.Keys.Space))
                {
                    spaceDown = true;
                }
                else
                {
                    spaceDown = false;
                }

                if (kbState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Delete) &&
                    oldKbState.IsKeyUp(Microsoft.Xna.Framework.Input.Keys.Delete))
                {
                    delDown = true;
                }
                else
                {
                    delDown = false;
                }

                if (lastRMouse != mouseState.RightButton)
                {
                    if (lastRMouse == Microsoft.Xna.Framework.Input.ButtonState.Released)
                    {
                        rMouseDown = true;
                    }
                    if (lastRMouse == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                    {
                        rMouseUp = true;
                    }
                }

                if (lastLMouse != mouseState.LeftButton)
                {
                    if (lastLMouse == Microsoft.Xna.Framework.Input.ButtonState.Released)
                    {
                        lMouseDown = true;
                    }
                    if (lastLMouse == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                    {
                        lMouseUp = true;
                    }
                }
                #endregion

                #region mouse input handling

                if (ClientRectangle.Contains(PointToClient(Control.MousePosition))) //if mouse position is over the control
                {
                    if (!this.Focused && mouseState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                    {
                        this.Focus(); //if not in focus, and left click is on control: give controlfocus
                    }
                    else //control is in focus
                    {
                        if (delDown)
                        {
                            parentForm.DeleteSelected();
                        }

                        if (parentForm.selectedToolTreeNode.Index == 0)//led is selected
                        {
                            #region ledMode

                            if (lMouseDown)
                            {
                                AddNewMasses(new Vector2(GetMousePos().X, GetMousePos().Y));
                            }

                            #endregion
                        }

                        else if (parentForm.selectedToolTreeNode.Index == 1) //select is selected
                        {
                            #region selectMode

                            bool offMass = false;
                            if (lMouseDown && selectedMass != null)
                            {
                                foreach (SimString massString in stringLists)
                                {
                                    foreach (SimMass mass in massString.MassList)
                                    {
                                        if (!(GetMousePos().X > mass.CurrPositionX - (massTexture.Width / 2) - 5f &&
                                              GetMousePos().X < mass.CurrPositionX + (massTexture.Width / 2) + 5f &&
                                              GetMousePos().Y > mass.CurrPositionY - (massTexture.Height / 2) - 5f &&
                                              GetMousePos().Y < mass.CurrPositionY + (massTexture.Height / 2) + 5f))          // deteect of mouse down over a mass
                                        {
                                            offMass = true;
                                        }
                                    }
                                }
                                if (offMass) //click was not on a mass
                                {
                                    if (selectedMass.SimObjectType == SimObjectType.PASSIVETEMP)
                                    {
                                        selectedMass.SimObjectType = SimObjectType.ACTIVE;
                                    }
                                    ClearSelection();
                                    offMass = false;
                                }
                            }

                            if (lMouseDown && selectedMass == null)
                            {
                                SimMass   newMass   = null;
                                SimString newString = null;
                                foreach (SimString massString in stringLists)
                                {
                                    foreach (SimMass mass in massString.MassList)
                                    {
                                        if (GetMousePos().X > mass.CurrPositionX - (massTexture.Width / 2) - 5f &&
                                            GetMousePos().X < mass.CurrPositionX + (massTexture.Width / 2) + 5f &&
                                            GetMousePos().Y > mass.CurrPositionY - (massTexture.Height / 2) - 5f &&
                                            GetMousePos().Y < mass.CurrPositionY + (massTexture.Height / 2) + 5f)            // deteect of mouse down over a mass
                                        {
                                            //mouse click was on a mass
                                            newMass   = mass;
                                            newString = massString;
                                        }
                                    }
                                }


                                if (newMass != null && newString != null)
                                {
                                    MakeSelection(newMass, newString);
                                }
                            }


                            if (dDown)
                            {
                                if (selectedMass != null)
                                {
                                    if (selectedMass.SimObjectType == SimObjectType.PASSIVETEMP)
                                    {
                                        selectedMass.SimObjectType = SimObjectType.ACTIVE;
                                    }
                                }
                                ClearSelection();

                                parentForm.saveState();
                            }

                            if ((spaceDown || rMouseDown) && selectedMass != null)
                            {
                                if (selectedMass.SimObjectType == SimObjectType.PASSIVETEMP) //toggle (temp)passive/active
                                {
                                    selectedMass.SimObjectType = SimObjectType.PASSIVE;
                                }
                                else
                                {
                                    selectedMass.SimObjectType = SimObjectType.PASSIVETEMP;
                                }

                                parentForm.saveState();
                            }


                            if (mouseState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                            {
                                if (selectedMass != null) //if left mouse held and mass is selected, mass is being moved
                                {
                                    selectedMass.CurrPosition = GetMousePos();

                                    if (selectedMass.SimObjectType == SimObjectType.ACTIVE)
                                    {
                                        selectedMass.SimObjectType = SimObjectType.PASSIVETEMP;
                                    }
                                }
                            }
                            #endregion
                        }

                        else if (parentForm.selectedToolTreeNode.Index == 2) //rectangle select is selected
                        {
                            #region rectSelectMode

                            if (lMouseDown)
                            {
                                startVector = new Vector2(this.PointToClient(Control.MousePosition).X, this.PointToClient(Control.MousePosition).Y);
                            }

                            if (mouseState.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                            {
                                Vector2 endVector  = new Vector2(this.PointToClient(Control.MousePosition).X, this.PointToClient(Control.MousePosition).Y);
                                int     rectWidth  = (int)(this.PointToClient(Control.MousePosition).X - startVector.X);
                                int     rectHeight = (int)(this.PointToClient(Control.MousePosition).Y - startVector.Y);

                                Vector2 startVectorTrans = TransformPos(startVector);
                                Vector2 endVectorTrans   = TransformPos(endVector);
                                Vector2 rectVector;
                                if (endVectorTrans.Length() > startVectorTrans.Length())
                                {
                                    rectVector = endVectorTrans - startVectorTrans;
                                }
                                else
                                {
                                    rectVector = startVectorTrans - endVectorTrans;
                                }

                                selectionRectangle = new Rectangle((int)startVectorTrans.X, (int)startVectorTrans.Y, (int)rectVector.X, (int)rectVector.Y);
                                spriteBatch.Begin();

                                spriteBatch.DrawRectangle(new Rectangle((int)startVector.X, (int)startVector.Y, rectWidth, rectHeight), Color.Black);

                                spriteBatch.End();
                            }

                            if (lMouseUp)
                            {
                                //check if any masses are inside rectangle
                                ObjectGroup currentGroup = new ObjectGroup();
                                foreach (SimString massString in stringLists)
                                {
                                    foreach (SimMass mass in massString.MassList)
                                    {
                                        if (selectionRectangle.Contains(new Point((int)mass.CurrPositionX, (int)mass.CurrPositionY)))
                                        {
                                            mass.Selected = true;
                                            foreach (SimMass sMass in massString.MassList)
                                            {
                                                sMass.Selected = true;
                                            }
                                        }
                                    }
                                }
                            }

                            if (dDown)
                            {
                                foreach (SimString massString in stringLists)
                                {
                                    foreach (SimMass mass in massString.MassList)
                                    {
                                        mass.Selected = false;
                                    }
                                }
                            }

                            if (spaceDown || rMouseDown)
                            {
                                foreach (SimString massString in stringLists)
                                {
                                    foreach (SimMass mass in massString.MassList)
                                    {
                                        if (mass.Selected)
                                        {
                                            if (mass.SimObjectType == SimObjectType.ACTIVE || mass.SimObjectType == SimObjectType.PASSIVETEMP)
                                            {
                                                mass.SimObjectType = SimObjectType.GROUPPASSIVE;
                                            }
                                            else if (mass.SimObjectType == SimObjectType.GROUPPASSIVE)
                                            {
                                                mass.SimObjectType = SimObjectType.ACTIVE;
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                }
                #endregion

                oldKbState = kbState;
                lastLMouse = mouseState.LeftButton;
                lastRMouse = mouseState.RightButton;
                spaceDown  = false;
            }
        }
Exemple #8
0
        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture   = new System.Globalization.CultureInfo("en-GB");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");

            //goto new button to clear current stuff
            if (!isNew)
            {
                newToolStripButton_Click(sender, e);
            }

            isNew = false;

            DialogResult diaRes = openFileDialog1.ShowDialog();

            if (diaRes == DialogResult.OK)
            {
                string file = openFileDialog1.FileName;

                #region XML Loading
                XDocument  loadDoc = XDocument.Load(file);
                XNamespace ns      = loadDoc.Root.GetDefaultNamespace();

                //running count for generating routers (starts at 1 for default router)
                int routerCount = 1;

                foreach (XElement element in loadDoc.Elements()) //scene element
                {
                    //each router
                    foreach (XElement routerEle in element.Elements())
                    {
                        routerCount++;
                        Router newRouter = new Router();
                        topology.Add(newRouter);
                        newRouter.Index = routerCount;
                        TreeNode newRouterNode = new TreeNode("Router " + routerCount);
                        newRouter.Reference = newRouterNode;
                        topo_treeView.Nodes.Add(newRouterNode);

                        List <SimString> stringList = new List <SimString>();
                        bool             exist      = false;
                        float            xPos;
                        float            yPos;
                        UInt32           id;
                        UInt16           id16;
                        byte             controllerID;
                        byte             lightID;

                        foreach (XElement ele in routerEle.Elements())
                        {
                            if (ele.Name.ToString().Contains("Light"))//if element is a lightpoint
                            {
                                id           = (UInt32)Convert.ToInt32(ele.Attribute("id").Value);
                                id16         = (UInt16)(id & 0xFFFF);
                                lightID      = (byte)(id16 & 0xFF);
                                controllerID = (byte)(id16 >> 8);

                                xPos = Math.Abs(Convert.ToSingle(ele.Attribute("x").Value) * 1000);
                                yPos = Math.Abs(Convert.ToSingle(ele.Attribute("y").Value) * 1000);

                                foreach (SimString sString in stringList)
                                {
                                    if (sString.ID == controllerID)
                                    { // a string has the id, add to it
                                        exist = true;
                                        sString.AddMass(new SimMass(new Microsoft.Xna.Framework.Vector2(xPos, yPos), lightID));
                                    }
                                }

                                if (!exist)
                                {
                                    SimString newString = new SimString();
                                    newString.ID = controllerID;
                                    newString.AddMass(new SimMass(new Microsoft.Xna.Framework.Vector2(xPos, yPos), lightID));
                                    stringList.Add(newString);
                                }
                                exist = false;
                            }
                        }

                        //add found lightpoints and controllers to the router
                        foreach (SimString sString in stringList)
                        {
                            //create controllers
                            Controller newController     = new Controller();
                            TreeNode   newControllerNode = new TreeNode("Controller " + newRouter.ControllerIDs);
                            newRouter.ControllerIDs++;
                            newController.IDNum     = (ushort)sString.ID;
                            newController.Reference = newControllerNode;
                            newController.Router    = newRouter;

                            newRouter.Controllers.Add(newController);
                            newRouterNode.Nodes.Add(newControllerNode);

                            LedString newString = new LedString();
                            newString.Controller = newController;
                            newString.ID         = sString.ID;
                            newString.MassList   = sString.MassList;
                            newString.NumOfLeds  = sString.MassList.Count;
                            newController.Connections.Add(newString);

                            TreeNode newStringNode = new TreeNode("String " + newController.StringCount);
                            newController.StringCount++;

                            newString.Reference = newStringNode;

                            newControllerNode.Nodes.Add(newStringNode);
                        }
                        //add this router's masses to the simulation
                        softBodyControl1.loadMasses(stringList);
                    }
                } //parsing done
                #endregion
            }
        }