bool EventDispatcher::DispatchSDLEvent(const SDL_Event &event) { switch (event.type) { case SDL_KEYDOWN: return Dispatch(KeyboardEvent(KeyboardEvent::KEY_DOWN, KeySym(event.key.keysym.sym, SDL_Keymod(event.key.keysym.mod)))); case SDL_KEYUP: return Dispatch(KeyboardEvent(KeyboardEvent::KEY_UP, KeySym(event.key.keysym.sym, SDL_Keymod(event.key.keysym.mod)))); case SDL_TEXTINPUT: Uint32 unicode; Text::utf8_decode_char(&unicode, event.text.text); return Dispatch(TextInputEvent(unicode)); case SDL_MOUSEWHEEL: return Dispatch(MouseWheelEvent(event.wheel.y > 0 ? MouseWheelEvent::WHEEL_UP : MouseWheelEvent::WHEEL_DOWN, m_lastMousePosition)); case SDL_MOUSEBUTTONDOWN: return Dispatch(MouseButtonEvent(MouseButtonEvent::BUTTON_DOWN, MouseButtonFromSDLButton(event.button.button), Point(event.button.x,event.button.y))); case SDL_MOUSEBUTTONUP: return Dispatch(MouseButtonEvent(MouseButtonEvent::BUTTON_UP, MouseButtonFromSDLButton(event.button.button), Point(event.button.x,event.button.y))); case SDL_MOUSEMOTION: return Dispatch(MouseMotionEvent(Point(event.motion.x,event.motion.y), Point(event.motion.xrel, event.motion.yrel))); } return false; }
bool EventDispatcher::DispatchSDLEvent(const SDL_Event &event) { switch (event.type) { case SDL_KEYDOWN: return Dispatch(KeyboardEvent(KeyboardEvent::KEY_DOWN, KeySym(event.key.keysym.sym, SDL_Keymod(event.key.keysym.mod)), event.key.repeat)); case SDL_KEYUP: return Dispatch(KeyboardEvent(KeyboardEvent::KEY_UP, KeySym(event.key.keysym.sym, SDL_Keymod(event.key.keysym.mod)), event.key.repeat)); case SDL_TEXTINPUT: Uint32 unicode; Text::utf8_decode_char(&unicode, event.text.text); return Dispatch(TextInputEvent(unicode)); case SDL_MOUSEWHEEL: return Dispatch(MouseWheelEvent(event.wheel.y > 0 ? MouseWheelEvent::WHEEL_UP : MouseWheelEvent::WHEEL_DOWN, m_lastMousePosition)); case SDL_MOUSEBUTTONDOWN: return Dispatch(MouseButtonEvent(MouseButtonEvent::BUTTON_DOWN, MouseButtonFromSDLButton(event.button.button), Point(event.button.x,event.button.y))); case SDL_MOUSEBUTTONUP: return Dispatch(MouseButtonEvent(MouseButtonEvent::BUTTON_UP, MouseButtonFromSDLButton(event.button.button), Point(event.button.x,event.button.y))); case SDL_MOUSEMOTION: return Dispatch(MouseMotionEvent(Point(event.motion.x,event.motion.y), Point(event.motion.xrel, event.motion.yrel))); case SDL_JOYAXISMOTION: // SDL joystick axis value is documented to have the range -32768 to 32767 // unfortunately this places the centre at -0.5, not at zero, which is clearly nuts... // so since that doesn't make any sense, we assume the range is *actually* -32767 to +32767, // and scale it accordingly, clamping the output so that if we *do* get -32768, it turns into -1 return Dispatch(JoystickAxisMotionEvent(event.jaxis.which, Clamp(event.jaxis.value * (1.0f / 32767.0f), -1.0f, 1.0f), event.jaxis.axis)); case SDL_JOYHATMOTION: return Dispatch(JoystickHatMotionEvent(event.jhat.which, JoystickHatMotionEvent::JoystickHatDirection(event.jhat.value), event.jhat.hat)); case SDL_JOYBUTTONDOWN: return Dispatch(JoystickButtonEvent(event.jbutton.which, JoystickButtonEvent::BUTTON_DOWN, event.jbutton.button)); case SDL_JOYBUTTONUP: return Dispatch(JoystickButtonEvent(event.jbutton.which, JoystickButtonEvent::BUTTON_UP, event.jbutton.button)); } return false; }
KeySym KeySym::FromString(const std::string &spec) { static const std::string delim("+"); SDL_Keycode sym = SDLK_UNKNOWN; Uint32 mod = KMOD_NONE; size_t start = 0, end = 0; while (end != std::string::npos) { // get to the first non-delim char start = spec.find_first_not_of(delim, end); // read the end, no more to do if (start == std::string::npos) break; // find the end - next delim or end of string end = spec.find_first_of(delim, start); // extract the fragment const std::string token(spec.substr(start, (end == std::string::npos) ? std::string::npos : end - start)); if (token == "ctrl") mod |= KMOD_CTRL; else if (token == "shift") mod |= KMOD_SHIFT; else if (token == "alt") mod |= KMOD_ALT; else if (token == "meta") mod |= KMOD_GUI; else { if (sym != SDLK_UNKNOWN) Output("key spec '%s' has multiple keys, ignoring '%s'\n", spec.c_str(), token.c_str()); else { for (const KeyMap *map = keymap; map->name; map++) if (token == map->name) sym = map->sym; if (sym == SDLK_UNKNOWN) Output("key spec '%s' has unkown token '%s', ignoring it\n", spec.c_str(), token.c_str()); } } } return KeySym(sym, SDL_Keymod(mod)); }