forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeybindings.cpp
143 lines (117 loc) · 3.39 KB
/
keybindings.cpp
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
//===== Copyright © 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#include "tier2/keybindings.h"
#include "tier2/tier2.h"
#include "inputsystem/iinputsystem.h"
#include "tier1/utlbuffer.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Set a key binding
//-----------------------------------------------------------------------------
void CKeyBindings::SetBinding( ButtonCode_t code, const char *pBinding )
{
if ( code == BUTTON_CODE_INVALID || code == KEY_NONE )
return;
// free old bindings
if ( !m_KeyInfo[code].IsEmpty() )
{
// Exactly the same, don't re-bind and fragment memory
if ( !Q_stricmp( m_KeyInfo[code], pBinding ) )
return;
}
// allocate memory for new binding
m_KeyInfo[code] = pBinding;
}
void CKeyBindings::SetBinding( const char *pButtonName, const char *pBinding )
{
ButtonCode_t code = g_pInputSystem->StringToButtonCode( pButtonName );
SetBinding( code, pBinding );
}
void CKeyBindings::Unbind( ButtonCode_t code )
{
if ( code != KEY_NONE && code != BUTTON_CODE_INVALID )
{
m_KeyInfo[code] = "";
}
}
void CKeyBindings::Unbind( const char *pButtonName )
{
ButtonCode_t code = g_pInputSystem->StringToButtonCode( pButtonName );
Unbind( code );
}
void CKeyBindings::UnbindAll()
{
for ( int i = 0; i < BUTTON_CODE_LAST; i++ )
{
m_KeyInfo[i] = "";
}
}
//-----------------------------------------------------------------------------
// Count number of lines of bindings we'll be writing
//-----------------------------------------------------------------------------
int CKeyBindings::GetBindingCount( ) const
{
int nCount = 0;
for ( int i = 0; i < BUTTON_CODE_LAST; i++ )
{
if ( m_KeyInfo[i].Length() )
{
nCount++;
}
}
return nCount;
}
//-----------------------------------------------------------------------------
// Writes lines containing "bind key value"
//-----------------------------------------------------------------------------
void CKeyBindings::WriteBindings( CUtlBuffer &buf )
{
for ( int i = 0; i < BUTTON_CODE_LAST; i++ )
{
if ( m_KeyInfo[i].Length() )
{
const char *pButtonCode = g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
buf.Printf( "bind \"%s\" \"%s\"\n", pButtonCode, m_KeyInfo[i].Get() );
}
}
}
//-----------------------------------------------------------------------------
// Returns the keyname to which a binding string is bound. E.g., if
// TAB is bound to +use then searching for +use will return "TAB"
//-----------------------------------------------------------------------------
const char *CKeyBindings::ButtonNameForBinding( const char *pBinding )
{
const char *pBind = pBinding;
if ( pBinding[0] == '+' )
{
++pBind;
}
for ( int i = 0; i < BUTTON_CODE_LAST; i++ )
{
if ( !m_KeyInfo[i].Length() )
continue;
if ( m_KeyInfo[i][0] == '+' )
{
if ( !Q_stricmp( &m_KeyInfo[i][1], pBind ) )
return g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
}
else
{
if ( !Q_stricmp( m_KeyInfo[i], pBind ) )
return g_pInputSystem->ButtonCodeToString( (ButtonCode_t)i );
}
}
return NULL;
}
const char *CKeyBindings::GetBindingForButton( ButtonCode_t code )
{
if ( m_KeyInfo[code].IsEmpty() )
return NULL;
return m_KeyInfo[ code ];
}