Files
funnygame/engine/humandevice.cpp
2026-03-05 21:25:59 +02:00

94 lines
2.4 KiB
C++

#include "ihumandevice.h"
#include "tier1/interface.h"
#include "tier1/utlvector.h"
class CHumanDeviceManager: public IHumanDeviceManager
{
public:
virtual void Frame() override;
virtual void SetDefaultInput( IHumanDeviceInput *pInput ) override;
virtual void PushInput( IHumanDeviceInput *pInput ) override;
virtual void PopInput() override;
virtual IHumanDeviceInput *GetCurrentInput() override;
virtual void SetButtonPressed( EInputButton eButton ) override;
virtual void SetButtonUnpressed( EInputButton eButton ) override;
virtual void WriteUTF8( uint32_t uCode ) override;
bool m_abIsButtonPressed[k_EInputButton_Count] = {};
bool m_abWasButtonPressed[k_EInputButton_Count] = {};
CUtlVector<IHumanDeviceInput*> m_apInputStack;
IHumanDeviceInput *m_pDefaultInput;
};
CHumanDeviceManager s_manager;
IHumanDeviceManager *g_pHumanDeviceManager = &s_manager;
void CHumanDeviceManager::Frame()
{
IHumanDeviceInput *pInput = GetCurrentInput();
if (pInput)
{
for ( int i = 0; i < k_EInputButton_Count; i++ )
{
if (m_abIsButtonPressed[i] == m_abWasButtonPressed[i])
continue;
pInput->OnGameButton(k_EInputDevice_Keyboard,
(EInputButton)i, m_abIsButtonPressed[i]);
}
}
V_memcpy(m_abWasButtonPressed, m_abIsButtonPressed, sizeof(m_abIsButtonPressed));
}
void CHumanDeviceManager::SetDefaultInput( IHumanDeviceInput *pInput )
{
m_pDefaultInput = pInput;
}
void CHumanDeviceManager::PushInput( IHumanDeviceInput *pInput )
{
m_apInputStack.AppendTail(pInput);
}
void CHumanDeviceManager::PopInput()
{
m_apInputStack.RemoveTail();
}
IHumanDeviceInput *CHumanDeviceManager::GetCurrentInput()
{
if (m_apInputStack.GetSize() == 0)
return m_pDefaultInput;
return m_apInputStack[m_apInputStack.GetSize()-1];
}
void CHumanDeviceManager::SetButtonPressed( EInputButton eButton )
{
m_abIsButtonPressed[eButton] = true;
IHumanDeviceInput *pInput = GetCurrentInput();
if (pInput)
pInput->OnButton(k_EInputDevice_Keyboard, eButton, true);
}
void CHumanDeviceManager::SetButtonUnpressed( EInputButton eButton )
{
m_abIsButtonPressed[eButton] = false;
IHumanDeviceInput *pInput = GetCurrentInput();
if (pInput)
pInput->OnButton(k_EInputDevice_Keyboard, eButton, false);
}
void CHumanDeviceManager::WriteUTF8( uint32_t uCode )
{
}
EXPOSE_INTERFACE_GLOBALVAR(CHumanDeviceManager, IHumanDeviceManager, HUMAN_DEVICE_MANAGER_INTERFACE_VERSION, g_pHumanDeviceManager)