void UserControl1_MouseDown(object sender, MouseEventArgs e) { // See where the mouse is clicked // If on an object, activate that object // If on the background, activate scrolling Vector2 location = viewport.TransformToViewport(ClientRectangle, e.Location); //IClickable last = clickedObject; DateTime t = DateTime.Now; for (int i = 0; i < 100; i++) { float closest = float.PositiveInfinity; foreach (Object o in this.objects) { IClickable c = o as IClickable; if (c != null) { float distance = c.IsPointOnObject(location); //Console.WriteLine("Distance = " + distance); if (distance < closest) { closest = distance; clickedObject = c; } } } } double time = (DateTime.Now - t).TotalMilliseconds; Console.WriteLine("Took " + time + " Milliseconds"); //if (clickedObject != null) //{ // if (last != clickedObject) // { // last.UnHover(); // } // clickedObject.Hover(location); //} if (clickedObject == null) { clickedObject = viewport as IClickable; } if (this.SelectedItemChanged != null) { SelectedItemChanged.Invoke(clickedObject, EventArgs.Empty); } if (e.Button == MouseButtons.Left) { clickedObject.MouseDownLeft(location); } else if (e.Button == MouseButtons.Right) { clickedObject.MouseDownRight(new Vector2(e.Location.X, e.Location.Y)); } this.Invalidate(); }