2003-02-16 04:01:45 +00:00
|
|
|
#include "global.h"
|
2002-05-20 08:59:37 +00:00
|
|
|
#include "InputQueue.h"
|
2002-07-27 19:29:51 +00:00
|
|
|
#include "RageTimer.h"
|
2006-09-08 03:16:35 +00:00
|
|
|
#include "InputEventPlus.h"
|
2002-05-20 08:59:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
InputQueue* INPUTQUEUE = NULL; // global and accessable from anywhere in our program
|
|
|
|
|
|
|
|
|
|
|
2002-07-27 19:29:51 +00:00
|
|
|
InputQueue::InputQueue()
|
|
|
|
|
{
|
2004-08-16 18:21:34 +00:00
|
|
|
FOREACH_GameController ( gc )
|
2006-09-08 03:16:35 +00:00
|
|
|
m_aQueue[gc].resize( MAX_INPUT_QUEUE_LENGTH );
|
2002-07-27 19:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
2006-09-08 03:16:35 +00:00
|
|
|
void InputQueue::RememberInput( const InputEventPlus &iep )
|
2002-07-27 19:29:51 +00:00
|
|
|
{
|
2006-09-08 03:16:35 +00:00
|
|
|
int c = iep.GameI.controller;
|
2002-10-31 03:16:02 +00:00
|
|
|
if( m_aQueue[c].size() >= MAX_INPUT_QUEUE_LENGTH ) // full
|
2002-10-31 04:11:08 +00:00
|
|
|
m_aQueue[c].erase( m_aQueue[c].begin(), m_aQueue[c].begin() + (m_aQueue[c].size()-MAX_INPUT_QUEUE_LENGTH+1) );
|
2006-09-08 03:16:35 +00:00
|
|
|
m_aQueue[c].push_back( iep );
|
2002-10-31 04:23:39 +00:00
|
|
|
}
|
2002-07-27 19:29:51 +00:00
|
|
|
|
2003-12-02 09:04:00 +00:00
|
|
|
bool InputQueue::MatchesSequence( GameController c, const GameButton* button_sequence, const int iNumButtons, float fMaxSecondsBack )
|
2002-07-27 19:29:51 +00:00
|
|
|
{
|
2006-09-26 20:59:40 +00:00
|
|
|
if( c == GameController_Invalid )
|
2004-01-25 15:45:50 +00:00
|
|
|
return false;
|
|
|
|
|
|
2005-04-24 06:30:32 +00:00
|
|
|
if( fMaxSecondsBack == -1 )
|
|
|
|
|
fMaxSecondsBack = 0.4f + iNumButtons*0.15f;
|
|
|
|
|
|
2006-09-08 03:44:14 +00:00
|
|
|
RageTimer OldestTimeAllowed;
|
|
|
|
|
OldestTimeAllowed += -fMaxSecondsBack;
|
2002-07-27 19:29:51 +00:00
|
|
|
|
|
|
|
|
int sequence_index = iNumButtons-1; // count down
|
2002-10-31 03:16:02 +00:00
|
|
|
for( int queue_index=m_aQueue[c].size()-1; queue_index>=0; queue_index-- ) // iterate newest to oldest
|
2002-07-27 19:29:51 +00:00
|
|
|
{
|
2006-09-08 03:16:35 +00:00
|
|
|
const InputEventPlus &iep = m_aQueue[c][queue_index];
|
|
|
|
|
if( iep.GameI.button != button_sequence[sequence_index] ||
|
2006-09-08 03:44:14 +00:00
|
|
|
iep.DeviceI.ts < OldestTimeAllowed )
|
2002-07-27 19:29:51 +00:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if( sequence_index == 0 ) // we matched the whole pattern
|
|
|
|
|
{
|
2002-10-24 20:15:24 +00:00
|
|
|
m_aQueue[c].clear(); // empty the queue so we don't match on it again
|
2002-07-27 19:29:51 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
sequence_index--;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2003-02-16 04:56:36 +00:00
|
|
|
}
|
2003-12-02 09:04:00 +00:00
|
|
|
|
2006-09-08 04:18:01 +00:00
|
|
|
bool InputQueue::WasPressedRecently( GameController c, const GameButton button, const RageTimer &OldestTimeAllowed, InputEventPlus *pIEP )
|
|
|
|
|
{
|
|
|
|
|
for( int queue_index=m_aQueue[c].size()-1; queue_index>=0; queue_index-- ) // iterate newest to oldest
|
|
|
|
|
{
|
|
|
|
|
const InputEventPlus &iep = m_aQueue[c][queue_index];
|
|
|
|
|
if( iep.DeviceI.ts < OldestTimeAllowed ) // buttons are too old. Stop searching because we're not going to find a match
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if( iep.GameI.button != button )
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if( pIEP != NULL )
|
|
|
|
|
*pIEP = iep;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false; // didn't find the button
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-02 09:04:00 +00:00
|
|
|
bool InputQueue::AllWerePressedRecently( GameController c, const GameButton* buttons, int iNumButtons, float fMaxSecondsBack )
|
|
|
|
|
{
|
2006-09-08 03:44:14 +00:00
|
|
|
RageTimer OldestTimeAllowed;
|
|
|
|
|
OldestTimeAllowed += -fMaxSecondsBack;
|
2003-12-02 09:04:00 +00:00
|
|
|
|
|
|
|
|
for( int b=0; b<iNumButtons; b++ )
|
|
|
|
|
{
|
2006-09-08 04:18:01 +00:00
|
|
|
if( !WasPressedRecently(c, buttons[b], OldestTimeAllowed) )
|
|
|
|
|
return false; // didn't find the button
|
2003-12-02 09:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_aQueue[c].clear(); // empty the queue so we don't match on it again
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2004-06-08 01:24:17 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (c) 2001-2003 Chris Danford
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|