|
|
@@ -17,8 +17,12 @@ namespace event { |
|
|
|
|
|
|
|
/** A per-event state shared and writable by all widgets that recursively handle an event. */ |
|
|
|
struct Context { |
|
|
|
widget::Widget *target = NULL; |
|
|
|
/** Whether the event should continue recursing to children Widgets. */ |
|
|
|
bool propagating = true; |
|
|
|
/** Whether the event has been consumed by an event handler and no more handlers should consume the event. */ |
|
|
|
bool consumed = false; |
|
|
|
/** The widget that responded to the event. */ |
|
|
|
widget::Widget *target = NULL; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@@ -29,30 +33,36 @@ struct Base { |
|
|
|
/** Prevents the event from being handled by more Widgets. |
|
|
|
*/ |
|
|
|
void stopPropagating() const { |
|
|
|
if (context) |
|
|
|
context->propagating = false; |
|
|
|
if (!context) return; |
|
|
|
context->propagating = false; |
|
|
|
} |
|
|
|
bool isPropagating() const { |
|
|
|
if (context) |
|
|
|
return context->propagating; |
|
|
|
return true; |
|
|
|
if (!context) return true; |
|
|
|
return context->propagating; |
|
|
|
} |
|
|
|
/** Tells the event handler that a particular Widget consumed the event. |
|
|
|
You usually want to stop propagation as well, so call consume() instead. |
|
|
|
*/ |
|
|
|
void setTarget(widget::Widget *w) const { |
|
|
|
if (context) |
|
|
|
context->target = w; |
|
|
|
if (!context) return; |
|
|
|
context->target = w; |
|
|
|
} |
|
|
|
widget::Widget *getTarget() const { |
|
|
|
if (context) |
|
|
|
return context->target; |
|
|
|
return NULL; |
|
|
|
if (!context) return NULL; |
|
|
|
return context->target; |
|
|
|
} |
|
|
|
/** Sets the target Widget and stops propagation. */ |
|
|
|
/** Sets the target Widget and stops propagating. |
|
|
|
A NULL Widget may be passed to consume but not set a target. |
|
|
|
*/ |
|
|
|
void consume(widget::Widget *w) const { |
|
|
|
setTarget(w); |
|
|
|
stopPropagating(); |
|
|
|
if (!context) return; |
|
|
|
context->propagating = false; |
|
|
|
context->consumed = true; |
|
|
|
context->target = w; |
|
|
|
} |
|
|
|
bool isConsumed() const { |
|
|
|
if (!context) return false; |
|
|
|
return context->consumed; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
@@ -150,7 +160,7 @@ struct Leave : Base { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** Occurs when a Widget begins consuming the Button press event. |
|
|
|
/** Occurs when a Widget begins consuming the Button press event for the left mouse button. |
|
|
|
Must consume to set the widget as selected. |
|
|
|
*/ |
|
|
|
struct Select : Base { |
|
|
@@ -177,22 +187,27 @@ struct SelectText : Base, TextBase { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct DragBase : Base { |
|
|
|
/** The mouse button held during the drag. */ |
|
|
|
int button; |
|
|
|
}; |
|
|
|
|
|
|
|
/** Occurs when a Widget begins being dragged. |
|
|
|
Must consume to set the widget as dragged. |
|
|
|
*/ |
|
|
|
struct DragStart : Base { |
|
|
|
struct DragStart : DragBase { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** Occurs when a Widget stops being dragged by releasing the mouse button. |
|
|
|
*/ |
|
|
|
struct DragEnd : Base { |
|
|
|
struct DragEnd : DragBase { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** Occurs every frame on the dragged Widget. |
|
|
|
*/ |
|
|
|
struct DragMove : Base { |
|
|
|
struct DragMove : DragBase { |
|
|
|
/** Change in mouse position since the last frame. Can be zero. */ |
|
|
|
math::Vec mouseDelta; |
|
|
|
}; |
|
|
@@ -201,7 +216,7 @@ struct DragMove : Base { |
|
|
|
/** Occurs every frame when the mouse is hovering over a Widget while another Widget (possibly the same one) is being dragged. |
|
|
|
Recurses until consumed. |
|
|
|
*/ |
|
|
|
struct DragHover : Base, PositionBase { |
|
|
|
struct DragHover : DragBase, PositionBase { |
|
|
|
/** The dragged widget */ |
|
|
|
widget::Widget *origin = NULL; |
|
|
|
/** Change in mouse position since the last frame. Can be zero. */ |
|
|
@@ -211,7 +226,7 @@ struct DragHover : Base, PositionBase { |
|
|
|
/** Occurs when the mouse enters a Widget while dragging. |
|
|
|
Must consume to set the widget as drag-hovered. |
|
|
|
*/ |
|
|
|
struct DragEnter : Base { |
|
|
|
struct DragEnter : DragBase { |
|
|
|
/** The dragged widget */ |
|
|
|
widget::Widget *origin = NULL; |
|
|
|
}; |
|
|
@@ -219,7 +234,7 @@ struct DragEnter : Base { |
|
|
|
|
|
|
|
/** Occurs when the mouse leaves a Widget while dragging. |
|
|
|
*/ |
|
|
|
struct DragLeave : Base { |
|
|
|
struct DragLeave : DragBase { |
|
|
|
/** The dragged widget */ |
|
|
|
widget::Widget *origin = NULL; |
|
|
|
}; |
|
|
@@ -227,7 +242,7 @@ struct DragLeave : Base { |
|
|
|
|
|
|
|
/** Occurs when the mouse button is released over a Widget while dragging. |
|
|
|
*/ |
|
|
|
struct DragDrop : Base { |
|
|
|
struct DragDrop : DragBase { |
|
|
|
/** The dragged widget */ |
|
|
|
widget::Widget *origin = NULL; |
|
|
|
}; |
|
|
@@ -308,6 +323,7 @@ struct State { |
|
|
|
*/ |
|
|
|
widget::Widget *hoveredWidget = NULL; |
|
|
|
widget::Widget *draggedWidget = NULL; |
|
|
|
int dragButton = 0; |
|
|
|
widget::Widget *dragHoveredWidget = NULL; |
|
|
|
widget::Widget *selectedWidget = NULL; |
|
|
|
/** For double-clicking */ |
|
|
@@ -315,7 +331,7 @@ struct State { |
|
|
|
widget::Widget *lastClickedWidget = NULL; |
|
|
|
|
|
|
|
void setHovered(widget::Widget *w); |
|
|
|
void setDragged(widget::Widget *w); |
|
|
|
void setDragged(widget::Widget *w, int button); |
|
|
|
void setDragHovered(widget::Widget *w); |
|
|
|
void setSelected(widget::Widget *w); |
|
|
|
/** Prepares a widget for deletion */ |
|
|
|