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: StringThe 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
impl Menu
Sourcepub fn new(title: impl Into<String>, items: Vec<MenuItem>) -> Menu
pub fn new(title: impl Into<String>, items: Vec<MenuItem>) -> Menu
A menu with a title and its items, the first selected.
Sourcepub fn set_stylesheet(
&mut self,
css: Option<&str>,
) -> Result<(), Vec<Diagnostic>>
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.
Sourcepub const fn selected(&self) -> usize
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.
Sourcepub fn selected_item(&self) -> Option<&MenuItem>
pub fn selected_item(&self) -> Option<&MenuItem>
The selected item, or None when there are none.
Sourcepub fn select_next(&mut self)
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.
Sourcepub fn select_previous(&mut self)
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.
Sourcepub fn select_id(&mut self, id: u64) -> bool
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.
Sourcepub const fn press(&mut self, down: bool)
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.
Sourcepub fn activate(&mut self) -> Option<u64>
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.
Sourcepub const fn clear_input(&mut self)
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.
Sourcepub fn slider(&self, id: u64) -> Option<f32>
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.
Sourcepub fn set_slider(&mut self, id: u64, position: f32) -> bool
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.
Sourcepub fn nudge_slider(&mut self, forward: bool) -> bool
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.
Sourcepub fn slider_highlighted(&self) -> bool
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.
Sourcepub fn cycler(&self, id: u64) -> Option<usize>
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.
Sourcepub fn set_cycler(&mut self, id: u64, chosen: usize) -> bool
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.
Sourcepub fn nudge_cycler(&mut self, forward: bool) -> bool
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.
Sourcepub fn cycler_highlighted(&self) -> bool
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.
Sourcepub fn set_item_hint(&mut self, id: u64, hint: impl Into<String>) -> bool
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.
Sourcepub fn state(&self, index: usize) -> ButtonState
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.
Sourcepub fn point(
&mut self,
layout: &MenuLayout,
ui: &mut UiState,
pointer: PointerInput,
) -> Option<u64>
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.
Sourcepub fn scroll_wheel(
&mut self,
layout: &MenuLayout,
pointer: Vec2,
delta: Vec2,
) -> bool
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.
Sourcepub fn layout(&self, extent: (u32, u32), atlas: &FontAtlas) -> MenuLayout
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.
Sourcepub fn layout_with(
&self,
extent: (u32, u32),
atlas: &FontAtlas,
style: &MenuStyle,
) -> MenuLayout
pub fn layout_with( &self, extent: (u32, u32), atlas: &FontAtlas, style: &MenuStyle, ) -> MenuLayout
Lays this menu out centred, at a style the caller chose.
Sourcepub fn layout_with_font(
&self,
extent: (u32, u32),
style: &MenuStyle,
font: &Font,
) -> MenuLayout
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.
Sourcepub fn layout_with_font_fitted(
&self,
extent: (u32, u32),
style: &MenuStyle,
font: &Font,
) -> Result<MenuLayout, MenuFitError>
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.
Sourcepub fn panel_size(&self, atlas: &FontAtlas, style: &MenuStyle) -> Vec2
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.
Sourcepub fn render_art(
&self,
dl: &mut DrawList,
layout: &MenuLayout,
skin: &MenuSkin,
)
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.
Sourcepub fn render(&self, dl: &mut DrawList, layout: &MenuLayout, skin: &MenuSkin)
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.