KeyCommand.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "KeyCommand.h"
  2. #include "WindowLocator.h"
  3. void KeyCommand::bindByKeyName(std::string p_keyName)
  4. {
  5. // Get the scancode by the key name, and call bind
  6. bind(WindowLocator::get().getScancode(p_keyName));
  7. }
  8. void KeyCommand::bind(Scancode p_scancode)
  9. {
  10. // If the scancode is valid, bind this key command to it
  11. if(p_scancode != Scancode::Key_Invalid)
  12. {
  13. WindowLocator::get().bindCommand(p_scancode, this);
  14. }
  15. }
  16. void KeyCommand::unbind(Scancode p_scancode)
  17. {
  18. // Iterate over all scancodes, if they match, unbind the command and remove the scancode
  19. for(decltype(m_scancodes.size()) i = 0, size = m_scancodes.size(); i < size; i++)
  20. if(m_scancodes[i] == p_scancode)
  21. {
  22. WindowLocator::get().unbindCommand(p_scancode, this);
  23. std::swap(m_scancodes[i], m_scancodes.back());
  24. m_scancodes.pop_back();
  25. return;
  26. }
  27. }
  28. void KeyCommand::unbindAll()
  29. {
  30. for(decltype(m_scancodes.size()) i = 0, size = m_scancodes.size(); i < size; i++)
  31. WindowLocator::get().unbindCommand(m_scancodes[i], this);
  32. removeAllScancodes();
  33. }