Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

184 строки
3.8 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdlib>
  14. #include <cmath>
  15. #if defined USE_SDL
  16. # include <SDL.h>
  17. #endif
  18. #include "core.h"
  19. namespace lol
  20. {
  21. /*
  22. * Input implementation class
  23. */
  24. static class InputData
  25. {
  26. friend class Input;
  27. public:
  28. InputData()
  29. : mouse(-1),
  30. buttons(0),
  31. nentities(0),
  32. lastfocus(0)
  33. { }
  34. private:
  35. ivec2 mouse;
  36. ivec3 buttons;
  37. static int const MAX_ENTITIES = 100;
  38. WorldEntity *entities[MAX_ENTITIES];
  39. int nentities;
  40. WorldEntity *lastfocus;
  41. }
  42. inputdata;
  43. static InputData * const data = &inputdata;
  44. /*
  45. * Public Input class
  46. */
  47. vec2 Input::GetAxis(int axis)
  48. {
  49. vec2 ret;
  50. #if defined USE_SDL
  51. /* Simulate a joystick using the keyboard. This SDL call is free. */
  52. #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION >= 3
  53. Uint8 *keystate = SDL_GetKeyboardState(NULL);
  54. #else
  55. Uint8 *keystate = SDL_GetKeyState(NULL);
  56. #endif
  57. int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
  58. int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
  59. ret.x += left;
  60. ret.y += up;
  61. if (left && up)
  62. ret = ret * sqrtf(0.5f);
  63. #else
  64. ret = vec2(0, 0);
  65. #endif
  66. return ret;
  67. }
  68. ivec2 Input::GetMousePos()
  69. {
  70. return data->mouse;
  71. }
  72. ivec3 Input::GetMouseButtons()
  73. {
  74. return data->buttons;
  75. }
  76. void Input::TrackMouse(WorldEntity *e)
  77. {
  78. if (data->nentities >= InputData::MAX_ENTITIES)
  79. return;
  80. data->entities[data->nentities] = e;
  81. data->nentities++;
  82. }
  83. void Input::UntrackMouse(WorldEntity *e)
  84. {
  85. for (int n = 0; n < data->nentities; n++)
  86. {
  87. if (data->entities[n] != e)
  88. continue;
  89. data->entities[n] = data->entities[data->nentities - 1];
  90. data->nentities--;
  91. n--;
  92. }
  93. }
  94. void Input::SetMousePos(ivec2 coord)
  95. {
  96. data->mouse = coord;
  97. WorldEntity *top = NULL;
  98. for (int n = 0; n < data->nentities; n++)
  99. {
  100. if (coord.x < data->entities[n]->bbox[0].x
  101. || coord.x >= data->entities[n]->bbox[1].x
  102. || coord.y < data->entities[n]->bbox[0].y
  103. || coord.y >= data->entities[n]->bbox[1].y)
  104. continue;
  105. if (!top || top->bbox[1].z < data->entities[n]->bbox[1].z)
  106. top = data->entities[n];
  107. }
  108. for (int n = 0; n < data->nentities; n++)
  109. {
  110. if (data->entities[n] == top)
  111. {
  112. data->entities[n]->mousepos = coord - (ivec2)top->bbox[0].xy;
  113. if (top != data->lastfocus)
  114. data->entities[n]->pressed = data->buttons;
  115. else
  116. data->entities[n]->clicked = ivec3(0);
  117. }
  118. else
  119. {
  120. data->entities[n]->mousepos = ivec2(-1);
  121. /* FIXME */
  122. data->entities[n]->released = ivec3(0);
  123. data->entities[n]->pressed = ivec3(0);
  124. data->entities[n]->clicked = ivec3(0);
  125. }
  126. }
  127. data->lastfocus = top;
  128. }
  129. void Input::SetMouseButton(int index)
  130. {
  131. data->buttons[index] = 1;
  132. if (data->lastfocus)
  133. {
  134. if (!data->lastfocus->pressed[index])
  135. data->lastfocus->clicked[index] = 1;
  136. data->lastfocus->pressed[index] = 1;
  137. data->lastfocus->released[index] = 0;
  138. }
  139. }
  140. void Input::UnsetMouseButton(int index)
  141. {
  142. data->buttons[index] = 0;
  143. if (data->lastfocus)
  144. {
  145. if (data->lastfocus->pressed[index])
  146. data->lastfocus->released[index] = 1;
  147. data->lastfocus->pressed[index] = 0;
  148. data->lastfocus->clicked[index] = 0;
  149. }
  150. }
  151. } /* namespace lol */