make it easy to prefill a textbox from wizard

This commit is contained in:
Dustin Carlino 2018-11-14 11:51:09 -08:00
parent ac1272f660
commit 7937586e52
7 changed files with 33 additions and 15 deletions

View File

@ -113,20 +113,25 @@ pub fn load_ab_test(map: &Map, wizard: &mut WrappedWizard, query: &str) -> Optio
}
pub fn input_tick(wizard: &mut WrappedWizard, query: &str) -> Option<Tick> {
wizard.input_something(query, Box::new(|line| Tick::parse(&line)))
wizard.input_something(query, None, Box::new(|line| Tick::parse(&line)))
}
pub fn input_weighted_usize(
wizard: &mut WrappedWizard,
query: &str,
) -> Option<WeightedUsizeChoice> {
wizard.input_something(query, Box::new(|line| WeightedUsizeChoice::parse(&line)))
wizard.input_something(
query,
None,
Box::new(|line| WeightedUsizeChoice::parse(&line)),
)
}
// TODO Validate the intersection exists? Let them pick it with the cursor?
pub fn choose_intersection(wizard: &mut WrappedWizard, query: &str) -> Option<IntersectionID> {
wizard.input_something(
query,
None,
Box::new(|line| {
usize::from_str_radix(&line, 10)
.ok()

View File

@ -44,6 +44,7 @@ impl Plugin for SearchState {
if input.unimportant_key_pressed(Key::Slash, DEBUG_EXTRA, "start searching") {
new_state = Some(SearchState::EnteringSearch(TextBox::new(
"Search for what?",
None,
)));
}
}

View File

@ -35,7 +35,10 @@ impl Plugin for WarpState {
DEBUG,
"start searching for something to warp to",
) {
new_state = Some(WarpState::EnteringSearch(TextBox::new("Warp to what?")));
new_state = Some(WarpState::EnteringSearch(TextBox::new(
"Warp to what?",
None,
)));
}
}
WarpState::EnteringSearch(tb) => match tb.event(ctx.input) {

View File

@ -14,14 +14,10 @@ pub struct TextBox {
}
impl TextBox {
pub fn new(prompt: &str) -> TextBox {
TextBox::new_prefilled(prompt, String::from(""))
}
pub fn new_prefilled(prompt: &str, line: String) -> TextBox {
pub fn new(prompt: &str, prefilled: Option<String>) -> TextBox {
TextBox {
prompt: prompt.to_string(),
line,
line: prefilled.unwrap_or(String::from("")),
cursor_x: 0,
shift_pressed: false,
}

View File

@ -62,6 +62,7 @@ impl Wizard {
fn input_with_text_box<R: Cloneable>(
&mut self,
query: &str,
prefilled: Option<String>,
input: &mut UserInput,
parser: Box<Fn(String) -> Option<R>>,
) -> Option<R> {
@ -73,7 +74,7 @@ impl Wizard {
}
if self.tb.is_none() {
self.tb = Some(TextBox::new(query));
self.tb = Some(TextBox::new(query, prefilled));
}
match self.tb.as_mut().unwrap().event(input) {
@ -109,6 +110,7 @@ impl<'a> WrappedWizard<'a> {
pub fn input_something<R: 'static + Clone + Cloneable>(
&mut self,
query: &str,
prefilled: Option<String>,
parser: Box<Fn(String) -> Option<R>>,
) -> Option<R> {
if !self.ready_results.is_empty() {
@ -116,7 +118,10 @@ impl<'a> WrappedWizard<'a> {
let item: &R = first.as_any().downcast_ref::<R>().unwrap();
return Some(item.clone());
}
if let Some(obj) = self.wizard.input_with_text_box(query, self.input, parser) {
if let Some(obj) = self
.wizard
.input_with_text_box(query, prefilled, self.input, parser)
{
self.wizard.confirmed_state.push(Box::new(obj.clone()));
Some(obj)
} else {
@ -125,16 +130,21 @@ impl<'a> WrappedWizard<'a> {
}
pub fn input_string(&mut self, query: &str) -> Option<String> {
self.input_something(query, Box::new(|line| Some(line)))
self.input_something(query, None, Box::new(|line| Some(line)))
}
pub fn input_string_prefilled(&mut self, query: &str, prefilled: String) -> Option<String> {
self.input_something(query, Some(prefilled), Box::new(|line| Some(line)))
}
pub fn input_usize(&mut self, query: &str) -> Option<usize> {
self.input_something(query, Box::new(|line| line.parse::<usize>().ok()))
self.input_something(query, None, Box::new(|line| line.parse::<usize>().ok()))
}
pub fn input_percent(&mut self, query: &str) -> Option<f64> {
self.input_something(
query,
None,
Box::new(|line| {
line.parse::<f64>().ok().and_then(|num| {
if num >= 0.0 && num <= 1.0 {

View File

@ -84,8 +84,7 @@ impl GUI for UI {
State::EditingRoad(id, ref mut wizard) => {
if let Some(s) = wizard
.wrap(&mut input)
// TODO prefill
.input_string("Specify the lanes")
.input_string_prefilled("Specify the lanes", self.model.get_lanes(id))
{
self.model.edit_lanes(id, s);
new_state = Some(State::Viewing);

View File

@ -247,6 +247,10 @@ impl Model {
}
None
}
pub fn get_lanes(&self, id: RoadID) -> String {
self.roads[&id].lanes.to_string()
}
}
impl Model {