MADS: Add support to event manager for intercepting events

This commit is contained in:
Paul Gilbert 2014-07-26 20:19:35 -04:00
parent b87723f34b
commit 7ea081e7a0
5 changed files with 29 additions and 20 deletions

View File

@ -46,6 +46,7 @@ EventsManager::EventsManager(MADSEngine *vm) {
_mouseMoved = false;
_vD8 = 0;
_rightMousePressed = false;
_eventTarget = nullptr;
}
EventsManager::~EventsManager() {
@ -138,6 +139,12 @@ void EventsManager::pollEvents() {
Common::Event event;
while (g_system->getEventManager()->pollEvent(event)) {
// If an event target is specified, pass the event to it
if (_eventTarget) {
_eventTarget->onEvent(event);
continue;
}
// Handle keypress
switch (event.type) {
case Common::EVENT_QUIT:

View File

@ -39,6 +39,11 @@ enum CursorType { CURSOR_NONE = 0, CURSOR_ARROW = 1, CURSOR_WAIT = 2, CURSOR_GO_
class MADSEngine;
class EventTarget {
public:
virtual bool onEvent(Common::Event &event) { return false; }
};
class EventsManager {
private:
MADSEngine *_vm;
@ -46,6 +51,7 @@ private:
uint32 _priorFrameTime;
Common::Point _mousePos;
Common::Point _currentPos;
EventTarget *_eventTarget;
/**
* Updates the cursor image when the current cursor changes
@ -121,6 +127,11 @@ public:
*/
void pollEvents();
/**
* Sets an event handler other than the events manager
*/
void setEventTarget(EventTarget *target) { _eventTarget = target; }
/**
* Return the current mouse position
*/

View File

@ -107,7 +107,7 @@ enum DialogTextAlign { ALIGN_NONE = 0, ALIGN_CENTER = -1, ALIGN_AT_CENTER = -2,
enum DialogState { DLGSTATE_UNSELECTED = 0, DLGSTATE_SELECTED = 1, DLGSTATE_FOCUSED = 2 };
class FullScreenDialog {
class FullScreenDialog: public EventTarget {
protected:
/**
* Engine reference

View File

@ -46,11 +46,10 @@ void MenuView::show() {
EventsManager &events = *_vm->_events;
display();
events.setEventTarget(this);
events.hideCursor();
while (!_breakFlag && !_vm->shouldQuit()) {
handleEvents();
if (_redrawFlag) {
scene.drawElements(_vm->_game->_fx, _vm->_game->_fx);
_redrawFlag = false;
@ -60,6 +59,8 @@ void MenuView::show() {
_vm->_game->_fx = kTransitionNone;
doFrame();
}
events.setEventTarget(nullptr);
}
void MenuView::display() {
@ -68,13 +69,6 @@ void MenuView::display() {
FullScreenDialog::display();
}
void MenuView::handleEvents() {
Common::Event event;
while (g_system->getEventManager()->pollEvent(event))
onEvent(event);
}
/*------------------------------------------------------------------------*/
MainMenu::MainMenu(MADSEngine *vm): MenuView(vm) {
@ -127,11 +121,6 @@ void MainMenu::doFrame() {
if (_menuItemIndex == 6)
return;
// Delete any previous sprite slots
scene._spriteSlots.deleteTimer(1);
if (_menuItemIndex == -1)
scene._spriteSlots.deleteTimer(2);
// If the user has chosen to skip the animation, show the full menu immediately
if (_skipFlag && _menuItemIndex >= 0) {
// Quickly loop through all the menu items to display each's final frame
@ -163,13 +152,14 @@ void MainMenu::doFrame() {
void MainMenu::addSpriteSlot() {
Scene &scene = _vm->_game->_scene;
SpriteSlots &spriteSlots = scene._spriteSlots;
spriteSlots.deleteTimer(_menuItemIndex);
SpriteAsset *menuItem = _menuItems[_menuItemIndex];
MSprite *spr = menuItem->getFrame(_frameIndex);
SpriteSlot &slot = spriteSlots[spriteSlots.add()];
slot._flags = IMG_UPDATE;
slot._seqIndex = (_frameIndex > 0) ? 1 : 2;
slot._seqIndex = _menuItemIndex;
slot._spritesIndex = _menuItemIndexes[_menuItemIndex];
slot._frameNumber = _frameIndex + 1;
slot._position = spr->_offset;
@ -180,6 +170,8 @@ void MainMenu::addSpriteSlot() {
}
bool MainMenu::onEvent(Common::Event &event) {
Scene &scene = _vm->_game->_scene;
// Handle keypresses - these can be done at any time, even when the menu items are being drawn
if (event.type == Common::EVENT_KEYDOWN) {
switch (event.kbd.keycode) {
@ -212,6 +204,9 @@ bool MainMenu::onEvent(Common::Event &event) {
// Goodness knows why, but Rex has a key to restart the menuitem animations
// Restart the animation
_menuItemIndex = -1;
for (int i = 0; i < 6; ++i)
scene._spriteSlots.deleteTimer(i);
_skipFlag = false;
_vm->_events->hideCursor();
break;

View File

@ -37,16 +37,12 @@ namespace Nebular {
enum MADSGameAction { START_GAME, RESUME_GAME, SHOW_INTRO, CREDITS, QUOTES, EXIT };
class MenuView: public FullScreenDialog {
private:
void handleEvents();
protected:
bool _breakFlag;
bool _redrawFlag;
virtual void doFrame() = 0;
virtual bool onEvent(Common::Event &event) = 0;
virtual void display();
public:
MenuView(MADSEngine *vm);