167 lines
4.0 KiB
C++
167 lines
4.0 KiB
C++
#include "input.h"
|
|
#include "console.h"
|
|
#include "tier0/lib.h"
|
|
#include "tier1/commandline.h"
|
|
#include "math3d.h"
|
|
|
|
char g_PressedKeys[KEY_NUM_KEYS];
|
|
float g_fAxisValues[AXIS_NUM_AXIS];
|
|
CUtlString g_bindings[256];
|
|
|
|
struct KeyName_t {
|
|
const char *szName;
|
|
EInputKey key;
|
|
};
|
|
|
|
KeyName_t keys[] = {
|
|
{"TAB",KEY_TAB},
|
|
{"ENTER",KEY_ENTER},
|
|
{"ESCAPE",KEY_ESCAPE},
|
|
{"SPACE",KEY_SPACE},
|
|
{"A",KEY_A},
|
|
{"B",KEY_B},
|
|
{"C",KEY_C},
|
|
{"D",KEY_D},
|
|
{"E",KEY_E},
|
|
{"F",KEY_F},
|
|
{"G",KEY_G},
|
|
{"H",KEY_H},
|
|
{"I",KEY_I},
|
|
{"J",KEY_J},
|
|
{"K",KEY_K},
|
|
{"L",KEY_L},
|
|
{"M",KEY_M},
|
|
{"N",KEY_N},
|
|
{"O",KEY_O},
|
|
{"P",KEY_P},
|
|
{"Q",KEY_Q},
|
|
{"R",KEY_R},
|
|
{"S",KEY_S},
|
|
{"T",KEY_T},
|
|
{"U",KEY_U},
|
|
{"V",KEY_V},
|
|
{"W",KEY_W},
|
|
{"X",KEY_X},
|
|
{"Y",KEY_Y},
|
|
{"Z",KEY_Z},
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Converts string (eg. tab, mouse0, w, 0) to keycode
|
|
//-----------------------------------------------------------------------------
|
|
EInputKey IInput_StringToKey( char *psz )
|
|
{
|
|
for (uint32_t i = 0; i<sizeof(keys)/sizeof(KeyName_t); i++)
|
|
{
|
|
if (!V_stricmp(keys[i].szName, psz))
|
|
return keys[i].key;
|
|
};
|
|
return KEY_NONE;
|
|
};
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void IInput::Init( void )
|
|
{
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Generates command call when pressed the key. eg +forward; -left;
|
|
// Just to be sure we put ; in the end of the command.
|
|
// When user releases the key command event is not generated. But if the first
|
|
// command contains + as first character ( in case of example +forward ), then
|
|
// it fires -forward
|
|
//-----------------------------------------------------------------------------
|
|
void IInput::KeyEvent( EInputKey key, EKeyEventType event )
|
|
{
|
|
if (event == KEY_EVENT_TYPE_DOWN) {
|
|
IConsole::AddCommand(g_bindings[key]);
|
|
IConsole::AddCommand(";");
|
|
}
|
|
if (event == KEY_EVENT_TYPE_UP)
|
|
{
|
|
auto binding = IConsole::ParseCommandLine(g_bindings[key]);
|
|
if (binding.GetSize()==0)
|
|
return;
|
|
if (binding[0].GetSize() == 0)
|
|
return;
|
|
if (binding[0][0].GetString()[0] == '+')
|
|
{
|
|
CUtlString command = binding[0][0];
|
|
command.GetString()[0] = '-';
|
|
IConsole::AddCommand(command);
|
|
IConsole::AddCommand(";");
|
|
}
|
|
}
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void IInput::AxisEvent( EInputAxis axis, float fValue )
|
|
{
|
|
if (axis == AXIS_MOUSE_X || axis == AXIS_MOUSE_Y)
|
|
{
|
|
g_fAxisValues[axis] += fValue*3.09;
|
|
}
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void IInput::Frame( void )
|
|
{
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void IInput::Deinit( void )
|
|
{
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void IInput_Bind( int argc, char **argv )
|
|
{
|
|
if (argc == 1)
|
|
return;
|
|
if (argc == 2)
|
|
return;
|
|
EInputKey key = IInput_StringToKey(argv[1]);
|
|
if (key == KEY_NONE)
|
|
return;
|
|
|
|
for ( int i = 2; i<argc; i++ )
|
|
{
|
|
g_bindings[key] = 0;
|
|
g_bindings[key].AppendTail(argv[i]);
|
|
}
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void IInput_Unbind( int argc, char **argv )
|
|
{
|
|
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void IInput_UnbindAll( int argc, char **argv )
|
|
{
|
|
|
|
};
|
|
|
|
ConCommand BindCmd("bind", IInput_Bind, 0);
|
|
ConCommand UnbindCmd("unbind", IInput_Unbind, 0);
|
|
ConCommand UnbindAllcmd("unbindall", IInput_UnbindAll, 0);
|