148 rader
2.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Benjamin Litzelmann
  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://www.wtfpl.net/ for more details.
  9. //
  10. #ifdef LOL_INPUT_V2
  11. #if defined HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include "core.h"
  15. namespace lol
  16. {
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // KeyBinding
  19. void KeyBinding::Bind(const char* device_name, const char* key_name)
  20. {
  21. m_device = InputDevice::Get(device_name);
  22. if (!m_device)
  23. {
  24. Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
  25. return;
  26. }
  27. m_keyindex = m_device->GetKeyIndex(key_name);
  28. if (m_keyindex < 0)
  29. {
  30. Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
  31. }
  32. }
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // AxisBinding
  35. void AxisBinding::Bind(const char* device_name, const char* axis_name)
  36. {
  37. m_device = InputDevice::Get(device_name);
  38. if (!m_device)
  39. {
  40. Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
  41. return;
  42. }
  43. m_axisindex = m_device->GetAxisIndex(axis_name);
  44. if (m_axisindex < 0)
  45. {
  46. Log::Warn("Trying to bind controller to axis %s.%s which doesn't exist", device_name, axis_name);
  47. }
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // Controller
  51. Array<Controller*> Controller::controllers;
  52. Controller::Controller(int nb_keys, int nb_axis)
  53. {
  54. m_gamegroup = GAMEGROUP_BEFORE;
  55. m_nb_keys = nb_keys;
  56. m_nb_axis = nb_axis;
  57. m_keys = new KeyBinding[m_nb_keys];
  58. m_axis = new AxisBinding[m_nb_axis];
  59. m_activate_nextframe = false;
  60. m_deactivate_nextframe = false;
  61. m_active = false;
  62. controllers.Push(this);
  63. }
  64. Controller::~Controller()
  65. {
  66. for (int i = 0; i < controllers.Count(); ++i)
  67. {
  68. if (controllers[i] == this)
  69. {
  70. controllers.Remove(i);
  71. break;
  72. }
  73. }
  74. }
  75. void Controller::TickGame(float seconds)
  76. {
  77. Entity::TickGame(seconds);
  78. for (int i = 0; i < m_nb_keys; ++i)
  79. {
  80. m_keys[i].Update();
  81. }
  82. for (int i = 0; i < m_nb_axis; ++i)
  83. {
  84. m_axis[i].Update();
  85. }
  86. if (m_activate_nextframe)
  87. m_active = true;
  88. if (m_deactivate_nextframe)
  89. m_active = false;
  90. m_activate_nextframe = false;
  91. m_deactivate_nextframe = false;
  92. }
  93. void Controller::Activate()
  94. {
  95. m_activate_nextframe = true;
  96. m_deactivate_nextframe = false;
  97. }
  98. void Controller::Deactivate()
  99. {
  100. m_deactivate_nextframe = true;
  101. m_activate_nextframe = false;
  102. }
  103. Array<Controller*> Controller::DeactivateAll()
  104. {
  105. Array<Controller*> result;
  106. for (int i = 0; i < controllers.Count(); ++i)
  107. {
  108. if (controllers[i]->m_active || controllers[i]->m_activate_nextframe)
  109. {
  110. result.Push(controllers[i]);
  111. controllers[i]->Deactivate();
  112. }
  113. }
  114. return result;
  115. }
  116. } /* namespace lol */
  117. #endif // LOL_INPUT_V2