00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "InputAgent.h"
00010
00011 #include "KeyBinder.h"
00012 #include "InputHandler.h"
00013
00014 #include "MessagerAgent.h"
00015 #include "SimpleMsg.h"
00016 #include "UnknownMsgException.h"
00017 #include "Name.h"
00018 #include "MouseStroke.h"
00019
00020 #include "SDL.h"
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 void
00033 InputAgent::own_init()
00034 {
00035 m_keyBinder = new KeyBinder();
00036 m_handler = NULL;
00037 m_keys = SDL_GetKeyState(NULL);
00038
00039 SDL_EnableUNICODE(1);
00040 }
00041
00042 void
00043 InputAgent::own_update()
00044 {
00045 SDL_Event event;
00046 while (SDL_PollEvent(&event)) {
00047 switch (event.type) {
00048 case SDL_QUIT:
00049 {
00050 BaseMsg *msg = new SimpleMsg(Name::APP_NAME, "quit");
00051 MessagerAgent::agent()->forwardNewMsg(msg);
00052 break;
00053 }
00054 case SDL_KEYDOWN:
00055 m_keyBinder->keyDown(event.key.keysym);
00056 if (m_handler) {
00057 m_handler->keyEvent(KeyStroke(event.key.keysym));
00058 }
00059 break;
00060 case SDL_MOUSEBUTTONDOWN:
00061 if (m_handler) {
00062 m_handler->mouseEvent(MouseStroke(event.button));
00063 }
00064 break;
00065 default:
00066 break;
00067 }
00068 }
00069
00070 if (m_handler) {
00071 Uint8 buttons;
00072 V2 mouseLoc = getMouseState(&buttons);
00073 m_handler->mouseState(mouseLoc, buttons);
00074 }
00075 }
00076
00077
00078
00079
00080 void
00081 InputAgent::own_shutdown()
00082 {
00083 delete m_keyBinder;
00084 }
00085
00086 void
00087 InputAgent::installHandler(InputHandler *handler)
00088 {
00089 if (m_handler) {
00090 m_handler->takePressed(NULL);
00091 m_handler->mouseState(V2(-1, -1), 0);
00092 }
00093 m_handler = handler;
00094 if (m_handler) {
00095 m_handler->takePressed(m_keys);
00096 Uint8 buttons;
00097 V2 mouseLoc = getMouseState(&buttons);
00098 m_handler->mouseState(mouseLoc, buttons);
00099 }
00100 }
00101
00102
00103
00104
00105
00106
00107 V2
00108 InputAgent::getMouseState(Uint8 *out_buttons)
00109 {
00110 int x;
00111 int y;
00112 Uint8 pressed = SDL_GetMouseState(&x, &y);
00113 if (out_buttons) {
00114 *out_buttons = pressed;
00115 }
00116 return V2(x, y);
00117 }
00118
00119