Refactoring: Move pushEvent/popEvent to the Widget class

This commit is contained in:
simon.kagstrom 2009-12-19 17:58:28 +00:00
parent 39db099cd4
commit 24568dc5ed
4 changed files with 77 additions and 77 deletions

View File

@ -317,71 +317,6 @@ void Menu::runLogic()
}
}
event_t Menu::popEvent()
{
event_t out;
if (this->ev_head == this->ev_tail)
return EVENT_NONE;
out = this->event_stack[this->ev_tail];
this->ev_tail = (this->ev_tail + 1) % 8;
return out;
}
void Menu::pushEvent(event_t ev)
{
/* Push... */
this->event_stack[this->ev_head] = ev;
/* ... and update */
this->ev_head = (this->ev_head + 1) % 8;
if (this->ev_head == this->ev_tail)
this->ev_tail = (this->ev_tail + 1) % 8;
}
void Menu::pushEvent(SDL_Event *ev)
{
switch(ev->type)
{
case SDL_KEYDOWN:
switch (ev->key.keysym.sym)
{
case SDLK_UP:
this->pushEvent(KEY_UP);
break;
case SDLK_DOWN:
this->pushEvent(KEY_DOWN);
break;
case SDLK_LEFT:
this->pushEvent(KEY_LEFT);
break;
case SDLK_RIGHT:
this->pushEvent(KEY_RIGHT);
break;
case SDLK_PAGEDOWN:
this->pushEvent(KEY_PAGEDOWN);
break;
case SDLK_PAGEUP:
this->pushEvent(KEY_PAGEUP);
break;
case SDLK_RETURN:
case SDLK_SPACE:
this->pushEvent(KEY_SELECT);
break;
case SDLK_HOME:
case SDLK_ESCAPE:
this->pushEvent(KEY_ESCAPE);
break;
default:
break;
}
default:
break;
}
}
void Menu::setText(const char **messages, int *submenu_defaults)
{
int submenu;

10
menu.hh
View File

@ -26,7 +26,6 @@ typedef struct
int sel;
} submenu_t;
typedef int event_t;
class Menu : public Widget
{
@ -56,8 +55,6 @@ public:
void setText(const char **messages, int *submenu_defaults = NULL);
void pushEvent(SDL_Event *ev);
void runLogic();
void draw(SDL_Surface *where,
@ -88,10 +85,6 @@ public:
virtual int selectNext(event_t ev);
virtual void pushEvent(event_t ev);
event_t popEvent();
protected:
const char **pp_msgs;
@ -114,9 +107,6 @@ protected:
int cur_sel; /* Main selection */
int n_entries;
int ev_head, ev_tail;
event_t event_stack[8];
};
#endif /* !__MENU_H__ */

66
widget.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "widget.hh"
event_t Widget::popEvent()
{
event_t out;
if (this->ev_head == this->ev_tail)
return EVENT_NONE;
out = this->event_stack[this->ev_tail];
this->ev_tail = (this->ev_tail + 1) % 8;
return out;
}
void Widget::pushEvent(event_t ev)
{
/* Push... */
this->event_stack[this->ev_head] = ev;
/* ... and update */
this->ev_head = (this->ev_head + 1) % 8;
if (this->ev_head == this->ev_tail)
this->ev_tail = (this->ev_tail + 1) % 8;
}
void Widget::pushEvent(SDL_Event *ev)
{
switch(ev->type)
{
case SDL_KEYDOWN:
switch (ev->key.keysym.sym)
{
case SDLK_UP:
this->pushEvent(KEY_UP);
break;
case SDLK_DOWN:
this->pushEvent(KEY_DOWN);
break;
case SDLK_LEFT:
this->pushEvent(KEY_LEFT);
break;
case SDLK_RIGHT:
this->pushEvent(KEY_RIGHT);
break;
case SDLK_PAGEDOWN:
this->pushEvent(KEY_PAGEDOWN);
break;
case SDLK_PAGEUP:
this->pushEvent(KEY_PAGEUP);
break;
case SDLK_RETURN:
case SDLK_SPACE:
this->pushEvent(KEY_SELECT);
break;
case SDLK_HOME:
case SDLK_ESCAPE:
this->pushEvent(KEY_ESCAPE);
break;
default:
break;
}
default:
break;
}
}

View File

@ -3,7 +3,7 @@
#include <SDL.h>
enum {
enum key_event {
EVENT_NONE = 0,
KEY_UP = 1,
KEY_DOWN = 2,
@ -16,16 +16,25 @@ enum {
KEY_HELP = 256,
};
typedef enum key_event event_t;
class Widget
{
public:
virtual void pushEvent(SDL_Event *ev) = 0;
virtual void pushEvent(event_t ev);
virtual void pushEvent(SDL_Event *ev);
virtual void runLogic() = 0;
virtual void draw(SDL_Surface *where,
int x, int y, int w, int h) = 0;
virtual event_t popEvent();
protected:
int ev_head, ev_tail;
event_t event_stack[8];
};
#endif