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