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).
This commit is contained in:
Simon Holmberg
2012-10-19 23:40:03 +02:00
parent 9d92736932
commit f8c1422eeb
2 changed files with 10 additions and 8 deletions
+7
View File
@@ -138,6 +138,13 @@ namespace DepthsBelow.GUI
/// </summary> /// </summary>
public event OnClickHandler OnClick; public event OnClickHandler OnClick;
/// <summary> /// <summary>
/// Gets the number of subscribers to the <see cref="OnClickHandler" /> event.
/// </summary>
public int OnClickCount
{
get { return (OnClick != null) ? OnClick.GetInvocationList().Length : 0; }
}
/// <summary>
/// <see cref="OnClickHandler" /> event arguments. /// <see cref="OnClickHandler" /> event arguments.
/// </summary> /// </summary>
public class OnClickArgs : EventArgs public class OnClickArgs : EventArgs
+3 -8
View File
@@ -29,14 +29,9 @@ namespace DepthsBelow
{ {
var clickRectangle = new Rectangle(position.X, position.Y, 1, 1); var clickRectangle = new Rectangle(position.X, position.Y, 1, 1);
var intersections = new List<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();
foreach (var frame in Frames) // Get the topmost frame of that list
{
if (clickRectangle.Intersects(frame.AbsoluteRectangle))
intersections.Add(frame);
}
var topFrame = GetTopFrame(intersections); var topFrame = GetTopFrame(intersections);
if (topFrame != null) if (topFrame != null)