callbacks.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "callbacks.h"
  2. #include "window.h"
  3. void windowFocusCallback(GLFWwindow *window, int focused)
  4. {
  5. auto ptr = glfwGetWindowUserPointer(window);
  6. pika::PikaWindow &pikaWindow = *(pika::PikaWindow *)ptr;
  7. if (focused)
  8. {
  9. pikaWindow.windowState.hasFocus = 1;
  10. }
  11. else
  12. {
  13. pikaWindow.windowState.hasFocus = 0;
  14. }
  15. }
  16. static void processAButton(pika::Button &b, int action)
  17. {
  18. if (action == GLFW_PRESS)
  19. {
  20. b.setHeld(true);
  21. }
  22. else if (action == GLFW_RELEASE)
  23. {
  24. b.setHeld(false);
  25. }
  26. else if (action == GLFW_REPEAT)
  27. {
  28. b.setHeld(true);
  29. b.setTyped(true);
  30. }
  31. };
  32. void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
  33. {
  34. auto ptr = glfwGetWindowUserPointer(window);
  35. pika::PikaWindow &pikaWindow = *(pika::PikaWindow *)ptr;
  36. if(key >= GLFW_KEY_A && key <= GLFW_KEY_Z)
  37. {
  38. int index = key - GLFW_KEY_A;
  39. processAButton(pikaWindow.input.buttons[pika::Button::A + index], action);
  40. }else if (key >= GLFW_KEY_0 && key <= GLFW_KEY_9)
  41. {
  42. int index = key - GLFW_KEY_0;
  43. processAButton(pikaWindow.input.buttons[pika::Button::NR0 + index], action);
  44. }else
  45. {
  46. if (key == GLFW_KEY_SPACE)
  47. {
  48. processAButton(pikaWindow.input.buttons[pika::Button::Space], action);
  49. }
  50. else
  51. if (key == GLFW_KEY_ENTER)
  52. {
  53. processAButton(pikaWindow.input.buttons[pika::Button::Enter], action);
  54. }
  55. else
  56. if (key == GLFW_KEY_ESCAPE)
  57. {
  58. processAButton(pikaWindow.input.buttons[pika::Button::Escape], action);
  59. }
  60. else
  61. if (key == GLFW_KEY_UP)
  62. {
  63. processAButton(pikaWindow.input.buttons[pika::Button::Up], action);
  64. }
  65. else
  66. if (key == GLFW_KEY_DOWN)
  67. {
  68. processAButton(pikaWindow.input.buttons[pika::Button::Down], action);
  69. }
  70. else
  71. if (key == GLFW_KEY_LEFT)
  72. {
  73. processAButton(pikaWindow.input.buttons[pika::Button::Left], action);
  74. }
  75. else
  76. if (key == GLFW_KEY_RIGHT)
  77. {
  78. processAButton(pikaWindow.input.buttons[pika::Button::Right], action);
  79. }
  80. else
  81. if (key == GLFW_KEY_LEFT_CONTROL)
  82. {
  83. processAButton(pikaWindow.input.buttons[pika::Button::LeftCtrl], action);
  84. }else
  85. if (key == GLFW_KEY_TAB)
  86. {
  87. processAButton(pikaWindow.input.buttons[pika::Button::Tab], action);
  88. }else
  89. if (key == GLFW_KEY_LEFT_ALT)
  90. {
  91. processAButton(pikaWindow.input.buttons[pika::Button::LeftAlt], action);
  92. }
  93. }
  94. }
  95. void mouseCallback(GLFWwindow *window, int key, int action, int mods)
  96. {
  97. auto ptr = glfwGetWindowUserPointer(window);
  98. pika::PikaWindow &pikaWindow = *(pika::PikaWindow *)ptr;
  99. if (key == GLFW_MOUSE_BUTTON_LEFT)
  100. {
  101. processAButton(pikaWindow.input.lMouse, action);
  102. }
  103. else if(key == GLFW_MOUSE_BUTTON_RIGHT)
  104. {
  105. processAButton(pikaWindow.input.rMouse, action);
  106. }
  107. }