00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <dialogs/SaveSelectDialog.h>
00022 #include <GLW/GLWTextButton.h>
00023 #include <GLW/GLWLabel.h>
00024 #include <GLW/GLWWindowManager.h>
00025 #include <GLW/GLWFont.h>
00026 #include <GLW/GLWTranslate.h>
00027 #include <client/ClientParams.h>
00028 #include <client/ClientMain.h>
00029 #include <graph/TextureStore.h>
00030 #include <common/Defines.h>
00031 #include <common/FileList.h>
00032
00033 GLWIconListSaveItem::GLWIconListSaveItem(
00034 const char *file, const char *time) :
00035 tip_(ToolTip::ToolTipHelp,
00036 LANG_RESOURCE("SAVE", "Save"),
00037 LANG_RESOURCE("SAVE_TOOLTIP", "Load a previously saved game.")),
00038 icon_(0.0f, 0.0f, 40.0f, 40.0f),
00039 file_(file), time_(time)
00040 {
00041 GLTexture *texture = TextureStore::instance()->loadTexture(
00042 S3D::getDataFile("data/windows/save.bmp"));
00043 icon_.setTexture(texture);
00044 }
00045
00046 GLWIconListSaveItem::~GLWIconListSaveItem()
00047 {
00048 }
00049
00050 void GLWIconListSaveItem::draw(float x, float y, float w)
00051 {
00052 icon_.setX(x + 2.0f);
00053 icon_.setY(y + 2.0f);
00054 icon_.draw();
00055
00056 GLWToolTip::instance()->addToolTip(&tip_,
00057 GLWTranslate::getPosX() + x,
00058 GLWTranslate::getPosY() + y, w, 50.0f);
00059
00060 GLWFont::instance()->getGameFont()->drawWidth(
00061 w - 50.0f,
00062 GLWFont::widgetFontColor,
00063 8.0f, x + 50.0f, y + 23.0f, 0.0f,
00064 file_.c_str());
00065 GLWFont::instance()->getGameFont()->drawWidth(
00066 w - 50.0f,
00067 GLWFont::widgetFontColor,
00068 8.0f, x + 50.0f, y + 12.0f, 0.0f,
00069 time_.c_str());
00070 }
00071
00072 SaveSelectDialog *SaveSelectDialog::instance_ = 0;
00073
00074 SaveSelectDialog *SaveSelectDialog::instance()
00075 {
00076 if (!instance_)
00077 {
00078 instance_ = new SaveSelectDialog;
00079 }
00080 return instance_;
00081 }
00082
00083 SaveSelectDialog::SaveSelectDialog() :
00084 GLWWindow("", 300.0f, 410.0f, 0, "")
00085 {
00086 iconList_ = new GLWIconList(10.0f, 40.0f, 280.0f, 360.0f, 50.0f);
00087 addWidget(iconList_);
00088
00089 ok_ = (GLWButton *) addWidget(new GLWTextButton(LANG_RESOURCE("OK", "Ok"), 235, 10, 55, this,
00090 GLWButton::ButtonFlagOk | GLWButton::ButtonFlagCenterX));
00091 cancelId_ = addWidget(new GLWTextButton(LANG_RESOURCE("CANCEL", "Cancel"), 120, 10, 105, this,
00092 GLWButton::ButtonFlagCancel | GLWButton::ButtonFlagCenterX))->getId();
00093 iconList_->setHandler(this);
00094 }
00095
00096 SaveSelectDialog::~SaveSelectDialog()
00097 {
00098
00099 }
00100
00101 void SaveSelectDialog::display()
00102 {
00103 iconList_->clear();
00104 ok_->setEnabled(false);
00105
00106 FileList fileList(
00107 S3D::getSaveFile(""),
00108 "*.s3d",
00109 false,
00110 false);
00111 if (fileList.getStatus())
00112 {
00113 FileList::ListType &files = fileList.getFiles();
00114 FileList::ListType::iterator itor;
00115 for (itor = files.begin();
00116 itor != files.end();
00117 itor++)
00118 {
00119 const char *shortPath = (*itor).c_str();
00120 std::string fullPath = S3D::getSaveFile(shortPath);
00121
00122 time_t modTime = S3D::fileModTime(fullPath);
00123 const char *modTimeStr = ctime(&modTime);
00124 char *nl = (char *) strchr(modTimeStr, '\n');
00125 if (nl) *nl = '\0';
00126
00127 GLWIconListSaveItem *item = new GLWIconListSaveItem(shortPath, modTimeStr);
00128 iconList_->addItem(item);
00129 }
00130 }
00131 }
00132
00133 void SaveSelectDialog::selected(unsigned int id, int position)
00134 {
00135 ok_->setEnabled(true);
00136 }
00137
00138 void SaveSelectDialog::chosen(unsigned int id, int position)
00139 {
00140 buttonDown(ok_->getId());
00141 }
00142
00143 void SaveSelectDialog::buttonDown(unsigned int id)
00144 {
00145 if (id == ok_->getId())
00146 {
00147 GLWWindowManager::instance()->hideWindow(id_);
00148
00149 GLWIconListSaveItem *selected =
00150 (GLWIconListSaveItem *) iconList_->getSelected();
00151 if (selected)
00152 {
00153 std::string targetFilePath = S3D::getSaveFile(selected->getFile());
00154 ClientParams::instance()->reset();
00155 ClientParams::instance()->setSaveFile(targetFilePath.c_str());
00156 ClientMain::startClient();
00157 }
00158 }
00159 else if (id == cancelId_)
00160 {
00161 GLWWindowManager::instance()->hideWindow(id_);
00162 }
00163 }