//End of the callbacks, beginning of the helper fuctions
        //Used when the user clicks in the painting area while using the select tool.
        // This will select the object who's center is closest to the mouse.
        private void painting_clicked_select(MouseEventArgs e)
        {
            XnaPoint mousePosition = Conversion.PointToPoint(pb_Level.PointToClient(MousePosition));
            currentlySelectedObject = null;

            //Find all possible objects that we might be selecting.
            List<PhysicsObject> candidates = new List<PhysicsObject>();

            foreach (PhysicsObject obj in world.Objects)
            {
                if (obj.getBBRelativeToWorld().Contains(mousePosition))
                {
                    candidates.Add(obj);
                }
            }

            //If the room is the currently selected room, the player is not null, and the click is close enough
            // to the player's location, add the player to the list of possible candidates.
            /*
            if(currentState == State.EDITING_WORLD &&
                currentlySelectedRoom == world.CurrentRoom &&
                world.player != null &&
                world.player.getBBRelativeToWorld().Contains(mousePosition))
            {
                candidates.Add(world.player);
            }
            */

            //Find the object whose center is closest to the current mouse position. If there is no
            // possible selection, then the currentlySelectedObject continues to be null.
            foreach (PhysicsObject cand in candidates)
            {
                if (currentlySelectedObject == null ||
                        dist(mousePosition, cand.getCenterPoint()) < dist(mousePosition, currentlySelectedObject.getCenterPoint()))
                {

                    currentlySelectedObject = cand;

                    refreshResizeObjectBox();
                }
            }

            //If the currently selected item is a door, enable the door menu item. Otherwise, disable it.
            /*
            if (currentlySelectedObject != null &&
                currentlySelectedObject is Door)
            {

                menu_door.Enabled = true;
            }
            else
            {
                menu_door.Enabled = false;
            }
            */

            // Set fields in Object Properties panel
            if (currentlySelectedObject != null)
            {
                tb_Density.Enabled = true;
                tb_Rotation.Enabled = true;
                tb_bound1.Enabled = true;
                tb_bound2.Enabled = true;
                b_ApplyProperties.Enabled = true;
                b_Front.Enabled = true;
                tb_Scale.Enabled = true;
                cBox_StaticObject.Enabled = true;
                tb_Rotation.Text = (currentlySelectedObject.Angle * 180.0f / MathHelper.Pi).ToString();
                tb_Density.Text = currentlySelectedObject.Body.GetMass().ToString();
                tb_Scale.Text = currentlySelectedObject.scale.ToString();
                tb_bound1.Text = currentlySelectedObject.scale.ToString();
                tb_bound2.Text = currentlySelectedObject.scale.ToString();
                cBox_StaticObject.Checked = (currentlySelectedObject.Body.GetMass() == 0);

                /*
                if (currentlySelectedObject is HazardStatic)
                {
                    tb_Damage.Text = ((HazardStatic)currentlySelectedObject).Damage.ToString();
                    tb_Damage.Enabled = true;
                }
                else if (currentlySelectedObject is HazardDynamic)
                {
                    // Other
                    tb_Damage.Text = ((HazardDynamic)currentlySelectedObject).Damage.ToString();
                    tb_Damage.Enabled = true;
                }
                else if (currentlySelectedObject is Survivor || currentlySelectedObject is VanishWall)
                {
                    tb_SLevel.Enabled = true;
                }
                */

                //SetScriptingFields();
            }
            else
            {
                // Clear all the properties fields
                tb_Density.Text = "";
                tb_Rotation.Text = "";
                tb_bound1.Text = "";
                tb_bound2.Text = "";
                tb_Scale.Text = "";
                b_ApplyProperties.Enabled = false;
                b_Front.Enabled = false;
                tb_Density.Enabled = false;
                tb_Rotation.Enabled = false;
                tb_bound1.Enabled = false;
                tb_bound2.Enabled = false;
                tb_Scale.Enabled = false;

                tb_Script.Text = "";
                tb_Script.Enabled = false;
                cbox_Scripted.Checked = false;
                cbox_Scripted.Enabled = false;
                cBox_StaticObject.Checked = false;
                cBox_StaticObject.Enabled = false;
            }
        }