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

/*! \file */

#include "reveal/impl/button_control.h"
#include "reveal/impl/gui_metrics.h"
#include "reveal/impl/menu.h"
#include "reveal/impl/tree.h"

#include "reveal/gui_colors.h"
#include "reveal/input_state.h"
#include "reveal/renderer.h"

namespace Reveal {
	namespace Impl {

// -------------------------------------------------------------------------------
void ButtonControl::DoRender( const Renderer& renderer, const GuiColors& guiColors ) const
{
	Reveal::uint32_t bgColor = guiColors.mWidgetBackgroundColor;
	Reveal::uint32_t buttonColor = mDown ? guiColors.mButtonDownColor : guiColors.mButtonColor;
	Reveal::uint32_t edgeColor   = mDown ? guiColors.mInteractiveForegroundColor : guiColors.mInteractiveBackgroundColor;

	if ( GetTree().GetWidgetWithFocus() && GetTree().GetWidgetWithFocus()->Parent() == Parent() ) 
		bgColor= guiColors.mWidgetSameLevelBackgroundColor;
	else
	if ( !HasFocus() ) 
	{
		// Make 'm a bit more transparent;
		buttonColor &= 0x33000000;
		edgeColor   &= 0x33000000;
	}
	
	if ( HasFocus() )
	{
		bgColor = guiColors.mWidgetHighlightedBackgroundColor;
		buttonColor = mDown ? guiColors.mButtonDownColor : guiColors.mWidgetHighlightedBackgroundColor;
	}

    renderer.Bar( X(), Y(), Width(), Height(), bgColor );

	renderer.Bar( X() + 2 * GuiMetrics::BORDER_WIDTH, Y() + 1, Width() - 4 * GuiMetrics::BORDER_WIDTH, Height() - 2, buttonColor );
	if ( mDown )
	{
		renderer.Line( X() + Width() - 2 * GuiMetrics::BORDER_WIDTH, Y() + 1, X() + Width() - 2 * GuiMetrics::BORDER_WIDTH, Y() + Height() - 2, edgeColor );
		renderer.Line( X() + 2 * GuiMetrics::BORDER_WIDTH, Y() + Height() - 2, X() + Width() - 2 * GuiMetrics::BORDER_WIDTH, Y() + Height() - 2, edgeColor );
		renderer.String( X() + 5 + 2 * GuiMetrics::TEXT_BORDER_WIDTH, Y() + 1 + GuiMetrics::TEXT_BORDER_HEIGHT, Name(), guiColors.mTextColor );   
	}
	else
	{
		renderer.Line( X() + 2 * GuiMetrics::BORDER_WIDTH, Y() + 1, X() + 2 * GuiMetrics::BORDER_WIDTH, Y() + Height() - 2, edgeColor );
		renderer.Line( X() + 2 * GuiMetrics::BORDER_WIDTH, Y() + 1, X() + Width() - 2 * GuiMetrics::BORDER_WIDTH, Y() + 1, edgeColor );
		renderer.String( X() + 4 * GuiMetrics::TEXT_BORDER_WIDTH, Y() + GuiMetrics::TEXT_BORDER_HEIGHT, Name(), guiColors.mTextColor );   
	}	
	
	DrawBorder( renderer, guiColors );
}

// -------------------------------------------------------------------------------
Widget* ButtonControl::DoHandle( const InputState& inputState )
{ 
	Widget* widgetThatHandledTheInput = NULL;
	
	mDown = false; // Until proven otherwise;

    if ( HasFocus() )
	{
		widgetThatHandledTheInput = this;
		
		if ( inputState.Down( InputState::CONFIRM | InputState::LEFT_MOUSE ) ) mDown = true;
        if ( inputState.Released( InputState::CONFIRM | InputState::LEFT_MOUSE ) ) 
		{
			MakeParentMostRecentlyUsed();
			mCallback();
		}
		else
		if ( inputState.Released( InputState::DOWN ) ) 
		{
			if ( Parent() ) widgetThatHandledTheInput = Parent()->GiveFocusToNextWidget( this );
		}
		else
		if ( inputState.Released( InputState::UP ) ) 
		{					
			if ( Parent() ) widgetThatHandledTheInput = Parent()->GiveFocusToPrevWidget( this );
		}
		else
		if ( inputState.Released( InputState::BACK | InputState::LEFT ) ) 
		{					
			widgetThatHandledTheInput = GiveFocusToParent();
		}
	}
    
	return widgetThatHandledTheInput;
}

// -------------------------------------------------------------------------------

	} // Namespace Impl;
} // Namespace Reveal;

