source: trunk/src/input/controller.cpp @ 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: 3.0 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#ifdef LOL_INPUT_V2
12
13#if defined HAVE_CONFIG_H
14#   include "config.h"
15#endif
16
17#include "core.h"
18
19namespace lol
20{
21
22///////////////////////////////////////////////////////////////////////////////
23// KeyBinding
24
25void KeyBinding::Bind(const char* device_name, const char* key_name)
26{
27        m_device = InputDevice::Get(device_name);
28        if (!m_device)
29        {
30                Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
31                return;
32        }
33
34        m_keyindex = m_device->GetKeyIndex(key_name);
35
36        if (m_keyindex < 0)
37        {
38                Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
39        }
40}
41
42///////////////////////////////////////////////////////////////////////////////
43// AxisBinding
44
45void AxisBinding::Bind(const char* device_name, const char* axis_name)
46{
47        m_device = InputDevice::Get(device_name);
48        if (!m_device)
49        {
50                Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
51                return;
52        }
53
54        m_axisindex = m_device->GetAxisIndex(axis_name);
55
56        if (m_axisindex < 0)
57        {
58                Log::Warn("Trying to bind controller to axis %s.%s which doesn't exist", device_name, axis_name);
59        }
60}
61
62
63///////////////////////////////////////////////////////////////////////////////
64// Controller
65
66Array<Controller*> Controller::controllers;
67
68Controller::Controller(int nb_keys, int nb_axis)
69{
70        m_gamegroup = GAMEGROUP_BEFORE;
71        m_nb_keys = nb_keys;
72        m_nb_axis = nb_axis;
73        m_keys = new KeyBinding[m_nb_keys];
74        m_axis = new AxisBinding[m_nb_axis];
75        m_activate_nextframe = false;
76        m_deactivate_nextframe = false;
77        m_active = false;
78        controllers.Push(this);
79}
80
81Controller::~Controller()
82{
83        for (int i = 0; i < controllers.Count(); ++i)
84        {
85                if (controllers[i] == this)
86                {
87                        controllers.Remove(i);
88                        break;
89                }
90        }
91}
92
93void Controller::TickGame(float seconds)
94{
95        Entity::TickGame(seconds);
96
97        for (int i = 0; i < m_nb_keys; ++i)
98        {
99                m_keys[i].Update();
100        }
101
102        for (int i = 0; i < m_nb_axis; ++i)
103        {
104                m_axis[i].Update();
105        }
106
107        if (m_activate_nextframe)
108                m_active = true;
109
110        if (m_deactivate_nextframe)
111                m_active = false;
112
113        m_activate_nextframe = false;
114        m_deactivate_nextframe = false;
115}
116
117void Controller::Activate()
118{
119        m_activate_nextframe = true;
120        m_deactivate_nextframe = false;
121}
122
123void Controller::Deactivate()
124{
125        m_deactivate_nextframe = true;
126        m_activate_nextframe = false;
127}
128
129Array<Controller*> Controller::DeactivateAll()
130{
131        Array<Controller*> result;
132
133        for (int i = 0; i < controllers.Count(); ++i)
134        {
135                if (controllers[i]->m_active || controllers[i]->m_activate_nextframe)
136                {
137                        result.Push(controllers[i]);
138                        controllers[i]->Deactivate();
139                }
140        }
141
142        return result;
143}
144
145} /* namespace lol */
146
147#endif // LOL_INPUT_V2
Note: See TracBrowser for help on using the repository browser.