U8SaveGump.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 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 "U8SaveGump.h"
00021 
00022 #include "RenderSurface.h"
00023 #include "DesktopGump.h"
00024 #include "EditWidget.h"
00025 #include "TextWidget.h"
00026 #include "GUIApp.h"
00027 #include "GameData.h"
00028 #include "Shape.h"
00029 #include "ShapeFrame.h"
00030 #include "FileSystem.h"
00031 #include "Savegame.h"
00032 #include "PagedGump.h"
00033 
00034 #include "IDataSource.h"
00035 #include "ODataSource.h"
00036 
00037 static const int entryfont = 4;
00038 
00039 DEFINE_RUNTIME_CLASSTYPE_CODE(U8SaveGump,Gump);
00040 
00041 U8SaveGump::U8SaveGump(bool save_, int page_)
00042         : Gump(0, 0, 5, 5), save(save_), page(page_)
00043 {
00044 
00045 }
00046 
00047 U8SaveGump::~U8SaveGump()
00048 {
00049 
00050 }
00051 
00052 
00053 // gumps: 36/0-11: number 1-12
00054 //        46/0: "Entry"
00055 
00056 void U8SaveGump::InitGump(Gump* newparent, bool take_focus)
00057 {
00058         Gump::InitGump(newparent, take_focus);
00059 
00060         dims.w = 220;
00061         dims.h = 170;
00062 
00063         FrameID entry_id(GameData::GUMPS, 46, 0);
00064         entry_id = _TL_SHP_(entry_id);
00065 
00066         Shape* entryShape;
00067         entryShape = GameData::get_instance()->getShape(entry_id);
00068         ShapeFrame* sf = entryShape->getFrame(entry_id.framenum);
00069         int entrywidth = sf->width;
00070         int entryheight = sf->height;
00071 
00072         if (save)
00073                 editwidgets.resize(6); // constant!
00074 
00075         loadDescriptions();
00076 
00077         for (int i = 0; i < 6; ++i) {
00078                 int index = page * 6 + i;
00079 
00080                 FrameID entrynum_id(GameData::GUMPS, 36, index);
00081                 entrynum_id = _TL_SHP_(entrynum_id);
00082 
00083                 int xbase = 3;
00084                 int yi = i;
00085                 if (i >= 3) {
00086                         xbase += dims.w/2 + 9;
00087                         yi -= 3;
00088                 }
00089 
00090                 Gump* gump = new Gump(xbase, 3+40*yi, 1, 1);
00091                 gump->SetShape(entry_id, true);
00092                 gump->InitGump(this, false);
00093                         
00094                 gump = new Gump(xbase+2+entrywidth, 3+40*yi, 1, 1);
00095                 gump->SetShape(entrynum_id, true);
00096                 gump->InitGump(this, false);
00097 
00098                 if (index == 0) {
00099                         // special case for 'The Beginning...' save
00100                         Gump* widget = new TextWidget(xbase, 12+entryheight,
00101                                                                                   _TL_("The Beginning..."),
00102                                                                                   true, entryfont, 95);
00103                         widget->InitGump(this, false);
00104 
00105                 } else {
00106 
00107                         if (save) {
00108                                 EditWidget* ew = new EditWidget(xbase, entryheight+4+40*yi,
00109                                                                                                 descriptions[i],
00110                                                                                                 true, entryfont,
00111                                                                                                 95, 38-entryheight, 0, true);
00112                                 ew->SetIndex(i+1);
00113                                 ew->InitGump(this, false);
00114                                 editwidgets[i] = ew;
00115                         } else {
00116                                 // load
00117                                 Gump* widget = new TextWidget(xbase, entryheight+4+40*yi,
00118                                                                                           descriptions[i], true, entryfont,
00119                                                                                           95);
00120                                 widget->InitGump(this, false);
00121                         }
00122                 }
00123 
00124         }
00125 
00126         // remove focus from children (just in case)
00127         if (focus_child) focus_child->OnFocus(false);
00128         focus_child = 0;
00129 }
00130 
00131 void U8SaveGump::Close(bool no_del)
00132 {
00133         Gump::Close(no_del);
00134 }
00135 
00136 void U8SaveGump::OnFocus(bool gain)
00137 {
00138         if (gain)
00139         {
00140                 if (save)
00141                         GUIApp::get_instance()->setMouseCursor(GUIApp::MOUSE_QUILL);
00142                 else
00143                         GUIApp::get_instance()->setMouseCursor(GUIApp::MOUSE_MAGGLASS);
00144         }
00145 }
00146 
00147 Gump* U8SaveGump::OnMouseDown(int button, int mx, int my)
00148 {
00149         // take all clicks
00150         return this;
00151 }
00152 
00153 
00154 void U8SaveGump::OnMouseClick(int button, int mx, int my)
00155 {
00156         if (button != BUTTON_LEFT) return;
00157 
00158         ParentToGump(mx,my);
00159 
00160         int x;
00161         if (mx >= 3 && mx <= 100)
00162                 x = 0;
00163         else if (mx >= dims.w/2 + 10)
00164                 x = 1;
00165         else
00166                 return;
00167 
00168         int y;
00169         if (my >= 3 && my <= 40)
00170                 y = 0;
00171         else if (my >= 43 && my <= 80)
00172                 y = 1;
00173         else if (my >= 83 && my <= 120)
00174                 y = 2;
00175         else
00176                 return;
00177 
00178         int i = 3*x + y;
00179         int index = 6*page + i + 1;
00180 
00181         if (save && !focus_child && editwidgets[i]) {
00182                 editwidgets[i]->MakeFocus();
00183                 PagedGump* p = p_dynamic_cast<PagedGump*>(parent);
00184                 if (p) p->enableButtons(false);
00185         }
00186 
00187         if (!save) {
00188                 loadgame(index); // 'this' will be deleted here!
00189         }
00190 }
00191 
00192 void U8SaveGump::ChildNotify(Gump *child, uint32 message)
00193 {
00194         if (child->IsOfType<EditWidget>() && message == EditWidget::EDIT_ENTER)
00195         {
00196                 // save
00197                 assert(save);
00198 
00199                 EditWidget* widget = p_dynamic_cast<EditWidget*>(child);
00200                 assert(widget);
00201 
00202                 std::string name = widget->getText();
00203                 if (name.empty()) return;
00204 
00205                 if (savegame(widget->GetIndex() + 6*page, name))
00206                         parent->Close(); // close PagedGump (and us)
00207 
00208                 return;
00209         }
00210 
00211         if (child->IsOfType<EditWidget>() && message == EditWidget::EDIT_ESCAPE)
00212         {
00213                 // cancel edit
00214                 assert(save);
00215 
00216                 // remove focus
00217                 if (focus_child) focus_child->OnFocus(false);
00218                 focus_child = 0;
00219 
00220                 PagedGump* p = p_dynamic_cast<PagedGump*>(parent);
00221                 if (p) p->enableButtons(true);
00222 
00223                 EditWidget* widget = p_dynamic_cast<EditWidget*>(child);
00224                 assert(widget);
00225                 widget->setText(descriptions[widget->GetIndex()]);
00226                 
00227                 return;
00228         }
00229 
00230 }
00231 
00232 bool U8SaveGump::OnKeyDown(int key, int mod)
00233 {
00234         if (Gump::OnKeyDown(key, mod)) return true;
00235 
00236         return false;
00237 }
00238 
00239 static std::string getFilename(int index) {
00240         char buf[32];
00241         sprintf(buf, "%02d", index);
00242 
00243         std::string filename = "@save/pent";
00244         filename += buf;
00245         filename += ".sav";
00246         return filename;
00247 }
00248 
00249 bool U8SaveGump::loadgame(int index)
00250 {
00251         if (index == 1) {
00252                 GUIApp::get_instance()->newGame();
00253                 return true;
00254         }
00255 
00256         pout << "Load " << index << std::endl;
00257 
00258         std::string filename = getFilename(index);
00259         GUIApp::get_instance()->loadGame(filename);
00260 
00261         return true;
00262 }
00263 
00264 bool U8SaveGump::savegame(int index, const std::string& name)
00265 {
00266         pout << "Save " << index << ": \"" << name << "\"" << std::endl;
00267 
00268         if (name.empty()) return false;
00269 
00270         std::string filename = getFilename(index);
00271         GUIApp::get_instance()->saveGame(filename, name, true);
00272         return true;
00273 }
00274 
00275 void U8SaveGump::loadDescriptions()
00276 {
00277         descriptions.resize(6);
00278 
00279         for (int i = 0; i < 6; ++i) {
00280                 int index = 6*page + i + 1;
00281 
00282                 std::string filename = getFilename(index);
00283                 IDataSource* ids = FileSystem::get_instance()->ReadFile(filename);
00284                 if (!ids) continue;
00285 
00286                 Savegame* sg = new Savegame(ids);
00287                 uint32 version = sg->getVersion();
00288                 descriptions[i] = "";
00289 
00290                 // FIXME: move version checks elsewhere!!
00291                 if (version == 0) {
00292                         descriptions[i] = "[corrupt] ";
00293                 } else if (version == 1) {
00294                         descriptions[i] = "[outdated] ";
00295                 } else if (version > Pentagram::savegame_version) {
00296                         descriptions[i] = "[too modern] ";
00297                 }
00298 
00299                 descriptions[i] += sg->getDescription();
00300                 delete sg;
00301         }
00302 }

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