[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: |
---|
[4329] | 14 | */ |
---|
| 15 | |
---|
[4346] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT |
---|
[4329] | 17 | |
---|
[4346] | 18 | #include "event_handler.h" |
---|
[4350] | 19 | #include "event_listener.h" |
---|
[4329] | 20 | |
---|
[4352] | 21 | #include "ini_parser.h" |
---|
| 22 | #include "keynames.h" |
---|
| 23 | |
---|
[4350] | 24 | #include "SDL_keysym.h" |
---|
| 25 | |
---|
[4329] | 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | \brief standard constructor |
---|
| 31 | */ |
---|
[4346] | 32 | EventHandler::EventHandler () |
---|
[4329] | 33 | { |
---|
[4352] | 34 | this->setClassID(CL_EVENT_HANDLER, "EventHandler"); |
---|
[4350] | 35 | |
---|
[4352] | 36 | this->listeners = new EventListener**[ES_NUMBER]; |
---|
| 37 | for(int i = 0; i < ES_NUMBER; ++i) |
---|
| 38 | this->listeners[i] = new EventListener*[SDLK_LAST]; |
---|
[4329] | 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | \brief the singleton reference to this class |
---|
| 43 | */ |
---|
[4346] | 44 | EventHandler* EventHandler::singletonRef = NULL; |
---|
[4329] | 45 | |
---|
| 46 | /** |
---|
| 47 | \returns a Pointer to this Class |
---|
| 48 | */ |
---|
[4346] | 49 | EventHandler* EventHandler::getInstance(void) |
---|
[4329] | 50 | { |
---|
[4346] | 51 | if (!EventHandler::singletonRef) |
---|
| 52 | EventHandler::singletonRef = new EventHandler(); |
---|
| 53 | return EventHandler::singletonRef; |
---|
[4329] | 54 | } |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | \brief standard deconstructor |
---|
| 58 | |
---|
| 59 | */ |
---|
[4346] | 60 | EventHandler::~EventHandler () |
---|
[4329] | 61 | { |
---|
[4346] | 62 | EventHandler::singletonRef = NULL; |
---|
[4352] | 63 | } |
---|
[4329] | 64 | |
---|
[4352] | 65 | |
---|
| 66 | /** |
---|
| 67 | \brief loads new key bindings from a file |
---|
| 68 | \param filename: The path and name of the file to load the bindings from |
---|
| 69 | */ |
---|
| 70 | void EventHandler::loadKeyBindings (const char* fileName) |
---|
| 71 | { |
---|
| 72 | FILE* stream; |
---|
| 73 | |
---|
| 74 | PRINTF(4)("Loading key bindings from %s\n", fileName); |
---|
| 75 | |
---|
| 76 | // remove old bindings if present |
---|
| 77 | if( this->keyAliases != NULL) |
---|
| 78 | { |
---|
| 79 | free (this->keyAliases); |
---|
| 80 | this->keyAliases = NULL; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // create parser |
---|
| 84 | IniParser parser(fileName); |
---|
| 85 | if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1) |
---|
| 86 | { |
---|
| 87 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName); |
---|
| 88 | return; |
---|
| 89 | } |
---|
| 90 | // allocate empty lookup table |
---|
| 91 | this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); |
---|
| 92 | |
---|
| 93 | char namebuf[256]; |
---|
| 94 | char valuebuf[256]; |
---|
| 95 | memset (namebuf, 0, 256); |
---|
| 96 | memset (valuebuf, 0, 256); |
---|
| 97 | int* index; |
---|
| 98 | |
---|
| 99 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
| 100 | { |
---|
| 101 | //index = nameToIndex (valuebuf); |
---|
| 102 | int c; |
---|
| 103 | if( (c = keynameToSDLK (valuebuf)) != -1) |
---|
| 104 | { |
---|
| 105 | index[1] = c; index[0] = 0; |
---|
| 106 | } |
---|
| 107 | if( (c = buttonnameToSDLB (valuebuf)) != -1) |
---|
| 108 | { |
---|
| 109 | index[1] = c; index[0] = 1; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | switch( index[0]) |
---|
| 113 | { |
---|
| 114 | case 0: |
---|
| 115 | PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); |
---|
| 116 | strcpy (this->keyAliases->keys[index[1]], namebuf); |
---|
| 117 | break; |
---|
| 118 | case 1: |
---|
| 119 | PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); |
---|
| 120 | strcpy (this->keyAliases->buttons[index[1]], namebuf); |
---|
| 121 | break; |
---|
| 122 | default: |
---|
| 123 | break; |
---|
| 124 | } |
---|
| 125 | memset (namebuf, 0, 256); |
---|
| 126 | memset (valuebuf, 0, 256); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | // PARSE MISC SECTION |
---|
| 131 | if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1) |
---|
| 132 | { |
---|
| 133 | PRINTF(1)("Could not find key bindings in %s\n", fileName); |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
| 138 | { |
---|
| 139 | //index = nameToIndex (valuebuf); |
---|
| 140 | int c; |
---|
| 141 | if( (c = keynameToSDLK (valuebuf)) != -1) |
---|
| 142 | { |
---|
| 143 | index[1] = c; index[0] = 0; |
---|
| 144 | } |
---|
| 145 | if( (c = buttonnameToSDLB (valuebuf)) != -1) |
---|
| 146 | { |
---|
| 147 | index[1] = c; index[0] = 1; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | switch( index[0]) |
---|
| 152 | { |
---|
| 153 | case 0: |
---|
| 154 | PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); |
---|
| 155 | strcpy (keyAliases->keys[index[1]], namebuf); |
---|
| 156 | break; |
---|
| 157 | case 1: |
---|
| 158 | PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); |
---|
| 159 | strcpy (keyAliases->buttons[index[1]], namebuf); |
---|
| 160 | break; |
---|
| 161 | default: |
---|
| 162 | break; |
---|
| 163 | } |
---|
| 164 | memset (namebuf, 0, 256); |
---|
| 165 | memset (valuebuf, 0, 256); |
---|
| 166 | } |
---|
[4329] | 167 | } |
---|
[4352] | 168 | |
---|
[4354] | 169 | |
---|
| 170 | void EventHandler::setState(elState state) |
---|
| 171 | { |
---|
| 172 | this->state = state; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void EventHandler::subscribeListener(EventListener* el, elState state, int eventType) |
---|
| 176 | { |
---|
| 177 | if( likely(this->listeners[state][eventType] == NULL)) |
---|
| 178 | this->listeners[state][eventType] = el; |
---|
| 179 | else |
---|
| 180 | PRINTF(0)("Someone tried to subscribe to event %i @ state %i but this event has already been subscribed\n", eventType, state); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | void EventHandler::unsubscribeListener(EventListener* el, elState state) |
---|
| 185 | {} |
---|
| 186 | |
---|
| 187 | void EventHandler::flush() |
---|
| 188 | {} |
---|