// ------------------------------------------------------------------------
/*
	\author: Jaap Suter
   
	Copyright (C) Electronic Arts Canada Inc. 2002.  All rights reserved.	
*/
// ------------------------------------------------------------------------

/*! \file */

#include "reveal/input_state.h"

#include <assert.h>

namespace Reveal {

// -------------------------------------------------------------------------------
void InputState::Update()
{
	DoUpdate();

	if ( (mDown == DoDown()) && (MouseRelX() == 0) && (MouseRelY() == 0) ) mChanged = false; else mChanged = true;

	mMouseX += MouseRelX();
    mMouseY += MouseRelY();
	
	if ( mMouseX < 0 ) mMouseX = 0;
	if ( mMouseY < 0 ) mMouseY = 0;
	if ( mMouseX >= Impl::GuiMetrics::SCREEN_WIDTH  ) mMouseX = Impl::GuiMetrics::SCREEN_WIDTH - 1;
	if ( mMouseY >= Impl::GuiMetrics::SCREEN_HEIGHT ) mMouseY = Impl::GuiMetrics::SCREEN_HEIGHT - 1;
	
	Reveal::uint32_t down = DoDown();
	for ( size_t i = 0; i < 32; ++i ) UpdateSingleButton( 0 != (down & (1 << i)), (1 << i) );
}

// -------------------------------------------------------------------------------
void InputState::UpdateSingleButton( bool down, Reveal::uint32_t button )
{
    assert( OnlyOneBitSet( button ) );

	mPressed  &= ~button;
	mReleased &= ~button;
    if ( down )
    {
        if ( !(mDown & button) ) 
        {
			mDown |= button;
            mPressed |= button;
        }
    }
    else
    {
        if ( mDown & button )
        {
			mDown &= ~button;
            mReleased |= button;
        }
    }
}

} // Namespace Reveal;

