Pointer
Overview
The Pointer
API provides a single interface for handling both mouse and touch inputs to maximize cross-platform compatibility.
API
pointer
Handles mouse and touch events simultaneously for easy cross-platform functionality.
pub fn pointer() -> Pointer
Pointer
pub struct Pointer {
/// The x position of the mouse cursor or most recent touch event
x: i32,
/// The y position of the mouse cursor or most recent touch event
y: i32,
/// The state of the left mouse button or touch
state: Button,
/// The scroll delta
scroll: (i32, i32),
}
Usage
Reading Pointer Position & State
let p = pointer();
// World‐space position (accounts for camera zoom & pan)
let (world_x, world_y) = p.xy();
// Fixed‐canvas position (e.g. for UI)
let (canvas_x, canvas_y) = p.xy_fixed();
// Button & touch states
if p.just_pressed() {
// Fired at the instant the button/touch goes down
}
if p.pressed() {
// True every frame the button/touch is held
}
if p.just_released() {
// Fired at the instant the button/touch goes up
}
if p.released() {
// True every frame the button/touch is up
}
// Scroll wheel or two‐finger swipe delta
let (scroll_dx, scroll_dy) = p.scroll_delta();
Hit-Testing Arbitrary Rectangles
World-space regions
// Suppose you have an in-world sprite at (50, 30) sized 100×20:
if p.intersects(50, 30, 100, 20) {
// Pointer is over that sprite in world coordinates
}
Fixed UI elements
// A button drawn at (10,10) sized 200×50 in screen space:
if p.intersects_fixed(10, 10, 200, 50) {
// Pointer is over that UI button
}
Hit-Testing Bounds
The (i32, i32)
tuple implements the IntersectBounds
trait to make detecting Bounds
intersection more ergonomic/flexible.
World-space bounds intersection
let pointer_world_xy = p.xy();
let pointer_fixed_xy = p.xy_fixed();
let some_bounds = Bounds::new(100, 100, 64, 64);
if pointer_world_xy.intersects_bounds(some_bounds) {
// Pointer position relative to the camera is inside `some_bounds`
}
if pointer_fixed_xy.intersects_bounds(some_bounds) {
// Pointer position relative to the canvas is inside `some_bounds`
}