From 9fbad1a5d4732356df68f03558cd2fdabf9913fb Mon Sep 17 00:00:00 2001 From: "les.sosna" Date: Tue, 12 Jan 2010 22:19:48 +0000 Subject: [PATCH] fixed bad key codes handling --- game/History.txt | 1 + game/src/tank/DefaultCamera.cpp | 156 ++++++++++++++-------------- game/src/tank/InputManager.cpp | 6 +- game/src/tank/Main.cpp | 4 +- game/src/tank/gc/Player.cpp | 34 +++--- game/src/tank/globals.h | 10 +- game/src/tank/ui/gui_scoretable.cpp | 2 +- game/src/tank/ui/gui_settings.cpp | 4 +- 8 files changed, 113 insertions(+), 104 deletions(-) diff --git a/game/History.txt b/game/History.txt index f52dcad9..201ad212 100644 --- a/game/History.txt +++ b/game/History.txt @@ -5,6 +5,7 @@ - Восстановлена поддержка путей файлов с повторяющимися разделителями. - Исправлена работа функции msgbox. - Исправлена прокрутка выпадающих списков. +- Исправлена обработка некорректных настроек управления. * 1.51.2 beta - Исправлено повреждение танка игрока пушкой гаусса. diff --git a/game/src/tank/DefaultCamera.cpp b/game/src/tank/DefaultCamera.cpp index 55117f73..c356743a 100644 --- a/game/src/tank/DefaultCamera.cpp +++ b/game/src/tank/DefaultCamera.cpp @@ -11,83 +11,83 @@ DefaultCamera::DefaultCamera() { _dwTimeX = _dwTimeY = GetTickCount(); } - -void DefaultCamera::HandleMovement(float worldWidth, float worldHeight, - float screenWidth, float screenHeight) -{ - static char lastIn = 0, LastOut = 0; - static float levels[] = { 0.0625f, 0.125f, 0.25f, 0.5f, 1.0f, 1.5f, 2.0f }; - static int level = 4; - - if( !lastIn && g_env.envInputs.keys[DIK_PGUP] ) - level = __min(level+1, sizeof(levels) / sizeof(float) - 1); - lastIn = g_env.envInputs.keys[DIK_PGUP]; - - if( !LastOut && g_env.envInputs.keys[DIK_PGDN] ) - level = __max(level-1, 0); - LastOut = g_env.envInputs.keys[DIK_PGDN]; - - _zoom = levels[level]; - - bool bMove = false; - DWORD dwCurTime = GetTickCount(); - DWORD dt = DWORD(_dt); - - if( 0 == g_env.envInputs.mouse_x || g_env.envInputs.keys[DIK_LEFTARROW] ) - { - bMove = true; - while( dwCurTime - _dwTimeX > dt ) - { - _pos.x -= CELL_SIZE; - _dwTimeX += dt; - } - } - else - if( screenWidth - 1 == g_env.envInputs.mouse_x || g_env.envInputs.keys[DIK_RIGHTARROW] ) - { - bMove = true; - while( dwCurTime - _dwTimeX > dt ) - { - _pos.x += CELL_SIZE; - _dwTimeX += dt; - } - } - else - _dwTimeX = GetTickCount(); - //--------------------------------------- - if( 0 == g_env.envInputs.mouse_y || g_env.envInputs.keys[DIK_UPARROW] ) - { - bMove = true; - while( dwCurTime - _dwTimeY > dt ) - { - _pos.y -= CELL_SIZE; - _dwTimeY += dt; - } - } - else - if( screenHeight - 1 == g_env.envInputs.mouse_y || g_env.envInputs.keys[DIK_DOWNARROW] ) - { - bMove = true; - while( dwCurTime - _dwTimeY > dt ) - { - _pos.y += CELL_SIZE; - _dwTimeY += dt; - } - } - else - _dwTimeY = GetTickCount(); - //--------------------------------------- - if( bMove ) - _dt = __max(10.0f, 1.0f / (1.0f / _dt + 0.001f)); - else - _dt = 50.0f; - //------------------------------------------------------ - int dx = __max(0, (int) (screenWidth / _zoom - worldWidth) / 2); - int dy = __max(0, (int) (screenHeight / _zoom - worldHeight) / 2); - _pos.x = (float) __max(int(_pos.x), dx); - _pos.x = (float) __min(int(_pos.x), int(worldWidth - screenWidth / _zoom) + dx); - _pos.y = (float) __max(int(_pos.y), dy); - _pos.y = (float) __min(int(_pos.y), int(worldHeight - screenHeight / _zoom) + dy); -} + +void DefaultCamera::HandleMovement(float worldWidth, float worldHeight, + float screenWidth, float screenHeight) +{ + static char lastIn = 0, LastOut = 0; + static float levels[] = { 0.0625f, 0.125f, 0.25f, 0.5f, 1.0f, 1.5f, 2.0f }; + static int level = 4; + + if( !lastIn && g_env.envInputs.IsKeyPressed(DIK_PGUP) ) + level = __min(level+1, sizeof(levels) / sizeof(float) - 1); + lastIn = g_env.envInputs.IsKeyPressed(DIK_PGUP); + + if( !LastOut && g_env.envInputs.IsKeyPressed(DIK_PGDN) ) + level = __max(level-1, 0); + LastOut = g_env.envInputs.IsKeyPressed(DIK_PGDN); + + _zoom = levels[level]; + + bool bMove = false; + DWORD dwCurTime = GetTickCount(); + DWORD dt = DWORD(_dt); + + if( 0 == g_env.envInputs.mouse_x || g_env.envInputs.IsKeyPressed(DIK_LEFTARROW) ) + { + bMove = true; + while( dwCurTime - _dwTimeX > dt ) + { + _pos.x -= CELL_SIZE; + _dwTimeX += dt; + } + } + else + if( screenWidth - 1 == g_env.envInputs.mouse_x || g_env.envInputs.IsKeyPressed(DIK_RIGHTARROW) ) + { + bMove = true; + while( dwCurTime - _dwTimeX > dt ) + { + _pos.x += CELL_SIZE; + _dwTimeX += dt; + } + } + else + _dwTimeX = GetTickCount(); + //--------------------------------------- + if( 0 == g_env.envInputs.mouse_y || g_env.envInputs.IsKeyPressed(DIK_UPARROW) ) + { + bMove = true; + while( dwCurTime - _dwTimeY > dt ) + { + _pos.y -= CELL_SIZE; + _dwTimeY += dt; + } + } + else + if( screenHeight - 1 == g_env.envInputs.mouse_y || g_env.envInputs.IsKeyPressed(DIK_DOWNARROW) ) + { + bMove = true; + while( dwCurTime - _dwTimeY > dt ) + { + _pos.y += CELL_SIZE; + _dwTimeY += dt; + } + } + else + _dwTimeY = GetTickCount(); + //--------------------------------------- + if( bMove ) + _dt = __max(10.0f, 1.0f / (1.0f / _dt + 0.001f)); + else + _dt = 50.0f; + //------------------------------------------------------ + int dx = __max(0, (int) (screenWidth / _zoom - worldWidth) / 2); + int dy = __max(0, (int) (screenHeight / _zoom - worldHeight) / 2); + _pos.x = (float) __max(int(_pos.x), dx); + _pos.x = (float) __min(int(_pos.x), int(worldWidth - screenWidth / _zoom) + dx); + _pos.y = (float) __max(int(_pos.y), dy); + _pos.y = (float) __min(int(_pos.y), int(worldHeight - screenHeight / _zoom) + dy); +} // end of file diff --git a/game/src/tank/InputManager.cpp b/game/src/tank/InputManager.cpp index 7dbfde64..340a71c7 100644 --- a/game/src/tank/InputManager.cpp +++ b/game/src/tank/InputManager.cpp @@ -9,7 +9,7 @@ InputManager::InputManager(HWND hWnd) { TRACE("init direct input"); - ZeroMemory(g_env.envInputs.keys, sizeof(g_env.envInputs.keys)); + ZeroMemory(g_env.envInputs._keys, sizeof(g_env.envInputs._keys)); DWORD dwPriority = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND; @@ -62,10 +62,10 @@ HRESULT InputManager::InquireInputDevices() return S_OK; } - ZeroMemory(g_env.envInputs.keys, sizeof(g_env.envInputs.keys)); + ZeroMemory(g_env.envInputs._keys, sizeof(g_env.envInputs._keys)); for( int i = 0; i < sizeof(data); ++i ) { - g_env.envInputs.keys[i] = (data[i] & 0x80) != 0; + g_env.envInputs._keys[i] = (data[i] & 0x80) != 0; } diff --git a/game/src/tank/Main.cpp b/game/src/tank/Main.cpp index 2a2cf7fe..d636d00b 100644 --- a/game/src/tank/Main.cpp +++ b/game/src/tank/Main.cpp @@ -126,9 +126,9 @@ static void RenderFrame(bool thumbnail) // check if print screen key is pressed static char _oldRQ = 0; - if( g_env.envInputs.keys[DIK_SYSRQ] && !_oldRQ ) + if( g_env.envInputs.IsKeyPressed(DIK_SYSRQ) && !_oldRQ ) OnPrintScreen(); - _oldRQ = g_env.envInputs.keys[DIK_SYSRQ]; + _oldRQ = g_env.envInputs.IsKeyPressed(DIK_SYSRQ); } /////////////////////////////////////////////////////////////////////////////// diff --git a/game/src/tank/gc/Player.cpp b/game/src/tank/gc/Player.cpp index 62a522c9..591cd08e 100644 --- a/game/src/tank/gc/Player.cpp +++ b/game/src/tank/gc/Player.cpp @@ -605,7 +605,7 @@ void GC_PlayerLocal::ReadControllerStateAndStepPredicted(VehicleState &vs, float // // lights // - bool tmp = g_env.envInputs.keys[_keyLight]; + bool tmp = g_env.envInputs.IsKeyPressed(_keyLight); if( tmp && !_lastLightKeyState && g_conf.sv_nightmode.Get() ) { PLAY(SND_LightSwitch, GetVehicle()->GetPos()); @@ -618,14 +618,14 @@ void GC_PlayerLocal::ReadControllerStateAndStepPredicted(VehicleState &vs, float // // pickup // - vs._bState_AllowDrop = g_env.envInputs.keys[_keyPickup] - || ( g_env.envInputs.keys[_keyForward] && g_env.envInputs.keys[_keyBack] ) - || ( g_env.envInputs.keys[_keyLeft] && g_env.envInputs.keys[_keyRight] ); + vs._bState_AllowDrop = g_env.envInputs.IsKeyPressed(_keyPickup) + || ( g_env.envInputs.IsKeyPressed(_keyForward) && g_env.envInputs.IsKeyPressed(_keyBack) ) + || ( g_env.envInputs.IsKeyPressed(_keyLeft) && g_env.envInputs.IsKeyPressed(_keyRight) ); // // fire // - vs._bState_Fire = g_env.envInputs.keys[_keyFire]; + vs._bState_Fire = g_env.envInputs.IsKeyPressed(_keyFire); // @@ -634,10 +634,10 @@ void GC_PlayerLocal::ReadControllerStateAndStepPredicted(VehicleState &vs, float if( _arcadeStyle ) { vec2d tmp(0, 0); - if( g_env.envInputs.keys[_keyForward] ) tmp.y -= 1; - if( g_env.envInputs.keys[_keyBack] ) tmp.y += 1; - if( g_env.envInputs.keys[_keyLeft] ) tmp.x -= 1; - if( g_env.envInputs.keys[_keyRight] ) tmp.x += 1; + if( g_env.envInputs.IsKeyPressed(_keyForward) ) tmp.y -= 1; + if( g_env.envInputs.IsKeyPressed(_keyBack) ) tmp.y += 1; + if( g_env.envInputs.IsKeyPressed(_keyLeft) ) tmp.x -= 1; + if( g_env.envInputs.IsKeyPressed(_keyRight) ) tmp.x += 1; tmp.Normalize(); bool move = tmp.x || tmp.y; @@ -654,10 +654,10 @@ void GC_PlayerLocal::ReadControllerStateAndStepPredicted(VehicleState &vs, float } else { - vs._bState_MoveForward = g_env.envInputs.keys[_keyForward]; - vs._bState_MoveBack = g_env.envInputs.keys[_keyBack ]; - vs._bState_RotateLeft = g_env.envInputs.keys[_keyLeft ]; - vs._bState_RotateRight = g_env.envInputs.keys[_keyRight ]; + vs._bState_MoveForward = g_env.envInputs.IsKeyPressed(_keyForward); + vs._bState_MoveBack = g_env.envInputs.IsKeyPressed(_keyBack ); + vs._bState_RotateLeft = g_env.envInputs.IsKeyPressed(_keyLeft ); + vs._bState_RotateRight = g_env.envInputs.IsKeyPressed(_keyRight ); } if( _moveToMouse ) @@ -709,10 +709,10 @@ void GC_PlayerLocal::ReadControllerStateAndStepPredicted(VehicleState &vs, float } else { - vs._bState_TowerLeft = g_env.envInputs.keys[_keyTowerLeft]; - vs._bState_TowerRight = g_env.envInputs.keys[_keyTowerRight]; - vs._bState_TowerCenter = g_env.envInputs.keys[_keyTowerCenter] - || g_env.envInputs.keys[_keyTowerLeft] && g_env.envInputs.keys[_keyTowerRight]; + vs._bState_TowerLeft = g_env.envInputs.IsKeyPressed(_keyTowerLeft); + vs._bState_TowerRight = g_env.envInputs.IsKeyPressed(_keyTowerRight); + vs._bState_TowerCenter = g_env.envInputs.IsKeyPressed(_keyTowerCenter) + || g_env.envInputs.IsKeyPressed(_keyTowerLeft) && g_env.envInputs.IsKeyPressed(_keyTowerRight); if( vs._bState_TowerCenter ) { vs._bState_TowerLeft = false; diff --git a/game/src/tank/globals.h b/game/src/tank/globals.h index 5840a14f..5c7cf389 100644 --- a/game/src/tank/globals.h +++ b/game/src/tank/globals.h @@ -51,13 +51,21 @@ extern SafePtr g_fs; struct InputState { - bool keys[300]; + bool _keys[300]; int mouse_x; int mouse_y; int mouse_wheel; bool bLButtonState; bool bRButtonState; bool bMButtonState; + bool IsKeyPressed(int code) + { + if( code > 0 && code < sizeof(_keys) / sizeof(_keys[0]) ) + { + return _keys[code]; + } + return false; + } }; diff --git a/game/src/tank/ui/gui_scoretable.cpp b/game/src/tank/ui/gui_scoretable.cpp index c209cf63..a1c94a1f 100644 --- a/game/src/tank/ui/gui_scoretable.cpp +++ b/game/src/tank/ui/gui_scoretable.cpp @@ -106,7 +106,7 @@ void ScoreTable::DrawChildren(const DrawingContext *dc, float sx, float sy) cons void ScoreTable::OnTimeStep(float dt) { - bool tab = g_env.envInputs.keys[DIK_TAB]; + bool tab = g_env.envInputs.IsKeyPressed(DIK_TAB); SetVisible(!g_level->IsEmpty() && !g_level->GetEditorMode() && (tab || g_level->_limitHit)); } diff --git a/game/src/tank/ui/gui_settings.cpp b/game/src/tank/ui/gui_settings.cpp index f93e16ef..e12717fa 100644 --- a/game/src/tank/ui/gui_settings.cpp +++ b/game/src/tank/ui/gui_settings.cpp @@ -345,9 +345,9 @@ void ControlProfileDlg::OnTimeStep(float dt) _time += dt; _actions->GetData()->SetItemText(_activeIndex, 1, fmodf(_time, 0.6f) > 0.3f ? "" : "..."); - for( int k = 0; k < sizeof(g_env.envInputs.keys) / sizeof(g_env.envInputs.keys[0]); ++k ) + for( int k = 0; k < sizeof(g_env.envInputs._keys) / sizeof(g_env.envInputs._keys[0]); ++k ) { - if( g_env.envInputs.keys[k] ) + if( g_env.envInputs.IsKeyPressed(k) ) { if( _skipNextKey ) {