use the action list to populate a menu in InfoPanel. cant choose the actions yet there.

This commit is contained in:
Dustin Carlino 2019-12-11 15:25:43 -08:00
parent 061aed2624
commit ec40780ee3
10 changed files with 57 additions and 25 deletions

View File

@ -34,9 +34,9 @@ struct Choice {
}
impl ModalMenu {
pub fn new<S: Into<String>>(
title: S,
raw_choices: Vec<(Option<MultiKey>, &str)>,
pub fn new<S1: Into<String>, S2: Into<String>>(
title: S1,
raw_choices: Vec<(Option<MultiKey>, S2)>,
ctx: &EventCtx,
) -> ModalMenu {
let mut m = ModalMenu {
@ -47,7 +47,7 @@ impl ModalMenu {
.into_iter()
.map(|(hotkey, label)| Choice {
hotkey,
label: label.to_string(),
label: label.into(),
active: false,
})
.collect(),

View File

@ -112,9 +112,6 @@ impl State for ABTestMode {
if ctx.redo_mouseover() {
ui.recalculate_current_selection(ctx);
}
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
// TODO Confirm first
if self.general_tools.action("quit") {
@ -199,6 +196,10 @@ impl State for ABTestMode {
self.recalculate_stuff(ui, ctx);
}*/
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
Transition::Keep
}

View File

@ -12,13 +12,22 @@ use std::collections::BTreeMap;
pub struct InfoPanel {
txt: Text,
menu: ModalMenu,
actions: Vec<String>,
}
impl InfoPanel {
pub fn new(id: ID, ui: &UI, ctx: &EventCtx) -> InfoPanel {
let mut menu_entries = vec![(hotkey(Key::Escape), "quit".to_string())];
let mut actions = Vec::new();
for (key, label) in ui.per_obj.consume() {
actions.push(label.clone());
menu_entries.push((hotkey(key), label));
}
InfoPanel {
txt: info_for(id, ui, ctx),
menu: ModalMenu::new("Info Panel", vec![(hotkey(Key::Escape), "quit")], ctx),
menu: ModalMenu::new("Info Panel", menu_entries, ctx),
actions,
}
}
}

View File

@ -52,6 +52,7 @@ impl CommonState {
}
}
// This has to be called after anything that calls ui.per_obj.action(). Oof.
pub fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Option<Transition> {
self.location_tools.event(ctx);

View File

@ -116,9 +116,6 @@ impl State for DebugMode {
self.save_tools.event(ctx);
ctx.canvas.handle_event(ctx.input);
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
self.associated.event(ui);
if self.general_tools.action("return to previous mode") {
@ -295,6 +292,10 @@ impl State for DebugMode {
return Transition::Push(floodfiller);
}
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
Transition::Keep
}

View File

@ -157,10 +157,6 @@ impl State for EditMode {
}
}
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
if ui.opts.dev && ctx.input.new_was_pressed(lctrl(Key::D).unwrap()) {
return Transition::Push(Box::new(DebugMode::new(ctx)));
}
@ -255,6 +251,10 @@ impl State for EditMode {
apply_map_edits(&mut ui.primary, &ui.cs, ctx, edits);
}
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
Transition::Keep
}

View File

@ -37,6 +37,8 @@ impl Game {
impl GUI for Game {
fn event(&mut self, ctx: &mut EventCtx) -> EventLoopMode {
self.ui.per_obj.reset();
// First rewrite the transitions to explicitly have EventLoopMode, to avoid duplicated
// code.
let transition = match self.states.last_mut().unwrap().event(ctx, &mut self.ui) {

View File

@ -172,10 +172,6 @@ impl State for ScenarioManager {
if ctx.redo_mouseover() {
ui.recalculate_current_selection(ctx);
}
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
if self.general_tools.action("quit") {
return Transition::Pop;
} else if self.general_tools.action("options") {
@ -254,6 +250,10 @@ impl State for ScenarioManager {
}
}
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
Transition::Keep
}

View File

@ -1,13 +1,29 @@
use ezgui::{EventCtx, Key};
use std::cell::RefCell;
pub struct PerObjectActions {}
pub struct PerObjectActions {
actions: RefCell<Vec<(Key, String)>>,
}
impl PerObjectActions {
pub fn new() -> PerObjectActions {
PerObjectActions {}
PerObjectActions {
actions: RefCell::new(Vec::new()),
}
}
// &self to avoid changing lots of code that previously took &UI
pub fn action<S: Into<String>>(&self, ctx: &mut EventCtx, key: Key, label: S) -> bool {
ctx.input.contextual_action(key, label)
let lbl = label.into();
self.actions.borrow_mut().push((key, lbl.clone()));
ctx.input.contextual_action(key, lbl)
}
pub fn consume(&self) -> Vec<(Key, String)> {
std::mem::replace(&mut self.actions.borrow_mut(), Vec::new())
}
pub fn reset(&mut self) {
self.actions = RefCell::new(Vec::new());
}
}

View File

@ -110,9 +110,6 @@ impl State for SandboxMode {
if ctx.redo_mouseover() {
ui.recalculate_current_selection(ctx);
}
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
if let Some(t) = self
.overlay
.event(ctx, ui, &mut self.info_tools, &self.gameplay.prebaked)
@ -257,6 +254,11 @@ impl State for SandboxMode {
ui.primary.clear_sim();
return Transition::Replace(Box::new(EditMode::new(ctx, self.gameplay.mode.clone())));
}
if let Some(t) = self.common.event(ctx, ui) {
return t;
}
if self.speed.is_paused() {
Transition::Keep
} else {