No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

101 líneas
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. namespace lol
  14. {
  15. class KeyBinding
  16. {
  17. public:
  18. KeyBinding() : m_device(nullptr), m_current(false), m_previous(false) {}
  19. bool IsDown() const { return m_current; }
  20. bool IsUp() const { return !m_current; }
  21. bool IsPressed() const { return m_current && !m_previous; }
  22. bool IsReleased() const { return !m_current && m_previous; }
  23. void Bind(const char* device_name, const char* key_name);
  24. bool IsBound() { return m_device && m_keyindex != -1; }
  25. protected:
  26. void Update() { m_previous = m_current; m_current = IsBound() ? 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. bool IsBound() { return m_device && m_axisindex != -1; }
  41. protected:
  42. void Update() { m_previous = m_current; m_current = IsBound() ? m_device->GetAxis(m_axisindex) : 0.0f; }
  43. const InputDevice* m_device;
  44. int m_axisindex;
  45. float m_current;
  46. float m_previous;
  47. friend class Controller;
  48. };
  49. class Controller : Entity
  50. {
  51. public:
  52. Controller(int nb_keys, int nb_axis);
  53. ~Controller();
  54. virtual void TickGame(float seconds);
  55. /** Activate the controller on next frame */
  56. void Activate();
  57. /** Deactivate the controller on next frame */
  58. void Deactivate();
  59. /** Deactivate every active controller on next frame and return an array of deactivated (previously active) controllers */
  60. static Array<Controller*> DeactivateAll();
  61. KeyBinding& GetKey(int index) { return m_keys[index]; }
  62. AxisBinding& GetAxis(int index) { return m_axis[index]; }
  63. protected:
  64. Array<KeyBinding> m_keys;
  65. Array<AxisBinding> m_axis;
  66. private:
  67. static Array<Controller*> controllers;
  68. bool m_activate_nextframe;
  69. bool m_deactivate_nextframe;
  70. bool m_active;
  71. };
  72. } /* namespace lol */
  73. #endif // __LOL_INPUT_CONTROLLER_H__