} /* draw_header() */ /*********************************************************** * * Method: * Game1 * * Description: * Constructor. * ***********************************************************/ public Game1(ms_game model, ms_controller ctlr) { /*---------------------------------------------------------- * Initialize graphics * ----------------------------------------------------------*/ graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; dims = new ms_gui_dimension(); mouse = new ms_mouse_state(); toolbar = new toolbar_type(); game_menu = new menu_type("Game"); game_menu.add_item("New Game", new_game_same); game_menu.add_item("Beginner", new_game_beginner); game_menu.add_item("Intermediate", new_game_intermediate); game_menu.add_item("Expert", new_game_expert); game_menu.add_item("Exit", Exit); /*---------------------------------------------------------- * Save the game model and controller * ----------------------------------------------------------*/ ms_model = model; ms_ctlr = ctlr; /*---------------------------------------------------------- * Start new game * ----------------------------------------------------------*/ new_game(9, 9, 10); } /* Game1() */
} /* draw() */ /*********************************************************** * * Method: * add_menu * * Description: * Adds menu to the toolbar. * ***********************************************************/ public void add_menu(menu_type menu) { /*---------------------------------------------------------- * Local variable * ----------------------------------------------------------*/ Vector2 origin; /*---------------------------------------------------------- * Determine origin point of menu * ----------------------------------------------------------*/ if (menus.Count > 0) { menu_type m = menus[menus.Count - 1]; origin = new Vector2(m.dims.toolbar_loc.Right, m.dims.toolbar_loc.Top); } else { origin = new Vector2(location.X, location.Y); } /*---------------------------------------------------------- * Set the menu dimensions * ----------------------------------------------------------*/ menu_dimension_type d = new menu_dimension_type(font, menu, origin, location.Height); menu.dims = d; /*---------------------------------------------------------- * Add the menu to the toolbar * ----------------------------------------------------------*/ menus.Add(menu); } /* add_menu() */
/*-------------------------------------------------------------------- * METHODS * --------------------------------------------------------------------*/ /*********************************************************** * * Method: * toolbar_type * * Description: * Constructor. * ***********************************************************/ public toolbar_type() { /*---------------------------------------------------------- * Initialize * ----------------------------------------------------------*/ menus = new List <menu_type>(); open_menu = null; highlight_idx = -1; h_color = new Color(179, 215, 243); hb_color = new Color(0, 120, 215); mouse = new mouse_state_type(); } /* toolbar_type() */
/*-------------------------------------------------------------------- * METHODS * --------------------------------------------------------------------*/ /*********************************************************** * * Method: * menu_dimension_type * * Description: * Constructor. * ***********************************************************/ public menu_dimension_type(SpriteFont f, menu_type menu, Vector2 origin, int menu_height) { item_str_locs = new Vector2[menu.items.Count]; item_hglt_locs = new Rectangle[menu.items.Count]; Vector2 string_size = f.MeasureString(menu.name); int i; /*---------------------------------------------------------- * Initialize * ----------------------------------------------------------*/ toolbar_loc.X = (int)origin.X; toolbar_loc.Y = (int)origin.Y; toolbar_loc.Height = menu_height; toolbar_loc.Width = (int)string_size.X + (2 * name_spacing); name_loc.Width = (int)string_size.X; name_loc.Height = (int)string_size.Y; name_loc.X = toolbar_loc.Center.X - name_loc.Width / 2 - 1; name_loc.Y = toolbar_loc.Center.Y - name_loc.Height / 2; highlight_loc.Width = toolbar_loc.Width - 2 * highlight_spacing; highlight_loc.Height = toolbar_loc.Height - highlight_spacing; highlight_loc.X = toolbar_loc.Center.X - highlight_loc.Width / 2; highlight_loc.Y = toolbar_loc.Center.Y - highlight_loc.Height / 2; menu_loc.X = highlight_loc.X; menu_loc.Y = highlight_loc.Y + highlight_loc.Height - 1; menu_loc.Width = 0; menu_loc.Height = 0; i = 0; foreach (menu_item_type item in menu.items) { Vector2 str_size = f.MeasureString(item.name); Vector2 str_loc = new Vector2(); Rectangle hglt_loc = new Rectangle(); str_loc.X = highlight_loc.X + name_spacing; str_loc.Y = menu_loc.Y + menu_loc.Height + item_spacing; hglt_loc.Width = (int)str_size.X + 2 * highlight_spacing; hglt_loc.Height = (int)str_size.Y + 2 * item_spacing; hglt_loc.X = highlight_loc.X + 1; hglt_loc.Y = menu_loc.Y + menu_loc.Height; menu_loc.Height += ((int)str_size.Y + 2 * item_spacing); menu_loc.Width = Math.Max(menu_loc.Width, ((int)str_size.X + 2 * name_spacing)); if (0 == i) { hglt_loc.Height--; hglt_loc.Y++; } else if ((menu.items.Count - 1) == i) { hglt_loc.Height--; } item_str_locs[i] = str_loc; item_hglt_locs[i] = hglt_loc; i++; } i = 0; foreach (menu_item_type item in menu.items) { item_hglt_locs[i].Width = menu_loc.Width - 2; i++; } } /* menu_dimension_type() */
} /* configure() */ /*********************************************************** * * Method: * input_handled * * Description: * Handle inputs to the menu. Returns true if the * input was handled by the menu. * ***********************************************************/ public Boolean input_handled() { Boolean handled = false; mouse.update(); int i = 0; if (null != open_menu) { foreach (menu_item_type item in open_menu.items) { if (open_menu.dims.item_hglt_locs[i].Contains(mouse.cursor)) { if ((button_state_type.UNHELD == mouse.left) || (button_state_type.HELD == mouse.left)) { item.highlight = true; } else if (button_state_type.UNCLICKED == mouse.left) { open_menu = null; item.highlight = false; item.handler(); break; } handled = true; } else { item.highlight = false; } i++; } /*---------------------------------------------------------- * If click down occurs outside open menu, close the menu * ----------------------------------------------------------*/ if ((null != open_menu) && (button_state_type.UNCLICKED == mouse.left) && ((!open_menu.dims.menu_loc.Contains(mouse.cursor)) && (!open_menu.dims.name_loc.Contains(mouse.cursor)))) { open_menu = null; } handled = true; } /*---------------------------------------------------------- * Check if the mouse is on a menu option * ----------------------------------------------------------*/ foreach (menu_type m in menus) { if (m.dims.highlight_loc.Contains(mouse.cursor)) { if (button_state_type.UNHELD == mouse.left) { m.highlight = true; } else if (button_state_type.CLICKED == mouse.left) { if (open_menu == m) { open_menu = null; } else { open_menu = m; } } handled = true; } else { m.highlight = false; } } return(handled); } /* input_handled() */