ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

common/events.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_EVENTS_H
00024 #define COMMON_EVENTS_H
00025 
00026 #include "common/keyboard.h"
00027 #include "common/queue.h"
00028 #include "common/rect.h"
00029 #include "common/noncopyable.h"
00030 
00031 #include "common/list.h"
00032 #include "common/singleton.h"
00033 
00034 namespace Common {
00035 
00045 enum EventType {
00046     EVENT_INVALID = 0,
00048     EVENT_KEYDOWN = 1,
00050     EVENT_KEYUP = 2,
00052     EVENT_MOUSEMOVE = 3,
00053     EVENT_LBUTTONDOWN = 4,
00054     EVENT_LBUTTONUP = 5,
00055     EVENT_RBUTTONDOWN = 6,
00056     EVENT_RBUTTONUP = 7,
00057     EVENT_WHEELUP = 8,
00058     EVENT_WHEELDOWN = 9,
00059     EVENT_MBUTTONDOWN = 13,
00060     EVENT_MBUTTONUP = 14,
00061 
00062     EVENT_MAINMENU = 15,
00063     EVENT_RTL = 16,
00064     EVENT_MUTE = 17,
00065 
00066     EVENT_QUIT = 10,
00067     EVENT_SCREEN_CHANGED = 11,
00075     EVENT_PREDICTIVE_DIALOG = 12
00076 
00077 #ifdef ENABLE_KEYMAPPER
00078     ,
00079     // IMPORTANT NOTE: This is part of the WIP Keymapper. If you plan to use
00080     // this, please talk to tsoliman and/or LordHoto.
00081     EVENT_CUSTOM_BACKEND_ACTION = 18,
00082     EVENT_CUSTOM_BACKEND_HARDWARE = 21,
00083     EVENT_GUI_REMAP_COMPLETE_ACTION = 22,
00084     EVENT_KEYMAPPER_REMAP = 19
00085 #endif
00086 #ifdef ENABLE_VKEYBD
00087     ,
00088     EVENT_VIRTUAL_KEYBOARD = 20
00089 #endif
00090 /* START of ResidualVM-specific code */
00091     ,
00092     EVENT_JOYAXIS_MOTION = 23,
00093     EVENT_JOYBUTTON_DOWN = 24,
00094     EVENT_JOYBUTTON_UP = 25
00095 };
00096 
00097 const int16 JOYAXIS_MIN = -32768;
00098 const int16 JOYAXIS_MAX = 32767;
00099 
00103 struct JoystickState {
00105     byte axis;
00107     int16 position;
00114     byte button;
00115 };
00116 
00120 enum JoystickButton {
00121     JOYSTICK_BUTTON_A,
00122     JOYSTICK_BUTTON_B,
00123     JOYSTICK_BUTTON_X,
00124     JOYSTICK_BUTTON_Y,
00125     JOYSTICK_BUTTON_BACK,
00126     JOYSTICK_BUTTON_GUIDE,
00127     JOYSTICK_BUTTON_START,
00128     JOYSTICK_BUTTON_LEFT_STICK,
00129     JOYSTICK_BUTTON_RIGHT_STICK,
00130     JOYSTICK_BUTTON_LEFT_SHOULDER,
00131     JOYSTICK_BUTTON_RIGHT_SHOULDER,
00132     JOYSTICK_BUTTON_DPAD_UP,
00133     JOYSTICK_BUTTON_DPAD_DOWN,
00134     JOYSTICK_BUTTON_DPAD_LEFT,
00135     JOYSTICK_BUTTON_DPAD_RIGHT,
00136     // Left and right trigger button events are generated in place of the axis events for now
00137     JOYSTICK_BUTTON_LEFT_TRIGGER,
00138     JOYSTICK_BUTTON_RIGHT_TRIGGER
00139 };
00140 
00141 /* END of ResidualVM-specific code */
00142 
00143 typedef uint32 CustomEventType;
00144 
00149 struct Event {
00150 
00152     EventType type;
00153 
00159     bool kbdRepeat;
00160 
00165     KeyState kbd;
00166 
00173     Point mouse;
00174 
00175 #ifdef ENABLE_KEYMAPPER
00176     // IMPORTANT NOTE: This is part of the WIP Keymapper. If you plan to use
00177     // this, please talk to tsoliman and/or LordHoto.
00178     CustomEventType customType;
00179 #endif
00180 
00186     Common::Point relMouse;
00187 
00194     JoystickState joystick;
00195 
00196     Event() : type(EVENT_INVALID), kbdRepeat(false) {
00197 #ifdef ENABLE_KEYMAPPER
00198         customType = 0;
00199 #endif
00200     }
00201 };
00202 
00209 class EventSource {
00210 public:
00211     virtual ~EventSource() {}
00212 
00219     virtual bool pollEvent(Event &event) = 0;
00220 
00229     virtual bool allowMapping() const { return true; }
00230 };
00231 
00238 class ArtificialEventSource : public EventSource {
00239 protected:
00240     Queue<Event> _artificialEventQueue;
00241 public:
00242     void addEvent(const Event &ev) {
00243         _artificialEventQueue.push(ev);
00244     }
00245 
00246     bool pollEvent(Event &ev) {
00247     if (!_artificialEventQueue.empty()) {
00248             ev = _artificialEventQueue.pop();
00249             return true;
00250         } else {
00251             return false;
00252         }
00253     }
00254 
00259     virtual bool allowMapping() const { return false; }
00260 };
00261 
00267 class EventObserver {
00268 public:
00269     virtual ~EventObserver() {}
00270 
00284     virtual bool notifyEvent(const Event &event) = 0;
00285 
00292     virtual bool notifyPoll() { return false; }
00293 };
00294 
00300 class EventMapper {
00301 public:
00302     virtual ~EventMapper() {}
00303 
00307     virtual List<Event> mapEvent(const Event &ev, EventSource *source) = 0;
00308 
00309     virtual List<Event> getDelayedEvents() = 0;
00310 };
00311 
00312 class DefaultEventMapper : public EventMapper {
00313 public:
00314     DefaultEventMapper() : _delayedEvents(), _delayedEffectiveTime(0) {}
00315     // EventMapper interface
00316     virtual List<Event> mapEvent(const Event &ev, EventSource *source);
00317     virtual List<Event> getDelayedEvents();
00318 protected:
00319     virtual void addDelayedEvent(uint32 millis, Event ev);
00320 
00321     struct DelayedEventsEntry {
00322         const uint32 timerOffset;
00323         const Event event;
00324         DelayedEventsEntry(const uint32 offset, const Event ev) : timerOffset(offset), event(ev) { }
00325     };
00326 
00327     Queue<DelayedEventsEntry> _delayedEvents;
00328     uint32 _delayedEffectiveTime;
00329 };
00330 
00344 class EventDispatcher {
00345 public:
00346     EventDispatcher();
00347     ~EventDispatcher();
00348 
00355     void dispatch();
00356 
00361     void clearEvents();
00362 
00377     void registerMapper(EventMapper *mapper, bool autoFree = true);
00378 
00382     EventMapper *queryMapper() const { return _mapper; }
00383 
00387     void registerSource(EventSource *source, bool autoFree);
00388 
00394     void unregisterSource(EventSource *source);
00395 
00402     void registerObserver(EventObserver *obs, uint priority, bool autoFree, bool listenPolls = false);
00403 
00409     void unregisterObserver(EventObserver *obs);
00410 private:
00411     bool _autoFreeMapper;
00412     EventMapper *_mapper;
00413 
00414     struct Entry {
00415         bool autoFree;
00416     };
00417 
00418     struct SourceEntry : public Entry {
00419         EventSource *source;
00420     };
00421 
00422     List<SourceEntry> _sources;
00423 
00424     struct ObserverEntry : public Entry {
00425         uint priority;
00426         EventObserver *observer;
00427         bool poll;
00428     };
00429 
00430     List<ObserverEntry> _observers;
00431 
00432     void dispatchEvent(const Event &event);
00433     void dispatchPoll();
00434 };
00435 
00436 class Keymapper;
00437 
00443 class EventManager : NonCopyable {
00444 public:
00445     EventManager() {}
00446     virtual ~EventManager() {}
00447 
00448     enum {
00449         LBUTTON = 1 << 0,
00450         RBUTTON = 1 << 1
00451     };
00452 
00453 
00458     virtual void init() {}
00459 
00465     virtual bool pollEvent(Event &event) = 0;
00466 
00470     virtual void pushEvent(const Event &event) = 0;
00471 
00475     virtual void purgeMouseEvents() = 0;
00476 
00478     virtual Point getMousePos() const = 0;
00479 
00485     virtual int getButtonState() const = 0;
00486 
00488     virtual int getModifierState() const = 0;
00489 
00494     virtual int shouldQuit() const = 0;
00495 
00499     virtual int shouldRTL() const = 0;
00500 
00505     virtual void resetRTL() = 0;
00506 #ifdef FORCE_RTL
00507     virtual void resetQuit() = 0;
00508 #endif
00509     // Optional: check whether a given key is currently pressed ????
00510     //virtual bool isKeyPressed(int keycode) = 0;
00511 
00512     // TODO: Keyboard repeat support?
00513 
00514     // TODO: Consider removing OSystem::getScreenChangeID and
00515     // replacing it by a generic getScreenChangeID method here
00516 #ifdef ENABLE_KEYMAPPER
00517     virtual Keymapper *getKeymapper() = 0;
00518 #endif
00519 
00520     enum {
00525         kEventManPriority = 0,
00530         kEventRecorderPriority = 1
00531     };
00532 
00536     EventDispatcher *getEventDispatcher() { return &_dispatcher; }
00537 
00538 protected:
00539     EventDispatcher _dispatcher;
00540 };
00541 
00542 } // End of namespace Common
00543 
00544 #endif


Generated on Sat Mar 16 2019 05:01:30 for ResidualVM by doxygen 1.7.1
curved edge   curved edge