Refactor spectator UI input handling

Use `IInput::KeyPress` instead of event callback for mouse presses. Remove subsequently unnecessary `m_Clicked` variable.
This commit is contained in:
Robert Müller 2024-07-16 17:12:03 +02:00
parent 846775c6b7
commit ed53b1c6dc
2 changed files with 34 additions and 47 deletions

View file

@ -232,24 +232,6 @@ void CSpectator::OnRender()
return; return;
} }
if(m_SelectedSpectatorId != NO_SELECTION)
{
// clicking a component
if(m_Clicked)
{
if(!GameClient()->m_MultiViewActivated)
Spectate(m_SelectedSpectatorId);
if(m_SelectedSpectatorId == MULTI_VIEW)
GameClient()->m_MultiViewActivated = true;
else if(m_SelectedSpectatorId == SPEC_FREEVIEW || m_SelectedSpectatorId == SPEC_FOLLOW)
GameClient()->m_MultiViewActivated = false;
if(!GameClient()->m_MultiViewActivated && m_SelectedSpectatorId >= 0 && m_SelectedSpectatorId < MAX_CLIENTS)
m_Clicked = false;
}
}
if(!m_pClient->m_Snap.m_SpecInfo.m_Active && Client()->State() != IClient::STATE_DEMOPLAYBACK) if(!m_pClient->m_Snap.m_SpecInfo.m_Active && Client()->State() != IClient::STATE_DEMOPLAYBACK)
{ {
m_Active = false; m_Active = false;
@ -276,6 +258,8 @@ void CSpectator::OnRender()
float BoxMove = -10.0f; float BoxMove = -10.0f;
float BoxOffset = 0.0f; float BoxOffset = 0.0f;
const bool MousePressed = Input()->KeyPress(KEY_MOUSE_1);
for(const auto &pInfo : m_pClient->m_Snap.m_apInfoByDDTeamName) for(const auto &pInfo : m_pClient->m_Snap.m_apInfoByDDTeamName)
{ {
if(!pInfo || pInfo->m_Team == TEAM_SPECTATORS) if(!pInfo || pInfo->m_Team == TEAM_SPECTATORS)
@ -330,6 +314,11 @@ void CSpectator::OnRender()
{ {
m_SelectedSpectatorId = SPEC_FREEVIEW; m_SelectedSpectatorId = SPEC_FREEVIEW;
FreeViewSelected = true; FreeViewSelected = true;
if(MousePressed)
{
GameClient()->m_MultiViewActivated = false;
Spectate(m_SelectedSpectatorId);
}
} }
TextRender()->TextColor(1.0f, 1.0f, 1.0f, FreeViewSelected ? 1.0f : 0.5f); TextRender()->TextColor(1.0f, 1.0f, 1.0f, FreeViewSelected ? 1.0f : 0.5f);
TextRender()->Text(Width / 2.0f - (ObjWidth - 40.0f), Height / 2.0f - 280.f + (60.f - BigFontSize) / 2.f, BigFontSize, Localize("Free-View"), -1.0f); TextRender()->Text(Width / 2.0f - (ObjWidth - 40.0f), Height / 2.0f - 280.f + (60.f - BigFontSize) / 2.f, BigFontSize, Localize("Free-View"), -1.0f);
@ -339,6 +328,10 @@ void CSpectator::OnRender()
{ {
m_SelectedSpectatorId = MULTI_VIEW; m_SelectedSpectatorId = MULTI_VIEW;
MultiViewSelected = true; MultiViewSelected = true;
if(MousePressed)
{
GameClient()->m_MultiViewActivated = true;
}
} }
TextRender()->TextColor(1.0f, 1.0f, 1.0f, MultiViewSelected ? 1.0f : 0.5f); TextRender()->TextColor(1.0f, 1.0f, 1.0f, MultiViewSelected ? 1.0f : 0.5f);
TextRender()->Text(Width / 2.0f - (ObjWidth - 40.0f) + (ObjWidth * 2.0f / 3.0f), Height / 2.0f - 280.f + (60.f - BigFontSize) / 2.f, BigFontSize, Localize("Multi-View"), -1.0f); TextRender()->Text(Width / 2.0f - (ObjWidth - 40.0f) + (ObjWidth * 2.0f / 3.0f), Height / 2.0f - 280.f + (60.f - BigFontSize) / 2.f, BigFontSize, Localize("Multi-View"), -1.0f);
@ -351,6 +344,11 @@ void CSpectator::OnRender()
{ {
m_SelectedSpectatorId = SPEC_FOLLOW; m_SelectedSpectatorId = SPEC_FOLLOW;
FollowSelected = true; FollowSelected = true;
if(MousePressed)
{
GameClient()->m_MultiViewActivated = false;
Spectate(m_SelectedSpectatorId);
}
} }
TextRender()->TextColor(1.0f, 1.0f, 1.0f, FollowSelected ? 1.0f : 0.5f); TextRender()->TextColor(1.0f, 1.0f, 1.0f, FollowSelected ? 1.0f : 0.5f);
TextRender()->Text(Width / 2.0f - (ObjWidth - 40.0f) + (ObjWidth * 2.0f * 2.0f / 3.0f), Height / 2.0f - 280.0f + (60.f - BigFontSize) / 2.f, BigFontSize, Localize("Follow"), -1.0f); TextRender()->Text(Width / 2.0f - (ObjWidth - 40.0f) + (ObjWidth * 2.0f * 2.0f / 3.0f), Height / 2.0f - 280.0f + (60.f - BigFontSize) / 2.f, BigFontSize, Localize("Follow"), -1.0f);
@ -426,29 +424,35 @@ void CSpectator::OnRender()
{ {
m_SelectedSpectatorId = m_pClient->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId; m_SelectedSpectatorId = m_pClient->m_Snap.m_apInfoByDDTeamName[i]->m_ClientId;
PlayerSelected = true; PlayerSelected = true;
if(GameClient()->m_MultiViewActivated && m_Clicked) if(MousePressed)
{ {
if(GameClient()->m_MultiViewTeam == DDTeam) if(GameClient()->m_MultiViewActivated)
{ {
GameClient()->m_aMultiViewId[m_SelectedSpectatorId] = !GameClient()->m_aMultiViewId[m_SelectedSpectatorId]; if(GameClient()->m_MultiViewTeam == DDTeam)
if(!GameClient()->m_aMultiViewId[m_pClient->m_Snap.m_SpecInfo.m_SpectatorId])
{ {
int NewClientId = GameClient()->FindFirstMultiViewId(); GameClient()->m_aMultiViewId[m_SelectedSpectatorId] = !GameClient()->m_aMultiViewId[m_SelectedSpectatorId];
if(NewClientId < MAX_CLIENTS && NewClientId >= 0) if(!GameClient()->m_aMultiViewId[m_pClient->m_Snap.m_SpecInfo.m_SpectatorId])
{ {
GameClient()->CleanMultiViewId(NewClientId); int NewClientId = GameClient()->FindFirstMultiViewId();
GameClient()->m_aMultiViewId[NewClientId] = true; if(NewClientId < MAX_CLIENTS && NewClientId >= 0)
Spectate(NewClientId); {
GameClient()->CleanMultiViewId(NewClientId);
GameClient()->m_aMultiViewId[NewClientId] = true;
Spectate(NewClientId);
}
} }
} }
else
{
GameClient()->ResetMultiView();
Spectate(m_SelectedSpectatorId);
m_MultiViewActivateDelay = Client()->LocalTime() + 0.3f;
}
} }
else else
{ {
GameClient()->ResetMultiView();
Spectate(m_SelectedSpectatorId); Spectate(m_SelectedSpectatorId);
m_MultiViewActivateDelay = Client()->LocalTime() + 0.3f;
} }
m_Clicked = false;
} }
} }
float TeeAlpha; float TeeAlpha;
@ -553,18 +557,3 @@ void CSpectator::SpectateClosest()
{ {
ConSpectateClosest(NULL, this); ConSpectateClosest(NULL, this);
} }
bool CSpectator::OnInput(const IInput::CEvent &Event)
{
if(m_Active && Event.m_Flags & IInput::FLAG_PRESS && Event.m_Key == KEY_MOUSE_1)
{
m_Clicked = true;
return true;
}
else if(Event.m_Flags & IInput::FLAG_RELEASE && Event.m_Key == KEY_MOUSE_1)
{
m_Clicked = false;
return false;
}
return false;
}

View file

@ -17,7 +17,6 @@ class CSpectator : public CComponent
bool m_Active; bool m_Active;
bool m_WasActive; bool m_WasActive;
bool m_Clicked;
int m_SelectedSpectatorId; int m_SelectedSpectatorId;
vec2 m_SelectorMouse; vec2 m_SelectorMouse;
@ -43,7 +42,6 @@ public:
virtual void OnRender() override; virtual void OnRender() override;
virtual void OnRelease() override; virtual void OnRelease() override;
virtual void OnReset() override; virtual void OnReset() override;
virtual bool OnInput(const IInput::CEvent &Event) override;
void Spectate(int SpectatorId); void Spectate(int SpectatorId);
void SpectateClosest(); void SpectateClosest();