source: trunk/src/input/controller.h @ 2810

Last change on this file since 2810 was 2810, checked in by benlitz, 8 years ago

Completely reworked input system. Unified device interface, button, axis and cursor support, controller and binding system, mouse capture. Tutorial 07_input provided. Require to compile lolcore with LOL_INPUT_V2 to be activated (also needed app-side before including core.h)

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