callbacks.cpp 3.2 KB

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