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); }
//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; } } } }
protected override void Initialize() { this.Dock = DockStyle.Fill; //Init camera and dimensions this.worldDimensions = new Rectangle(0, 0, this.Width, this.Height); previousScroll = Mouse.GetState().ScrollWheelValue; zoomIncrement = 0.01f; camera = new Camera(GraphicsDevice.Viewport, (int)worldDimensions.Width * worldSizeMultiplier, (int)worldDimensions.Height * worldSizeMultiplier, 1f); numOfMasses = 80; ContentManager content = new ContentManager(Services); content.RootDirectory = "Content"; massTexture = content.Load <Texture2D>("Image1"); selectedTexture = content.Load <Texture2D>("selectedRing"); spriteBatch = new SpriteBatch(GraphicsDevice); selectedMass = null; lastLMouse = Microsoft.Xna.Framework.Input.ButtonState.Released; lastRMouse = Microsoft.Xna.Framework.Input.ButtonState.Released; //area selection startVector = new Vector2(); //group list objectGroupList = new List <ObjectGroup>(); // Start the animation timer. timer = new Timer(); timer.Interval = 1; timer.Tick += Tick; timer.Start(); //create simulation springSim = new Simulation(this); springSim.Stiffness = stiffness; springSim.Damping = damping; springSim.Length = 10f; springSim.MassPerString = numOfMasses; stringLists = new List <SimString>(); //add forces gravity = new Gravity(new Vector2(0f, 9.81f)); springSim.AddGlobalForceGenerator(gravity); air = new Medium(0.5f); springSim.AddGlobalForceGenerator(air); barrier = new ControlSide(new Rectangle(worldDimensions.X, worldDimensions.Y, worldDimensions.Width * worldSizeMultiplier, worldDimensions.Height * worldSizeMultiplier)); springSim.AddGlobalForceGenerator(barrier); //add integrator integrator = new ForwardEulerIntegrator(this); springSim.Integrator = integrator; parentForm.saveState(); }
//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; } }
public void addMass(SimMass newObj) { this.groupList.Add(newObj); }