2009-11-28 09:22:19 +01:00
|
|
|
#include <SDL_image.h>
|
|
|
|
#include <SDL_ttf.h>
|
2009-12-19 14:20:15 +01:00
|
|
|
#include <arpa/inet.h>
|
2009-11-28 09:22:19 +01:00
|
|
|
|
|
|
|
#include "menu.hh"
|
2009-12-06 10:11:04 +01:00
|
|
|
#include "gui.hh"
|
2009-12-04 20:10:49 +01:00
|
|
|
#include "menu_messages.hh"
|
2009-12-06 10:03:09 +01:00
|
|
|
#include "help_box.hh"
|
2009-12-05 13:04:00 +01:00
|
|
|
#include "dialogue_box.hh"
|
2009-11-28 09:22:19 +01:00
|
|
|
#include "sdl_ttf_font.hh"
|
|
|
|
#include "utils.hh"
|
2009-12-19 20:14:17 +01:00
|
|
|
#include "virtual_keyboard.hh"
|
2009-11-28 09:22:19 +01:00
|
|
|
|
|
|
|
extern SDL_Surface *screen;
|
|
|
|
|
2009-12-13 09:54:25 +01:00
|
|
|
#define THEME_ROOT_PATH "themes"
|
2009-12-19 14:20:15 +01:00
|
|
|
#define METADATA_ROOT_PATH "metadata"
|
|
|
|
#define GAME_ROOT_PATH "discs"
|
2009-12-13 09:54:25 +01:00
|
|
|
|
2009-12-06 10:06:25 +01:00
|
|
|
static const char *get_theme_path(const char *dir, const char *what)
|
2009-11-29 19:29:15 +01:00
|
|
|
{
|
|
|
|
static char buf[255];
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2009-12-13 09:54:25 +01:00
|
|
|
snprintf(buf, 254, "%s/%s/%s",
|
|
|
|
THEME_ROOT_PATH, dir, what);
|
2009-11-29 19:29:15 +01:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-12-06 10:17:21 +01:00
|
|
|
/* These are a bit of special cases... */
|
2009-12-13 11:02:27 +01:00
|
|
|
#include "disc_menu.cpp"
|
2010-01-06 17:42:55 +01:00
|
|
|
#include "bind_keys_menu.cpp"
|
2010-01-05 16:23:19 +01:00
|
|
|
#include "theme_menu.cpp"
|
2009-12-28 13:38:41 +01:00
|
|
|
#include "options_menu.cpp"
|
2009-12-29 14:23:01 +01:00
|
|
|
#include "network_menu.cpp"
|
2009-12-06 10:17:21 +01:00
|
|
|
#include "main_menu.cpp"
|
|
|
|
|
2009-12-13 09:59:06 +01:00
|
|
|
GuiView::GuiView()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-12-31 14:25:46 +01:00
|
|
|
void GuiView::updateTheme()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiView::viewPushCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiView::viewPopCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-11-28 09:22:19 +01:00
|
|
|
Gui::Gui()
|
|
|
|
{
|
2010-01-01 10:30:05 +01:00
|
|
|
this->np = NULL;
|
|
|
|
|
2009-11-28 09:22:19 +01:00
|
|
|
this->focus = NULL;
|
|
|
|
|
|
|
|
this->bg_left = NULL;
|
|
|
|
this->bg_middle = NULL;
|
|
|
|
this->bg_right = NULL;
|
|
|
|
this->bg_submenu_left = NULL;
|
|
|
|
this->bg_submenu_middle = NULL;
|
|
|
|
this->bg_submenu_right = NULL;
|
2009-11-28 09:43:37 +01:00
|
|
|
this->background = NULL;
|
2009-11-28 09:59:42 +01:00
|
|
|
this->main_menu_bg = NULL;
|
2009-12-04 18:55:25 +01:00
|
|
|
this->infobox = NULL;
|
|
|
|
this->textbox = NULL;
|
2009-11-29 19:29:15 +01:00
|
|
|
this->default_font = NULL;
|
2009-12-05 13:04:00 +01:00
|
|
|
this->dialogue_bg = NULL;
|
2009-12-04 21:06:52 +01:00
|
|
|
this->small_font = NULL;
|
2009-11-29 19:29:15 +01:00
|
|
|
|
|
|
|
this->n_views = 0;
|
|
|
|
this->views = NULL;
|
2009-12-16 18:47:03 +01:00
|
|
|
this->timerController = new TimerController();
|
2009-11-28 09:22:19 +01:00
|
|
|
|
2009-12-19 20:14:17 +01:00
|
|
|
VirtualKeyboard::kbd = new VirtualKeyboard(NULL);
|
|
|
|
|
2009-12-19 14:20:15 +01:00
|
|
|
this->theme_base_path = THEME_ROOT_PATH;
|
|
|
|
this->metadata_base_path = METADATA_ROOT_PATH;
|
|
|
|
this->game_base_path = GAME_ROOT_PATH;
|
|
|
|
|
2009-12-31 14:25:46 +01:00
|
|
|
this->dlg = NULL;
|
2010-01-02 13:28:18 +01:00
|
|
|
this->kbd = NULL;
|
2009-12-31 14:25:46 +01:00
|
|
|
|
2009-11-29 19:29:15 +01:00
|
|
|
/* Create the views */
|
2009-12-13 11:02:27 +01:00
|
|
|
this->mv = new MainView();
|
|
|
|
this->dv = new DiscView();
|
2009-12-28 13:38:41 +01:00
|
|
|
this->ov = new OptionsView();
|
2009-12-29 14:23:01 +01:00
|
|
|
this->nv = new NetworkView();
|
2010-01-05 16:23:19 +01:00
|
|
|
this->tv = new ThemeView();
|
2010-01-06 17:42:55 +01:00
|
|
|
this->bkv = new BindKeysView();
|
2009-12-04 21:45:17 +01:00
|
|
|
this->pushView(mv);
|
2009-11-28 09:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Gui::setTheme(const char *path)
|
|
|
|
{
|
|
|
|
this->bg_left = this->loadThemeImage(path, "bg_left.png");
|
|
|
|
this->bg_middle = this->loadThemeImage(path, "bg_middle.png");
|
|
|
|
this->bg_right = this->loadThemeImage(path, "bg_right.png");
|
|
|
|
this->bg_submenu_left = this->loadThemeImage(path, "bg_submenu_left.png");
|
|
|
|
this->bg_submenu_middle = this->loadThemeImage(path, "bg_submenu_middle.png");
|
|
|
|
this->bg_submenu_right = this->loadThemeImage(path, "bg_submenu_right.png");
|
|
|
|
|
2009-11-28 09:43:37 +01:00
|
|
|
this->background = this->loadThemeImage(path, "background.png");
|
2009-11-28 09:59:42 +01:00
|
|
|
this->main_menu_bg = this->loadThemeImage(path, "main_menu_bg.png");
|
2009-12-04 18:55:25 +01:00
|
|
|
this->infobox = this->loadThemeImage(path, "infobox.png");
|
|
|
|
this->textbox = this->loadThemeImage(path, "textbox.png");
|
2009-12-05 13:04:00 +01:00
|
|
|
this->dialogue_bg = this->loadThemeImage(path, "dialogue_box.png");
|
2009-12-13 11:10:33 +01:00
|
|
|
this->disc_info = this->loadThemeImage(path, "disc_info.png");
|
2009-11-28 09:43:37 +01:00
|
|
|
|
2009-12-20 11:12:47 +01:00
|
|
|
this->highlighted_key = this->loadThemeImage(path, "highlighted_key.png");
|
|
|
|
this->selected_key = this->loadThemeImage(path, "selected_key.png");
|
|
|
|
|
2009-12-04 21:06:52 +01:00
|
|
|
this->default_font = this->loadThemeFont(path, "font.ttf", 18);
|
|
|
|
this->small_font = this->loadThemeFont(path, "font.ttf", 16);
|
2009-11-28 09:22:19 +01:00
|
|
|
|
|
|
|
if (!this->bg_left || !this->bg_right || !this->bg_middle ||
|
|
|
|
!this->bg_submenu_left || !this->bg_submenu_right ||
|
|
|
|
!this->bg_submenu_middle ||
|
2009-12-05 13:04:00 +01:00
|
|
|
!this->dialogue_bg ||
|
2009-12-13 11:10:33 +01:00
|
|
|
!this->disc_info ||
|
2009-12-20 11:12:47 +01:00
|
|
|
!this->selected_key ||
|
|
|
|
!this->highlighted_key ||
|
2009-12-04 21:06:52 +01:00
|
|
|
!this->default_font ||
|
|
|
|
!this->small_font)
|
2009-11-28 09:22:19 +01:00
|
|
|
{
|
|
|
|
SDL_FreeSurface(this->bg_left);
|
|
|
|
SDL_FreeSurface(this->bg_middle);
|
|
|
|
SDL_FreeSurface(this->bg_right);
|
|
|
|
SDL_FreeSurface(this->bg_submenu_left);
|
|
|
|
SDL_FreeSurface(this->bg_submenu_middle);
|
|
|
|
SDL_FreeSurface(this->bg_submenu_right);
|
2009-11-28 09:43:37 +01:00
|
|
|
SDL_FreeSurface(this->background);
|
2009-11-28 09:59:42 +01:00
|
|
|
SDL_FreeSurface(this->main_menu_bg);
|
2009-12-04 18:55:25 +01:00
|
|
|
SDL_FreeSurface(this->infobox);
|
2009-12-05 13:04:00 +01:00
|
|
|
SDL_FreeSurface(this->dialogue_bg);
|
2009-12-13 11:10:33 +01:00
|
|
|
SDL_FreeSurface(this->disc_info);
|
2009-12-04 18:55:25 +01:00
|
|
|
SDL_FreeSurface(this->textbox);
|
2009-12-20 11:12:47 +01:00
|
|
|
SDL_FreeSurface(this->selected_key);
|
|
|
|
SDL_FreeSurface(this->highlighted_key);
|
2009-11-28 09:22:19 +01:00
|
|
|
|
2009-11-29 19:29:15 +01:00
|
|
|
if (this->default_font)
|
|
|
|
delete this->default_font;
|
2009-12-04 21:06:52 +01:00
|
|
|
if (this->small_font)
|
|
|
|
delete this->small_font;
|
2009-11-28 09:22:19 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-29 19:29:15 +01:00
|
|
|
|
2009-12-13 11:02:27 +01:00
|
|
|
this->mv->updateTheme();
|
|
|
|
this->dv->updateTheme();
|
2009-12-28 13:38:41 +01:00
|
|
|
this->ov->updateTheme();
|
2009-12-29 14:23:01 +01:00
|
|
|
this->nv->updateTheme();
|
2010-01-05 16:23:19 +01:00
|
|
|
this->tv->updateTheme();
|
2010-01-06 17:42:55 +01:00
|
|
|
this->bkv->updateTheme();
|
2009-11-28 09:22:19 +01:00
|
|
|
|
2010-01-02 13:28:18 +01:00
|
|
|
VirtualKeyboard::kbd->updateTheme();
|
|
|
|
|
2009-11-28 09:22:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gui::runLogic(void)
|
|
|
|
{
|
2009-12-05 10:33:58 +01:00
|
|
|
GuiView *cur_view = this->peekView();
|
2009-11-29 19:29:15 +01:00
|
|
|
|
2009-12-05 10:33:58 +01:00
|
|
|
if (!this->is_active || !cur_view)
|
|
|
|
return;
|
2009-12-31 14:25:46 +01:00
|
|
|
if (this->dlg)
|
|
|
|
this->dlg->runLogic();
|
2010-01-02 13:28:18 +01:00
|
|
|
else if (this->kbd)
|
|
|
|
this->kbd->runLogic();
|
2009-12-31 14:25:46 +01:00
|
|
|
else
|
|
|
|
cur_view->runLogic();
|
2009-12-16 18:47:03 +01:00
|
|
|
this->timerController->tick();
|
2009-11-28 09:22:19 +01:00
|
|
|
}
|
|
|
|
|
2009-12-04 21:45:17 +01:00
|
|
|
void Gui::pushView(GuiView *view)
|
2009-11-28 09:22:19 +01:00
|
|
|
{
|
2009-11-29 19:29:15 +01:00
|
|
|
int cur = this->n_views;
|
2009-11-28 09:22:19 +01:00
|
|
|
|
2009-11-29 19:29:15 +01:00
|
|
|
this->n_views++;
|
|
|
|
this->views = (GuiView**)xrealloc(this->views,
|
|
|
|
sizeof(GuiView*) * this->n_views);
|
|
|
|
this->views[cur] = view;
|
2009-12-31 14:25:46 +01:00
|
|
|
view->viewPushCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gui::pushDialogueBox(DialogueBox *dlg)
|
|
|
|
{
|
|
|
|
this->dlg = dlg;
|
|
|
|
}
|
|
|
|
|
|
|
|
DialogueBox *Gui::popDialogueBox()
|
|
|
|
{
|
|
|
|
DialogueBox *out = this->dlg;
|
|
|
|
this->dlg = NULL;
|
|
|
|
|
|
|
|
return out;
|
2009-11-28 09:22:19 +01:00
|
|
|
}
|
|
|
|
|
2009-12-04 21:45:17 +01:00
|
|
|
GuiView *Gui::popView()
|
|
|
|
{
|
2009-12-06 20:21:48 +01:00
|
|
|
GuiView *cur = this->peekView();
|
|
|
|
|
2009-12-31 14:25:46 +01:00
|
|
|
if (!cur)
|
|
|
|
return NULL;
|
|
|
|
cur->viewPopCallback();
|
2009-12-06 20:21:48 +01:00
|
|
|
|
2009-12-04 21:45:17 +01:00
|
|
|
this->n_views--;
|
|
|
|
if (this->n_views <= 0)
|
|
|
|
{
|
|
|
|
free(this->views);
|
2009-12-06 20:21:48 +01:00
|
|
|
this->views = NULL;
|
|
|
|
this->n_views = 0;
|
2009-12-13 10:21:27 +01:00
|
|
|
/* Deactivate when no views are left */
|
|
|
|
this->is_active = false;
|
2009-12-06 20:21:48 +01:00
|
|
|
|
2009-12-04 21:45:17 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->views = (GuiView**)xrealloc(this->views,
|
|
|
|
sizeof(GuiView*) * this->n_views);
|
2009-12-31 14:25:46 +01:00
|
|
|
return cur;
|
2009-12-04 21:45:17 +01:00
|
|
|
}
|
|
|
|
|
2009-12-05 10:33:58 +01:00
|
|
|
void Gui::exitMenu()
|
2009-12-04 21:45:17 +01:00
|
|
|
{
|
2009-12-05 10:34:58 +01:00
|
|
|
printf("Exiting the menu system\n");
|
2009-12-06 20:21:48 +01:00
|
|
|
|
|
|
|
/* Pop all views */
|
|
|
|
while (this->popView())
|
|
|
|
;
|
2009-12-04 21:45:17 +01:00
|
|
|
}
|
|
|
|
|
2009-11-28 09:22:19 +01:00
|
|
|
void Gui::pushEvent(SDL_Event *ev)
|
|
|
|
{
|
2009-12-05 10:33:58 +01:00
|
|
|
GuiView *cur_view = this->peekView();
|
|
|
|
|
2009-12-05 10:36:24 +01:00
|
|
|
if (!this->is_active || !cur_view)
|
|
|
|
return;
|
2009-12-31 14:25:46 +01:00
|
|
|
if (this->dlg)
|
|
|
|
this->dlg->pushEvent(ev);
|
2010-01-02 13:28:18 +01:00
|
|
|
else if (this->kbd)
|
|
|
|
this->kbd->pushEvent(ev);
|
|
|
|
else
|
|
|
|
cur_view->pushEvent(ev);
|
2009-11-28 09:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Gui::draw(SDL_Surface *where)
|
|
|
|
{
|
2009-12-05 10:33:58 +01:00
|
|
|
GuiView *cur_view = this->peekView();
|
|
|
|
|
|
|
|
if (!this->is_active || !cur_view)
|
2009-11-28 09:22:19 +01:00
|
|
|
return;
|
2009-11-28 09:43:37 +01:00
|
|
|
|
|
|
|
SDL_BlitSurface(this->background, NULL, screen, NULL);
|
2009-12-05 10:33:58 +01:00
|
|
|
cur_view->draw(where);
|
2010-01-02 13:28:18 +01:00
|
|
|
if (this->kbd)
|
|
|
|
this->kbd->draw(where);
|
2009-12-31 14:25:46 +01:00
|
|
|
if (this->dlg)
|
|
|
|
this->dlg->draw(where);
|
2009-11-28 09:43:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Gui::activate()
|
|
|
|
{
|
|
|
|
this->is_active = true;
|
2010-01-01 10:30:05 +01:00
|
|
|
/* FIXME! TMP! TMP! */
|
|
|
|
this->np = new Prefs();
|
2009-11-28 09:43:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Gui::deActivate()
|
|
|
|
{
|
2010-01-01 10:30:05 +01:00
|
|
|
/* FIXME! TMP! TMP! */
|
|
|
|
delete this->np;
|
2009-11-28 09:43:37 +01:00
|
|
|
this->is_active = false;
|
2009-11-28 09:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Surface *Gui::loadThemeImage(const char *dir, const char *what)
|
|
|
|
{
|
2009-11-29 19:29:15 +01:00
|
|
|
return IMG_Load(get_theme_path(dir, what));
|
2009-11-28 09:22:19 +01:00
|
|
|
}
|
|
|
|
|
2009-12-04 21:06:52 +01:00
|
|
|
Font *Gui::loadThemeFont(const char *dir, const char *what, int size)
|
2009-11-28 09:22:19 +01:00
|
|
|
{
|
|
|
|
TTF_Font *fnt;
|
|
|
|
|
2009-12-04 21:06:52 +01:00
|
|
|
fnt = read_and_alloc_font(get_theme_path(dir, what), size);
|
2009-11-28 09:22:19 +01:00
|
|
|
if (!fnt)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return new Font_TTF(fnt, 255,255,255);
|
|
|
|
}
|
2009-12-13 09:54:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* The singleton/factory stuff */
|
|
|
|
Gui *Gui::gui;
|
|
|
|
void Gui::init()
|
|
|
|
{
|
2009-12-13 11:02:27 +01:00
|
|
|
Gui::gui = new Gui();
|
2009-12-13 09:54:25 +01:00
|
|
|
|
|
|
|
/* Set the default theme */
|
2009-12-13 11:02:27 +01:00
|
|
|
panic_if(!Gui::gui->setTheme("default"),
|
2009-12-13 09:54:25 +01:00
|
|
|
"Setting default theme failed\n");
|
|
|
|
}
|