| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Patrick Boenzli |
|---|
| 13 | co-programmer: |
|---|
| 14 | |
|---|
| 15 | This code is inspired by Christian Meyers CommandNode code (in Orxonox v0.2.3) |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT |
|---|
| 19 | |
|---|
| 20 | #include "event_handler.h" |
|---|
| 21 | #include "event_listener.h" |
|---|
| 22 | #include "event.h" |
|---|
| 23 | |
|---|
| 24 | #include "ini_parser.h" |
|---|
| 25 | #include "keynames.h" |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | using namespace std; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | \brief standard constructor |
|---|
| 33 | */ |
|---|
| 34 | EventHandler::EventHandler () |
|---|
| 35 | { |
|---|
| 36 | this->setClassID(CL_EVENT_HANDLER, "EventHandler"); |
|---|
| 37 | |
|---|
| 38 | this->listeners = new EventListener**[ES_NUMBER]; |
|---|
| 39 | for(int i = 0; i < ES_NUMBER; ++i) |
|---|
| 40 | this->listeners[i] = new EventListener*[EV_NUMBER]; |
|---|
| 41 | |
|---|
| 42 | /* now initialize them all to zero */ |
|---|
| 43 | for(int i = 0; i < ES_NUMBER; ++i) |
|---|
| 44 | { |
|---|
| 45 | for(int j = 0; j < SDLK_LAST; ++j) |
|---|
| 46 | { |
|---|
| 47 | this->listeners[i][j] = NULL; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | \brief the singleton reference to this class |
|---|
| 54 | */ |
|---|
| 55 | EventHandler* EventHandler::singletonRef = NULL; |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | \returns a Pointer to this Class |
|---|
| 59 | */ |
|---|
| 60 | EventHandler* EventHandler::getInstance(void) |
|---|
| 61 | { |
|---|
| 62 | if (!EventHandler::singletonRef) |
|---|
| 63 | EventHandler::singletonRef = new EventHandler(); |
|---|
| 64 | return EventHandler::singletonRef; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | \brief standard deconstructor |
|---|
| 69 | |
|---|
| 70 | */ |
|---|
| 71 | EventHandler::~EventHandler () |
|---|
| 72 | { |
|---|
| 73 | EventHandler::singletonRef = NULL; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | void EventHandler::setState(elState state) |
|---|
| 79 | { |
|---|
| 80 | this->state = state; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void EventHandler::subscribeListener(EventListener* el, elState state, int eventType) |
|---|
| 84 | { |
|---|
| 85 | if( likely(this->listeners[state][eventType] == NULL)) |
|---|
| 86 | this->listeners[state][eventType] = el; |
|---|
| 87 | else |
|---|
| 88 | PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | void EventHandler::unsubscribeListener(int eventType, elState state) |
|---|
| 93 | { |
|---|
| 94 | this->listeners[state][eventType] = NULL; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void EventHandler::flush(elState state) |
|---|
| 98 | { |
|---|
| 99 | if( state == ES_ALL) |
|---|
| 100 | { |
|---|
| 101 | for(int i = 0; i < ES_NUMBER; ++i) |
|---|
| 102 | { |
|---|
| 103 | for(int j = 0; j < SDLK_LAST; ++j) |
|---|
| 104 | { |
|---|
| 105 | this->listeners[i][j] = NULL; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | else |
|---|
| 110 | { |
|---|
| 111 | for(int j = 0; j < SDLK_LAST; ++j) |
|---|
| 112 | { |
|---|
| 113 | this->listeners[state][j] = NULL; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | void EventHandler::process() |
|---|
| 121 | { |
|---|
| 122 | SDL_Event event; |
|---|
| 123 | Event ev; |
|---|
| 124 | EventListener* listener; |
|---|
| 125 | while( SDL_PollEvent (&event)) |
|---|
| 126 | { |
|---|
| 127 | switch( event.type) |
|---|
| 128 | { |
|---|
| 129 | case SDL_KEYDOWN: |
|---|
| 130 | ev.bPressed = true; |
|---|
| 131 | ev.type = event.key.keysym.sym; |
|---|
| 132 | break; |
|---|
| 133 | case SDL_KEYUP: |
|---|
| 134 | ev.bPressed = false; |
|---|
| 135 | ev.type = event.key.keysym.sym; |
|---|
| 136 | break; |
|---|
| 137 | case SDL_MOUSEMOTION: |
|---|
| 138 | ev.bPressed = false; |
|---|
| 139 | ev.type = EV_MOUSE_MOTION; |
|---|
| 140 | ev.x = event.motion.x; |
|---|
| 141 | ev.y = event.motion.y; |
|---|
| 142 | ev.xRel = event.motion.xrel; |
|---|
| 143 | ev.yRel = event.motion.yrel; |
|---|
| 144 | break; |
|---|
| 145 | case SDL_MOUSEBUTTONUP: |
|---|
| 146 | ev.bPressed = false; |
|---|
| 147 | ev.type = EV_MOUSE_BUTTON; |
|---|
| 148 | break; |
|---|
| 149 | case SDL_MOUSEBUTTONDOWN: |
|---|
| 150 | ev.bPressed = true; |
|---|
| 151 | ev.type = EV_MOUSE_BUTTON; |
|---|
| 152 | break; |
|---|
| 153 | case SDL_JOYAXISMOTION: |
|---|
| 154 | ev.bPressed = false; |
|---|
| 155 | ev.type = EV_JOY_AXIS_MOTION; |
|---|
| 156 | break; |
|---|
| 157 | case SDL_JOYBALLMOTION: |
|---|
| 158 | ev.bPressed = false; |
|---|
| 159 | ev.type = EV_JOY_BALL_MOTION; |
|---|
| 160 | break; |
|---|
| 161 | case SDL_JOYHATMOTION: |
|---|
| 162 | ev.bPressed = false; |
|---|
| 163 | ev.type = EV_JOY_HAT_MOTION; |
|---|
| 164 | break; |
|---|
| 165 | case SDL_JOYBUTTONDOWN: |
|---|
| 166 | ev.bPressed = true; |
|---|
| 167 | ev.type = EV_JOY_BUTTON; |
|---|
| 168 | break; |
|---|
| 169 | case SDL_JOYBUTTONUP: |
|---|
| 170 | ev.bPressed = true; |
|---|
| 171 | ev.type = EV_JOY_BUTTON; |
|---|
| 172 | break; |
|---|
| 173 | default: |
|---|
| 174 | break; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /* small debug routine: shows alle events dispatched by the event handler */ |
|---|
| 178 | PRINT(0)("\n==========================| EventHandler::process () |===\n"); |
|---|
| 179 | PRINT(0)("= Got Event nr%i\n, for state %i", event.type, this->state); |
|---|
| 180 | |
|---|
| 181 | listener = this->listeners[this->state][event.key.keysym.sym]; |
|---|
| 182 | if( listener != NULL) |
|---|
| 183 | { |
|---|
| 184 | listener->process(ev); |
|---|
| 185 | PRINTF(0)("= Event dispatcher msg: This event has been consumed\n"); |
|---|
| 186 | } |
|---|
| 187 | else |
|---|
| 188 | { |
|---|
| 189 | PRINTF(0)("= Event dispatcher msg: This event has NOT been consumed\n"); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | PRINT(0)("=======================================================\n"); |
|---|
| 193 | } |
|---|
| 194 | } |
|---|