summaryrefslogtreecommitdiffstats
path: root/game/code/input/Keyboard.cpp
blob: f366e57550b4017f419c03af58b2b5e0fae7b6ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <input/Keyboard.h>

#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

//--------------------------------------------------------
// Constructor/Destructor 
//--------------------------------------------------------

Keyboard::Keyboard()
: RealController( KEYBOARD )
{
    ClearMappedButtons();
}

Keyboard::~Keyboard()
{
}

//==============================================================================
// Keyboard::IsValidInput
//==============================================================================
//
// Description: Returns true if this is a valid input for the controller.
//
// Parameters:	dxkey - the directinput index identifying the physical input.
//
// Return:      true if key it's a valid input
//
//==============================================================================

bool Keyboard::IsValidInput( int dxKey ) const
{
    return dxKey >= DIK_ESCAPE && dxKey <= DIK_MEDIASELECT;
}

//==============================================================================
// Keyboard::IsBannedInput
//==============================================================================
//
// Description: Returns true if the key is banned for mapping.
//
// Parameters:	dxkey - the directinput index identifying the physical input.
//
// Return:      true if key is banned for mapping
//
//==============================================================================

bool Keyboard::IsBannedInput( int dxKey ) const
{
    switch( dxKey )
    {
        case DIK_LMENU:
        case DIK_RMENU:
        case DIK_LWIN:
        case DIK_RWIN:
            return true;
        default:
            return false;
    }
}

//==============================================================================
// Keyboard::SetMap
//==============================================================================
//
// Description: Sets up a mapping from a dxkey/direction to a virtual button
//
// Parameters:	dxkey - the directinput index identifying the physical input.
//              direction - for keyboard this must be DIR_UP
//              virtualbutton - the virtual button that has been mapped
//
// Return:      true if mapped successfully
//
//==============================================================================

bool Keyboard::SetMap( int dxKey, eDirectionType dir, int virtualButton )
{
    rAssert( dxKey >= 0 && dxKey < NUM_KEYBOARD_BUTTONS );
    rAssert( virtualButton >= 0 && virtualButton < Input::MaxPhysicalButtons );
    rAssert( dir == DIR_UP );

    eMapType maptype = VirtualInputs::GetType( virtualButton );
    
    if( dxKey >= 0 && dxKey < NUM_KEYBOARD_BUTTONS )
    {
        m_ButtonMap[ maptype ][ dxKey ] = virtualButton;
        return true;
    }
    else
    {
        return false;
    }
}

//==============================================================================
// Keyboard::ClearMap
//==============================================================================
//
// Description: Clears the specified mapping so it no longer exists.
//
// Parameters:	dxkey - the directinput index identifying the physical input.
//              direction - the direction of the input
//
// Return:      n/a
//
//==============================================================================

void Keyboard::ClearMap( int dxKey, eDirectionType dir, int virtualButton )
{
    rAssert( dxKey >= 0 && dxKey < NUM_KEYBOARD_BUTTONS );
    rAssert( virtualButton >= 0 && virtualButton < Input::MaxPhysicalButtons );
    rAssert( dir == DIR_UP );

    eMapType maptype = VirtualInputs::GetType( virtualButton );

    if( dxKey >= 0 && dxKey < NUM_KEYBOARD_BUTTONS )
    {
        m_ButtonMap[ maptype ][ dxKey ] = Input::INVALID_CONTROLLERID;
    }
}

//==============================================================================
// Keyboard::GetMap
//==============================================================================
//
// Description: Retrieves the virtual button of the given type mapped to
//              a dxKey, direction
//
// Parameters:	dxkey - the directinput index identifying the physical input.
//              direction - for keyboard only DIR_UP is used.
//              maptype - the type of virtual button to look up
//
// Return:      n/a
//
//==============================================================================

int Keyboard::GetMap( int dxKey, eDirectionType dir, eMapType map ) const
{
    rAssert( dxKey >= 0 && dxKey < NUM_KEYBOARD_BUTTONS );
 
    if( dir == DIR_UP )
    {
        return m_ButtonMap[ map ][ dxKey ];
    }
    else
    {
        return Input::INVALID_CONTROLLERID;
    }
}

//==============================================================================
// Keyboard::ClearMappedButtons
//==============================================================================
//
// Description: Clears the cached button mappings
//
// Parameters:	n/a
//
// Return:      n/a
//
//==============================================================================

void Keyboard::ClearMappedButtons()
{
    memset( &m_ButtonMap, Input::INVALID_CONTROLLERID, sizeof( m_ButtonMap ) );
}

//==============================================================================
// Keyboard::MapInputToDICode
//==============================================================================
//
// Description: Creates an input point -> direct input virtual key code mapping
//              for the keyboard
//
// Parameters:	n/a
//
// Return:      n/a
//
//==============================================================================

void Keyboard::MapInputToDICode()
{
    if( m_InputToDICode != NULL )
    {
        delete [] m_InputToDICode;
        m_InputToDICode = NULL;
    }

    if( m_radController != NULL )
    {
        // Get the number of input points
        m_numInputPoints = m_radController->GetNumberOfInputPoints();

        // Set up a cleared index -> di map.
        m_InputToDICode = new int[ m_numInputPoints ];

        for( int i = 0; i < m_numInputPoints; i++ )
        {
            m_InputToDICode[ i ] = Input::INVALID_CONTROLLERID;
        }

        // Reverse the di -> index map into what we want.
        for( int di = DIK_ESCAPE; di <= NUM_KEYBOARD_BUTTONS; di++ )
        {
            int inputpoint = VirtualKeyToIndex[ di ];

            if( inputpoint >= 0 &&
                inputpoint < m_numInputPoints )
            {
                m_InputToDICode[ inputpoint ] = di;
            }
        }
    }
}