From b571f410efe52ebf6665adbce26e2274cbbd24a9 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sun, 16 Dec 2018 19:51:37 -0800 Subject: [PATCH] spawn submenus... but not in the right place. no input wiring yet. --- ezgui/src/runner.rs | 2 +- ezgui/src/top_menu.rs | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ezgui/src/runner.rs b/ezgui/src/runner.rs index 8c793d96f5..a556ce8f0d 100644 --- a/ezgui/src/runner.rs +++ b/ezgui/src/runner.rs @@ -90,7 +90,7 @@ pub fn run>(mut gui: G, window_title: &str, initial_width: u32, ini gui.get_mut_canvas(), ); 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) = match panic::catch_unwind(panic::AssertUnwindSafe(|| gui.event(&mut input))) { diff --git a/ezgui/src/top_menu.rs b/ezgui/src/top_menu.rs index 3e969bdba0..360030bc21 100644 --- a/ezgui/src/top_menu.rs +++ b/ezgui/src/top_menu.rs @@ -1,3 +1,4 @@ +use crate::menu::Menu; use crate::text::LINE_HEIGHT; use crate::{Canvas, Color, GfxCtx, Key, Text, UserInput}; use geom::{Polygon, Pt2D}; @@ -9,6 +10,7 @@ pub struct TopMenu { txt: Text, highlighted: Option, + submenu: Option>, } impl TopMenu { @@ -46,16 +48,32 @@ impl TopMenu { folders, txt, 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() { // TODO Could quickly filter out by checking y self.highlighted = self .folders .iter() .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); 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); + } } }