Events
Both display objects and UI components emit events, which are documented in whack::events::disp::* and whack::events::comp::*.
Here are some events that display objects include:
added_to_stageremoved_from_stageenter_frame(a broadcast event)exit_frame(a broadcast event)- Pointer events
Events include tunneling information, since they are dispatched in three phases:
- Capturing phase (optional). Goes from the root parent to the parent of the target.
- Target phase. Goes at the target object.
- Bubbling phase (optional). Goes from the parent of the target to the root parent.
Handling DOM events
Events that are pre-defined in UiComponent and DisplayObject may be handled as follows:
#![allow(unused)] fn main() { // Use comp= for UI components // (whack::components::UiComponent) whack::evt!(comp=button, click, |e, d| { // }); // Use disp= for display objects // (whack::gfx::DisplayObject) whack::evt!(disp=image, click, |e, d| { // }); }
Note:
eholds the event tunneling information anddholds event-specific details (for example, for aclickevent, the (x, y) coordinates and other properties).
The whack::evt! macro returns an EventListener object with a .remove() method to detach the listener from the tied DisplayObject or UiComponent.
Dynamic events may be handled using dyn=event_type, requiring an explicit detail type to cast to if used:
#![allow(unused)] fn main() { whack::evt!(disp=image, dyn="custom_event", |e, d: CustomEvent| { // }); }
Note that the e and d parameters may be omitted if unused.
Capture
You may specify the capture option in whack::evt!, equivalent to the capture option in the WHATWG DOM [object EventTarget].addEventListener(..., { capture }).
#![allow(unused)] fn main() { whack::evt!(disp=obj, click, || { // }, capture = true); }
Emitting events
Here is a simple example demonstrating an .emit() method call.
#![allow(unused)] fn main() { obj.emit(whack::Event::new("event_name", whack::EventOptions { detail: Rc::new(whack::GenericEvent), ..default() })); }