userInput.cpp

00001 /*
00002   ユーザ入力情報の管理
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "userInput.h"
00008 #include "joystickCtrl.h"
00009 #include <ctype.h>
00010 #include <algorithm>
00011 
00012 
00013 UserInput* UserInput::obj = NULL;
00014 
00015 
00016 UserInput::UserInput(void) {
00017   stateClear();
00018   state.quit = false;
00019   SDL_EnableUNICODE(1);
00020 }
00021 
00022 
00023 UserInput::~UserInput(void) {
00024 }
00025 
00026 
00027 UserInput& UserInput::getObject(void) {
00028   if (!obj) {
00029     obj = new UserInput();
00030   }
00031   return *obj;
00032 }
00033 
00034 
00035 void UserInput::stateClear(void) {
00036   if (!obj) {
00037     return;
00038   }
00039   obj->state.left_released = false;
00040   obj->state.left_clicked = false;
00041   obj->state.wheel_moved = 0;
00042   obj->state.mouse_moved = false;
00043   obj->state.keys.clear();
00044   obj->state.sdl_keys.clear();
00045   obj->state.tab_pressed = false;
00046   obj->state.ctrl_pressed = false;
00047   obj->state.alt_pressed = false;
00048 }
00049 
00050 
00051 void UserInput::internal_stateClear(void) {
00052   toggle_fullscreen = false;
00053   left_release = false;
00054 }
00055 
00056 
00057 void UserInput::internal_update(void) {
00058   internal_stateClear();
00059 
00060   SDL_Event event;
00061   while (SDL_PollEvent(&event)) {
00062     int mx, my;
00063     switch (event.type) {
00064     case SDL_QUIT:
00065       state.quit = true;
00066       break;
00067 
00068     case SDL_KEYUP:
00069       keyupHandler(event.key);
00070       break;
00071 
00072     case SDL_KEYDOWN:
00073       keydownHandler(event.key);
00074       break;
00075 
00076     case SDL_MOUSEBUTTONUP:
00077     case SDL_MOUSEBUTTONDOWN:
00078       mousebuttonHandler(event.button);
00079       break;
00080 
00081     case SDL_MOUSEMOTION:
00082       SDL_GetMouseState(&mx, &my);
00083       state.mxx = mx - mpos.x;
00084       state.myy = my - mpos.y;
00085       mpos.x = mx;
00086       mpos.y = my;
00087       state.mpos = mpos;
00088       if (state.mxx || state.myy) {
00089         state.mouse_moved = true;
00090       }
00091       break;
00092     }
00093     JoystickCtrl::joystickEventHandler(event);
00094 
00095     // マウスの押下情報の更新
00096     // !!!
00097   }
00098 }
00099 
00100 
00101 void UserInput::keydownHandler(SDL_KeyboardEvent& key_event) {
00102   SDLKey sym = key_event.keysym.sym;
00103   SDLMod mod = key_event.keysym.mod;
00104 
00105   if (sym == SDLK_TAB) {        // TAB キー
00106     state.tab_pressed = true;
00107   }
00108 
00109   if (mod & KMOD_CTRL) {        // Ctrl キー
00110     state.ctrl_pressed = true;
00111 
00112     if (sym == SDLK_q) {
00113       state.quit = true;
00114       return;
00115     }
00116   }
00117   if (mod & KMOD_ALT) {         // Alt キー
00118     state.alt_pressed = true;
00119 
00120     if (sym == SDLK_RETURN) {
00121       toggle_fullscreen = true;
00122       return;
00123     } else if (sym == SDLK_F4) {
00124       state.quit = true;
00125       return;
00126     }
00127   }
00128 
00129   char ch = static_cast<char>(key_event.keysym.unicode);
00130   if (isprint(ch)) {
00131     state.keys.push_back(ch);
00132   }
00133   keys_sys.push_back(ch);
00134   sdl_keys_sys.push_back(sym);
00135   sdl_mods_sys.push_back(mod);
00136   state.sdl_keys.push_back(sym);
00137 
00138   if (find(state.now_keys.begin(),
00139            state.now_keys.end(), sym) == state.now_keys.end()) {
00140     state.now_keys.push_back(sym);
00141   }
00142 }
00143 
00144 
00145 void UserInput::keyupHandler(SDL_KeyboardEvent& key_event) {
00146   SDLKey sym = key_event.keysym.sym;
00147   now_keys_t::iterator it =
00148     find(state.now_keys.begin(), state.now_keys.end(), sym);
00149   if (it != state.now_keys.end()) {
00150     state.now_keys.erase(it);
00151   }
00152 }
00153 
00154 
00155 void UserInput::mousebuttonHandler(SDL_MouseButtonEvent& button_event) {
00156   if (button_event.button == SDL_BUTTON_LEFT) {
00157     if (button_event.type == SDL_MOUSEBUTTONUP) {
00158       left_release = true;
00159       state.left_released = true;
00160     } else if (button_event.type == SDL_MOUSEBUTTONDOWN) {
00161       state.left_clicked = true;
00162     }
00163   }
00164 
00165   if (button_event.button == SDL_BUTTON_WHEELUP) {
00166     --state.wheel_moved;
00167   } else if (button_event.button == SDL_BUTTON_WHEELDOWN) {
00168     ++state.wheel_moved;
00169   }
00170 }
00171 
00172 
00173 UserInput::userInput_t UserInput::getInputed(void) {
00174   if (!obj) {
00175     getObject();
00176     stateClear();
00177   }
00178   userInput_t ret_state = obj->state;
00179   stateClear();
00180 
00181   return ret_state;
00182 }
00183 
00184 
00185 bool UserInput::userInput_t::isPressed(char ch) const {
00186   return (find(keys.begin(), keys.end(), ch) == keys.end())
00187     ? false : true;
00188 }
00189 
00190 
00191 bool UserInput::userInput_t::isPressedCode(int code) const {
00192   return (find(sdl_keys.begin(), sdl_keys.end(), code) == sdl_keys.end())
00193     ? false : true;
00194 }
00195 
00196 
00197 bool UserInput::userInput_t::nowPressedCode(int code) const {
00198   return (find(now_keys.begin(), now_keys.end(), code) == now_keys.end())
00199     ? false : true;
00200 }
00201 

Generated on Mon Apr 13 22:52:01 2009 by  doxygen 1.5.7.1