Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

InputAgent.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
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  * Enable SDL_UNICODE.
00025  * Enable key repeat.
00026  * Set console handler to ScriptAgent.
00027  *
00028  * NOTE: every SDL_InitSubSystem will disable UNICODE
00029  * hence InputAgent init must be after VideoAgent and SoundAgent.
00030  * NOTE: KeyConsole() use Path which asks OptionAgent
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  * Delete console.
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  * Return mouse location.
00104  * @param out_buttons place where to store state of buttons
00105  * @return (mouseX, mouseY)
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 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2