From c749c3993659e502189d7f0f45d766e6d9052f0b Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Fri, 4 Dec 2009 18:01:59 +0000 Subject: [PATCH] Add callback for hovering over an entry --- frodo_menu.cpp | 5 +++++ menu.cpp | 25 ++++++++++++++++++------- menu.hh | 8 +++++--- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/frodo_menu.cpp b/frodo_menu.cpp index 7d11a77..b040824 100644 --- a/frodo_menu.cpp +++ b/frodo_menu.cpp @@ -54,6 +54,11 @@ public: exit(0); } + virtual void hoverCallback(int which) + { + printf("entry %d hover over: %s\n", which, this->pp_msgs[which]); + } + virtual void escapeCallback(int which) { printf("entry %d escaped: %s\n", which, this->pp_msgs[which]); diff --git a/menu.cpp b/menu.cpp index db24698..0a0d7c7 100644 --- a/menu.cpp +++ b/menu.cpp @@ -213,7 +213,7 @@ submenu_t *Menu::findSubmenu(int index) } -void Menu::selectOne(int which) +int Menu::selectOne(int which) { if (which >= this->n_entries) which = 0; @@ -222,9 +222,11 @@ void Menu::selectOne(int which) if (this->pp_msgs[this->cur_sel][0] == ' ' || IS_SUBMENU(this->pp_msgs[this->cur_sel])) this->selectNext(0, 1); + + return this->cur_sel; } -void Menu::selectNext(int dx, int dy) +int Menu::selectNext(int dx, int dy) { int next; @@ -234,8 +236,7 @@ void Menu::selectNext(int dx, int dy) /* We want to skip this for some reason */ if (this->pp_msgs[this->cur_sel][0] == ' ' || IS_SUBMENU(this->pp_msgs[this->cur_sel]) ) { - this->selectNext(dx, dy); - return; + return this->selectNext(dx, dy); } /* If the next is a submenu */ @@ -247,9 +248,11 @@ void Menu::selectNext(int dx, int dy) p_submenu->sel = (p_submenu->sel + dx) < 0 ? p_submenu->n_entries - 1 : (p_submenu->sel + dx) % p_submenu->n_entries; } + + return this->cur_sel; } -void Menu::selectNext(event_t ev) +int Menu::selectNext(event_t ev) { switch (ev) { @@ -264,6 +267,8 @@ void Menu::selectNext(event_t ev) default: panic("selectNext(ev) called with event %d\n", ev); } + + return this->cur_sel; } void Menu::runLogic() @@ -278,8 +283,14 @@ void Menu::runLogic() case KEY_DOWN: case KEY_LEFT: case KEY_RIGHT: - this->selectNext(ev); - break; + { + int now = this->cur_sel; + int next; + + next = this->selectNext(ev); + if (now != next) + this->hoverCallback(this->cur_sel); + } break; case KEY_SELECT: this->selectCallback(this->cur_sel); break; case KEY_ESCAPE: diff --git a/menu.hh b/menu.hh index 195e9d6..88ac12b 100644 --- a/menu.hh +++ b/menu.hh @@ -84,6 +84,8 @@ protected: void printText(SDL_Surface *where, const char *msg, SDL_Color clr, int x, int y, int w, int h); + virtual void hoverCallback(int which) = 0; + virtual void selectCallback(int which) = 0; virtual void escapeCallback(int which) = 0; @@ -92,11 +94,11 @@ protected: int getNextEntry(int dy); - void selectOne(int which); + int selectOne(int which); - void selectNext(int dx, int dy); + int selectNext(int dx, int dy); - void selectNext(event_t ev); + int selectNext(event_t ev); void pushEvent(event_t ev);