spawn submenus... but not in the right place. no input wiring yet.

This commit is contained in:
Dustin Carlino 2018-12-16 19:51:37 -08:00
parent 7c5ba61e89
commit b571f410ef
2 changed files with 24 additions and 2 deletions

View File

@ -90,7 +90,7 @@ pub fn run<T, G: GUI<T>>(mut gui: G, window_title: &str, initial_width: u32, ini
gui.get_mut_canvas(), gui.get_mut_canvas(),
); );
if let Some(ref mut menu) = top_menu { if let Some(ref mut menu) = top_menu {
menu.event(&mut input); menu.event(&mut input, gui.get_mut_canvas());
} }
let (new_event_mode, data) = let (new_event_mode, data) =
match panic::catch_unwind(panic::AssertUnwindSafe(|| gui.event(&mut input))) { match panic::catch_unwind(panic::AssertUnwindSafe(|| gui.event(&mut input))) {

View File

@ -1,3 +1,4 @@
use crate::menu::Menu;
use crate::text::LINE_HEIGHT; use crate::text::LINE_HEIGHT;
use crate::{Canvas, Color, GfxCtx, Key, Text, UserInput}; use crate::{Canvas, Color, GfxCtx, Key, Text, UserInput};
use geom::{Polygon, Pt2D}; use geom::{Polygon, Pt2D};
@ -9,6 +10,7 @@ pub struct TopMenu {
txt: Text, txt: Text,
highlighted: Option<usize>, highlighted: Option<usize>,
submenu: Option<Menu<Key>>,
} }
impl TopMenu { impl TopMenu {
@ -46,16 +48,32 @@ impl TopMenu {
folders, folders,
txt, txt,
highlighted: None, highlighted: None,
submenu: None,
} }
} }
pub fn event(&mut self, input: &mut UserInput) { pub fn event(&mut self, input: &mut UserInput, canvas: &Canvas) {
if let Some(cursor) = input.get_moved_mouse() { if let Some(cursor) = input.get_moved_mouse() {
// TODO Could quickly filter out by checking y // TODO Could quickly filter out by checking y
self.highlighted = self self.highlighted = self
.folders .folders
.iter() .iter()
.position(|f| f.rectangle.contains(cursor)); .position(|f| f.rectangle.contains(cursor));
return;
}
if self.highlighted.is_some() && input.left_mouse_button_pressed() {
self.submenu = Some(Menu::new(
None,
self.folders[self.highlighted.unwrap()]
.actions
.iter()
.map(|(key, action)| (Some(*key), action.to_string(), *key))
.collect(),
false,
canvas.get_cursor_in_map_space(),
canvas,
));
} }
} }
@ -80,6 +98,10 @@ impl TopMenu {
g.unfork(old_ctx); g.unfork(old_ctx);
canvas.draw_text_at_screenspace_topleft(g, self.txt.clone(), (0.0, 0.0)); canvas.draw_text_at_screenspace_topleft(g, self.txt.clone(), (0.0, 0.0));
if let Some(ref menu) = self.submenu {
menu.draw(g, canvas);
}
} }
} }