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.
 
 
 

103 lines
2.5 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. #if !defined __LOL_INPUT_CONTROLLER_H__
  11. #define __LOL_INPUT_CONTROLLER_H__
  12. #include "core.h"
  13. #if defined LOL_INPUT_V2
  14. namespace lol
  15. {
  16. class KeyBinding
  17. {
  18. public:
  19. KeyBinding() : m_device(nullptr), m_current(false), m_previous(false) {}
  20. bool IsDown() const { return m_current; }
  21. bool IsUp() const { return !m_current; }
  22. bool IsPressed() const { return m_current && !m_previous; }
  23. bool IsReleased() const { return !m_current && m_previous; }
  24. void Bind(const char* device_name, const char* key_name);
  25. protected:
  26. void Update() { m_previous = m_current; m_current = m_device ? m_device->GetKey(m_keyindex) : false; }
  27. const InputDevice* m_device;
  28. int m_keyindex;
  29. bool m_current;
  30. bool m_previous;
  31. friend class Controller;
  32. };
  33. class AxisBinding
  34. {
  35. public:
  36. AxisBinding() : m_device(nullptr), m_current(0.0f), m_previous(0.0f) {}
  37. float GetValue() const { return m_current; }
  38. float GetDelta() const { return m_current - m_previous; }
  39. void Bind(const char* device_name, const char* axis_name);
  40. protected:
  41. void Update() { m_previous = m_current; m_current = m_device ? m_device->GetAxis(m_axisindex) : 0.0f; }
  42. const InputDevice* m_device;
  43. int m_axisindex;
  44. float m_current;
  45. float m_previous;
  46. friend class Controller;
  47. };
  48. class Controller : Entity
  49. {
  50. public:
  51. Controller(int nb_keys, int nb_axis);
  52. ~Controller();
  53. virtual void TickGame(float seconds);
  54. /** Activate the controller on next frame */
  55. void Activate();
  56. /** Deactivate the controller on next frame */
  57. void Deactivate();
  58. /** Deactivate every active controller on next frame and return an array of deactivated (previously active) controllers */
  59. static Array<Controller*> DeactivateAll();
  60. KeyBinding& GetKey(int index) { ASSERT(index >= 0 && index < m_nb_keys); return m_keys[index]; }
  61. AxisBinding& GetAxis(int index) { ASSERT(index >= 0 && index < m_nb_axis); return m_axis[index]; }
  62. protected:
  63. KeyBinding* m_keys;
  64. AxisBinding* m_axis;
  65. int m_nb_keys;
  66. int m_nb_axis;
  67. private:
  68. static Array<Controller*> controllers;
  69. bool m_activate_nextframe;
  70. bool m_deactivate_nextframe;
  71. bool m_active;
  72. };
  73. } /* namespace lol */
  74. #endif // LOL_INPUT_V2
  75. #endif // __LOL_INPUT_CONTROLLER_H__