00001
00002
00003
00004
00005
00006
00007
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
00028
00029
00030
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
00065
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