00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "Menu.h"
00013
00014
00015
00016 Menu::Menu(
const string &label,
const string &name,
Font *font, MenuType type)
00017 {
00018 label_ = label;
00019 name_ = name;
00020 font_ = font;
00021 selectedItem_ = 0;
00022 menuSpace_ = 2;
00023 isSelected_ =
false;
00024 hasFocus_ =
true;
00025 screenHeight_ = 768;
00026 screenWidth_ = 1024;
00027 type_ = type;
00028 }
00029
00030 Menu::~Menu()
00031 {
00032
for (
unsigned int i=0; i<items_.size(); i++){
00033
delete items_[i];
00034 }
00035 }
00036
00037 void Menu::setVirtualScreenSize(
float width,
float height)
00038 {
00039 screenWidth_ = width;
00040 screenHeight_ = height;
00041 }
00042
00043 void Menu::addChild(
MenuItem *item)
00044 {
00045 items_.push_back(item);
00046 }
00047
00048 void Menu::save(ofstream &file)
00049 {
00050
for (
unsigned int i=0; i<items_.size(); i++){
00051 items_[i]->save(file);
00052 }
00053 }
00054
00055 void Menu::load(ifstream &file)
00056 {
00057
for (
unsigned int i=0; i<items_.size(); i++){
00058 items_[i]->load(file);
00059 }
00060 }
00061
00062
00063 void Menu::draw(
float x,
float y,
float zoom)
00064 {
00065 font_->
printString(label_, x, y, zoom);
00066 }
00067
00068
float Menu::itemZoom(
unsigned int item)
00069 {
00070
if (item==selectedItem_)
return 2;
00071
else return 1;
00072 }
00073
00074 void Menu::renderMenu()
00075 {
00076 isSelected_ =
true;
00077
00078
if (hasFocus_ ==
false){
00079
if (items_[selectedItem_]->getType()==MENU)
00080
return ((
Menu*)items_[selectedItem_])->renderMenu();
00081
else hasFocus_ =
true;
00082 }
00083
00084
if (type_ ==
NORMAL){
00085
float y = screenHeight_;
00086
for (
unsigned int i=0; i<items_.size(); i++){
00087 y -= items_[i]->getHeight(menuSpace_);
00088 }
00089 y /= 2;
00090
for (
unsigned int i=0; i<items_.size(); i++){
00091
float x = (screenWidth_-items_[i]->getWidth(itemZoom(i)))/2;
00092
if (itemZoom(i) == 1){
00093 items_[i]->draw(x,y,1);
00094 }
00095
else {
00096 items_[i]->draw(x,y-
getHeight(itemZoom(i))/4,itemZoom(i));
00097 }
00098 y += items_[i]->getHeight(menuSpace_);
00099 }
00100 }
00101
else {
00102
float yCenter = (screenHeight_/2.0);
00103
00104
00105
float x = (screenWidth_-items_[selectedItem_]->getWidth(
00106 itemZoom(selectedItem_)))/2;
00107
float yPos = yCenter-(font_->
getHeight(itemZoom(selectedItem_))/2.0);
00108 items_[selectedItem_]->draw(x,yPos,itemZoom(selectedItem_));
00109
00110
00111
float y = yCenter+items_[selectedItem_]->getHeight(menuSpace_);
00112
for (
unsigned int i=selectedItem_+1; i<items_.size(); i++){
00113 x = (screenWidth_-items_[i]->getWidth(itemZoom(i)))/2;
00114 yPos = y-(font_->
getHeight(itemZoom(i))/2.0);
00115 items_[i]->draw(x,yPos,itemZoom(i));
00116 y += items_[i]->getHeight(menuSpace_);
00117 }
00118
00119
00120 y = yCenter-items_[selectedItem_]->getHeight(menuSpace_);
00121
for (
unsigned int i=selectedItem_-1; i<items_.size(); i--){
00122 x = (screenWidth_-items_[i]->getWidth(itemZoom(i)))/2;
00123 yPos = y-(font_->
getHeight(itemZoom(i))/2.0);
00124 items_[i]->draw(x,yPos,itemZoom(i));
00125 y -= items_[i]->getHeight(menuSpace_);
00126 }
00127 }
00128 }
00129
00130 float Menu::getWidth(
float zoom)
00131 {
00132
return font_->
getWidth(label_, zoom);
00133 }
00134
00135 float Menu::getHeight(
float zoom)
00136 {
00137
return font_->
getHeight(zoom);
00138 }
00139
00140 bool Menu::handleKeyEvent(SDL_Event *event)
00141 {
00142 isSelected_ =
true;
00143
00144
bool handled=
false;
00145
if (items_[selectedItem_]->getType()!=MENU && items_[selectedItem_]->getType()!=BACK_BUTTON){
00146 handled = items_[selectedItem_]->handleKeyEvent(event);
00147 }
00148
00149
if (hasFocus_==
false){
00150 handled = items_[selectedItem_]->handleKeyEvent(event);
00151
if (!((
Menu*)items_[selectedItem_])->getSelected()) hasFocus_ =
true;
00152 }
00153
00154
if (!handled){
00155
if (event->type != SDL_KEYDOWN)
return false;
00156
if (event->key.keysym.sym == SDLK_ESCAPE){
00157 isSelected_ =
false;
00158
return true;
00159 }
00160
if (event->key.keysym.sym == SDLK_RETURN){
00161
if (items_[selectedItem_]->getType()==MENU) {
00162 hasFocus_ =
false;
00163
return true;
00164 }
00165
if (items_[selectedItem_]->getType()==BACK_BUTTON) {
00166 isSelected_ =
false;
00167
return true;
00168 }
00169
else return false;
00170 }
00171
if (event->key.keysym.sym == SDLK_DOWN) {
00172 selectedItem_++;
00173
if (selectedItem_ >= items_.size()) selectedItem_ = 0;
00174
return true;
00175 }
00176
if (event->key.keysym.sym == SDLK_UP) {
00177
if (selectedItem_==0) selectedItem_= items_.size()-1;
00178
else selectedItem_--;
00179
00180
return true;
00181 }
00182
return false;
00183 }
00184
else return true;
00185
00186
return false;
00187 }
00188
00189 MenuItem *
Menu::findItem(
const string &name)
00190 {
00191
MenuItem *result=0;
00192
for (
unsigned int i=0; i<items_.size(); i++){
00193
if (items_[i]->getName() == name){
00194
return items_[i];
00195 }
00196 }
00197
for (
unsigned int i=0; i<items_.size(); i++){
00198
if (items_[i]->getType() == MENU){
00199 result = ((
Menu*)items_[i])->findItem(name);
00200
if (result != 0)
return result;
00201 }
00202 }
00203
return result;
00204 }