MenuGump.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2004-2005  The Pentagram Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "pent_include.h"
00020 #include "MenuGump.h"
00021 
00022 #include "GameData.h"
00023 #include "GumpShapeArchive.h"
00024 #include "Shape.h"
00025 #include "ShapeFrame.h"
00026 #include "GUIApp.h"
00027 #include "DesktopGump.h"
00028 #include "ButtonWidget.h"
00029 #include "TextWidget.h"
00030 #include "QuitGump.h"
00031 #include "ControlsGump.h"
00032 #include "OptionsGump.h"
00033 #include "PagedGump.h"
00034 #include "Game.h"
00035 #include "MainActor.h"
00036 #include "Font.h"
00037 #include "RenderedText.h"
00038 #include "FontManager.h"
00039 #include "SettingManager.h"
00040 #include "MusicProcess.h"
00041 #include "EditWidget.h"
00042 #include "U8SaveGump.h"
00043 #include "getObject.h"
00044 
00045 #include "IDataSource.h"
00046 #include "ODataSource.h"
00047 
00048 DEFINE_RUNTIME_CLASSTYPE_CODE(MenuGump,ModalGump);
00049 
00050 MenuGump::MenuGump(bool nameEntryMode_)
00051         : ModalGump(0, 0, 5, 5, 0, FLAG_DONT_SAVE)
00052 {
00053         nameEntryMode = nameEntryMode_;
00054 
00055         GUIApp * app = GUIApp::get_instance();
00056         app->pushMouseCursor();
00057         if (!nameEntryMode)
00058                 app->setMouseCursor(GUIApp::MOUSE_HAND);
00059         else
00060                 app->setMouseCursor(GUIApp::MOUSE_NONE);
00061 
00062         // Save old music state
00063         MusicProcess *musicprocess = MusicProcess::get_instance();
00064         if (musicprocess) oldMusicTrack = musicprocess->getTrack();
00065         else oldMusicTrack = 0;
00066 }
00067 
00068 MenuGump::~MenuGump()
00069 {
00070 }
00071 
00072 void MenuGump::Close(bool no_del)
00073 {
00074         // Restore old music state
00075         MusicProcess *musicprocess = MusicProcess::get_instance();
00076         if (musicprocess) musicprocess->playMusic(oldMusicTrack);
00077 
00078         GUIApp* guiapp = GUIApp::get_instance();
00079         guiapp->popMouseCursor();
00080 
00081         ModalGump::Close(no_del);
00082 }
00083 
00084 static const int gumpShape = 35;
00085 static const int paganShape = 32;
00086 static const int menuEntryShape = 37;
00087 
00088 void MenuGump::InitGump(Gump* newparent, bool take_focus)
00089 {
00090         ModalGump::InitGump(newparent, take_focus);
00091 
00092         shape = GameData::get_instance()->getGumps()->getShape(gumpShape);
00093         ShapeFrame* sf = shape->getFrame(0);
00094         assert(sf);
00095 
00096         dims.w = sf->width;
00097         dims.h = sf->height;
00098 
00099         Shape* logoShape;
00100         logoShape = GameData::get_instance()->getGumps()->getShape(paganShape);
00101         sf = logoShape->getFrame(0);
00102         assert(sf);
00103 
00104         Gump * logo = new Gump(42, 10, sf->width, sf->height);
00105         logo->SetShape(logoShape, 0);
00106         logo->InitGump(this, false);
00107 
00108         if (!nameEntryMode) {
00109                 SettingManager* settingman = SettingManager::get_instance();
00110                 bool endgame, quotes;
00111                 settingman->get("endgame", endgame);
00112                 settingman->get("quotes", quotes);
00113 
00114                 int x = dims.w / 2 + 14;
00115                 int y = 18;
00116                 Gump * widget;
00117                 for (int i = 0; i < 8; ++i)
00118                 {
00119                         if ((quotes || i != 6) && (endgame || i != 7)) {
00120                                 FrameID frame_up(GameData::GUMPS, menuEntryShape, i * 2);
00121                                 FrameID frame_down(GameData::GUMPS, menuEntryShape, i * 2 + 1);
00122                                 frame_up = _TL_SHP_(frame_up);
00123                                 frame_down = _TL_SHP_(frame_down);
00124                                 widget = new ButtonWidget(x, y, frame_up, frame_down, true);
00125                                 widget->InitGump(this, false);
00126                                 widget->SetIndex(i + 1);
00127                         }
00128 
00129                         y += 14;
00130                 }
00131                 
00132                 MainActor* av = getMainActor();
00133                 std::string name;
00134                 if (av)
00135                         name = av->getName();
00136 
00137                 if (!name.empty()) {
00138                         Pentagram::Rect rect;
00139                         widget = new TextWidget(0, 0, name, true, 6);
00140                         widget->InitGump(this, false);
00141                         widget->GetDims(rect);
00142                         widget->Move(90 - rect.w / 2, dims.h - 40);
00143                 }
00144         } else {
00145                 Gump * widget;
00146                 widget = new TextWidget(0, 0, _TL_("Give thy name:"), true, 6); // CONSTANT!
00147                 widget->InitGump(this, false);
00148                 widget->Move(dims.w / 2 + 6, 10);
00149 
00150                 Pentagram::Rect textdims;
00151                 widget->GetDims(textdims);
00152 
00153                 widget = new EditWidget(0, 0, "", true, 6, 110, 40, 15); // CONSTANTS!
00154                 widget->InitGump(this, true);
00155                 widget->Move(dims.w / 2 + 6, 10 + textdims.h);
00156                 widget->MakeFocus();
00157         }
00158 }
00159 
00160 
00161 void MenuGump::PaintThis(RenderSurface* surf, sint32 lerp_factor, bool scaled)
00162 {
00163         Gump::PaintThis(surf, lerp_factor, scaled);
00164 }
00165 
00166 bool MenuGump::OnKeyDown(int key, int mod)
00167 {
00168         if (Gump::OnKeyDown(key, mod)) return true;
00169 
00170         if (!nameEntryMode) {
00171 
00172                 if (key == SDLK_ESCAPE) {
00173                         // FIXME: this check should probably be in Game or GUIApp
00174                         MainActor* av = getMainActor();
00175                         if (av && !(av->getActorFlags() & Actor::ACT_DEAD))
00176                                 Close(); // don't allow closing if dead/game over
00177                 } else if (key >= SDLK_1 && key <=SDLK_9) {
00178                         selectEntry(key - SDLK_1 + 1);
00179                 }
00180 
00181         } 
00182 
00183         return true;
00184 }
00185 
00186 void MenuGump::ChildNotify(Gump *child, uint32 message)
00187 {
00188         if (child->IsOfType<EditWidget>() && message == EditWidget::EDIT_ENTER)
00189         {
00190                 EditWidget* editwidget = p_dynamic_cast<EditWidget*>(child);
00191                 assert(editwidget);
00192                 std::string name = editwidget->getText();
00193                 if (!name.empty()) {
00194                         MainActor* av = getMainActor();
00195                         av->setName(name);
00196                         Close();
00197                 }
00198         }
00199 
00200         if (child->IsOfType<ButtonWidget>() && message==ButtonWidget::BUTTON_CLICK)
00201         {
00202                 selectEntry(child->GetIndex());
00203         }
00204 }
00205 
00206 void MenuGump::selectEntry(int entry)
00207 {
00208         SettingManager* settingman = SettingManager::get_instance();
00209         bool endgame, quotes;
00210         settingman->get("endgame", endgame);
00211         settingman->get("quotes", quotes);
00212 
00213         switch (entry)
00214         {
00215         case 1: // Intro
00216                 Game::get_instance()->playIntroMovie();
00217                 break;
00218         case 2: case 3: // Read/Write Diary
00219         {
00220                 if (entry == 3) {
00221                         // can't save if game over
00222                         // FIXME: this check should probably be in Game or GUIApp
00223                         MainActor* av = getMainActor();
00224                         if (!av || (av->getActorFlags() & Actor::ACT_DEAD)) break;
00225                 }
00226 
00227                 PagedGump * gump = new PagedGump(34, -38, 3, gumpShape);
00228                 gump->InitGump(this);
00229 
00230                 U8SaveGump* save = new U8SaveGump(entry == 3, 0);
00231                 save->InitGump(gump, false);
00232                 gump->addPage(save);
00233 
00234                 save = new U8SaveGump(entry == 3, 1);
00235                 save->InitGump(gump, false);
00236                 gump->addPage(save);
00237 
00238                 gump->setRelativePosition(CENTER);
00239         } break;
00240         case 4: // Options
00241         {
00242                 PagedGump * gump = new PagedGump(34, -38, 3, gumpShape);
00243                 gump->InitGump(this);
00244 
00245                 OptionsGump * options = new OptionsGump();
00246                 options->InitGump(gump, false);
00247                 gump->addPage(options);
00248                 gump->setRelativePosition(CENTER);
00249         } break;
00250         case 5: // Credits
00251                 Game::get_instance()->playCredits();
00252                 break;
00253         case 6: // Quit
00254                 QuitGump::verifyQuit();
00255                 break;
00256         case 7: // Quotes
00257                 if (quotes) Game::get_instance()->playQuotes();
00258                 break;
00259         case 8: // End Game
00260                 if (endgame) Game::get_instance()->playEndgameMovie();
00261                 break;
00262         default:
00263                 break;
00264         }
00265 }
00266 
00267 bool MenuGump::OnTextInput(int unicode)
00268 {
00269         if (Gump::OnTextInput(unicode)) return true;
00270 
00271         return true;
00272 }
00273 
00274 //static
00275 void MenuGump::showMenu()
00276 {
00277         ModalGump* gump = new MenuGump();
00278         gump->InitGump(0);
00279         gump->setRelativePosition(CENTER);
00280 }
00281 
00282 // static
00283 void MenuGump::ConCmd_showMenu(const Console::ArgvType &argv)
00284 {
00285         MenuGump::showMenu();
00286 }
00287 
00288 //static
00289 void MenuGump::inputName()
00290 {
00291         ModalGump* gump = new MenuGump(true);
00292         gump->InitGump(0);
00293         gump->setRelativePosition(CENTER);
00294 }

Generated on Fri Jul 27 22:27:25 2007 for pentagram by  doxygen 1.4.7