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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "inputsystem/InputEnums.h"
#include "vgui/KeyCode.h"
#include "vgui/keyrepeat.h"
#include "tier0/dbg.h"
// memdbgon must be the last include file in a .cpp file
#include "tier0/memdbgon.h"
//#define DEBUG_REPEATS
#ifdef DEBUG_REPEATS
#define DbgRepeat(...) ConMsg( __VA_ARGS__ )
#else
#define DbgRepeat(...)
#endif
using namespace vgui;
vgui::KeyCode g_iCodesForAliases[FM_NUM_KEYREPEAT_ALIASES] =
{
KEY_XBUTTON_UP,
KEY_XBUTTON_DOWN,
KEY_XBUTTON_LEFT,
KEY_XBUTTON_RIGHT
};
//-----------------------------------------------------------------------------
// Purpose: Map joystick codes to our internal ones
//-----------------------------------------------------------------------------
static int GetIndexForCode( vgui::KeyCode code )
{
KeyCode localCode = GetBaseButtonCode( code );
switch ( localCode )
{
case KEY_XBUTTON_DOWN:
case KEY_XSTICK1_DOWN:
case KEY_XSTICK2_DOWN:
return KR_ALIAS_DOWN; break;
case KEY_XBUTTON_UP:
case KEY_XSTICK1_UP:
case KEY_XSTICK2_UP:
return KR_ALIAS_UP; break;
case KEY_XBUTTON_LEFT:
case KEY_XSTICK1_LEFT:
case KEY_XSTICK2_LEFT:
return KR_ALIAS_LEFT; break;
case KEY_XBUTTON_RIGHT:
case KEY_XSTICK1_RIGHT:
case KEY_XSTICK2_RIGHT:
return KR_ALIAS_RIGHT; break;
default:
break;
}
return -1;
}
//-----------------------------------------------------------------------------
CKeyRepeatHandler::CKeyRepeatHandler()
{
Reset();
for ( int i = 0; i < FM_NUM_KEYREPEAT_ALIASES; i++ )
{
m_flRepeatTimes[i] = 0.16;
}
}
//-----------------------------------------------------------------------------
// Purpose: Clear all state
//-----------------------------------------------------------------------------
void CKeyRepeatHandler::Reset()
{
DbgRepeat( "KeyRepeat: Reset\n" );
memset( m_bAliasDown, 0, sizeof( m_bAliasDown ) );
m_bHaveKeyDown = false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CKeyRepeatHandler::KeyDown( vgui::KeyCode code )
{
int joyStick = GetJoystickForCode( code );
int iIndex = GetIndexForCode(code);
if ( iIndex == -1 )
return;
if ( m_bAliasDown[ joyStick ][ iIndex ] )
return;
DbgRepeat( "KeyRepeat: KeyDown %d(%d)\n", joyStick, iIndex );
Reset();
m_bAliasDown[ joyStick ][ iIndex ] = true;
m_flNextKeyRepeat[ joyStick ] = Plat_FloatTime() + 0.4;
m_bHaveKeyDown = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CKeyRepeatHandler::KeyUp( vgui::KeyCode code )
{
int joyStick = GetJoystickForCode( code );
int iIndex = GetIndexForCode(code);
if ( iIndex == -1 )
return;
DbgRepeat( "KeyRepeat: KeyUp %d(%d)\n", joyStick, iIndex );
m_bAliasDown[ joyStick ][ iIndex ] = false;
m_bHaveKeyDown = false;
for ( int i = 0; i < FM_NUM_KEYREPEAT_ALIASES; i++ )
{
for ( int j = 0; j < MAX_JOYSTICKS; j++ )
{
if ( m_bAliasDown[ j ][ i ] )
{
m_bHaveKeyDown = true;
break;
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
vgui::KeyCode CKeyRepeatHandler::KeyRepeated( void )
{
if ( IsPC() )
return BUTTON_CODE_NONE;
if ( !m_bHaveKeyDown )
return BUTTON_CODE_NONE;
float currentTime = Plat_FloatTime();
for ( int j = 0; j < MAX_JOYSTICKS; j++ )
{
if ( m_flNextKeyRepeat[ j ] < currentTime )
{
for ( int i = 0; i < FM_NUM_KEYREPEAT_ALIASES; i++ )
{
if ( m_bAliasDown[ j ][ i ] )
{
m_flNextKeyRepeat[ j ] = currentTime + m_flRepeatTimes[i];
DbgRepeat( "KeyRepeat: Repeat %d(%d)\n", j, i );
return ButtonCodeToJoystickButtonCode( g_iCodesForAliases[i], j );
}
}
}
}
return BUTTON_CODE_NONE;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CKeyRepeatHandler::SetKeyRepeatTime( vgui::KeyCode code, float flRepeat )
{
int iIndex = GetIndexForCode(code);
Assert( iIndex != -1 );
m_flRepeatTimes[ iIndex ] = flRepeat;
}
|