302 lines
7.3 KiB
C++
302 lines
7.3 KiB
C++
#include "input.h"
|
|
#include "console.h"
|
|
#include "interface.h"
|
|
#include "mainmenu.h"
|
|
#include "tier0/lib.h"
|
|
#include "tier1/commandline.h"
|
|
#include "math3d.h"
|
|
|
|
char g_PressedKeys[KEY_NUM_KEYS];
|
|
float g_fAxisValues[AXIS_NUM_AXIS];
|
|
float g_fAxisModifiers[AXIS_NUM_AXIS];
|
|
CUtlString g_bindings[256];
|
|
bool g_bController = false;
|
|
|
|
struct KeyName_t {
|
|
const char *szName;
|
|
EInputKey key;
|
|
};
|
|
|
|
KeyName_t keys[] = {
|
|
|
|
{"ESCAPE",KEY_ESCAPE},
|
|
{"ESC",KEY_ESCAPE},
|
|
{"TAB",KEY_TAB},
|
|
{"ENTER",KEY_ENTER},
|
|
{"CTRL",KEY_CONTROL},
|
|
{"CONTROL",KEY_CONTROL},
|
|
{"SHIFT",KEY_SHIFT},
|
|
{"ALT",KEY_ALT},
|
|
{"SPACE",KEY_SPACE},
|
|
|
|
{"BACKSPACE",KEY_BACKSPACE},
|
|
{"[",KEY_LBRACKET},
|
|
{"]",KEY_RBRACKET},
|
|
{"{",KEY_LBRACKET},
|
|
{"}",KEY_RBRACKET},
|
|
{"\\",KEY_BACKSLASH},
|
|
{";",KEY_SEMICOLON},
|
|
{":",KEY_SEMICOLON},
|
|
{"\'",KEY_APOSTROPHE},
|
|
{"\"",KEY_APOSTROPHE},
|
|
|
|
{"F1",KEY_F1},
|
|
{"F2",KEY_F2},
|
|
{"F3",KEY_F3},
|
|
{"F4",KEY_F4},
|
|
{"F5",KEY_F5},
|
|
{"F6",KEY_F6},
|
|
{"F7",KEY_F7},
|
|
{"F8",KEY_F8},
|
|
{"F9",KEY_F9},
|
|
{"F10",KEY_F10},
|
|
{"F11",KEY_F11},
|
|
{"F12",KEY_F12},
|
|
|
|
{"1",KEY_1},
|
|
{"2",KEY_2},
|
|
{"3",KEY_3},
|
|
{"4",KEY_4},
|
|
{"5",KEY_5},
|
|
{"6",KEY_6},
|
|
{"7",KEY_7},
|
|
{"8",KEY_8},
|
|
{"9",KEY_9},
|
|
{"0",KEY_0},
|
|
|
|
{"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;
|
|
};
|
|
|
|
|
|
interface CInput: public IInput
|
|
{
|
|
public:
|
|
virtual void Init() override;
|
|
virtual void Frame() override;
|
|
virtual void Deinit() override;
|
|
virtual void KeyEvent( EInputKey key, EKeyEventType event ) override;
|
|
virtual void AxisEvent( EInputAxis axis, float fValue ) override;
|
|
};
|
|
|
|
DECLARE_ENGINE_INTERFACE(Input, CInput)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void CInput::Init( void )
|
|
{
|
|
}
|
|
|
|
CUtlVector<EInputMode> g_inputModeStack = {INPUT_MODE_GAME, INPUT_MODE_MENU};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Key event may have different different effects based on current input mode.
|
|
//
|
|
// GAME:
|
|
// 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
|
|
//
|
|
// MENU:
|
|
// Sends all event to FGUI. KEY_ESCAPE key makes leave the menu and get back to
|
|
// the game.
|
|
//
|
|
// CONSOLE:
|
|
// Works as permanent input field.
|
|
//
|
|
// INPUT FIELD:
|
|
// Doesn't recieve any events except for KEY_ESCAPE and KEY_ENTER
|
|
//-----------------------------------------------------------------------------
|
|
void CInput::KeyEvent( EInputKey key, EKeyEventType event )
|
|
{
|
|
if (event == KEY_EVENT_TYPE_DOWN && key == KEY_ESCAPE)
|
|
{
|
|
if (g_inputModeStack.GetSize() == 1)
|
|
{
|
|
g_inputModeStack.AppendTail(INPUT_MODE_MENU);
|
|
IInput::SetInputMode(INPUT_MODE_MENU);
|
|
MainMenu()->SetVisibility(true);
|
|
return;
|
|
}
|
|
if (IConsoleUI::IsVisibile())
|
|
IConsoleUI::SetVisibility(false);
|
|
IInput::SetInputMode(g_inputModeStack[g_inputModeStack.GetSize()-2]);
|
|
g_inputModeStack.RemoveTail();
|
|
|
|
if (g_inputModeStack[g_inputModeStack.GetSize()-1] != INPUT_MODE_MENU)
|
|
{
|
|
MainMenu()->SetVisibility(false);
|
|
}
|
|
return;
|
|
};
|
|
|
|
if (event == KEY_EVENT_TYPE_DOWN && key == KEY_TILDE)
|
|
{
|
|
if (IConsoleUI::IsVisibile())
|
|
{
|
|
IInput::SetInputMode(g_inputModeStack[g_inputModeStack.GetSize()-2]);
|
|
g_inputModeStack.RemoveTail();
|
|
IConsoleUI::SetVisibility(false);
|
|
} else
|
|
{
|
|
g_inputModeStack.AppendTail(INPUT_MODE_CONSOLE);
|
|
IInput::SetInputMode(INPUT_MODE_CONSOLE);
|
|
IConsoleUI::SetVisibility(true);
|
|
}
|
|
|
|
return;
|
|
};
|
|
|
|
if (event == KEY_EVENT_TYPE_DOWN && key == KEY_F11)
|
|
{
|
|
Console()->AddCommand("exit;");
|
|
return;
|
|
}
|
|
|
|
|
|
if (g_inputModeStack[g_inputModeStack.GetSize()-1] == INPUT_MODE_GAME)
|
|
{
|
|
if (event == KEY_EVENT_TYPE_DOWN) {
|
|
Console()->AddCommand(g_bindings[key]);
|
|
Console()->AddCommand(";");
|
|
}
|
|
if (event == KEY_EVENT_TYPE_UP)
|
|
{
|
|
auto binding = Console()->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] = '-';
|
|
Console()->AddCommand(command);
|
|
Console()->AddCommand(";");
|
|
}
|
|
}
|
|
}
|
|
if (g_inputModeStack[g_inputModeStack.GetSize()-1] == INPUT_MODE_MENU)
|
|
{
|
|
|
|
};
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Axis events for the input devices such as mouse and controller.
|
|
// Game needs to explicitly support all of the devices.
|
|
//-----------------------------------------------------------------------------
|
|
void CInput::AxisEvent( EInputAxis axis, float fValue )
|
|
{
|
|
if (g_inputModeStack[g_inputModeStack.GetSize()-1] == INPUT_MODE_GAME)
|
|
{
|
|
if (axis == AXIS_MOUSE_X || axis == AXIS_MOUSE_Y)
|
|
{
|
|
g_bController = false;
|
|
g_fAxisValues[axis] += fValue*3.09;
|
|
}
|
|
if (axis == AXIS_CONTROLLER_PITCH || axis == AXIS_CONTROLLER_YAW)
|
|
{
|
|
g_bController = true;
|
|
g_fAxisModifiers[axis] = fValue*3.09;
|
|
}
|
|
}
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void CInput::Frame( void )
|
|
{
|
|
g_fAxisValues[AXIS_MOUSE_X] += g_fAxisModifiers[AXIS_CONTROLLER_PITCH];
|
|
g_fAxisValues[AXIS_MOUSE_Y] += g_fAxisModifiers[AXIS_CONTROLLER_YAW];
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
void CInput::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;
|
|
|
|
g_bindings[key] = 0;
|
|
for ( int i = 2; i<argc; i++ )
|
|
{
|
|
g_bindings[key].AppendTail(argv[i]);
|
|
g_bindings[key].AppendTail(" ");
|
|
}
|
|
};
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
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);
|