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

KeyBinder.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 "KeyBinder.h"
00010 
00011 #include "Log.h"
00012 #include "LogicException.h"
00013 #include "BaseMsg.h"
00014 
00015 #include "SDL.h"
00016 
00017 //-----------------------------------------------------------------
00018 KeyBinder::~KeyBinder()
00019 {
00020     t_strokes::iterator end = m_strokes.end();
00021     for (t_strokes::iterator i = m_strokes.begin(); i != end; ++i) {
00022         delete i->second;
00023     }
00024 }
00025 //-----------------------------------------------------------------
00026 /**
00027  * Bind keystroke.
00028  * @param stroke keystroke
00029  * @param msg message to raise, will be deleted
00030  * @throws LogicException when keystroke is occupied
00031  */
00032 void
00033 KeyBinder::addStroke(const KeyStroke &stroke, BaseMsg *msg)
00034 {
00035     std::pair<t_strokes::iterator,bool> status =
00036         m_strokes.insert(
00037                 std::pair<KeyStroke,BaseMsg*>(stroke, msg));
00038     if (!status.second) {
00039         throw LogicException(ExInfo("keystroke is occupied")
00040                 .addInfo("keystroke", stroke.toString()));
00041     }
00042     else {
00043         LOG_DEBUG(ExInfo("binding keystroke")
00044                 .addInfo("keystroke", stroke.toString())
00045                 .addInfo("msg", msg->toString()));
00046     }
00047 }
00048 //-----------------------------------------------------------------
00049 void
00050 KeyBinder::removeStroke(const KeyStroke &stroke)
00051 {
00052     t_strokes::iterator it = m_strokes.find(stroke);
00053     if (m_strokes.end() != it) {
00054         delete it->second;
00055         m_strokes.erase(it);
00056     }
00057     else {
00058         LOG_WARNING(ExInfo("keystroke does not exist")
00059                 .addInfo("keystroke", stroke.toString()));
00060     }
00061 }
00062 //-----------------------------------------------------------------
00063 /**
00064  * Handle keydown event,
00065  * find keystroke and send message.
00066  */
00067 void
00068 KeyBinder::keyDown(const SDL_keysym &keysym) const
00069 {
00070     KeyStroke stroke(keysym);
00071     t_strokes::const_iterator it = m_strokes.find(stroke);
00072     if (m_strokes.end() != it) {
00073         it->second->sendClone();
00074     }
00075 }
00076 

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