efa14d
efa14d
efa14d
#include <tools inputstate.h=""></tools>
efa14d
d8eddc
#include <iomanip></iomanip>
d8eddc
#include <qkeysequence></qkeysequence>
d8eddc
efa14d
efa14d
//*****************************************************************************************
49945e
//    TKey static members
49945e
//*****************************************************************************************
49945e
49945e
const TKey TKey::shift   ( Qt::Key_Shift   , true );
49945e
const TKey TKey::control ( Qt::Key_Control , true );
49945e
const TKey TKey::alt     ( Qt::Key_Alt     , true );
49945e
const TKey TKey::meta    ( Qt::Key_Meta    , true );
49945e
49945e
49945e
Qt::Key
49945e
TKey::mapKey(Qt::Key key) {
49945e
  switch(key) {
49945e
  case Qt::Key_AltGr: return Qt::Key_Alt;
49945e
  default: break;
49945e
  }
49945e
  return key;
49945e
}
49945e
49945e
49945e
bool
49945e
TKey::isModifier(Qt::Key key) {
49945e
  key = mapKey(key);
49945e
  return key == Qt::Key_Shift
49945e
      || key == Qt::Key_Control
49945e
      || key == Qt::Key_Alt
49945e
      || key == Qt::Key_AltGr
49945e
      || key == Qt::Key_Meta;
49945e
}
49945e
49945e
49945e
bool
49945e
TKey::isNumber(Qt::Key key) {
49945e
  key = mapKey(key);
49945e
  return key >= Qt::Key_0 && key <= Qt::Key_9;
49945e
}
49945e
49945e
49945e
//*****************************************************************************************
49945e
//    TInputState implementation
efa14d
//*****************************************************************************************
efa14d
efa14d
TInputState::TInputState():
49945e
  m_ticks(),
49945e
  m_keyHistory(new KeyHistory())
efa14d
  { }
efa14d
efa14d
TInputState::~TInputState()
efa14d
  { }
efa14d
efa14d
void
efa14d
TInputState::touch(TTimerTicks ticks) {
49945e
  if (m_ticks < ticks)
49945e
    m_ticks = ticks;
efa14d
  else
49945e
    ++m_ticks;
efa14d
}
efa14d
efa14d
TInputState::ButtonHistory::Pointer
efa14d
TInputState::buttonHistory(DeviceId device) const {
49945e
  ButtonHistory::Pointer &history = m_buttonHistories[device];
efa14d
  if (!history) history = new ButtonHistory();
efa14d
  return history;
efa14d
}
efa14d
efa14d
TInputState::ButtonState::Pointer
efa14d
TInputState::buttonFindAny(Button button, DeviceId &outDevice) {
49945e
  for(ButtonHistoryMap::const_iterator i = m_buttonHistories.begin(); i != m_buttonHistories.end(); ++i) {
efa14d
    ButtonState::Pointer state = i->second->current()->find(button);
efa14d
    if (state) {
efa14d
      outDevice = i->first;
efa14d
      return state;
efa14d
    }
efa14d
  }
efa14d
  outDevice = DeviceId();
efa14d
  return ButtonState::Pointer();
efa14d
}
d8eddc
d8eddc
d8eddc
namespace {
d8eddc
  
d8eddc
template<typename t=""></typename>
d8eddc
void printKey(const T &k, std::ostream &stream)
d8eddc
  { stream << k; }
d8eddc
d8eddc
template<>
d8eddc
void printKey<tkey>(const TKey &k, std::ostream &stream) {</tkey>
d8eddc
  stream << QKeySequence(k.key).toString().toStdString() << "[" << std::hex << k.key << std::dec;
d8eddc
  if (k.generic) stream << "g";
d8eddc
  if (k.numPad) stream << "n";
d8eddc
  stream << "]";
d8eddc
}
d8eddc
d8eddc
template<typename t=""></typename>
d8eddc
class Print {
d8eddc
public:
d8eddc
  typedef T Type;
d8eddc
  typedef TKeyHistoryT<type> History;</type>
d8eddc
  typedef typename History::StatePointer StatePointer;
d8eddc
  typedef typename History::StateMap StateMap;
d8eddc
  typedef typename History::LockSet LockSet;
d8eddc
d8eddc
  static void print(const History &history, std::ostream &stream, const std::string &tab) {
d8eddc
    const StateMap &states = history.getStates();
d8eddc
    stream << tab << "states: " << std::endl;
d8eddc
    for(typename StateMap::const_iterator i = states.begin(); i != states.end(); ++i) {
d8eddc
      stream << tab << "- " << i->first << std::endl;
d8eddc
      for(StatePointer p = i->second; p; p = p->previous) {
d8eddc
        stream << tab << "- - " << p->ticks << ": ";
d8eddc
        printKey(p->value, stream);
d8eddc
        stream << std::endl;
d8eddc
      }
d8eddc
    }
d8eddc
    
d8eddc
    const LockSet &locks = history.getLocks();
d8eddc
    stream << tab << "locks: ";
d8eddc
    for(typename LockSet::const_iterator i = locks.begin(); i != locks.end(); ++i) {
d8eddc
      if (i != locks.begin()) stream << ", ";
d8eddc
      stream << *i;
d8eddc
    }
d8eddc
    stream << std::endl;
d8eddc
  }
d8eddc
};
d8eddc
}
d8eddc
d8eddc
d8eddc
void TInputState::print(std::ostream &stream, const std::string &tab) const {
d8eddc
  stream << tab << "keys:" << std::endl;
d8eddc
  Print<tkey>::print(*m_keyHistory, stream, tab + "  ");</tkey>
d8eddc
  for(ButtonHistoryMap::const_iterator i = m_buttonHistories.begin(); i != m_buttonHistories.end(); ++i) {
d8eddc
    stream << tab << "buttons[" << i->first << "]:" << std::endl;
d8eddc
    Print<tkey>::print(*m_keyHistory, stream, tab + "  ");</tkey>
d8eddc
  }
d8eddc
}
d8eddc