Skip to main content

StatBar

Struct StatBar 

Source
pub struct StatBar<'a> { /* private fields */ }
Expand description

A labeled stat bar: label, a bar filling current / max of the remaining width colored by super::Meter, and a trailing "current/max" readout.

Only the first row of area is used. Same layout and coloring as super::Gauge, but for integer current/max pairs (health, mana, stamina) with a literal readout instead of a percentage: "45/100" reads as a stat, not a load. max == 0 renders as an empty, unfilled bar with a "0/0" readout rather than a special-cased blank output. If current exceeds max (e.g. a temporarily buffed stat), the bar fill still caps at 100%, but the readout shows the true, uncapped numbers ("120/100") so the overflow stays visible in text. label_style defaults to Theme::DARK’s dim role (as if StatBar::theme had been called); set it with StatBar::label_style. The fill (and readout) color defaults to super::Meter’s green→yellow→red load ramp, which reads backwards for a health/mana/stamina stat (full renders as danger, not safe); override it with StatBar::fill_color.

§Examples

use retroglyph_core::grid::{Grid, Rect};
use retroglyph_ui::{StatBar, Surface, Widget};

let area = Rect::new(0, 0, 20, 1);
let mut grid = Grid::new(20, 1);
StatBar::new("HP", 45, 100).render(&mut Surface::new(&mut grid, area, 0));

Implementations§

Source§

impl<'a> StatBar<'a>

Source

pub fn new(label: &'a str, current: u32, max: u32) -> Self

A stat bar for label, reading current out of max, with label_style styled from Theme::DARK (as if StatBar::theme had been called).

Source

pub const fn label_style(self, style: Style) -> Self

Set the label’s style.

Source

pub const fn fill_color(self, fill_color: fn(f32) -> Color) -> Self

Overrides the bar’s fill (and readout text) color from super::Meter’s default green→yellow→red load ramp to fill_color, called with current / max clamped to 0.0..=1.0.

The default ramp reads backwards for a descending stat like health: full renders as danger (red), not safe. Supply a ramp that fits instead, e.g. |r| Color::lerp(Color::RED, Color::GREEN, r). A plain fn pointer rather than a boxed closure, so StatBar stays Copy; a non-capturing closure like the one above coerces to it automatically.

Source

pub fn theme(self, theme: Theme) -> Self

Sets label_style to theme.dim on theme.panel_bg, the same mapping (and the same “assumes it’s drawn on theme.panel_bg” caveat) as super::Gauge::theme; see its doc comment for the full explanation, including why the bar’s own load-colored fill stays outside theme’s role palette, unless overridden separately with StatBar::fill_color.

Call before any manual StatBar::label_style override you want to keep.

Source

pub fn theme_on(self, theme: Theme, bg: Color) -> Self

Same as StatBar::theme, but label_style is drawn on bg instead of theme.panel_bg: for a stat bar drawn directly on a backdrop other than a themed super::Panel/super::Modal’s fill. StatBar::theme is exactly theme_on(theme, theme.panel_bg).

Trait Implementations§

Source§

impl<'a> Clone for StatBar<'a>

Source§

fn clone(&self) -> StatBar<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for StatBar<'a>

Source§

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

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

impl Widget for StatBar<'_>

Source§

fn render(&self, surface: &mut Surface<'_>)

Draw this widget into surface, filling surface.area(). Read more
Source§

impl<'a> Copy for StatBar<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for StatBar<'a>

§

impl<'a> RefUnwindSafe for StatBar<'a>

§

impl<'a> Send for StatBar<'a>

§

impl<'a> Sync for StatBar<'a>

§

impl<'a> Unpin for StatBar<'a>

§

impl<'a> UnsafeUnpin for StatBar<'a>

§

impl<'a> UnwindSafe for StatBar<'a>

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.
§

impl<C> WithAlpha for C
where C: Copy,

§

fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>

Wraps self with alpha, storing alpha before the color in memory (see [AlphaFirst]).
§

fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>

Wraps self with alpha, storing alpha after the color in memory (see [AlphaLast]).