Skip to main content

Value

Enum Value 

Source
pub enum Value {
    Bool(bool),
    Number(f32),
    Choice(usize, String),
    Text(String),
}
Expand description

A value carried by a UI event or stored in a menu item.

Variants§

§

Bool(bool)

Checkbox state.

§

Number(f32)

Slider value.

§

Choice(usize, String)

Selected option of a choice: index and text.

§

Text(String)

Text field contents.

Implementations§

Source§

impl Value

Source

pub fn as_bool(&self) -> bool

The value as bool. Numbers are true when not zero, text when not empty.

Source

pub fn as_f32(&self) -> f32

The value as number (choices give their index, text is parsed, bool gives 0 or 1).

Examples found in repository?
examples/menus.rs (line 70)
50    fn on_ui(&mut self, event: &UiEvent, ctx: &mut Context) {
51        match event {
52            UiEvent::Activated { menu, item } => match (menu.as_str(), item.as_str()) {
53                ("main", "play") => {
54                    let s = ctx.get::<Settings>().expect("settings exist");
55                    let text = format!("Good luck, {}! (this is where your game would start)", s.name);
56                    ctx.ui.message("intro", text);
57                }
58                ("main", "options") => {
59                    let menu = options_menu(ctx.get::<Settings>().expect("settings exist"));
60                    ctx.ui.open(menu);
61                }
62                ("main", "name") => ctx.ui.open(TextInput::new("name", "Enter your name").max_length(20).required()),
63                ("main", "quit") => ctx.ui.open(Confirm::new("quit", "Do you really want to quit?").default_no()),
64                ("options", "back") => ctx.ui.close(),
65                _ => {}
66            },
67            UiEvent::Changed { menu, item, value } if menu == "options" => {
68                let s = ctx.get_mut::<Settings>().expect("settings exist");
69                match item.as_str() {
70                    "volume" => s.volume = value.as_f32() / 100.0,
71                    "difficulty" => s.difficulty = value.as_index(),
72                    "name" => s.name = value.as_str().to_string(),
73                    _ => {}
74                }
75                let volume = s.volume;
76                ctx.audio.set_master_volume(volume);
77            }
78            UiEvent::TextSubmitted { id, text } if id == "name" => {
79                ctx.get_mut::<Settings>().expect("settings exist").name = text.clone();
80                ctx.speech.speak_queued(format!("Hello, {text}"));
81            }
82            UiEvent::Confirmed { id, yes: true } if id == "quit" => ctx.quit(),
83            _ => {}
84        }
85    }
More examples
Hide additional examples
examples/bell_catcher.rs (line 136)
92    fn on_ui(&mut self, event: &UiEvent, ctx: &mut Context) {
93        match event {
94            UiEvent::Activated { menu, item } if menu == "title" => match item.as_str() {
95                "play" => {
96                    ctx.ui.close_all();
97                    ctx.switch_scene(Game::default());
98                }
99                "voice" => match voice_menu(ctx) {
100                    Some(menu) => ctx.ui.open(menu),
101                    None => ctx.ui.message(
102                        "no voices",
103                        "Your screen reader speaks with its own voice. Change it in the screen reader's settings.",
104                    ),
105                },
106                "help" => ctx.ui.message(
107                    "help",
108                    "Bells fall in one of seven lanes in front of you. Listen where a bell rings, \
109                     step under it with left and right, and catch it before it lands. \
110                     The bell rings faster and higher the closer it gets. You have three lives. \
111                     Press S for your score, Escape for the menu.",
112                ),
113                "quit" => ctx.quit(),
114                _ => {}
115            },
116            UiEvent::Activated { menu, item } if menu == "voices" => {
117                if item == "back" {
118                    ctx.ui.close();
119                    return;
120                }
121                if item == "default" {
122                    ctx.speech.reset_voice();
123                    ctx.storage.remove("voice");
124                } else if let Some(voice) =
125                    item.strip_prefix("voice").and_then(|i| i.parse::<usize>().ok()).and_then(|i| ctx.speech.voices().get(i).cloned())
126                    && ctx.speech.set_voice(&voice.id)
127                {
128                    ctx.storage.set("voice", &voice.id);
129                }
130                ctx.ui.close();
131                refresh_title(ctx, "voice");
132                let name = ctx.speech.voice().map_or("Default voice".to_string(), |v| v.name.clone());
133                ctx.speech.speak_queued(format!("{name}. This is how I sound now."));
134            }
135            UiEvent::Changed { menu, item, value } if menu == "title" && item == "rate" => {
136                ctx.speech.set_rate(value.as_f32() / 100.0);
137                ctx.storage.set("rate", value.as_f32());
138            }
139            _ => {}
140        }
141    }
Source

pub fn as_index(&self) -> usize

The value as index (for choices) or rounded number.

Examples found in repository?
examples/menus.rs (line 71)
50    fn on_ui(&mut self, event: &UiEvent, ctx: &mut Context) {
51        match event {
52            UiEvent::Activated { menu, item } => match (menu.as_str(), item.as_str()) {
53                ("main", "play") => {
54                    let s = ctx.get::<Settings>().expect("settings exist");
55                    let text = format!("Good luck, {}! (this is where your game would start)", s.name);
56                    ctx.ui.message("intro", text);
57                }
58                ("main", "options") => {
59                    let menu = options_menu(ctx.get::<Settings>().expect("settings exist"));
60                    ctx.ui.open(menu);
61                }
62                ("main", "name") => ctx.ui.open(TextInput::new("name", "Enter your name").max_length(20).required()),
63                ("main", "quit") => ctx.ui.open(Confirm::new("quit", "Do you really want to quit?").default_no()),
64                ("options", "back") => ctx.ui.close(),
65                _ => {}
66            },
67            UiEvent::Changed { menu, item, value } if menu == "options" => {
68                let s = ctx.get_mut::<Settings>().expect("settings exist");
69                match item.as_str() {
70                    "volume" => s.volume = value.as_f32() / 100.0,
71                    "difficulty" => s.difficulty = value.as_index(),
72                    "name" => s.name = value.as_str().to_string(),
73                    _ => {}
74                }
75                let volume = s.volume;
76                ctx.audio.set_master_volume(volume);
77            }
78            UiEvent::TextSubmitted { id, text } if id == "name" => {
79                ctx.get_mut::<Settings>().expect("settings exist").name = text.clone();
80                ctx.speech.speak_queued(format!("Hello, {text}"));
81            }
82            UiEvent::Confirmed { id, yes: true } if id == "quit" => ctx.quit(),
83            _ => {}
84        }
85    }
Source

pub fn as_str(&self) -> &str

The value as text.

Examples found in repository?
examples/menus.rs (line 72)
50    fn on_ui(&mut self, event: &UiEvent, ctx: &mut Context) {
51        match event {
52            UiEvent::Activated { menu, item } => match (menu.as_str(), item.as_str()) {
53                ("main", "play") => {
54                    let s = ctx.get::<Settings>().expect("settings exist");
55                    let text = format!("Good luck, {}! (this is where your game would start)", s.name);
56                    ctx.ui.message("intro", text);
57                }
58                ("main", "options") => {
59                    let menu = options_menu(ctx.get::<Settings>().expect("settings exist"));
60                    ctx.ui.open(menu);
61                }
62                ("main", "name") => ctx.ui.open(TextInput::new("name", "Enter your name").max_length(20).required()),
63                ("main", "quit") => ctx.ui.open(Confirm::new("quit", "Do you really want to quit?").default_no()),
64                ("options", "back") => ctx.ui.close(),
65                _ => {}
66            },
67            UiEvent::Changed { menu, item, value } if menu == "options" => {
68                let s = ctx.get_mut::<Settings>().expect("settings exist");
69                match item.as_str() {
70                    "volume" => s.volume = value.as_f32() / 100.0,
71                    "difficulty" => s.difficulty = value.as_index(),
72                    "name" => s.name = value.as_str().to_string(),
73                    _ => {}
74                }
75                let volume = s.volume;
76                ctx.audio.set_master_volume(volume);
77            }
78            UiEvent::TextSubmitted { id, text } if id == "name" => {
79                ctx.get_mut::<Settings>().expect("settings exist").name = text.clone();
80                ctx.speech.speak_queued(format!("Hello, {text}"));
81            }
82            UiEvent::Confirmed { id, yes: true } if id == "quit" => ctx.quit(),
83            _ => {}
84        }
85    }
Source

pub fn to_text(&self) -> String

The value formatted for speaking.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Self

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 Value

Source§

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

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

impl PartialEq for Value

Source§

fn eq(&self, other: &Self) -> 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 Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more