| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "KeyBinder.h" |
| 10 |
|
| 11 |
#include "Log.h" |
| 12 |
#include "LogicException.h" |
| 13 |
#include "BaseMsg.h" |
| 14 |
|
| 15 |
#include "SDL.h" |
| 16 |
|
| 17 |
//----------------------------------------------------------------- |
| 18 |
KeyBinder::~KeyBinder() |
| 19 |
{ |
| 20 |
t_strokes::iterator end = m_strokes.end(); |
| 21 |
for (t_strokes::iterator i = m_strokes.begin(); i != end; ++i) { |
| 22 |
delete i->second; |
| 23 |
} |
| 24 |
} |
| 25 |
//----------------------------------------------------------------- |
| 26 |
/** |
| 27 |
* Bind keystroke. |
| 28 |
* @param stroke keystroke |
| 29 |
* @param msg message to raise, will be deleted |
| 30 |
* @throws LogicException when keystroke is occupied |
| 31 |
*/ |
| 32 |
void |
| 33 |
KeyBinder::addStroke(const KeyStroke &stroke, BaseMsg *msg) |
| 34 |
{ |
| 35 |
std::pair<t_strokes::iterator,bool> status = |
| 36 |
m_strokes.insert( |
| 37 |
std::pair<KeyStroke,BaseMsg*>(stroke, msg)); |
| 38 |
if (!status.second) { |
| 39 |
throw LogicException(ExInfo("keystroke is occupied") |
| 40 |
.addInfo("keystroke", stroke.toString())); |
| 41 |
} |
| 42 |
else { |
| 43 |
LOG_DEBUG(ExInfo("binding keystroke") |
| 44 |
.addInfo("keystroke", stroke.toString()) |
| 45 |
.addInfo("msg", msg->toString())); |
| 46 |
} |
| 47 |
} |
| 48 |
//----------------------------------------------------------------- |
| 49 |
void |
| 50 |
KeyBinder::removeStroke(const KeyStroke &stroke) |
| 51 |
{ |
| 52 |
t_strokes::iterator it = m_strokes.find(stroke); |
| 53 |
if (m_strokes.end() != it) { |
| 54 |
delete it->second; |
| 55 |
m_strokes.erase(it); |
| 56 |
} |
| 57 |
else { |
| 58 |
LOG_WARNING(ExInfo("keystroke does not exist") |
| 59 |
.addInfo("keystroke", stroke.toString())); |
| 60 |
} |
| 61 |
} |
| 62 |
//----------------------------------------------------------------- |
| 63 |
/** |
| 64 |
* Handle keydown event, |
| 65 |
* find keystroke and send message. |
| 66 |
*/ |
| 67 |
void |
| 68 |
KeyBinder::keyDown(const SDL_keysym &keysym) const |
| 69 |
{ |
| 70 |
KeyStroke stroke(keysym); |
| 71 |
t_strokes::const_iterator it = m_strokes.find(stroke); |
| 72 |
if (m_strokes.end() != it) { |
| 73 |
it->second->sendClone(); |
| 74 |
} |
| 75 |
} |
| 76 |
|