From f8c1422eeb1ed0cad74afedce1c6f9424acfad23 Mon Sep 17 00:00:00 2001 From: Simon Holmberg Date: Fri, 19 Oct 2012 23:40:03 +0200 Subject: [PATCH] Events bubble down the frame stack until it finds a frame with a handler. For example, this allows you to have an OnClick handler on a parent frame, and child frames who cover the parent frame, but the parent frame still receives the click events (unless the child subscribes to the event). --- DepthsBelow/DepthsBelow/GUI/Frame.cs | 7 +++++++ DepthsBelow/DepthsBelow/GUIManager.cs | 11 +++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/DepthsBelow/DepthsBelow/GUI/Frame.cs b/DepthsBelow/DepthsBelow/GUI/Frame.cs index 9c0e47e..843b49f 100755 --- a/DepthsBelow/DepthsBelow/GUI/Frame.cs +++ b/DepthsBelow/DepthsBelow/GUI/Frame.cs @@ -138,6 +138,13 @@ namespace DepthsBelow.GUI /// public event OnClickHandler OnClick; /// + /// Gets the number of subscribers to the event. + /// + public int OnClickCount + { + get { return (OnClick != null) ? OnClick.GetInvocationList().Length : 0; } + } + /// /// event arguments. /// public class OnClickArgs : EventArgs diff --git a/DepthsBelow/DepthsBelow/GUIManager.cs b/DepthsBelow/DepthsBelow/GUIManager.cs index c95de15..b5cfe48 100755 --- a/DepthsBelow/DepthsBelow/GUIManager.cs +++ b/DepthsBelow/DepthsBelow/GUIManager.cs @@ -29,14 +29,9 @@ namespace DepthsBelow { var clickRectangle = new Rectangle(position.X, position.Y, 1, 1); - var intersections = new List(); - - foreach (var frame in Frames) - { - if (clickRectangle.Intersects(frame.AbsoluteRectangle)) - intersections.Add(frame); - } - + // Create a list of all frames which have an OnClick handler and intersects with the click position + var intersections = Frames.Where(frame => frame.OnClickCount > 0).Where(frame => clickRectangle.Intersects(frame.AbsoluteRectangle)).ToList(); + // Get the topmost frame of that list var topFrame = GetTopFrame(intersections); if (topFrame != null)