From a7aec61e02d56121180c5a3165329ac9b99c9f22 Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Sun, 13 Dec 2009 10:02:27 +0000 Subject: [PATCH] Add a disc menu (not quite finished yet), and continued refactoring --- Makefile | 2 +- disc_menu.cpp | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ gui.cpp | 17 +++---- gui.hh | 4 ++ main_menu.cpp | 8 +-- utils.hh | 2 + 6 files changed, 156 insertions(+), 14 deletions(-) create mode 100644 disc_menu.cpp diff --git a/Makefile b/Makefile index f07b7de..a087721 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ all: menu menu.oo: menu.cpp menu.hh utils.hh font.hh Makefile gui.oo: Makefile gui.cpp gui.hh font.hh menu.hh sdl_ttf_font.hh \ - dialogue_box.hh help_box.hh main_menu.cpp + dialogue_box.hh help_box.hh main_menu.cpp disc_menu.cpp utils.oo: utils.cpp utils.hh Makefile diff --git a/disc_menu.cpp b/disc_menu.cpp new file mode 100644 index 0000000..46041737 --- /dev/null +++ b/disc_menu.cpp @@ -0,0 +1,137 @@ +#include "menu.hh" + +class DiscView; +class DiscMenu : public Menu +{ + friend class DiscView; + +public: + DiscMenu(Font *font, GuiView *parent) : Menu(font) + { + this->parent = parent; + this->path = NULL; + + /* If nothing else: Set the default list */ + this->setDefaultFileList(); + } + + ~DiscMenu() + { + this->freeFileList(); + } + + virtual void selectCallback(int which) + { + printf("entry %d selected: %s\n", which, this->pp_msgs[which]); + } + + void setDirectory(const char *path) + { + const char *exts[] = {".d64", ".D64", ".t64", ".T64", ".prg", + ".PRG", ".p00", ".P00"}; + + this->freeFileList(); + this->file_list = get_file_list(path, exts); + if (!this->file_list) + this->setDefaultFileList(); + this->setText(this->file_list); + } + + virtual void hoverCallback(int which) + { + } + + virtual void escapeCallback(int which) + { + Gui::gui->exitMenu(); + } + +private: + void setDefaultFileList() + { + this->file_list = (const char **)xmalloc(2 * sizeof(char*)); + this->file_list[0] = xstrdup("None"); + } + + void freeFileList() + { + if (!this->file_list) + return; + for (int i = 0; this->file_list[i]; i++) + free((void*)this->file_list[i]); + free(this->file_list); + } + + const char *path; + const char **file_list; + GuiView *parent; +}; + + +class DiscView : public GuiView +{ +public: + DiscView() : GuiView() + { + this->menu = new DiscMenu(NULL, this); + + this->bg = NULL; + this->infobox = NULL; + this->textbox = NULL; + } + + ~DiscView() + { + delete this->menu; + } + + void setDirectory(const char *path) + { + this->menu->setDirectory(path); + } + + void updateTheme() + { + this->bg = Gui::gui->main_menu_bg; + this->infobox = Gui::gui->infobox; + this->textbox = Gui::gui->textbox; + + this->menu->setFont(Gui::gui->default_font); + this->menu->setSelectedBackground(Gui::gui->bg_left, Gui::gui->bg_middle, + Gui::gui->bg_right, Gui::gui->bg_submenu_left, + Gui::gui->bg_submenu_middle, Gui::gui->bg_submenu_right); + } + + void runLogic() + { + this->menu->runLogic(); + } + + void pushEvent(SDL_Event *ev) + { + this->menu->pushEvent(ev); + } + + void draw(SDL_Surface *where) + { + SDL_Rect dst; + + /* Blit the backgrounds */ + dst = (SDL_Rect){20,45,300,400}; + SDL_BlitSurface(this->bg, NULL, where, &dst); + + dst = (SDL_Rect){350,13,0,0}; + SDL_BlitSurface(this->infobox, NULL, where, &dst); + + dst = (SDL_Rect){350,242,0,0}; + SDL_BlitSurface(this->textbox, NULL, where, &dst); + + this->menu->draw(where, 50, 70, 300, 400); + } + +protected: + DiscMenu *menu; + SDL_Surface *bg; + SDL_Surface *infobox; + SDL_Surface *textbox; +}; diff --git a/gui.cpp b/gui.cpp index 44bc4c5..0741e8d 100644 --- a/gui.cpp +++ b/gui.cpp @@ -11,10 +11,6 @@ extern SDL_Surface *screen; -class Gui; -class MainMenu; -class MainView; - #define THEME_ROOT_PATH "themes" static const char *get_theme_path(const char *dir, const char *what) @@ -29,6 +25,7 @@ static const char *get_theme_path(const char *dir, const char *what) } /* These are a bit of special cases... */ +#include "disc_menu.cpp" #include "main_menu.cpp" GuiView::GuiView() @@ -57,7 +54,8 @@ Gui::Gui() this->views = NULL; /* Create the views */ - MainView *mv = new MainView(); + this->mv = new MainView(); + this->dv = new DiscView(); this->pushView(mv); } @@ -107,8 +105,8 @@ bool Gui::setTheme(const char *path) return false; } - for (int i = 0; i < this->n_views; i++) - this->views[i]->updateTheme(); + this->mv->updateTheme(); + this->dv->updateTheme(); return true; } @@ -216,10 +214,9 @@ Font *Gui::loadThemeFont(const char *dir, const char *what, int size) Gui *Gui::gui; void Gui::init() { - Gui *p = new Gui(); + Gui::gui = new Gui(); /* Set the default theme */ - panic_if(!p->setTheme("default"), + panic_if(!Gui::gui->setTheme("default"), "Setting default theme failed\n"); - Gui::gui = p; } diff --git a/gui.hh b/gui.hh index f0acef0..4c2267f 100644 --- a/gui.hh +++ b/gui.hh @@ -7,6 +7,8 @@ #include "font.hh" class Gui; +class MainView; +class DiscView; class GuiView { @@ -76,6 +78,8 @@ public: Font *default_font; Font *small_font; + MainView *mv; + DiscView *dv; GuiView **views; int n_views; diff --git a/main_menu.cpp b/main_menu.cpp index a9791a1..c99e573 100644 --- a/main_menu.cpp +++ b/main_menu.cpp @@ -65,9 +65,11 @@ public: printf("entry %d selected: %s\n", which, this->pp_msgs[which]); switch (which) { - case 1: - if (this->p_submenus[0].sel == 0) - ; + case 0: + if (this->p_submenus[0].sel == 0) { + Gui::gui->dv->setDirectory("discs"); + Gui::gui->pushView(Gui::gui->dv); + } break; case 11: diff --git a/utils.hh b/utils.hh index 7615ad4..c65c77d 100644 --- a/utils.hh +++ b/utils.hh @@ -49,4 +49,6 @@ static inline void *xrealloc(void *ptr, size_t sz) TTF_Font *read_and_alloc_font(const char *path, int pt_size); +const char **get_file_list(const char *base_dir, const char *exts[]); + #endif /* __UTILS_H__ */