textbox and menu take UserInput and consume it

This commit is contained in:
Dustin Carlino 2018-09-20 19:14:57 -07:00
parent 4a9306e1e2
commit b66e6891f6
8 changed files with 53 additions and 68 deletions

View File

@ -38,7 +38,7 @@ impl ColorPicker {
}
}
ColorPicker::Choosing(ref mut menu) => {
match menu.event(input.use_event_directly()) {
match menu.event(input) {
InputResult::Canceled => {
new_state = Some(ColorPicker::Inactive);
}

View File

@ -103,30 +103,27 @@ impl DrawPolygonState {
));
}
}
DrawPolygonState::NamingPolygon(tb, pts) => {
match tb.event(input.use_event_directly()) {
InputResult::Canceled => {
println!("Never mind!");
new_state = Some(DrawPolygonState::Empty);
}
InputResult::Done(name) => {
let path = format!("../data/polygons/{}/{}", map.get_name(), name);
abstutil::write_json(
&path,
&polygons::PolygonSelection {
name,
points: pts.clone(),
},
).expect("Saving polygon selection failed");
println!("Saved {}", path);
new_state = Some(DrawPolygonState::Empty);
}
InputResult::StillActive => {}
DrawPolygonState::NamingPolygon(tb, pts) => match tb.event(input) {
InputResult::Canceled => {
println!("Never mind!");
new_state = Some(DrawPolygonState::Empty);
}
input.consume_event();
}
InputResult::Done(name) => {
let path = format!("../data/polygons/{}/{}", map.get_name(), name);
abstutil::write_json(
&path,
&polygons::PolygonSelection {
name,
points: pts.clone(),
},
).expect("Saving polygon selection failed");
println!("Saved {}", path);
new_state = Some(DrawPolygonState::Empty);
}
InputResult::StillActive => {}
},
DrawPolygonState::ListingPolygons(ref mut menu, polygons) => {
match menu.event(input.use_event_directly()) {
match menu.event(input) {
InputResult::Canceled => {
new_state = Some(DrawPolygonState::Empty);
}

View File

@ -36,18 +36,15 @@ impl SearchState {
)));
}
}
SearchState::EnteringSearch(tb) => {
match tb.event(input.use_event_directly()) {
InputResult::Canceled => {
new_state = Some(SearchState::Empty);
}
InputResult::Done(filter) => {
new_state = Some(SearchState::FilterOSM(filter));
}
InputResult::StillActive => {}
SearchState::EnteringSearch(tb) => match tb.event(input) {
InputResult::Canceled => {
new_state = Some(SearchState::Empty);
}
input.consume_event();
}
InputResult::Done(filter) => {
new_state = Some(SearchState::FilterOSM(filter));
}
InputResult::StillActive => {}
},
SearchState::FilterOSM(filter) => {
if input.key_pressed(
Key::Return,

View File

@ -29,19 +29,16 @@ impl WarpState {
new_state = Some(WarpState::EnteringSearch(TextBox::new("Warp to what?")));
}
}
WarpState::EnteringSearch(tb) => {
match tb.event(input.use_event_directly()) {
InputResult::Canceled => {
new_state = Some(WarpState::Empty);
}
InputResult::Done(to) => {
warp(to, map, sim, canvas, selected);
new_state = Some(WarpState::Empty);
}
InputResult::StillActive => {}
WarpState::EnteringSearch(tb) => match tb.event(input) {
InputResult::Canceled => {
new_state = Some(WarpState::Empty);
}
input.consume_event();
}
InputResult::Done(to) => {
warp(to, map, sim, canvas, selected);
new_state = Some(WarpState::Empty);
}
InputResult::StillActive => {}
},
};
if let Some(s) = new_state {
*self = s;

View File

@ -166,13 +166,7 @@ impl Wizard {
self.menu = Some(Menu::new(choices));
}
let result = self
.menu
.as_mut()
.unwrap()
.event(input.use_event_directly());
input.consume_event();
match result {
match self.menu.as_mut().unwrap().event(input) {
InputResult::Canceled => {
self.menu = None;
self.alive = false;
@ -209,18 +203,13 @@ impl Wizard {
self.tb = Some(TextBox::new(query));
}
match self.tb.as_mut().unwrap().event(input.use_event_directly()) {
InputResult::StillActive => {
input.consume_event();
None
}
match self.tb.as_mut().unwrap().event(input) {
InputResult::StillActive => None,
InputResult::Canceled => {
input.consume_event();
self.alive = false;
None
}
InputResult::Done(line) => {
input.consume_event();
self.tb = None;
if let Some(result) = parser(line.clone()) {
Some(result)

View File

@ -151,7 +151,7 @@ impl UserInput {
}
// Should only be called publicly after using event directly
pub fn consume_event(&mut self) {
pub(crate) fn consume_event(&mut self) {
assert!(!self.event_consumed);
self.event_consumed = true;
}

View File

@ -1,5 +1,5 @@
use piston::input::{Button, Event, Key, PressEvent};
use {InputResult, TextOSD};
use piston::input::{Button, Key, PressEvent};
use {InputResult, TextOSD, UserInput};
pub struct Menu {
choices: Vec<String>,
@ -14,8 +14,10 @@ impl Menu {
}
}
// TODO take UserInput
pub fn event(&mut self, ev: &Event) -> InputResult {
pub fn event(&mut self, input: &mut UserInput) -> InputResult {
let ev = input.use_event_directly().clone();
input.consume_event();
if let Some(Button::Keyboard(Key::Escape)) = ev.press_args() {
return InputResult::Canceled;
}

View File

@ -1,6 +1,6 @@
use keys::key_to_char;
use piston::input::{Button, ButtonEvent, Event, Key, PressEvent, ReleaseEvent};
use {Canvas, GfxCtx, InputResult, TextOSD};
use piston::input::{Button, ButtonEvent, Key, PressEvent, ReleaseEvent};
use {Canvas, GfxCtx, InputResult, TextOSD, UserInput};
// TODO right now, only a single line
@ -34,7 +34,10 @@ impl TextBox {
canvas.draw_centered_text(g, osd);
}
pub fn event(&mut self, ev: &Event) -> InputResult {
pub fn event(&mut self, input: &mut UserInput) -> InputResult {
let ev = input.use_event_directly().clone();
input.consume_event();
if let Some(Button::Keyboard(Key::Escape)) = ev.press_args() {
return InputResult::Canceled;
}