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

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

input: added functions to bind keys over axis (not tested)

  • Property svn:eol-style set to LF
File size: 4.8 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(int nb_keys, int nb_axis)
143{
144    m_gamegroup = GAMEGROUP_BEFORE;
145    m_keys.Resize(nb_keys);
146    m_axis.Resize(nb_axis);
147    m_activate_nextframe = false;
148    m_deactivate_nextframe = false;
149    m_active = false;
150    controllers.Push(this);
151}
152
153Controller::~Controller()
154{
155    for (int i = 0; i < controllers.Count(); ++i)
156    {
157        if (controllers[i] == this)
158        {
159            controllers.Remove(i);
160            break;
161        }
162    }
163}
164
165void Controller::TickGame(float seconds)
166{
167    Entity::TickGame(seconds);
168
169    for (int i = 0; i < m_keys.Count(); ++i)
170        m_keys[i].Update();
171
172    for (int i = 0; i < m_axis.Count(); ++i)
173        m_axis[i].Update();
174
175    if (m_activate_nextframe)
176        m_active = true;
177
178    if (m_deactivate_nextframe)
179        m_active = false;
180
181    m_activate_nextframe = false;
182    m_deactivate_nextframe = false;
183}
184
185void Controller::Activate()
186{
187    m_activate_nextframe = true;
188    m_deactivate_nextframe = false;
189}
190
191void Controller::Deactivate()
192{
193    m_deactivate_nextframe = true;
194    m_activate_nextframe = false;
195}
196
197Array<Controller*> Controller::DeactivateAll()
198{
199    Array<Controller*> result;
200
201    for (int i = 0; i < controllers.Count(); ++i)
202    {
203        if (controllers[i]->m_active || controllers[i]->m_activate_nextframe)
204        {
205            result.Push(controllers[i]);
206            controllers[i]->Deactivate();
207        }
208    }
209
210    return result;
211}
212
213} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.