source: trunk/src/input/controller.cpp @ 2925

Last change on this file since 2925 was 2925, checked in by benlitz, 6 years ago

input: added a name the controllers, and a static Get() function to easily retrieve a registered controller

  • Property svn:eol-style set to LF
File size: 5.2 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 HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16
17namespace lol
18{
19
20///////////////////////////////////////////////////////////////////////////////
21// KeyBinding
22
23void KeyBinding::Bind(const char* device_name, const char* key_name)
24{
25    ClearBinding();
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
42void KeyBinding::ClearBinding()
43{
44    m_keyindex = -1;
45}
46
47///////////////////////////////////////////////////////////////////////////////
48// AxisBinding
49
50void AxisBinding::Bind(const char* device_name, const char* axis_name)
51{
52    ClearBinding();
53
54    m_device = InputDevice::Get(device_name);
55    if (!m_device)
56    {
57        Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
58        return;
59    }
60
61    m_axisindex = m_device->GetAxisIndex(axis_name);
62
63    if (m_axisindex < 0)
64    {
65        Log::Warn("Trying to bind controller to axis %s.%s which doesn't exist", device_name, axis_name);
66    }
67}
68
69void AxisBinding::BindKey(const char* device_name, const char* key_name)
70{
71    ClearBinding();
72
73    m_device = InputDevice::Get(device_name);
74    if (!m_device)
75    {
76        Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
77        return;
78    }
79
80    m_maxkeyindex = m_device->GetKeyIndex(key_name);
81
82    if (m_maxkeyindex < 0)
83    {
84        Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
85    }
86}
87
88void AxisBinding::BindKeys(const char* device_name, const char* min_key_name, const char* max_key_name)
89{
90    ClearBinding();
91    m_device = InputDevice::Get(device_name);
92    if (!m_device)
93    {
94        Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
95        return;
96    }
97
98    m_minkeyindex = m_device->GetKeyIndex(min_key_name);
99    m_maxkeyindex = m_device->GetKeyIndex(max_key_name);
100
101    if (m_minkeyindex < 0)
102    {
103        Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, min_key_name);
104        ClearBinding();
105    }
106
107    if (m_maxkeyindex < 0)
108    {
109        Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, max_key_name);
110        ClearBinding();
111    }
112}
113
114void AxisBinding::ClearBinding()
115{
116    m_axisindex = -1;
117    m_minkeyindex = -1;
118    m_maxkeyindex = -1;
119}
120
121float AxisBinding::RetrieveCurrentValue()
122{
123    if (m_axisindex != -1)
124    {
125        return m_device->GetAxis(m_axisindex);
126    }
127
128    float value = 0.0f;
129    if (m_minkeyindex != -1)
130        value += m_device->GetKey(m_minkeyindex) ? -1.0f : 0.0f;
131    if (m_maxkeyindex != -1)
132        value += m_device->GetKey(m_maxkeyindex) ? 1.0f : 0.0f;
133
134    return value;
135}
136
137///////////////////////////////////////////////////////////////////////////////
138// Controller
139
140Array<Controller*> Controller::controllers;
141
142Controller::Controller(char const* name, int nb_keys, int nb_axis)
143{
144    m_gamegroup = GAMEGROUP_BEFORE;
145    m_name = name;
146    m_keys.Resize(nb_keys);
147    m_axis.Resize(nb_axis);
148    m_activate_nextframe = false;
149    m_deactivate_nextframe = false;
150    m_active = false;
151        if (Get(name) != nullptr)
152        {
153                Log::Warn("a controller with this name has already been registered");
154        }
155    controllers.Push(this);
156}
157
158Controller::~Controller()
159{
160    for (int i = 0; i < controllers.Count(); ++i)
161    {
162        if (controllers[i] == this)
163        {
164            controllers.Remove(i);
165            break;
166        }
167    }
168}
169
170Controller* Controller::Get(char const* name)
171{
172    for (int i = 0; i < controllers.Count(); ++i)
173    {
174        if (controllers[i]->m_name == name)
175            return controllers[i];
176    }
177    return nullptr;
178}
179
180void Controller::TickGame(float seconds)
181{
182    Entity::TickGame(seconds);
183
184    for (int i = 0; i < m_keys.Count(); ++i)
185        m_keys[i].Update();
186
187    for (int i = 0; i < m_axis.Count(); ++i)
188        m_axis[i].Update();
189
190    if (m_activate_nextframe)
191        m_active = true;
192
193    if (m_deactivate_nextframe)
194        m_active = false;
195
196    m_activate_nextframe = false;
197    m_deactivate_nextframe = false;
198}
199
200void Controller::Activate()
201{
202    m_activate_nextframe = true;
203    m_deactivate_nextframe = false;
204}
205
206void Controller::Deactivate()
207{
208    m_deactivate_nextframe = true;
209    m_activate_nextframe = false;
210}
211
212Array<Controller*> Controller::DeactivateAll()
213{
214    Array<Controller*> result;
215
216    for (int i = 0; i < controllers.Count(); ++i)
217    {
218        if (controllers[i]->m_active || controllers[i]->m_activate_nextframe)
219        {
220            result.Push(controllers[i]);
221            controllers[i]->Deactivate();
222        }
223    }
224
225    return result;
226}
227
228} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.