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.
 
 
 

180 line
3.7 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. vec2i mouse;
  36. vec3i 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. Uint8 *keystate = SDL_GetKeyState(NULL);
  53. int left = keystate[SDLK_d] - (keystate[SDLK_a] | keystate[SDLK_q]);
  54. int up = (keystate[SDLK_w] | keystate[SDLK_z]) - keystate[SDLK_s] ;
  55. ret.x += left;
  56. ret.y += up;
  57. if (left && up)
  58. ret = ret * sqrtf(0.5f);
  59. #else
  60. ret = 0;
  61. #endif
  62. return ret;
  63. }
  64. vec2i Input::GetMousePos()
  65. {
  66. return data->mouse;
  67. }
  68. vec3i Input::GetMouseButtons()
  69. {
  70. return data->buttons;
  71. }
  72. void Input::TrackMouse(WorldEntity *e)
  73. {
  74. if (data->nentities >= InputData::MAX_ENTITIES)
  75. return;
  76. data->entities[data->nentities] = e;
  77. data->nentities++;
  78. }
  79. void Input::UntrackMouse(WorldEntity *e)
  80. {
  81. for (int n = 0; n < data->nentities; n++)
  82. {
  83. if (data->entities[n] != e)
  84. continue;
  85. data->entities[n] = data->entities[data->nentities - 1];
  86. data->nentities--;
  87. n--;
  88. }
  89. }
  90. void Input::SetMousePos(vec2i coord)
  91. {
  92. data->mouse = coord;
  93. WorldEntity *top = NULL;
  94. for (int n = 0; n < data->nentities; n++)
  95. {
  96. if (coord.x < data->entities[n]->bbox[0].x
  97. || coord.x >= data->entities[n]->bbox[1].x
  98. || coord.y < data->entities[n]->bbox[0].y
  99. || coord.y >= data->entities[n]->bbox[1].y)
  100. continue;
  101. if (!top || top->bbox[1].z < data->entities[n]->bbox[1].z)
  102. top = data->entities[n];
  103. }
  104. for (int n = 0; n < data->nentities; n++)
  105. {
  106. if (data->entities[n] == top)
  107. {
  108. data->entities[n]->mousepos = (vec2i)((vec3i)coord - top->bbox[0]);
  109. if (top != data->lastfocus)
  110. data->entities[n]->pressed = data->buttons;
  111. else
  112. data->entities[n]->clicked = 0;
  113. }
  114. else
  115. {
  116. data->entities[n]->mousepos = vec2i(-1);
  117. /* FIXME */
  118. data->entities[n]->released = 0;
  119. data->entities[n]->pressed = 0;
  120. data->entities[n]->clicked = 0;
  121. }
  122. }
  123. data->lastfocus = top;
  124. }
  125. void Input::SetMouseButton(int index)
  126. {
  127. data->buttons[index] = 1;
  128. if (data->lastfocus)
  129. {
  130. if (!data->lastfocus->pressed[index])
  131. data->lastfocus->clicked[index] = 1;
  132. data->lastfocus->pressed[index] = 1;
  133. data->lastfocus->released[index] = 0;
  134. }
  135. }
  136. void Input::UnsetMouseButton(int index)
  137. {
  138. data->buttons[index] = 0;
  139. if (data->lastfocus)
  140. {
  141. if (data->lastfocus->pressed[index])
  142. data->lastfocus->released[index] = 1;
  143. data->lastfocus->pressed[index] = 0;
  144. data->lastfocus->clicked[index] = 0;
  145. }
  146. }
  147. } /* namespace lol */