Add a disc menu (not quite finished yet), and continued refactoring

This commit is contained in:
simon.kagstrom 2009-12-13 10:02:27 +00:00
parent 281b5a8b05
commit a7aec61e02
6 changed files with 156 additions and 14 deletions

View File

@ -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

137
disc_menu.cpp Normal file
View File

@ -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;
};

17
gui.cpp
View File

@ -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;
}

4
gui.hh
View File

@ -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;

View File

@ -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:

View File

@ -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__ */