diff options
author | Stijn Buys <ingar@osirion.org> | 2012-01-09 20:52:41 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2012-01-09 20:52:41 +0000 |
commit | 28eeb651acfd294b2b0bb7d4e14f09eb365caf7c (patch) | |
tree | 1dd65f6907ded300d5a1554f1db2a9bdc5300cda /src/client | |
parent | 7178e0a298047fa95afe1651114905f1e226928b (diff) |
Added save/load menu prototype.
Diffstat (limited to 'src/client')
-rw-r--r-- | src/client/Makefile.am | 2 | ||||
-rw-r--r-- | src/client/mainmenu.cc | 5 | ||||
-rw-r--r-- | src/client/savegamemenu.cc | 194 | ||||
-rw-r--r-- | src/client/savegamemenu.h | 65 |
4 files changed, 264 insertions, 2 deletions
diff --git a/src/client/Makefile.am b/src/client/Makefile.am index c46d885..77ff3c3 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -30,6 +30,7 @@ noinst_HEADERS = \ mapwidget.h \ mapwindow.h \ notifications.h \ + savegamemenu.h \ soundext.h \ targeticonbutton.h \ targets.h \ @@ -59,6 +60,7 @@ libclient_la_SOURCES = \ mapwidget.cc \ mapwindow.cc \ notifications.cc \ + savegamemenu.cc \ soundext.cc \ targeticonbutton.cc \ targets.cc \ diff --git a/src/client/mainmenu.cc b/src/client/mainmenu.cc index cfebade..e2b3170 100644 --- a/src/client/mainmenu.cc +++ b/src/client/mainmenu.cc @@ -6,6 +6,7 @@ #include "client/buttonmenu.h" #include "client/mainmenu.h" +#include "client/savegamemenu.h" #include "core/core.h" #include "core/gameinterface.h" #include "core/application.h" @@ -200,10 +201,10 @@ void MainMenu::load() mainmenu_connectmenu = 0; // load game menu - mainmenu_loadmenu = 0; + mainmenu_loadmenu = (ui::Widget *) new SaveGameMenu(this, "load", SaveGameMenu::Load); // save game menu - mainmenu_savemenu = 0; + mainmenu_savemenu = (ui::Widget *) new SaveGameMenu(this, "save", SaveGameMenu::Save); show_menu(); } diff --git a/src/client/savegamemenu.cc b/src/client/savegamemenu.cc new file mode 100644 index 0000000..18d474d --- /dev/null +++ b/src/client/savegamemenu.cc @@ -0,0 +1,194 @@ +/* + client/savegamemenu.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "client/savegamemenu.h" +#include "filesystem/filesystem.h" +#include "ui/ui.h" + +namespace client +{ + +SaveGameMenu::SaveGameMenu(ui::Widget *parent, const char *label, const Mode mode) : + ui::Window(parent) +{ + set_label(label); + set_border(true); + set_background(true); + set_font(ui::root()->font_small()); + + savegamemenu_mode = mode; + + // window title + savegamemenu_titlelabel = new ui::Label(this); + savegamemenu_titlelabel->set_label("title"); + savegamemenu_titlelabel->set_background(false); + savegamemenu_titlelabel->set_border(false); + savegamemenu_titlelabel->set_font(ui::root()->font_large()); + savegamemenu_titlelabel->set_alignment(ui::AlignCenter); + if (savegamemenu_mode == Save) { + savegamemenu_titlelabel->set_text("SAVE GAME"); + } else { + savegamemenu_titlelabel->set_text("LOAD GAME"); + } + + // close button + savegamemenu_closebutton = new ui::IconButton(savegamemenu_titlelabel, "bitmaps/icons/window_close"); + savegamemenu_closebutton->set_label("close"); + + // file listview + savegamemenu_filelistview = new ui::ListView(this); + savegamemenu_filelistview->set_label("files"); + + savegamemenu_descrlabel = new ui::Label(this); + savegamemenu_descrlabel->set_label("description"); + + //savegamemenu_descrinput = new ui::InputBox(this); + + // delete button + savegamemenu_deletebutton = new ui::IconButton(this, "bitmaps/icons/button_delete"); + savegamemenu_deletebutton->set_label("delete"); +} + +SaveGameMenu::~SaveGameMenu() +{ +} + +void SaveGameMenu::resize() +{ + const float padding = ui::root()->font_large()->height(); + const float icon_size = 24.0f; // small icons + + // resize title label + savegamemenu_titlelabel->set_size(width() - padding * 2.0f, savegamemenu_titlelabel->font()->height()); + savegamemenu_titlelabel->set_location(padding, padding); + + // resize close button + savegamemenu_closebutton->set_size(savegamemenu_titlelabel->font()->height(), savegamemenu_titlelabel->font()->height()); + savegamemenu_closebutton->set_location(savegamemenu_titlelabel->width() - savegamemenu_closebutton->width(), 0); + + // resize files listview + savegamemenu_filelistview->set_size( + ui::UI::elementsize.width(), + height() - icon_size - padding * 3.0f - savegamemenu_titlelabel->bottom() + ); + savegamemenu_filelistview->set_location( + padding, + savegamemenu_titlelabel->bottom() + padding + ); + + // resize description label + savegamemenu_descrlabel->set_size( + width() - savegamemenu_filelistview->right() - 2.0f * padding, + savegamemenu_filelistview->height() + ); + savegamemenu_descrlabel->set_location(savegamemenu_filelistview->right() + padding, savegamemenu_filelistview->top()); + + + // resize eject button + savegamemenu_deletebutton->set_size(icon_size, icon_size); + savegamemenu_deletebutton->set_location( + savegamemenu_filelistview->right() - icon_size, + savegamemenu_filelistview->bottom() + padding + ); +} + +void SaveGameMenu::refresh() +{ + ui::ListItem *listitem = 0; + + std::string savegamepath(filesystem::writedir()); + savegamepath.append("savegames"); + + filesystem::Directory directory(savegamepath); + + savegamemenu_filelistview->clear(); + + if (savegamemenu_mode == Save) { + listitem = new ui::ListItem(savegamemenu_filelistview, "NEW SAVEGAME"); + listitem->set_label("new"); + listitem->set_font(ui::root()->font_tiny()); + listitem->set_height(listitem->font()->height() * 2.0f); + } + + for (filesystem::Directory::FileNames::const_iterator it = directory.filenames().begin(); + it != directory.filenames().end(); ++it) + { + const std::string filename((*it).c_str()); + if (filename.size() < 5) { + continue; + } + + std::string ext = filename.substr(filename.size() - 4); + aux::lowercase(ext); + if (ext.compare(".sav")) { + continue; + } + + std::string label = filename.substr(0, filename.size() - 4); + + listitem = new ui::ListItem(savegamemenu_filelistview, label.c_str()); + listitem->set_value(filename); + + listitem->set_font(ui::root()->font_tiny()); + listitem->set_height(listitem->font()->height() * 2.0f); + + // TODO savegame info, sortkey timestamp + } + + savegamemenu_filelistview->event_resize(); +} + +void SaveGameMenu::show() +{ + Window::show(); + event_resize(); + refresh(); +} + +void SaveGameMenu::show_file_info() +{ + savegamemenu_descrlabel->clear(); + + if (savegamemenu_filelistview->selected()) { + std::string filename(savegamemenu_filelistview->selected()->value().c_str()); + savegamemenu_descrlabel->set_text(filename); + } +} +bool SaveGameMenu::on_keypress(const int key, const unsigned int modifier) +{ + if (key == SDLK_ESCAPE) { + if (visible()) { + // hide the parent mainwindow + parent()->hide(); + return true; + } + } + + return false; +} + +bool SaveGameMenu::on_emit(ui::Widget *sender, const ui::Widget::Event event, void *data) +{ + if (sender == savegamemenu_closebutton) { + if (event == ui::Widget::EventButtonClicked) { + // hide the parent mainwindow + parent()->hide(); + return true; + } + } else if (sender == savegamemenu_filelistview) { + if (event == ui::Widget::EventListViewChanged) { + show_file_info(); + return true; + } + } + + return Window::on_emit(sender, event, data); +} + + +} // namespace client + + diff --git a/src/client/savegamemenu.h b/src/client/savegamemenu.h new file mode 100644 index 0000000..d5b69e3 --- /dev/null +++ b/src/client/savegamemenu.h @@ -0,0 +1,65 @@ +/* + client/savegamewindow.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_CLIENT_SAVEGAMEMENU_H__ +#define __INCLUDED_CLIENT_SAVEGAMEMENU_H__ + +#include "ui/button.h" +#include "ui/iconbutton.h" +#include "ui/inputbox.h" +#include "ui/label.h" +#include "ui/listview.h" +#include "ui/window.h" + +namespace client +{ + +class SaveGameMenu : public ui::Window +{ +public: + enum Mode { Save = 0, Load = 1 }; + + SaveGameMenu(ui::Widget *parent = 0, const char *label = 0, const Mode mode = Save); + virtual ~SaveGameMenu(); + +protected: + + /// called when the widget receives a key press + virtual bool on_keypress(const int key, const unsigned int modifier); + + /// called if the widget receives an emit evet + virtual bool on_emit(ui::Widget *sender, const ui::Widget::Event event, void *data); + + virtual void resize(); + + virtual void show(); + + void show_file_info(); + + void refresh(); + +private: + Mode savegamemenu_mode; + + ui::Label *savegamemenu_titlelabel; + ui::IconButton *savegamemenu_closebutton; + ui::ListView *savegamemenu_filelistview; + ui::IconButton *savegamemenu_deletebutton; + + ui::Label *savegamemenu_descrlabel; + ui::InputBox *savegamemenu_descrinput; + + ui::Button *savegamemenu_savebutton; + +}; // class SaveGameMenu + + +} // namespace client + + +#endif // __INCLUDED_CLIENT_SAVEGAMEMENU_H__ + + |