git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / dialogs / SaveSelectDialog.cpp
blobc0975dbd7da2c286e364d0cee50ea783f6d218ae
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <dialogs/SaveSelectDialog.h>
22 #include <GLW/GLWTextButton.h>
23 #include <GLW/GLWLabel.h>
24 #include <GLW/GLWWindowManager.h>
25 #include <GLW/GLWFont.h>
26 #include <GLW/GLWTranslate.h>
27 #include <client/ClientParams.h>
28 #include <client/ClientMain.h>
29 #include <graph/TextureStore.h>
30 #include <common/Defines.h>
31 #include <common/FileList.h>
33 GLWIconListSaveItem::GLWIconListSaveItem(
34 const char *file, const char *time) :
35 tip_(ToolTip::ToolTipHelp,
36 LANG_RESOURCE("SAVE", "Save"),
37 LANG_RESOURCE("SAVE_TOOLTIP", "Load a previously saved game.")),
38 icon_(0.0f, 0.0f, 40.0f, 40.0f),
39 file_(file), time_(time)
41 GLTexture *texture = TextureStore::instance()->loadTexture(
42 S3D::getDataFile("data/windows/save.bmp"));
43 icon_.setTexture(texture);
46 GLWIconListSaveItem::~GLWIconListSaveItem()
50 void GLWIconListSaveItem::draw(float x, float y, float w)
52 icon_.setX(x + 2.0f);
53 icon_.setY(y + 2.0f);
54 icon_.draw();
56 GLWToolTip::instance()->addToolTip(&tip_,
57 GLWTranslate::getPosX() + x,
58 GLWTranslate::getPosY() + y, w, 50.0f);
60 GLWFont::instance()->getGameFont()->drawWidth(
61 w - 50.0f,
62 GLWFont::widgetFontColor,
63 8.0f, x + 50.0f, y + 23.0f, 0.0f,
64 file_.c_str());
65 GLWFont::instance()->getGameFont()->drawWidth(
66 w - 50.0f,
67 GLWFont::widgetFontColor,
68 8.0f, x + 50.0f, y + 12.0f, 0.0f,
69 time_.c_str());
72 SaveSelectDialog *SaveSelectDialog::instance_ = 0;
74 SaveSelectDialog *SaveSelectDialog::instance()
76 if (!instance_)
78 instance_ = new SaveSelectDialog;
80 return instance_;
83 SaveSelectDialog::SaveSelectDialog() :
84 GLWWindow("", 300.0f, 410.0f, 0, "")
86 iconList_ = new GLWIconList(10.0f, 40.0f, 280.0f, 360.0f, 50.0f);
87 addWidget(iconList_);
89 ok_ = (GLWButton *) addWidget(new GLWTextButton(LANG_RESOURCE("OK", "Ok"), 235, 10, 55, this,
90 GLWButton::ButtonFlagOk | GLWButton::ButtonFlagCenterX));
91 cancelId_ = addWidget(new GLWTextButton(LANG_RESOURCE("CANCEL", "Cancel"), 120, 10, 105, this,
92 GLWButton::ButtonFlagCancel | GLWButton::ButtonFlagCenterX))->getId();
93 iconList_->setHandler(this);
96 SaveSelectDialog::~SaveSelectDialog()
101 void SaveSelectDialog::display()
103 iconList_->clear();
104 ok_->setEnabled(false);
106 FileList fileList(
107 S3D::getSaveFile(""),
108 "*.s3d",
109 false, // Full path
110 false); // Recurse
111 if (fileList.getStatus())
113 FileList::ListType &files = fileList.getFiles();
114 FileList::ListType::iterator itor;
115 for (itor = files.begin();
116 itor != files.end();
117 itor++)
119 const char *shortPath = (*itor).c_str();
120 std::string fullPath = S3D::getSaveFile(shortPath);
122 time_t modTime = S3D::fileModTime(fullPath);
123 const char *modTimeStr = ctime(&modTime);
124 char *nl = (char *) strchr(modTimeStr, '\n');
125 if (nl) *nl = '\0';
127 GLWIconListSaveItem *item = new GLWIconListSaveItem(shortPath, modTimeStr);
128 iconList_->addItem(item);
133 void SaveSelectDialog::selected(unsigned int id, int position)
135 ok_->setEnabled(true);
138 void SaveSelectDialog::chosen(unsigned int id, int position)
140 buttonDown(ok_->getId());
143 void SaveSelectDialog::buttonDown(unsigned int id)
145 if (id == ok_->getId())
147 GLWWindowManager::instance()->hideWindow(id_);
149 GLWIconListSaveItem *selected =
150 (GLWIconListSaveItem *) iconList_->getSelected();
151 if (selected)
153 std::string targetFilePath = S3D::getSaveFile(selected->getFile());
154 ClientParams::instance()->reset();
155 ClientParams::instance()->setSaveFile(targetFilePath.c_str());
156 ClientMain::startClient();
159 else if (id == cancelId_)
161 GLWWindowManager::instance()->hideWindow(id_);