From 80a058c872c9746841e284f4855688bac9f52cf2 Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Fri, 4 Dec 2009 19:10:49 +0000 Subject: [PATCH] Implemented help system (OK, has some memory corruption currently) --- Makefile | 2 +- frodo_menu.cpp | 58 +++++++++++++++++++++++++++++++---------------- menu.cpp | 44 ++++++++++++++++++++++------------- menu.hh | 1 - menu_messages.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++ menu_messages.hh | 7 ++++++ 6 files changed, 128 insertions(+), 38 deletions(-) create mode 100644 menu_messages.cpp create mode 100644 menu_messages.hh diff --git a/Makefile b/Makefile index 5a4ae89..bad46e3 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS=menu.oo main.oo utils.oo frodo_menu.oo dialogue_box.oo +OBJS=menu.oo main.oo utils.oo frodo_menu.oo dialogue_box.oo menu_messages.oo all: menu diff --git a/frodo_menu.cpp b/frodo_menu.cpp index b040824..539af38 100644 --- a/frodo_menu.cpp +++ b/frodo_menu.cpp @@ -3,6 +3,7 @@ #include "menu.hh" #include "frodo_menu.hh" +#include "menu_messages.hh" #include "sdl_ttf_font.hh" #include "utils.hh" @@ -21,30 +22,40 @@ const char *get_theme_path(const char *dir, const char *what) return buf; } +class HelpMenu : public Menu +{ +public: + HelpMenu(Font *font, const char ***all_messages) : Menu(font) + { + this->all_messages = all_messages; + } + + void updateHelpMessage(int which) + { + this->setText(this->all_messages[which]); + } + + virtual void selectCallback(int which) + { + } + virtual void hoverCallback(int which) + { + } + virtual void escapeCallback(int which) + { + } + +protected: + const char ***all_messages; +}; + class MainMenu : public Menu { public: - MainMenu(Font *font, GuiView *parent) : Menu(font) + MainMenu(Font *font, HelpMenu *help, GuiView *parent) : Menu(font) { - static const char *messages[] = { - /*00*/ "File", - /*01*/ "^|Insert|Start", - /*02*/ "States", - /*03*/ "^|Load|Save|Delete", - /*04*/ "Keyboard", - /*05*/ "^|Type|Macro|Bind", - /*06*/ " ", - /*07*/ "Reset the C=64", - /*08*/ "Networking", - /*09*/ "Options", - /*10*/ "Advanced Options", - /*11*/ "Help", - /*12*/ "Quit", - NULL - }; - - this->setText(messages); this->parent = parent; + this->help = help; } virtual void selectCallback(int which) @@ -57,6 +68,7 @@ public: virtual void hoverCallback(int which) { printf("entry %d hover over: %s\n", which, this->pp_msgs[which]); + this->help->updateHelpMessage(which); } virtual void escapeCallback(int which) @@ -66,6 +78,7 @@ public: private: GuiView *parent; + HelpMenu *help; }; @@ -74,7 +87,9 @@ class MainView : public GuiView public: MainView(Gui *parent) : GuiView(parent) { - this->menu = new MainMenu(NULL, this); + this->help = new HelpMenu(NULL, main_menu_help); + this->menu = new MainMenu(NULL, this->help, this); + this->menu->setText(main_menu_messages); this->bg = NULL; this->infobox = NULL; this->textbox = NULL; @@ -87,6 +102,7 @@ public: this->textbox = parent->textbox; this->menu->setFont(this->parent->default_font); + this->help->setFont(this->parent->default_font); this->menu->setSelectedBackground(this->parent->bg_left, this->parent->bg_middle, this->parent->bg_right, this->parent->bg_submenu_left, this->parent->bg_submenu_middle, this->parent->bg_submenu_right); @@ -117,10 +133,12 @@ public: SDL_BlitSurface(this->textbox, NULL, where, &dst); this->menu->draw(where, 50, 70, 300, 400); + this->help->draw(where, 354, 24, 264, 210); } protected: MainMenu *menu; + HelpMenu *help; SDL_Surface *bg; SDL_Surface *infobox; SDL_Surface *textbox; diff --git a/menu.cpp b/menu.cpp index 0a0d7c7..4f40ddb 100644 --- a/menu.cpp +++ b/menu.cpp @@ -35,9 +35,6 @@ void Menu::printText(SDL_Surface *where, const char *msg, SDL_Color clr, int last_char = w / pixels_per_char; /* FIXME! Handle some corner cases here (short strings etc) */ - panic_if((unsigned)last_char > strlen(msg), - "last character (%d) is after the string length (%d)\n", - last_char, strlen(msg)); if (last_char > 3) { buf[last_char - 2] = '.'; @@ -101,7 +98,9 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h) int entries_visible = h / line_height - 2; int start_entry_visible = 0; - panic_if(!this->pp_msgs, "Set the messages before drawing, thank you\n"); + /* No messages - nothing to draw */ + if (!this->pp_msgs) + return; if (this->cur_sel - start_entry_visible > entries_visible) { @@ -215,6 +214,9 @@ submenu_t *Menu::findSubmenu(int index) int Menu::selectOne(int which) { + panic_if(!this->pp_msgs, + "Can't select a message without any messages!"); + if (which >= this->n_entries) which = 0; this->cur_sel = which; @@ -222,6 +224,7 @@ int Menu::selectOne(int which) if (this->pp_msgs[this->cur_sel][0] == ' ' || IS_SUBMENU(this->pp_msgs[this->cur_sel])) this->selectNext(0, 1); + this->hoverCallback(this->cur_sel); return this->cur_sel; } @@ -230,6 +233,9 @@ int Menu::selectNext(int dx, int dy) { int next; + panic_if(!this->pp_msgs, + "Can't select the next message without any messages!"); + this->cur_sel = this->getNextEntry(dy); next = this->getNextEntry(dy + 1); @@ -254,20 +260,26 @@ int Menu::selectNext(int dx, int dy) int Menu::selectNext(event_t ev) { + int now = this->cur_sel; + int next; + switch (ev) { case KEY_UP: - this->selectNext(0, -1); break; + next = this->selectNext(0, -1); break; case KEY_DOWN: - this->selectNext(0, 1); break; + next = this->selectNext(0, 1); break; case KEY_LEFT: - this->selectNext(-1, 0); break; + next = this->selectNext(-1, 0); break; case KEY_RIGHT: - this->selectNext(1, 0); break; + next = this->selectNext(1, 0); break; default: panic("selectNext(ev) called with event %d\n", ev); } + if (now != next) + this->hoverCallback(this->cur_sel); + return this->cur_sel; } @@ -283,14 +295,8 @@ void Menu::runLogic() case KEY_DOWN: case KEY_LEFT: case KEY_RIGHT: - { - int now = this->cur_sel; - int next; - - next = this->selectNext(ev); - if (now != next) - this->hoverCallback(this->cur_sel); - } break; + this->selectNext(ev); + break; case KEY_SELECT: this->selectCallback(this->cur_sel); break; case KEY_ESCAPE: @@ -376,6 +382,12 @@ void Menu::setText(const char **messages, int *submenu_defaults) free(this->p_submenus); free(this->pp_msgs); + /* Empty messages are allowed */ + this->p_submenus = NULL; + this->pp_msgs = NULL; + if (!messages) + return; + for (this->n_entries = 0; messages[this->n_entries]; this->n_entries++) { /* Is this a submenu? */ diff --git a/menu.hh b/menu.hh index 88ac12b..98b534a 100644 --- a/menu.hh +++ b/menu.hh @@ -104,7 +104,6 @@ protected: event_t popEvent(); - const char *title; const char **pp_msgs; Font *font; SDL_Color text_color; diff --git a/menu_messages.cpp b/menu_messages.cpp new file mode 100644 index 0000000..eccbbbf --- /dev/null +++ b/menu_messages.cpp @@ -0,0 +1,54 @@ +#include + +#include "menu_messages.hh" + +const char **main_menu_messages = (const char*[]){ + /*00*/ "File", + /*01*/ "^|Insert|Start", + /*02*/ "States", + /*03*/ "^|Load|Save|Delete", + /*04*/ "Keyboard", + /*05*/ "^|Type|Macro|Bind", + /*06*/ " ", + /*07*/ "Reset the C=64", + /*08*/ "Networking", + /*09*/ "Options", + /*10*/ "Help", + /*11*/ "Quit", + NULL +}; + +const char **main_menu_help[] = { + (const char*[]){ + "Insert a disc/tape or", + "start it", + NULL, + }, + NULL, + (const char*[]){ + "Load/save or delete game", + "states", + NULL, + }, + NULL, + (const char*[]){ + "Bind keyboard keys to the", + "joysticks, use pre-defined", + "macros, or type with the", + "virtual keyboard", + NULL, + }, + NULL, + NULL, + NULL, + (const char*[]){ + "Networking menu for playing", + "C64 games online.", + NULL, + }, + (const char*[]){ + "Configure Frodo", + NULL, + }, + NULL, +}; diff --git a/menu_messages.hh b/menu_messages.hh new file mode 100644 index 0000000..67140da --- /dev/null +++ b/menu_messages.hh @@ -0,0 +1,7 @@ +#ifndef MENU_MESSAGES_HH +#define MENU_MESSAGES_HH + +extern const char **main_menu_messages; +extern const char **main_menu_help[]; + +#endif