Screen.Hit_gridSourceSpatial indexing for mouse hit testing.
A hit grid maps screen coordinates to integer element IDs. Lookup is O(1); registration is O(region area).
empty_id is 0. Represents the absence of any element.
The type for rectangular areas in cell coordinates.
The type for hit grids. Internally backed by an int32 bigarray for cache locality.
create ~width ~height is a hit grid of the given dimensions with all cells set to empty_id.
resize t ~width ~height updates t's dimensions to width and height. All cells are reset to empty_id. Internal storage is grown only when necessary.
add t ~x ~y ~width ~height ~id fills the rectangular region with id (painter's algorithm: overwrites any existing IDs). The rectangle is clipped to the grid bounds and the active clip region. Zero or negative dimensions are a no-op.
get t ~x ~y is the element ID at (x, y), or empty_id if the coordinates are out of bounds.
blit ~src ~dst copies the content of src into dst. dst is resized to match src.
Hierarchical clipping for hit regions. When a clip is active, add operations are constrained to the intersection of the clip rectangle and the grid bounds. This prevents elements inside overflow-hidden containers from receiving mouse events outside their visible area.
Push/pop pairs must be balanced.
push_clip t r pushes a clipping rectangle. The effective clip is the intersection of r with the current clip (hierarchical narrowing).