//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<SpaceObject> candidates = new List<SpaceObject>();

            foreach (SpaceObject obj in currentlySelectedRoom.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 (SpaceObject 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_Damage.Enabled = false; // Only true if this is a hazard (below)
                tb_Rotation.Enabled = true;
                b_ApplyProperties.Enabled = true;
                b_Front.Enabled = true;
                tb_SLevel.Enabled = false;
                tb_Rotation.Text = (currentlySelectedObject.Rotation * 180.0f / MathHelper.Pi).ToString();

                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_Damage.Text = "";
                tb_Rotation.Text = "";
                tb_SLevel.Text = "";
                b_ApplyProperties.Enabled = false;
                b_Front.Enabled = false;
                tb_Damage.Enabled = false;
                tb_Rotation.Enabled = false;
                tb_SLevel.Enabled = false;

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