You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inputhandler.cpp 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: inputhandler.cpp
  5. *
  6. * Description: Source for inputhandler
  7. *
  8. * Version: 1.0
  9. * Created: 03/28/2014 06:19:17 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: YOUR NAME (),
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include "inputhandler.h"
  19. #include <iostream>
  20. namespace vtk {
  21. void InputHandler::update() {
  22. while (SDL_PollEvent(&event) ) {
  23. if (events.find(event.type) != events.end()) {
  24. events[event.type]();
  25. }
  26. }
  27. mouseButtons = SDL_GetMouseState(NULL, NULL);
  28. keys = SDL_GetKeyboardState(NULL);
  29. }
  30. bool InputHandler::isActionDown(const std::string& action) {
  31. ActionIdentifier& curAction = actions[action];
  32. if (curAction.mouse) {
  33. switch (curAction.mouseCode) {
  34. case 1:
  35. return mouseButtons&SDL_BUTTON(1);
  36. break;
  37. case 2:
  38. return mouseButtons&SDL_BUTTON(2);
  39. break;
  40. case 3:
  41. return mouseButtons&SDL_BUTTON(3);
  42. break;
  43. case 4:
  44. return mouseButtons&SDL_BUTTON(4);
  45. break;
  46. case 5:
  47. return mouseButtons&SDL_BUTTON(5);
  48. break;
  49. case 6:
  50. return mouseButtons&SDL_BUTTON(6);
  51. break;
  52. case 7:
  53. return mouseButtons&SDL_BUTTON(7);
  54. break;
  55. default:
  56. break;
  57. }
  58. } else {
  59. return keys[SDL_GetScancodeFromKey(curAction.keyCode)];
  60. }
  61. return false;
  62. }
  63. bool InputHandler::setAction(const std::string& actionName, const std::string& actionButton) {
  64. ActionIdentifier newIdentifier;
  65. if (actionButton.find("Mouse ") != std::string::npos) {
  66. newIdentifier.mouse = true;
  67. if (actionButton == "Mouse Left") {
  68. newIdentifier.mouseCode = 1;
  69. } else if (actionButton == "Mouse Middle") {
  70. newIdentifier.mouseCode = 2;
  71. } else if (actionButton == "Mouse Right") {
  72. newIdentifier.mouseCode = 3;
  73. } else if (actionButton == "Mouse Wheel Up") {
  74. newIdentifier.mouseCode = 4;
  75. } else if (actionButton == "Mouse Wheel Down") {
  76. newIdentifier.mouseCode = 5;
  77. } else if (actionButton == "Mouse Shoulder 1") {
  78. newIdentifier.mouseCode = 6;
  79. } else if (actionButton == "Mouse Shoulder 2") {
  80. newIdentifier.mouseCode = 7;
  81. }
  82. } else {
  83. newIdentifier.mouse = false;
  84. newIdentifier.keyCode = SDL_GetKeyFromName(actionButton.c_str());
  85. }
  86. actions[actionName] = newIdentifier;
  87. return true;
  88. }
  89. Nano::signal<void()>& InputHandler::getEventSignal(const Uint32& type) {
  90. return events[type];
  91. }
  92. }