Initial commit
This commit is contained in:
378
engines/ultima/ultima4/events/event_handler.cpp
Normal file
378
engines/ultima/ultima4/events/event_handler.cpp
Normal file
@@ -0,0 +1,378 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ultima/ultima4/events/event_handler.h"
|
||||
#include "ultima/ultima4/controllers/wait_controller.h"
|
||||
#include "ultima/ultima4/core/settings.h"
|
||||
#include "ultima/ultima4/core/utils.h"
|
||||
#include "ultima/ultima4/filesys/savegame.h"
|
||||
#include "ultima/ultima4/game/context.h"
|
||||
#include "ultima/ultima4/views/textview.h"
|
||||
#include "ultima/ultima4/gfx/screen.h"
|
||||
#include "ultima/ultima4/map/location.h"
|
||||
#include "common/events.h"
|
||||
#include "common/system.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
|
||||
bool EventHandler::_controllerDone = false;
|
||||
bool EventHandler::_ended = false;
|
||||
|
||||
EventHandler *EventHandler::_instance = nullptr;
|
||||
|
||||
EventHandler::EventHandler() : _timer(settings._eventTimerGranularity),
|
||||
_updateScreen(nullptr), _isRightButtonDown(false) {
|
||||
}
|
||||
|
||||
EventHandler *EventHandler::getInstance() {
|
||||
if (_instance == nullptr)
|
||||
_instance = new EventHandler();
|
||||
return _instance;
|
||||
}
|
||||
|
||||
void EventHandler::wait_msecs(uint msecs) {
|
||||
int msecs_per_cycle = (1000 / settings._gameCyclesPerSecond);
|
||||
int cycles = msecs / msecs_per_cycle;
|
||||
|
||||
if (cycles > 0) {
|
||||
WaitController waitCtrl(cycles);
|
||||
getInstance()->pushController(&waitCtrl);
|
||||
waitCtrl.wait();
|
||||
}
|
||||
// Sleep the rest of the msecs we can't wait for
|
||||
EventHandler::sleep(msecs % msecs_per_cycle);
|
||||
}
|
||||
|
||||
void EventHandler::sleep(uint msec) {
|
||||
g_system->delayMillis(msec);
|
||||
}
|
||||
|
||||
void EventHandler::wait_cycles(uint cycles) {
|
||||
WaitController waitCtrl(cycles);
|
||||
getInstance()->pushController(&waitCtrl);
|
||||
waitCtrl.wait();
|
||||
}
|
||||
|
||||
void EventHandler::setControllerDone(bool done) {
|
||||
_controllerDone = done;
|
||||
#if defined(IOS_ULTIMA4)
|
||||
if (done)
|
||||
controllerStopped_helper();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool EventHandler::getControllerDone() {
|
||||
return _controllerDone;
|
||||
}
|
||||
|
||||
void EventHandler::end() {
|
||||
// End all event processing
|
||||
_ended = true;
|
||||
}
|
||||
|
||||
TimedEventMgr *EventHandler::getTimer() {
|
||||
return &_timer;
|
||||
}
|
||||
|
||||
Controller *EventHandler::pushController(Controller *c) {
|
||||
c->setActive();
|
||||
_controllers.push_back(c);
|
||||
getTimer()->add(&Controller::timerCallback, c->getTimerInterval(), c);
|
||||
return c;
|
||||
}
|
||||
|
||||
Controller *EventHandler::popController() {
|
||||
if (_controllers.empty())
|
||||
return nullptr;
|
||||
|
||||
Controller *controller = _controllers.back();
|
||||
getTimer()->remove(&Controller::timerCallback, controller);
|
||||
_controllers.pop_back();
|
||||
|
||||
controller = getController();
|
||||
if (controller)
|
||||
controller->setActive();
|
||||
|
||||
return controller;
|
||||
}
|
||||
|
||||
Controller *EventHandler::getController() const {
|
||||
if (_controllers.empty())
|
||||
return nullptr;
|
||||
|
||||
return _controllers.back();
|
||||
}
|
||||
|
||||
void EventHandler::setController(Controller *c) {
|
||||
while (popController() != nullptr) {}
|
||||
pushController(c);
|
||||
}
|
||||
|
||||
void EventHandler::pushMouseAreaSet(const MouseArea *mouseAreas) {
|
||||
_mouseAreaSets.push_front(mouseAreas);
|
||||
}
|
||||
|
||||
void EventHandler::popMouseAreaSet() {
|
||||
if (_mouseAreaSets.size())
|
||||
_mouseAreaSets.pop_front();
|
||||
}
|
||||
|
||||
const MouseArea *EventHandler::getMouseAreaSet() const {
|
||||
if (_mouseAreaSets.size())
|
||||
return _mouseAreaSets.front();
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void EventHandler::run() {
|
||||
if (_updateScreen)
|
||||
(*_updateScreen)();
|
||||
g_screen->update();
|
||||
|
||||
while (!_ended && !_controllerDone && !g_ultima->shouldQuit()) {
|
||||
Common::Event event;
|
||||
if (g_system->getEventManager()->pollEvent(event)) {
|
||||
switch (event.type) {
|
||||
case Common::EVENT_KEYDOWN:
|
||||
handleKeyDownEvent(event, getController(), _updateScreen);
|
||||
break;
|
||||
|
||||
case Common::EVENT_LBUTTONDOWN:
|
||||
case Common::EVENT_RBUTTONDOWN:
|
||||
case Common::EVENT_MBUTTONDOWN:
|
||||
handleMouseButtonDownEvent(event, getController(), _updateScreen);
|
||||
break;
|
||||
|
||||
case Common::EVENT_LBUTTONUP:
|
||||
case Common::EVENT_RBUTTONUP:
|
||||
case Common::EVENT_MBUTTONUP:
|
||||
handleMouseButtonUpEvent(event, getController(), _updateScreen);
|
||||
break;
|
||||
|
||||
case Common::EVENT_MOUSEMOVE:
|
||||
handleMouseMotionEvent(event);
|
||||
continue;
|
||||
|
||||
case Common::EVENT_CUSTOM_ENGINE_ACTION_START:
|
||||
getController()->keybinder((KeybindingAction)event.customType);
|
||||
break;
|
||||
|
||||
case Common::EVENT_QUIT:
|
||||
_ended = true;
|
||||
return;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Brief delay
|
||||
g_system->delayMillis(10);
|
||||
|
||||
// Poll the timer manager
|
||||
_timer.poll();
|
||||
|
||||
// Update the screen
|
||||
g_screen->screenFrame();
|
||||
}
|
||||
}
|
||||
|
||||
void EventHandler::setScreenUpdate(void (*updateScreen)(void)) {
|
||||
_updateScreen = updateScreen;
|
||||
}
|
||||
|
||||
void EventHandler::pushKeyHandler(KeyHandler kh) {
|
||||
KeyHandler *new_kh = new KeyHandler(kh);
|
||||
KeyHandlerController *khc = new KeyHandlerController(new_kh);
|
||||
pushController(khc);
|
||||
}
|
||||
|
||||
void EventHandler::popKeyHandler() {
|
||||
if (_controllers.empty())
|
||||
return;
|
||||
|
||||
popController();
|
||||
}
|
||||
|
||||
KeyHandler *EventHandler::getKeyHandler() const {
|
||||
if (_controllers.empty())
|
||||
return nullptr;
|
||||
|
||||
KeyHandlerController *khc = dynamic_cast<KeyHandlerController *>(_controllers.back());
|
||||
assertMsg(khc != nullptr, "EventHandler::getKeyHandler called when controller wasn't a keyhandler");
|
||||
if (khc == nullptr)
|
||||
return nullptr;
|
||||
|
||||
return khc->getKeyHandler();
|
||||
}
|
||||
|
||||
void EventHandler::setKeyHandler(KeyHandler kh) {
|
||||
while (popController() != nullptr) {
|
||||
}
|
||||
pushKeyHandler(kh);
|
||||
}
|
||||
|
||||
const MouseArea *EventHandler::mouseAreaForPoint(int x, int y) {
|
||||
int i;
|
||||
const MouseArea *areas = getMouseAreaSet();
|
||||
|
||||
if (!areas)
|
||||
return nullptr;
|
||||
|
||||
for (i = 0; areas[i]._nPoints != 0; i++) {
|
||||
if (g_screen->screenPointInMouseArea(x, y, &(areas[i]))) {
|
||||
return &(areas[i]);
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void EventHandler::handleMouseMotionEvent(const Common::Event &event) {
|
||||
if (!settings._mouseOptions._enabled)
|
||||
return;
|
||||
|
||||
const MouseArea *area;
|
||||
area = eventHandler->mouseAreaForPoint(event.mouse.x, event.mouse.y);
|
||||
if (area) {
|
||||
g_screen->setMouseCursor(area->_cursor);
|
||||
|
||||
if (_isRightButtonDown) {
|
||||
int xd = (event.mouse.x / settings._scale) - 96,
|
||||
yd = (event.mouse.y / settings._scale) - 96;
|
||||
double dist = sqrt((double)(xd * xd + yd * yd));
|
||||
_walk.setDelta(area->_direction, (int)dist);
|
||||
}
|
||||
} else {
|
||||
g_screen->setMouseCursor(MC_DEFAULT);
|
||||
if (_isRightButtonDown)
|
||||
_walk.setDelta(DIR_NONE, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void EventHandler::handleMouseButtonDownEvent(const Common::Event &event, Controller *controller, updateScreenCallback updateScreen) {
|
||||
if (!settings._mouseOptions._enabled)
|
||||
return;
|
||||
|
||||
if (event.type == Common::EVENT_LBUTTONDOWN) {
|
||||
// handle the keypress
|
||||
bool processed = controller->notifyMousePress(event.mouse);
|
||||
|
||||
if (processed) {
|
||||
if (updateScreen)
|
||||
(*updateScreen)();
|
||||
g_screen->update();
|
||||
}
|
||||
|
||||
} else if (event.type == Common::EVENT_RBUTTONDOWN) {
|
||||
_isRightButtonDown = true;
|
||||
handleMouseMotionEvent(event);
|
||||
}
|
||||
|
||||
if (updateScreen)
|
||||
(*updateScreen)();
|
||||
g_screen->update();
|
||||
}
|
||||
|
||||
void EventHandler::handleMouseButtonUpEvent(const Common::Event &event, Controller *controller, updateScreenCallback updateScreen) {
|
||||
if (!settings._mouseOptions._enabled)
|
||||
return;
|
||||
|
||||
if (event.type == Common::EVENT_RBUTTONUP)
|
||||
_isRightButtonDown = false;
|
||||
}
|
||||
|
||||
void EventHandler::handleKeyDownEvent(const Common::Event &event, Controller *controller, updateScreenCallback updateScreen) {
|
||||
int key;
|
||||
bool processed;
|
||||
|
||||
key = (event.kbd.ascii != 0 && event.kbd.ascii < 128) ?
|
||||
event.kbd.ascii : (int)event.kbd.keycode;
|
||||
|
||||
key += (event.kbd.flags & (Common::KBD_CTRL |
|
||||
Common::KBD_ALT | Common::KBD_META)) << 16;
|
||||
|
||||
debug(1, "key event: sym = %d, mod = %d; translated = %d",
|
||||
event.kbd.keycode, event.kbd.flags, key);
|
||||
|
||||
// handle the keypress
|
||||
processed = controller->notifyKeyPressed(key);
|
||||
|
||||
if (processed) {
|
||||
if (updateScreen)
|
||||
(*updateScreen)();
|
||||
g_screen->update();
|
||||
}
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------*/
|
||||
|
||||
void WalkTrigger::reset() {
|
||||
_action = KEYBIND_NONE;
|
||||
_ticksCtr = 0;
|
||||
}
|
||||
|
||||
void WalkTrigger::setDelta(Direction dir, int distance) {
|
||||
if (distance > 96) {
|
||||
distance = 0;
|
||||
dir = DIR_NONE;
|
||||
}
|
||||
|
||||
KeybindingAction action;
|
||||
switch (dir) {
|
||||
case DIR_NORTH:
|
||||
action = KEYBIND_UP;
|
||||
break;
|
||||
case DIR_SOUTH:
|
||||
action = KEYBIND_DOWN;
|
||||
break;
|
||||
case DIR_WEST:
|
||||
action = KEYBIND_LEFT;
|
||||
break;
|
||||
case DIR_EAST:
|
||||
action = KEYBIND_RIGHT;
|
||||
break;
|
||||
default:
|
||||
action = KEYBIND_NONE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (action != _action) {
|
||||
// Walk quadrant changed
|
||||
_action = action;
|
||||
_ticksCtr = 0;
|
||||
}
|
||||
|
||||
_ticksPerWalk = 4 - (distance / 25);
|
||||
}
|
||||
|
||||
KeybindingAction WalkTrigger::getAction() {
|
||||
if (--_ticksCtr <= 0) {
|
||||
_ticksCtr = _ticksPerWalk;
|
||||
return _action;
|
||||
}
|
||||
|
||||
return KEYBIND_NONE;
|
||||
}
|
||||
|
||||
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
209
engines/ultima/ultima4/events/event_handler.h
Normal file
209
engines/ultima/ultima4/events/event_handler.h
Normal file
@@ -0,0 +1,209 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ULTIMA4_EVENTS_EVENT_HANDLER_H
|
||||
#define ULTIMA4_EVENTS_EVENT_HANDLER_H
|
||||
|
||||
#include "ultima/ultima4/events/timed_event_mgr.h"
|
||||
#include "ultima/ultima4/controllers/key_handler_controller.h"
|
||||
#include "ultima/ultima4/core/types.h"
|
||||
#include "ultima/ultima4/gfx/screen.h"
|
||||
#include "ultima/shared/std/containers.h"
|
||||
#include "common/events.h"
|
||||
#include "common/list.h"
|
||||
#include "common/rect.h"
|
||||
#include "common/str.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
|
||||
#define eventHandler (EventHandler::getInstance())
|
||||
|
||||
#if defined(IOS_ULTIMA4)
|
||||
#ifndef __OBJC__
|
||||
typedef void *TimedManagerHelper;
|
||||
typedef void *UIEvent;
|
||||
#else
|
||||
@class TimedManagerHelper;
|
||||
@class UIEvent;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef void(*updateScreenCallback)();
|
||||
|
||||
/**
|
||||
* Encapsulates the logic for deciding how frequently to walk
|
||||
* when holding down the right moues button in enhanced mode
|
||||
*/
|
||||
class WalkTrigger {
|
||||
private:
|
||||
int _ticksCtr, _ticksPerWalk;
|
||||
KeybindingAction _action;
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
WalkTrigger() : _ticksCtr(0), _ticksPerWalk(0), _action(KEYBIND_NONE) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the walker
|
||||
*/
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* Sets the delta from the center of the map
|
||||
*/
|
||||
void setDelta(Direction dir, int distance);
|
||||
|
||||
/**
|
||||
* Checks for whether to walk, and if so, returns the direction
|
||||
*/
|
||||
KeybindingAction getAction();
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* A class for handling game events.
|
||||
*/
|
||||
class EventHandler {
|
||||
typedef Common::List<const MouseArea *> MouseAreaList;
|
||||
private:
|
||||
static EventHandler *_instance;
|
||||
WalkTrigger _walk;
|
||||
bool _isRightButtonDown;
|
||||
private:
|
||||
void handleMouseMotionEvent(const Common::Event &event);
|
||||
void handleMouseButtonDownEvent(const Common::Event &event, Controller *controller, updateScreenCallback updateScreen);
|
||||
void handleMouseButtonUpEvent(const Common::Event &event, Controller *controller, updateScreenCallback updateScreen);
|
||||
void handleKeyDownEvent(const Common::Event &event, Controller *controller, updateScreenCallback updateScreen);
|
||||
protected:
|
||||
static bool _controllerDone;
|
||||
static bool _ended;
|
||||
TimedEventMgr _timer;
|
||||
Std::vector<Controller *> _controllers;
|
||||
MouseAreaList _mouseAreaSets;
|
||||
updateScreenCallback _updateScreen;
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
EventHandler();
|
||||
|
||||
/* Static functions */
|
||||
static EventHandler *getInstance();
|
||||
|
||||
/**
|
||||
* Delays program execution for the specified number of milliseconds.
|
||||
* This doesn't actually stop events, but it stops the user from interacting
|
||||
* While some important event happens (e.g., getting hit by a cannon ball or a spell effect).
|
||||
*/
|
||||
static void sleep(uint usec);
|
||||
|
||||
/**
|
||||
* Waits a given number of milliseconds before continuing
|
||||
*/
|
||||
static void wait_msecs(uint msecs);
|
||||
|
||||
/**
|
||||
* Waits a given number of game cycles before continuing
|
||||
*/
|
||||
static void wait_cycles(uint cycles);
|
||||
|
||||
static void setControllerDone(bool exit = true);
|
||||
|
||||
/**
|
||||
* Returns the current value of the global exit flag
|
||||
*/
|
||||
static bool getControllerDone();
|
||||
static void end();
|
||||
|
||||
/* Member functions */
|
||||
TimedEventMgr *getTimer();
|
||||
|
||||
/* Event functions */
|
||||
void run();
|
||||
void setScreenUpdate(void (*updateScreen)(void));
|
||||
#if defined(IOS_ULTIMA4)
|
||||
void handleEvent(UIEvent *);
|
||||
static void controllerStopped_helper();
|
||||
updateScreenCallback screenCallback() {
|
||||
return updateScreen;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Controller functions */
|
||||
Controller *pushController(Controller *c);
|
||||
Controller *popController();
|
||||
Controller *getController() const;
|
||||
void setController(Controller *c);
|
||||
|
||||
/* Key handler functions */
|
||||
/**
|
||||
* Adds a key handler to the stack.
|
||||
*/
|
||||
void pushKeyHandler(KeyHandler kh);
|
||||
|
||||
/**
|
||||
* Pops a key handler off the stack.
|
||||
* Returns a pointer to the resulting key handler after
|
||||
* the current handler is popped.
|
||||
*/
|
||||
void popKeyHandler();
|
||||
|
||||
/**
|
||||
* Returns a pointer to the current key handler.
|
||||
* Returns nullptr if there is no key handler.
|
||||
*/
|
||||
KeyHandler *getKeyHandler() const;
|
||||
|
||||
/**
|
||||
* Eliminates all key handlers and begins stack with new handler.
|
||||
* This pops all key handlers off the stack and adds
|
||||
* the key handler provided to the stack, making it the
|
||||
* only key handler left. Use this function only if you
|
||||
* are sure the key handlers in the stack are disposable.
|
||||
*/
|
||||
void setKeyHandler(KeyHandler kh);
|
||||
|
||||
/* Mouse area functions */
|
||||
void pushMouseAreaSet(const MouseArea *mouseAreas);
|
||||
void popMouseAreaSet();
|
||||
|
||||
/**
|
||||
* Get the currently active mouse area set off the top of the stack.
|
||||
*/
|
||||
const MouseArea *getMouseAreaSet() const;
|
||||
|
||||
const MouseArea *mouseAreaForPoint(int x, int y);
|
||||
|
||||
/**
|
||||
* Checks for whether to walk, and if so, returns the direction action
|
||||
*/
|
||||
KeybindingAction getAction() {
|
||||
return _isRightButtonDown ? _walk.getAction() : KEYBIND_NONE;
|
||||
}
|
||||
};
|
||||
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
|
||||
#endif
|
||||
133
engines/ultima/ultima4/events/timed_event_mgr.cpp
Normal file
133
engines/ultima/ultima4/events/timed_event_mgr.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ultima/ultima4/events/timed_event_mgr.h"
|
||||
#include "ultima/ultima4/gfx/screen.h"
|
||||
#include "common/system.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
|
||||
TimedEventMgr::TimedEventMgr(int baseInterval) :
|
||||
_baseInterval(baseInterval), _lastTickTime(0), _locked(false) {
|
||||
}
|
||||
|
||||
void TimedEventMgr::poll() {
|
||||
uint32 time = g_system->getMillis();
|
||||
if (time >= (_lastTickTime + _baseInterval)) {
|
||||
_lastTickTime = time;
|
||||
tick();
|
||||
|
||||
g_screen->update();
|
||||
}
|
||||
}
|
||||
|
||||
void TimedEventMgr::reset(uint interval) {
|
||||
_baseInterval = interval;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------*/
|
||||
|
||||
TimedEvent::TimedEvent(TimedEvent::Callback cb, int i, void *d) :
|
||||
_callback(cb),
|
||||
_data(d),
|
||||
_interval(i),
|
||||
_current(0) {
|
||||
}
|
||||
|
||||
TimedEvent::Callback TimedEvent::getCallback() const {
|
||||
return _callback;
|
||||
}
|
||||
|
||||
void *TimedEvent::getData() {
|
||||
return _data;
|
||||
}
|
||||
|
||||
void TimedEvent::tick() {
|
||||
if (++_current >= _interval) {
|
||||
(*_callback)(_data);
|
||||
_current = 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool TimedEventMgr::isLocked() const {
|
||||
return _locked;
|
||||
}
|
||||
|
||||
void TimedEventMgr::add(TimedEvent::Callback theCallback, int interval, void *data) {
|
||||
_events.push_back(new TimedEvent(theCallback, interval, data));
|
||||
}
|
||||
|
||||
TimedEventMgr::List::iterator TimedEventMgr::remove(List::iterator i) {
|
||||
if (isLocked()) {
|
||||
_deferredRemovals.push_back(*i);
|
||||
return i;
|
||||
} else {
|
||||
delete *i;
|
||||
return _events.erase(i);
|
||||
}
|
||||
}
|
||||
|
||||
void TimedEventMgr::remove(TimedEvent *event) {
|
||||
List::iterator i;
|
||||
for (i = _events.begin(); i != _events.end(); i++) {
|
||||
if ((*i) == event) {
|
||||
remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TimedEventMgr::remove(TimedEvent::Callback theCallback, void *data) {
|
||||
List::iterator i;
|
||||
for (i = _events.begin(); i != _events.end(); i++) {
|
||||
if ((*i)->getCallback() == theCallback && (*i)->getData() == data) {
|
||||
remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TimedEventMgr::tick() {
|
||||
List::iterator i;
|
||||
lock();
|
||||
|
||||
for (i = _events.begin(); i != _events.end(); i++)
|
||||
(*i)->tick();
|
||||
|
||||
unlock();
|
||||
|
||||
// Remove events that have been deferred for removal
|
||||
for (i = _deferredRemovals.begin(); i != _deferredRemovals.end(); i++)
|
||||
_events.remove(*i);
|
||||
}
|
||||
|
||||
void TimedEventMgr::lock() {
|
||||
_locked = true;
|
||||
}
|
||||
|
||||
void TimedEventMgr::unlock() {
|
||||
_locked = false;
|
||||
}
|
||||
|
||||
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
140
engines/ultima/ultima4/events/timed_event_mgr.h
Normal file
140
engines/ultima/ultima4/events/timed_event_mgr.h
Normal file
@@ -0,0 +1,140 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ULTIMA4_EVENTS_TIMED_EVENT_MGR_H
|
||||
#define ULTIMA4_EVENTS_TIMED_EVENT_MGR_H
|
||||
|
||||
#include "common/list.h"
|
||||
|
||||
namespace Ultima {
|
||||
namespace Ultima4 {
|
||||
|
||||
/**
|
||||
* A class for handling timed events.
|
||||
*/
|
||||
class TimedEvent {
|
||||
public:
|
||||
/* Typedefs */
|
||||
typedef Common::List<TimedEvent *> List;
|
||||
typedef void (*Callback)(void *);
|
||||
|
||||
/* Constructors */
|
||||
TimedEvent(Callback callback, int interval, void *data = nullptr);
|
||||
|
||||
/* Member functions */
|
||||
Callback getCallback() const;
|
||||
void *getData();
|
||||
|
||||
/**
|
||||
* Advances the timed event forward a tick.
|
||||
* When (current >= interval), then it executes its callback function.
|
||||
*/
|
||||
void tick();
|
||||
|
||||
/* Properties */
|
||||
protected:
|
||||
Callback _callback;
|
||||
void *_data;
|
||||
int _interval;
|
||||
int _current;
|
||||
};
|
||||
|
||||
/**
|
||||
* A class for managing timed events
|
||||
*/
|
||||
class TimedEventMgr {
|
||||
public:
|
||||
/* Typedefs */
|
||||
typedef TimedEvent::List List;
|
||||
|
||||
/* Constructors */
|
||||
/**
|
||||
* Constructs a timed event manager object.
|
||||
* Adds a timer callback to the SDL subsystem, which
|
||||
* will drive all of the timed events that this object
|
||||
* controls.
|
||||
*/
|
||||
TimedEventMgr(int baseInterval);
|
||||
|
||||
/**
|
||||
* Destructs a timed event manager object.
|
||||
* It removes the callback timer and un-initializes the
|
||||
* SDL subsystem if there are no other active TimedEventMgr
|
||||
* objects.
|
||||
*/
|
||||
~TimedEventMgr() {}
|
||||
|
||||
/**
|
||||
* Checks whether the frame time has expired, and if so,
|
||||
* triggers a tick
|
||||
*/
|
||||
void poll();
|
||||
|
||||
/**
|
||||
* Returns true if the event queue is locked (in use)
|
||||
*/
|
||||
bool isLocked() const;
|
||||
|
||||
/**
|
||||
* Adds a timed event to the event queue.
|
||||
*/
|
||||
void add(TimedEvent::Callback theCallback, int interval, void *data = nullptr);
|
||||
|
||||
/**
|
||||
* Removes a timed event from the event queue.
|
||||
*/
|
||||
List::iterator remove(List::iterator i);
|
||||
void remove(TimedEvent *event);
|
||||
void remove(TimedEvent::Callback theCallback, void *data = nullptr);
|
||||
|
||||
/**
|
||||
* Runs each of the callback functions of the TimedEvents associated with this manager.
|
||||
*/
|
||||
void tick();
|
||||
|
||||
/**
|
||||
* Re-initializes the timer manager to a new timer granularity
|
||||
*/
|
||||
void reset(uint interval); /**< Re-initializes the event manager to a new base interval */
|
||||
#if defined(IOS_ULTIMA4)
|
||||
bool hasActiveTimer() const;
|
||||
#endif
|
||||
|
||||
private:
|
||||
void lock(); /**< Locks the event list */
|
||||
void unlock(); /**< Unlocks the event list */
|
||||
|
||||
/* Properties */
|
||||
protected:
|
||||
uint32 _lastTickTime;
|
||||
uint32 _baseInterval;
|
||||
bool _locked;
|
||||
List _events;
|
||||
List _deferredRemovals;
|
||||
#if defined(IOS_ULTIMA4)
|
||||
TimedManagerHelper *m_helper;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // End of namespace Ultima4
|
||||
} // End of namespace Ultima
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user