scummvm/engines/ags/events.cpp

99 lines
3.0 KiB
C++

/* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "ags/events.h"
#include "common/system.h"
namespace AGS3 {
extern volatile char want_exit, abort_engine;
extern char check_dynamic_sprites_at_exit;
}
namespace AGS {
EventsManager *g_events;
EventsManager::EventsManager() {
g_events = this;
}
EventsManager::~EventsManager() {
g_events = nullptr;
}
void EventsManager::pollEvents() {
Common::Event e;
while (g_system->getEventManager()->pollEvent(e)) {
if (e.type == Common::EVENT_QUIT) {
::AGS3::want_exit = 1;
::AGS3::abort_engine = 1;
::AGS3::check_dynamic_sprites_at_exit = 0;
} else if (e.type == Common::EVENT_KEYDOWN) {
if (!isModifierKey(e.kbd.keycode)) {
// Add keypresses to the pending key list
_pendingKeys.push(e.kbd);
}
} else {
// Add other event types to the pending events queue. If the event is a
// mouse move and the prior one was also, then discard the prior one.
// This'll help prevent too many mouse move events accumulating
if (e.type == Common::EVENT_MOUSEMOVE && !_pendingEvents.empty() &&
_pendingEvents.back().type == Common::EVENT_MOUSEMOVE)
_pendingEvents.back() = e;
else
_pendingEvents.push(e);
}
}
}
bool EventsManager::isModifierKey(const Common::KeyCode &keycode) const {
return keycode == Common::KEYCODE_LCTRL || keycode == Common::KEYCODE_LALT
|| keycode == Common::KEYCODE_RCTRL || keycode == Common::KEYCODE_RALT
|| keycode == Common::KEYCODE_LSHIFT || keycode == Common::KEYCODE_RSHIFT
|| keycode == Common::KEYCODE_LSUPER || keycode == Common::KEYCODE_RSUPER
|| keycode == Common::KEYCODE_CAPSLOCK || keycode == Common::KEYCODE_NUMLOCK
|| keycode == Common::KEYCODE_SCROLLOCK;
}
bool EventsManager::keypressed() {
pollEvents();
return !_pendingKeys.empty();
}
Common::KeyState EventsManager::readKey() {
pollEvents();
return _pendingKeys.empty() ? Common::KeyState() : _pendingKeys.pop();
}
Common::Event EventsManager::readEvent() {
pollEvents();
return _pendingEvents.empty() ? Common::Event() : _pendingEvents.pop();
}
void EventsManager::warpMouse(const Common::Point &newPos) {
g_system->warpMouse(newPos.x, newPos.y);
}
} // namespace AGS