00001 00005 /* Copyright © 2009 James Legg. 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 */ 00011 #include "Menu.h" 00012 #include "SubMenuItem.h" 00013 #include <Debug.h> 00014 00015 #include <GL/gl.h> 00016 #include <typeinfo> 00017 00018 namespace UI 00019 { 00020 00021 Menu::Menu() 00022 : max_width(0), 00023 selected_item(0), 00024 active_sub_menu(0), 00025 parent(0), 00026 quit(false) 00027 { 00028 } 00029 00030 Menu::~Menu() 00031 { 00032 } 00033 00034 void Menu::insert_item(MenuItem * item) 00035 { 00036 items.push_back(item); 00037 // see if maximum size must be adjusted. 00038 if (item->get_min_width() > max_width) 00039 { 00040 max_width = item->get_min_width(); 00041 DEBUG_MESSAGE("Setting new maximum width of " << max_width << "."); 00042 for (std::vector<MenuItem *>::iterator it = items.begin(); 00043 it != items.end(); it++) 00044 { 00045 (*it)->set_width(max_width); 00046 } 00047 } else { 00048 // just set the size of the new item to the size of the largest of the 00049 // existing items 00050 items.back()->set_width(max_width); 00051 } 00052 00053 // if it is the first item added, make it selected. 00054 if (items.size() == 1) 00055 { 00056 items.back()->set_state(MenuItem::S_PRELIGHT); 00057 } 00058 } 00059 00060 void Menu::draw() 00061 { 00062 // are we drawing a sub menu? 00063 if (active_sub_menu) 00064 { 00065 // draw the sub menu, then shift across and draw ourselves. 00066 active_sub_menu->draw(); 00067 } 00068 glPushMatrix(); 00069 // draw with whatever is at the top centred around (0,0) 00070 glTranslatef(-float(get_total_width()), 00071 float (items.size() * 32),0.0); 00072 for (std::vector<MenuItem *>::iterator it = items.begin(); 00073 it != items.end(); it++) 00074 { 00075 (*it)->draw(); 00076 glTranslatef(0.0, -64.0, 0.0); 00077 } 00078 glPopMatrix(); 00079 } 00080 00081 void Menu::take_input(Engine::InputReport & report) 00082 { 00083 if (active_sub_menu) 00084 { 00085 active_sub_menu->take_input(report); 00086 return; 00087 } 00088 switch (report.get_report_type()) 00089 { 00090 case Engine::InputReport::RT_MENU_UP: 00091 items[selected_item]->set_state(MenuItem::S_NORMAL); 00092 if (selected_item == 0) 00093 { 00094 selected_item = items.size() - 1; 00095 } else { 00096 selected_item--; 00097 } 00098 items[selected_item]->set_state(MenuItem::S_PRELIGHT); 00099 break; 00100 case Engine::InputReport::RT_MENU_DOWN: 00101 items[selected_item]->set_state(MenuItem::S_NORMAL); 00102 if (selected_item == items.size() - 1) 00103 { 00104 selected_item = 0; 00105 } else { 00106 selected_item++; 00107 } 00108 items[selected_item]->set_state(MenuItem::S_PRELIGHT); 00109 break; 00110 case Engine::InputReport::RT_MENU_SELECT: 00111 items[selected_item]->activate(); 00112 // check if it is a submenu item. 00113 { 00114 SubMenuItem * item = dynamic_cast<SubMenuItem *>(items[selected_item]); 00115 if (item) 00116 { 00117 // hand over control 00118 DEBUG_MESSAGE("Switching to submenu."); 00119 active_sub_menu = item->get_menu(); 00120 active_sub_menu->parent = this; 00121 } else { 00122 DEBUG_MESSAGE("Activated a (non SubMenuItem) MenuItem."); 00123 // not a submenu item. Close menu? 00124 } 00125 } 00126 break; 00127 case Engine::InputReport::RT_MENU_BACK: 00128 // if we are a submenu, give control back to parent 00129 if (parent) 00130 { 00131 parent->active_sub_menu = 0; 00132 parent->items[parent->selected_item]->set_state(MenuItem::S_PRELIGHT); 00133 parent = 0; 00134 } else { 00135 quit = true; 00136 } 00137 break; 00138 default: 00139 // don't respond to other events. 00140 break; 00141 } 00142 } 00143 00144 bool Menu::want_to_quit() 00145 { 00146 return quit; 00147 } 00148 00149 unsigned int Menu::get_total_width() 00150 { 00151 if (active_sub_menu) 00152 { 00153 return active_sub_menu->get_total_width() + max_width; 00154 } else { 00155 return max_width / 2; 00156 } 00157 } 00158 00159 }
Generated at Mon Sep 6 00:41:13 2010 by Doxygen version 1.4.7 for Racer version svn335.