Skip to main content

Menu

Struct Menu 

Source
pub struct Menu {
    pub title: String,
    pub subtitle: Vec<Caption>,
    /* private fields */
}
Expand description

Re-exported for the same reason: menu_skin returns a MenuSkin, and a caller that can name this crate’s menu art should not have to add a second dependency to lay out and draw the menu it skins. It is also what lets an end-to-end suite that depends on this crate take a golden image of the real menu rather than of a replica. A modal menu: a title, some items, and which one is selected.

Retained across frames — unlike the rest of this crate — because a selection is state by definition, and an immediate-mode menu whose highlight was recomputed from nothing every frame would have no keyboard at all.

Fields§

§title: String

The heading, drawn above the items.

§subtitle: Vec<Caption>

Lines drawn under the title, centred and each in its CaptionTone’s colour — see the module’s Captions. Empty by default, which draws exactly the menu there was before captions existed.

Implementations§

Source§

impl Menu

Source

pub fn new(title: impl Into<String>, items: Vec<MenuItem>) -> Menu

A menu with a title and its items, the first selected.

Source

pub fn set_stylesheet( &mut self, css: Option<&str>, ) -> Result<(), Vec<Diagnostic>>

Draws this menu with css over default.css’s menu rules, or with those rules alone for None.

The sheet cascades as an app sheet does: after default.css, whatever either’s selectors, and under what the menu sets inline — every length, borders’ widths among them, which are MenuStyle’s: a flat 1 px border is a style whose MenuStyle::panel and MenuStyle::button are 1 px, laid out with Menu::layout_with. So a sheet sets the colours, the frames (border-image: none drops one, for a flat background-color and border-color in its place) and the text’s family, and it is used for measuring as well as drawing, so a layout and the render it feeds agree. MenuStyle’s colours keep restating default.css’s, not this sheet’s.

§Errors

The sheet’s errors, when it has any; the menu keeps the sheet it had.

Source

pub fn items(&self) -> &[MenuItem]

The items, in the order they are drawn.

Source

pub const fn selected(&self) -> usize

Which item is selected. Zero for an empty menu, which has no items to index and never activates one.

Source

pub fn selected_item(&self) -> Option<&MenuItem>

The selected item, or None when there are none.

Source

pub fn select_next(&mut self)

Moves the selection down one, wrapping at the end.

A scrolled list — see Menu::layout_with_font_fitted — is scrolled to the new selection by the next layout, by as little as brings it wholly into view. “As little” is measured from the scroll the menu holds, which Menu::point and Menu::scroll_wheel keep up to date: a caller that calls neither still always sees the selection, but held at the edge it scrolled past rather than moving inside a still list.

Source

pub fn select_previous(&mut self)

Moves the selection up one, wrapping at the start. A scrolled list follows it, as Menu::select_next says.

Source

pub fn select_id(&mut self, id: u64) -> bool

Selects the item with this id, if it is in the menu.

Returns whether it was found — a caller restoring a selection across a rebuild wants to know when the item it remembered is gone.

Source

pub const fn press(&mut self, down: bool)

Holds the highlighted item down, or lets it up.

Called from the key-down and key-up of whatever key commits — the visible half of “the button is being pressed”. Activation is Menu::activate; this only changes what is drawn.

Source

pub fn activate(&mut self) -> Option<u64>

Fires the highlighted item and returns its id.

The highlighted one, which is the hovered item when a pointer is over one and the selected item otherwise — so Enter commits what the player can see is about to happen, whichever device put the highlight there. A MenuItemKind::Slider row reports nothing: there is no press of a commit key that means anything to a value, and an id fired from one would reach the game’s action table as if a button had been clicked. A MenuItemKind::Cycler row reports nothing for the same reason, and the press steps it forward, round the end — the one-key walk of the whole list the caller reads back with Menu::cycler.

Source

pub const fn clear_input(&mut self)

Drops any hover and any held key.

For a menu being taken off the screen: a menu re-shown with a stale Pressed on it draws a button nobody is touching.

Source

pub fn slider(&self, id: u64) -> Option<f32>

Where the handle of the slider row named id sits, in 0.0..=1.0.

None for an id this menu has no row for, and for a row that is a button rather than a slider.

Source

pub fn set_slider(&mut self, id: u64, position: f32) -> bool

Moves the handle of the slider row named id, and reports whether it moved.

Refused while the player is dragging that row, which is what lets a caller push the value back in unconditionally every frame: the state the caller is mirroring came from this handle in the first place, so the drag is the newer of the two and writing over it would pin the handle under the cursor.

So false means one of three things — no such row, not a slider, or the player has hold of it — and a caller that needs to tell them apart asks Menu::slider first.

Source

pub fn nudge_slider(&mut self, forward: bool) -> bool

Moves the highlighted slider one Slider::KEY_STEP, and reports whether a handle moved.

The keyboard’s half of a slider, and until it existed there was no such half: Menu::activate reports nothing for a slider row by design, so a player with no pointer could select a volume and then had no way to change it. This module’s own opening says the keyboard is the primary input; a row that only a mouse could work was that claim being false in the one place it is most visible.

The highlighted row, on Menu::activate’s terms — the hovered one when a pointer is over one, the selected one otherwise — so the key moves what the player can see is about to move.

false means nothing moved: the row is a button, there is no row, the handle is already at that end of the groove, or the pointer has hold of it. The last is Menu::set_slider’s rule and it is here for the same reason: a drag is the newer of the two inputs.

Source

pub fn slider_highlighted(&self) -> bool

Whether the highlighted row is a slider.

What a caller deciding whether a key belongs to the menu has to ask before offering it: nudge_slider reports whether a handle moved, which is also false at the end of the groove, and a key claimed on that would be handed back to the game halfway through a player holding it down.

Source

pub fn cycler(&self, id: u64) -> Option<usize>

Which choice the cycler row named id holds: an index into the caller’s list.

None for an id this menu has no row for, and for a row that is not a cycler.

Source

pub fn set_cycler(&mut self, id: u64, chosen: usize) -> bool

Picks the choice of the cycler row named id, clamped to its last, and reports whether there was such a row.

Nothing refuses it: a cycler is never dragged, so the caller’s write is always the newer of the two inputs and mirroring a value back in every frame is safe — the row it mirrors into is the row it read.

Source

pub fn nudge_cycler(&mut self, forward: bool) -> bool

Steps the highlighted cycler one choice, stopping at the ends, and reports whether the choice changed.

The arrows’ half of a cycler, beside Menu::nudge_slider: the same keys, the same highlighted row, and the same false at the end of the list that a slider gives at the end of its groove — a key held against the last choice does not come round to the first, because a player holding Right to reach the top of a list would overshoot it. The commit key is the one that goes round; see Menu::activate.

Source

pub fn cycler_highlighted(&self) -> bool

Whether the highlighted row is a cycler — slider_highlighted’s question for the other row the arrows drive, and asked for the same reason.

Source

pub fn set_item_hint(&mut self, id: u64, hint: impl Into<String>) -> bool

The value text a slider row draws, replaced.

Separate from Menu::set_slider because the two move for different reasons: the handle is refused mid-drag and the caption never is — a player dragging the exposure is precisely who needs to read the number it has reached.

Source

pub fn state(&self, index: usize) -> ButtonState

How item index is drawn.

ButtonState::Pressed for the item the current press belongs to — the pointer’s captured item, or the selected one under the keyboard — ButtonState::Hovered for the highlighted item otherwise, and ButtonState::Idle for everything else — which is what makes a keyboard-only player see the same three frames of art a mouse would light up.

Source

pub fn point( &mut self, layout: &MenuLayout, ui: &mut UiState, pointer: PointerInput, ) -> Option<u64>

Runs one frame of pointer input against layout, and reports a click.

Entirely optional: a game that never calls this is driven by the keyboard alone and every item stays at the state Menu::state derives from the selection. A game that does call it gets hover, press capture through ui — so a press that starts on one item and is released over another fires neither — and a click that also moves the selection, so the keyboard picks up where the mouse left off.

In a scrolled list only the part of a row inside the list’s viewport is hit, and a row scrolled wholly out of it is not hit at all. While the list follows the keyboard, the scroll layout settled on is kept as the one the next keyboard move scrolls from — see Menu::select_next.

Source

pub fn scroll_wheel( &mut self, layout: &MenuLayout, pointer: Vec2, delta: Vec2, ) -> bool

Scrolls a scrolled item list by a wheel’s delta, in pixels, and says whether it moved: positive y moves toward the end of the list, as Ui::scroll_wheel reads a wheel. x is ignored — the list scrolls up and down only.

Only while pointer is over the panel, since the list is the one thing in it that moves. The list stops following the keyboard’s selection until the keyboard next moves it, so the selection may be scrolled out of view — and the scroll is kept to the fraction of a pixel, so a trackpad’s small steps add up, while each layout draws it at the whole pixel nearest. false for a list that does not scroll, so the caller can give the wheel to something else.

A separate call rather than a PointerInput field, as Ui::scroll_wheel is, because every caller builds that by literal.

Source

pub fn layout(&self, extent: (u32, u32), atlas: &FontAtlas) -> MenuLayout

Lays this menu out centred in an extent-sized framebuffer, at the largest scale that fits.

The scale is a pure function of the extent and the menu’s own contents: the largest whole number up to MenuStyle::MAX_SCALE whose panel fits inside FIT_FRACTION of the framebuffer on both axes, and 1 when none of them do. Whole numbers because the art is pixel art; the fit because a menu that overflowed a 1440×400 canvas would have its buttons off the bottom of the screen, which is the failure this replaces.

The panel is measured once, at scale 1, and each candidate is that size times the scale: every length MenuStyle::pixel_art makes and every glyph advance is a whole number times the scale, so the tree lays the panel out exactly proportionally — the_panel_grows_exactly_with_the_scale holds that — and one layout answers every candidate.

Source

pub fn layout_with( &self, extent: (u32, u32), atlas: &FontAtlas, style: &MenuStyle, ) -> MenuLayout

Lays this menu out centred, at a style the caller chose.

Source

pub fn layout_with_font( &self, extent: (u32, u32), style: &MenuStyle, font: &Font, ) -> MenuLayout

Lays this menu out centred at style, with every line of its text measured in font rather than the built-in FontAtlas — and drawn in it too, since Menu::render draws in the font the layout it is given was measured in.

The text is a registered font’s in the menu’s own tree, so it is measured by the tree’s one text path, as any span in a registered font is: font-size is MenuStyle’s, and the line pitch is the font’s line-height: normal. Menu::layout’s scale fit has no counterpart here — it counts on the bitmap font’s advances growing exactly with the scale, which a parsed font’s do not — so style is the caller’s; see Menu::layout_with_font_fitted for a fit that measures instead. A cycler’s chevrons keep its caption still only in a font whose <, > and space advance alike.

Source

pub fn layout_with_font_fitted( &self, extent: (u32, u32), style: &MenuStyle, font: &Font, ) -> Result<MenuLayout, MenuFitError>

Lays this menu out centred in font, as Menu::layout_with_font does, at the largest size up to style that fits the framebuffer — shrinking it first and scrolling its item list after, as the module’s Fitting a small window, then scrolling describes.

Fits is Menu::layout’s word: inside FIT_FRACTION of the framebuffer on both axes. style is the largest the menu is drawn at, and is what it is drawn at when that fits. Otherwise the item font steps down by FIT_FONT_STEP to MenuStyle::MIN_FONT_SIZE — or stays at style’s own, if that is smaller — and every other length shrinks with it by the same factor, the art to the whole-number scale the factor leaves and never below one. The step is found by bisection over the steps, which is the largest that fits so long as a smaller step never lays out a bigger panel — every length shrinks with the font, so only a rounded pixel could. If even the smallest step is too tall, the item list at that step is capped at the height the rest of the panel leaves, and scrolls: see MenuLayout::viewport.

Every length but the font sizes is rounded to a whole pixel, and each row’s line pitch is the font’s line-height: normal rounded up to one, so every row is a whole number of pixels tall — which is what lets a scrolled list draw only the rows in view where this layout put them. A style that fits is therefore drawn at exactly its own lengths only when they are whole, as MenuStyle::pixel_art’s are.

A pure function of the menu — its contents, its selection and its scroll — and its arguments: the same inputs lay out the same menu.

§Errors

MenuFitError::TooWide when the panel is wider than the room even at the smallest step, which scrolling up and down cannot help, and MenuFitError::TooShort when the frame, the title and the captions leave less height than the list’s tallest row — or no list at all.

Source

pub fn panel_size(&self, atlas: &FontAtlas, style: &MenuStyle) -> Vec2

The panel’s size at style, before it is placed anywhere.

Public because the fit half of Menu::layout is a question a caller may reasonably ask on its own (“would this menu fit at scale 3?”), and because a test that could not measure the panel without placing it could not tell a menu that grew from one that moved.

Source

pub fn render_art( &self, dl: &mut DrawList, layout: &MenuLayout, skin: &MenuSkin, )

Emits the menu’s pictures alone: the scrim over the framebuffer, the window frame, and each button’s frame for its state.

The half of render that is art: the same tree, with every command that is not a picture left out — the text, and a slider’s groove and handle. The frames are nine-sliced at MenuStyle::scale pixels per texel, which is what keeps the art’s corners the layout’s corners.

Source

pub fn render(&self, dl: &mut DrawList, layout: &MenuLayout, skin: &MenuSkin)

Emits the whole menu into dl: the tree Menu::layout_with measured at layout’s style and framebuffer, drawn with skin. Its text is measured in the font layout was — the built-in FontAtlas, or the font Menu::layout_with_font was given — so it lands where layout says.

The order is the tree’s paint order: the scrim first, then the window frame and the title, then each row — its frame, then what sits on it. The draw list is drawn in the order commands went in, so the scrim dims what was pushed before this call, the frame covers the scrim, and every label is drawn over its own row’s frame; rows do not overlap, so no frame covers another row’s text.

Trait Implementations§

Source§

impl Clone for Menu

Source§

fn clone(&self) -> Menu

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 Debug for Menu

Source§

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

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

impl Default for Menu

Source§

fn default() -> Menu

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Menu

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Menu

Auto Trait Implementations§

§

impl Forget for Menu

§

impl Freeze for Menu

§

impl RefUnwindSafe for Menu

§

impl Send for Menu

§

impl Sync for Menu

§

impl Unpin for Menu

§

impl UnsafeUnpin for Menu

§

impl UnwindSafe for Menu

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> HalThreadSafe for T
where T: Send + Sync,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.