Add IInput::ShiftIsPressed and IInput::AltIsPressed

To avoid duplicate code when checking for both right and left shift or alt keys.
This commit is contained in:
Robert Müller 2022-11-14 22:50:59 +01:00
parent 26c95de743
commit 04be9d6e72
11 changed files with 45 additions and 42 deletions

View file

@ -3408,7 +3408,7 @@ void CClient::Run()
bool CClient::CtrlShiftKey(int Key, bool &Last)
{
if(Input()->ModifierIsPressed() && (Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT)) && !Last && Input()->KeyIsPressed(Key))
if(Input()->ModifierIsPressed() && Input()->ShiftIsPressed() && !Last && Input()->KeyIsPressed(Key))
{
Last = true;
return true;

View file

@ -101,6 +101,8 @@ public:
void Shutdown() override;
bool ModifierIsPressed() const override { return KeyState(KEY_LCTRL) || KeyState(KEY_RCTRL) || KeyState(KEY_LGUI) || KeyState(KEY_RGUI); }
bool ShiftIsPressed() const override { return KeyState(KEY_LSHIFT) || KeyState(KEY_RSHIFT); }
bool AltIsPressed() const override { return KeyState(KEY_LALT) || KeyState(KEY_RALT); }
bool KeyIsPressed(int Key) const override { return KeyState(Key); }
bool KeyPress(int Key, bool CheckCounter) const override { return CheckCounter ? (m_aInputCount[Key] == m_InputCounter) : m_aInputCount[Key]; }

View file

@ -68,6 +68,8 @@ public:
// keys
virtual bool ModifierIsPressed() const = 0;
virtual bool ShiftIsPressed() const = 0;
virtual bool AltIsPressed() const = 0;
virtual bool KeyIsPressed(int Key) const = 0;
virtual bool KeyPress(int Key, bool CheckCounter = false) const = 0;
const char *KeyName(int Key) const { return (Key >= 0 && Key < g_MaxKeys) ? g_aaKeyStrings[Key] : g_aaKeyStrings[0]; }

View file

@ -363,7 +363,7 @@ void CGameConsole::CInstance::OnInput(IInput::CEvent Event)
}
else if(Event.m_Key == KEY_TAB)
{
const int Direction = m_pGameConsole->m_pClient->Input()->KeyIsPressed(KEY_LSHIFT) || m_pGameConsole->m_pClient->Input()->KeyIsPressed(KEY_RSHIFT) ? -1 : 1;
const int Direction = m_pGameConsole->m_pClient->Input()->ShiftIsPressed() ? -1 : 1;
// command completion
if(m_Type == CGameConsole::CONSOLETYPE_LOCAL || m_pGameConsole->Client()->RconAuthed())

View file

@ -1654,7 +1654,7 @@ void CMapLayers::OnRender()
Render = true;
}
if(Render && pLayer->m_Type == LAYERTYPE_TILES && Input()->ModifierIsPressed() && (Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT)) && Input()->KeyPress(KEY_KP_0))
if(Render && pLayer->m_Type == LAYERTYPE_TILES && Input()->ModifierIsPressed() && Input()->ShiftIsPressed() && Input()->KeyPress(KEY_KP_0))
{
CMapItemLayerTilemap *pTMap = (CMapItemLayerTilemap *)pLayer;
CTile *pTiles = (CTile *)m_pLayers->Map()->GetData(pTMap->m_Data);

View file

@ -529,7 +529,7 @@ int CMenus::DoValueSelector(void *pID, CUIRect *pRect, const char *pLabel, bool
{
float delta = UI()->MouseDeltaX();
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
s_Value += delta * 0.05f;
else
s_Value += delta;
@ -2652,7 +2652,7 @@ void CMenus::RenderBackground()
bool CMenus::CheckHotKey(int Key) const
{
return m_Popup == POPUP_NONE &&
!Input()->KeyIsPressed(KEY_LSHIFT) && !Input()->KeyIsPressed(KEY_RSHIFT) && !Input()->ModifierIsPressed() && // no modifier
!Input()->ShiftIsPressed() && !Input()->ModifierIsPressed() && // no modifier
Input()->KeyIsPressed(Key) && m_pClient->m_GameConsole.IsClosed();
}

View file

@ -190,7 +190,7 @@ void CMenus::RenderServerbrowserServerList(CUIRect View)
if(UI()->ConsumeHotkey(CUI::HOTKEY_TAB))
{
const int Direction = Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT) ? -1 : 1;
const int Direction = Input()->ShiftIsPressed() ? -1 : 1;
g_Config.m_UiToolboxPage = (g_Config.m_UiToolboxPage + 3 + Direction) % 3;
}
@ -589,7 +589,7 @@ void CMenus::RenderServerbrowserServerList(CUIRect View)
static int s_ClearButton = 0;
static float s_Offset = 0.0f;
if(Input()->KeyPress(KEY_X) && (Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT)) && Input()->ModifierIsPressed())
if(Input()->KeyPress(KEY_X) && Input()->ShiftIsPressed() && Input()->ModifierIsPressed())
UI()->SetActiveItem(&g_Config.m_BrExcludeString);
if(UI()->DoClearableEditBox(&g_Config.m_BrExcludeString, &s_ClearButton, &QuickExclude, g_Config.m_BrExcludeString, sizeof(g_Config.m_BrExcludeString), 12.0f, &s_Offset, false, IGraphics::CORNER_ALL))
Client()->ServerBrowserUpdate();

View file

@ -221,7 +221,7 @@ void CMenus::RenderDemoPlayer(CUIRect MainView)
if(m_pClient->m_GameConsole.IsClosed() && m_DemoPlayerState == DEMOPLAYER_NONE && g_Config.m_ClDemoKeyboardShortcuts)
{
// increase/decrease speed
if(!Input()->KeyIsPressed(KEY_LSHIFT) && !Input()->KeyIsPressed(KEY_RSHIFT))
if(!Input()->ShiftIsPressed())
{
if(Input()->KeyPress(KEY_MOUSE_WHEEL_UP) || Input()->KeyPress(KEY_UP))
{
@ -410,7 +410,7 @@ void CMenus::RenderDemoPlayer(CUIRect MainView)
static float s_PrevAmount = 0.0f;
float AmountSeek = (UI()->MouseX() - SeekBar.x) / SeekBar.w;
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
{
AmountSeek = s_PrevAmount + (AmountSeek - s_PrevAmount) * 0.05f;
if(AmountSeek > 0.0f && AmountSeek < 1.0f && absolute(s_PrevAmount - AmountSeek) >= 0.0001f)

View file

@ -263,7 +263,7 @@ bool CUI::OnInput(const IInput::CEvent &Event)
m_HotkeysPressed |= HOTKEY_ENTER;
else if(Event.m_Key == KEY_ESCAPE)
m_HotkeysPressed |= HOTKEY_ESCAPE;
else if(Event.m_Key == KEY_TAB && !Input()->KeyIsPressed(KEY_LALT) && !Input()->KeyIsPressed(KEY_RALT))
else if(Event.m_Key == KEY_TAB && !Input()->AltIsPressed())
m_HotkeysPressed |= HOTKEY_TAB;
else if(Event.m_Key == KEY_DELETE)
m_HotkeysPressed |= HOTKEY_DELETE;
@ -672,8 +672,8 @@ bool CUI::DoEditBox(const void *pID, const CUIRect *pRect, char *pStr, unsigned
m_CurCursor = minimum(str_length(pStr), m_CurCursor);
bool IsShiftPressed = Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT);
bool IsModPressed = Input()->ModifierIsPressed();
const bool IsShiftPressed = Input()->ShiftIsPressed();
const bool IsModPressed = Input()->ModifierIsPressed();
if(Enabled() && !IsShiftPressed && IsModPressed && Input()->KeyPress(KEY_V))
{
@ -1101,7 +1101,7 @@ float CUI::DoScrollbarV(const void *pID, const CUIRect *pRect, float Current)
if(MouseButton(0))
{
Grabbed = true;
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
m_MouseSlow = true;
}
else
@ -1181,7 +1181,7 @@ float CUI::DoScrollbarH(const void *pID, const CUIRect *pRect, float Current, co
if(MouseButton(0))
{
Grabbed = true;
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
m_MouseSlow = true;
}
else

View file

@ -71,7 +71,7 @@ void CScrollRegion::End()
if(UI()->Enabled() && UI()->MouseHovered(&RegionRect))
{
const bool IsPageScroll = Input()->KeyIsPressed(KEY_LALT) || Input()->KeyIsPressed(KEY_RALT);
const bool IsPageScroll = Input()->AltIsPressed();
const float ScrollUnit = IsPageScroll ? m_ClipRect.h : m_Params.m_ScrollUnit;
if(UI()->ConsumeHotkey(CUI::HOTKEY_SCROLL_UP))
{

View file

@ -613,7 +613,7 @@ int CEditor::UiDoValueSelector(void *pID, CUIRect *pRect, const char *pLabel, in
{
if(UI()->MouseButton(0))
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
s_Value += m_MouseDeltaX * 0.05f;
else
s_Value += m_MouseDeltaX;
@ -852,8 +852,8 @@ static int EntitiesListdirCallback(const char *pName, int IsDir, int StorageType
void CEditor::DoToolbar(CUIRect ToolBar)
{
bool ModPressed = Input()->ModifierIsPressed();
bool ShiftPressed = Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT);
const bool ModPressed = Input()->ModifierIsPressed();
const bool ShiftPressed = Input()->ShiftIsPressed();
CUIRect TB_Top, TB_Bottom;
CUIRect Button;
@ -1312,8 +1312,7 @@ void CEditor::DoSoundSource(CSoundSource *pSource, int Index)
if(dx * dx + dy * dy < 50)
UI()->SetHotItem(pID);
bool IgnoreGrid;
IgnoreGrid = Input()->KeyIsPressed(KEY_LALT) || Input()->KeyIsPressed(KEY_RALT);
const bool IgnoreGrid = Input()->AltIsPressed();
if(UI()->CheckActiveItem(pID))
{
@ -1433,7 +1432,7 @@ void CEditor::DoQuad(CQuad *pQuad, int Index)
if(dx * dx + dy * dy < 50)
UI()->SetHotItem(pID);
const bool IgnoreGrid = Input()->KeyIsPressed(KEY_LALT) || Input()->KeyIsPressed(KEY_RALT);
const bool IgnoreGrid = Input()->AltIsPressed();
// draw selection background
if(IsQuadSelected(Index))
@ -1590,7 +1589,7 @@ void CEditor::DoQuad(CQuad *pQuad, int Index)
if(UI()->MouseButton(0))
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
{
s_Operation = OP_MOVE_PIVOT;
@ -1632,7 +1631,7 @@ void CEditor::DoQuad(CQuad *pQuad, int Index)
if(UI()->MouseButton(1))
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
{
s_Operation = OP_DELETE;
@ -1693,7 +1692,7 @@ void CEditor::DoQuadPoint(CQuad *pQuad, int QuadIndex, int V)
static bool s_Moved;
static int s_Operation = OP_NONE;
const bool IgnoreGrid = Input()->KeyIsPressed(KEY_LALT) || Input()->KeyIsPressed(KEY_RALT);
const bool IgnoreGrid = Input()->AltIsPressed();
if(UI()->CheckActiveItem(pID))
{
@ -1797,7 +1796,7 @@ void CEditor::DoQuadPoint(CQuad *pQuad, int QuadIndex, int V)
{
if(!s_Moved)
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
m_SelectedPoints ^= 1 << V;
else
m_SelectedPoints = 1 << V;
@ -1821,7 +1820,7 @@ void CEditor::DoQuadPoint(CQuad *pQuad, int QuadIndex, int V)
{
UI()->SetActiveItem(pID);
s_Moved = false;
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
{
s_Operation = OP_MOVEUV;
m_LockMouse = true;
@ -1831,7 +1830,7 @@ void CEditor::DoQuadPoint(CQuad *pQuad, int QuadIndex, int V)
if(!(m_SelectedPoints & (1 << V)))
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
m_SelectedPoints |= 1 << V;
else
m_SelectedPoints = 1 << V;
@ -1851,7 +1850,7 @@ void CEditor::DoQuadPoint(CQuad *pQuad, int QuadIndex, int V)
UI()->SetActiveItem(pID);
if(!(m_SelectedPoints & (1 << V)))
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
m_SelectedPoints |= 1 << V;
else
m_SelectedPoints = 1 << V;
@ -1897,7 +1896,7 @@ void CEditor::DoQuadKnife(int QuadIndex)
CLayerQuads *pLayer = (CLayerQuads *)GetSelectedLayerType(0, LAYERTYPE_QUADS);
CQuad *pQuad = &pLayer->m_vQuads[QuadIndex];
bool IgnoreGrid = Input()->KeyIsPressed(KEY_LALT) || Input()->KeyIsPressed(KEY_RALT);
const bool IgnoreGrid = Input()->AltIsPressed();
float SnapRadius = 4.f * m_MouseWScale;
vec2 Mouse = vec2(UI()->MouseWorldX(), UI()->MouseWorldY());
@ -2261,7 +2260,7 @@ void CEditor::DoQuadEnvPoint(const CQuad *pQuad, int QIndex, int PIndex)
s_CurQIndex = QIndex;
}
const bool IgnoreGrid = Input()->KeyIsPressed(KEY_LALT) || Input()->KeyIsPressed(KEY_RALT);
const bool IgnoreGrid = Input()->AltIsPressed();
if(UI()->CheckActiveItem(pID) && s_CurQIndex == QIndex)
{
@ -2745,7 +2744,7 @@ void CEditor::DoMapEditor(CUIRect View)
}
CLayerTiles *pLayer = (CLayerTiles *)GetSelectedLayerType(0, LAYERTYPE_TILES);
if((Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT)) && pLayer)
if(Input()->ShiftIsPressed() && pLayer)
s_Operation = OP_BRUSH_PAINT;
}
@ -2863,7 +2862,7 @@ void CEditor::DoMapEditor(CUIRect View)
UI()->SetActiveItem(nullptr);
}
}
if(!Input()->KeyIsPressed(KEY_LSHIFT) && !Input()->KeyIsPressed(KEY_RSHIFT) &&
if(!Input()->ShiftIsPressed() &&
!Input()->ModifierIsPressed() &&
m_Dialog == DIALOG_NONE && m_EditBoxActive == 0)
{
@ -3105,7 +3104,7 @@ int CEditor::DoProperties(CUIRect *pToolBox, CProperty *pProps, int *pIDs, int *
CUIRect Inc, Dec;
Shifter.VSplitRight(10.0f, &Shifter, &Inc);
Shifter.VSplitLeft(10.0f, &Dec, &Shifter);
bool Shift = Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT);
const bool Shift = Input()->ShiftIsPressed();
int Step = Shift ? 1 : 45;
int Value = pProps[i].m_Value;
@ -3354,7 +3353,7 @@ void CEditor::RenderLayers(CUIRect LayersBox)
if(g != m_SelectedGroup)
SelectLayer(0, g);
if((Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT)) && m_SelectedGroup == g)
if(Input()->ShiftIsPressed() && m_SelectedGroup == g)
{
m_vSelectedLayers.clear();
for(size_t i = 0; i < m_Map.m_vpGroups[g]->m_vpLayers.size(); i++)
@ -3448,7 +3447,7 @@ void CEditor::RenderLayers(CUIRect LayersBox)
static CLayerPopupContext s_LayerPopupContext = {};
if(Result == 1)
{
if((Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT)) && m_SelectedGroup == g)
if(Input()->ShiftIsPressed() && m_SelectedGroup == g)
{
auto Position = std::find(m_vSelectedLayers.begin(), m_vSelectedLayers.end(), i);
if(Position != m_vSelectedLayers.end())
@ -3456,7 +3455,7 @@ void CEditor::RenderLayers(CUIRect LayersBox)
else
AddSelectedLayer(i);
}
else if(!(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT)))
else if(!Input()->ShiftIsPressed())
{
SelectLayer(i, g);
}
@ -3497,7 +3496,7 @@ void CEditor::RenderLayers(CUIRect LayersBox)
if(Input()->KeyPress(KEY_DOWN) && m_Dialog == DIALOG_NONE && m_EditBoxActive == 0)
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
{
if(m_vSelectedLayers[m_vSelectedLayers.size() - 1] < (int)m_Map.m_vpGroups[m_SelectedGroup]->m_vpLayers.size() - 1)
AddSelectedLayer(m_vSelectedLayers[m_vSelectedLayers.size() - 1] + 1);
@ -3528,7 +3527,7 @@ void CEditor::RenderLayers(CUIRect LayersBox)
}
if(Input()->KeyPress(KEY_UP) && m_Dialog == DIALOG_NONE && m_EditBoxActive == 0)
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
{
if(m_vSelectedLayers[m_vSelectedLayers.size() - 1] > 0)
AddSelectedLayer(m_vSelectedLayers[m_vSelectedLayers.size() - 1] - 1);
@ -5284,7 +5283,7 @@ void CEditor::RenderEnvelopeEditor(CUIRect View)
}
else
{
if(Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT))
if(Input()->ShiftIsPressed())
{
if(i != 0)
{
@ -5860,9 +5859,9 @@ void CEditor::Render()
if(m_Dialog == DIALOG_NONE)
{
bool ModPressed = Input()->ModifierIsPressed();
bool ShiftPressed = Input()->KeyIsPressed(KEY_LSHIFT) || Input()->KeyIsPressed(KEY_RSHIFT);
bool AltPressed = Input()->KeyIsPressed(KEY_LALT) || Input()->KeyIsPressed(KEY_RALT);
const bool ModPressed = Input()->ModifierIsPressed();
const bool ShiftPressed = Input()->ShiftIsPressed();
const bool AltPressed = Input()->AltIsPressed();
// ctrl+n to create new map
if(Input()->KeyPress(KEY_N) && ModPressed)
{