Struct HudRegions

Source
pub struct HudRegions { /* private fields */ }
Expand description

An allocation-free, bounded HUD composition. Rebuild on resize with Self::new or reuse the current screen/safe area with Self::clear. There is no retained input, simulation, font, renderer or clock state.

Implementations§

Source§

impl HudRegions

Source

pub fn new( screen: [f32; 2], insets: [f32; 4], minimum_content: [f32; 2], ) -> Result<HudRegions, HudLayoutError>

Create a layout in absolute logical screen coordinates. Insets are left, top, right, bottom, with the same proportional collapse as Viewport::responsive. Dimensions, insets and minimum_content must be finite and nonnegative. Empty screens are valid when the minimum permits.

minimum_content is an authored size in screen units, independent of the world’s design size. Insufficient space is explicit so callers can choose a compact arrangement or omit secondary rows while retaining readable text.

Source

pub fn bounds(&self) -> Rect

Original safe-area rectangle, before any reservations.

Source

pub fn content(&self) -> Rect

Remaining world/content rectangle in absolute logical screen units.

Source

pub fn regions(&self) -> &[HudRegion]

Borrow the occupied slots in reservation order; no temporary allocation.

Source

pub fn region(&self, name: &str) -> Option<Rect>

Look up a named rectangle. This accepts non-static queries as well.

Source

pub fn reserve( &mut self, name: &'static str, edge: HudEdge, thickness: f32, ) -> Result<Rect, HudLayoutError>

Reserve a strip with the given thickness in logical screen units. Top/bottom consume height; left/right consume width. Repeated edges are allowed. A zero thickness creates an inert named strip and uses one slot.

Invalid requests, duplicate names, capacity and then available space are checked in that order. Every failure is atomic. Floating-point extents are rounded inward when necessary to keep rectangles disjoint; a positive thickness whose edge cannot be represented is rejected as invalid.

use ferravex_play::ui::{HudEdge, HudLayoutError, HudRegions};
let mut hud = HudRegions::new([320., 200.], [0.; 4], [160., 100.])?;
hud.reserve("header", HudEdge::Top, 60.)?;
let before = hud;
assert_eq!(hud.reserve("footer", HudEdge::Bottom, 44.),
           Err(HudLayoutError::InsufficientSpace));
assert_eq!(hud, before);
Source

pub fn clear(&mut self)

Remove all reservations and restore the original safe area/minimum policy. Reuses the fixed storage, including after a failed reservation.

use ferravex_play::ui::{HudEdge, HudRegions};
let mut hud = HudRegions::new([640., 360.], [8.; 4], [160., 90.])?;
hud.reserve("header", HudEdge::Top, 44.)?;
hud.clear();
assert!(hud.regions().is_empty());
assert_eq!(hud.region("header"), None);
assert_eq!(hud.content(), hud.bounds());
hud.reserve("header", HudEdge::Top, 60.)?;
Source

pub fn fit_canvas( &self, design: [f32; 2], ) -> Result<Option<CanvasFit>, HudLayoutError>

Fit a positive, finite world design size into the remaining content. Returns None when either content dimension is below one logical pixel; callers must skip world rendering and pointer handling in that case. An unrepresentable scale, camera zoom or displayed extent returns an error. HUD reservations and fonts are never scaled by this operation.

use ferravex_play::ui::{HudEdge, HudRegions};
let mut hud = HudRegions::new([800., 600.], [10., 20., 30., 40.], [0.; 2])?;
hud.reserve("header", HudEdge::Top, 60.)?;
let fit = hud.fit_canvas([400., 200.])?.unwrap();
let world = fit.viewport();
assert_eq!(world.scale(), 1.9);
assert_eq!(world.pixels().min, [10., 130.]);
assert_eq!(world.unproject([390., 320.]), Some([200., 100.]));
assert_eq!(world.unproject([40., 40.]), None); // Header is not world input.
assert_eq!(fit.screen(), [800., 600.]);
assert!((fit.camera_offset()[0] + 0.025).abs() < 0.00001);
assert!((fit.camera_offset()[1] + 1. / 15.).abs() < 0.00001);

Trait Implementations§

Source§

impl Clone for HudRegions

Source§

fn clone(&self) -> HudRegions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HudRegions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for HudRegions

Source§

fn eq(&self, other: &HudRegions) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for HudRegions

Source§

impl StructuralPartialEq for HudRegions

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.