
///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_PSP_SCEUtility_OSKHandler.cpp
// Author : Andy Tate
// Date   : 4-Oct-2005
//
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Include files...
////////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

// This file's header...
#include "PSPEmul/SCEUtility/Asura_PSP_SCEUtility_OSKHandler.h"

// Asura independent header-files...
#include "Core/Asura_Maths.h"
#include "Core/Asura_Timers.h"
#include "Core/Asura_UserInput.h"
#include "Rendering/Asura_Font_System.h"

// Asura platform header-files...

// Game independent header-files...

// Game platform header-files...


////////////////////////////////////////////////////////////////////////////////
// Constant declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Static variable declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Private functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Protected functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Public functions...
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtility_OSKHandler::Asura_PSP_SCEUtility_OSKHandler
// Author       : Andy Tate
// Date         : 4-Oct-2005
//
// Description  : Constructor for the PSP SCE Utility on-screen keyboard handler.
//
// Parameters   : The text for the message to be displayed, the text before
//				  editting, the maximum number of characters available and the
//				  location to store the resulting text post-edit.
//
// Return value : N/A.
////////////////////////////////////////////////////////////////////////////////

Asura_PSP_SCEUtility_OSKHandler::Asura_PSP_SCEUtility_OSKHandler (const Asura_UnicodeChar* wszMessage,
																  const Asura_UnicodeChar* wszTextBeforeEdit,
																  const u_int uMaxChars,
																  Asura_UnicodeChar* wszTextAfterEdit) :
	m_wszMessage(wszMessage),
	m_wszTextBeforeEdit(wszTextBeforeEdit),
	m_uMaxChars(uMaxChars),
	m_wszTextAfterEdit(wszTextAfterEdit)
{
	// Copy the input text to the output buffer...
	wcsncpy	(wszTextAfterEdit, wszTextBeforeEdit, uMaxChars);

	// Set the current position to the end of the string...
	m_uCurrentPosition	= wcslen (wszTextAfterEdit);
}

/// End of function 'Asura_PSP_SCEUtility_OSKHandler::Asura_PSP_SCEUtility_OSKHandler'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtility_OSKHandler::Update
// Author       : Andy Tate
// Date         : 4-Oct-2005
//
// Description  : Handles the updating of the PSP SCE Utility on-screen keyboard.
//
// Parameters	: A reference to the cancelled flag to be filled in.
//
// Return value : "true" if update is in progress, "false" if we're done.
////////////////////////////////////////////////////////////////////////////////

bool Asura_PSP_SCEUtility_OSKHandler::Update (bool& bCancelled)
{
	// Have any special keys been pressed?
	if (Asura_UserInput::DebouncedKeyPressed(ASURA_KEYID_ENTER))
	{
		// Return - accept the changes...
		bCancelled	= false;

		return	(false);
	}

	else if (Asura_UserInput::DebouncedKeyPressed(ASURA_KEYID_ESCAPE))
	{
		// Escape - restore the default and return...
		bCancelled	= true;

		wcsncpy	(m_wszTextAfterEdit, m_wszTextBeforeEdit, m_uMaxChars);
		return	(false);
	}

	else
	{
		// No - better update the input buffer...
		Asura_UserInput::UpdateInputBuffer	(m_wszTextAfterEdit, m_uMaxChars, m_uCurrentPosition, NULL);
		return								(true);
	}
}

/// End of function 'Asura_PSP_SCEUtility_OSKHandler::Update'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_PSP_SCEUtility_OSKHandler::Render
// Author       : Andy Tate
// Date         : 4-Oct-2005
//
// Description  : Handles the rendering of the PSP SCE Utility on-screen keyboard.
//
// Return value : None.
////////////////////////////////////////////////////////////////////////////////

void Asura_PSP_SCEUtility_OSKHandler::Render (void)
{
	const Asura_IntVector_2	xIntMessagePosition (240, 80);
	const Asura_IntVector_2	xIntTextPosition (240, 160);

	const float				fScreenScale	= CalculateScreenScale ();
	const float				fTime			= Asura_Timers::GetServerAbsoluteTime ();
	const int				iSeconds		= static_cast<int>(fTime);
	const int				iFifths			= static_cast<int>((fTime - static_cast<float>(iSeconds)) * 5.0f);

	Asura_Vector_2			xMessagePosition;
	Asura_Vector_2			xTextPosition;

	// Calculate the actual screen position...
	CalculateScreenPosition	(fScreenScale,	xIntMessagePosition,	xMessagePosition);
	CalculateScreenPosition	(fScreenScale,	xIntTextPosition,		xTextPosition);

	// Print the message...
	Asura_Font_System::PrintLongString
	(
		m_wszMessage,
		NULL,
		xMessagePosition [0],
		xMessagePosition [1],
		1.0f,
		fScreenScale,
		0xFFFFFFFF,
		ASURA_TEXTFORMAT_CENTREJUSTIFIED
	);

	// Print the text being inputted...
	Asura_Font_System::PrintLongString
	(
		L">  ",
		NULL,
		xTextPosition [0],
		xTextPosition [1],
		1.0f,
		fScreenScale,
		0xFFFFFFFF,
		ASURA_TEXTFORMAT_RIGHTJUSTIFIED
	);

	Asura_Font_System::PrintLongString
	(
		m_wszTextAfterEdit,
		NULL,
		xTextPosition [0],
		xTextPosition [1],
		1.0f,
		fScreenScale,
		0xFFFFFFFF,
		ASURA_TEXTFORMAT_LEFTJUSTIFIED
	);

	// Do we need to render the cursor?
	if (1 < iFifths)
	{
		// Yes - print the cursor...
		xTextPosition.x	+= Asura_Font_System::GetWidthOfString (m_wszTextAfterEdit, fScreenScale, m_uCurrentPosition);

		Asura_Font_System::PrintLongString
		(
			L"_",
			NULL,
			xTextPosition [0],
			xTextPosition [1],
			1.0f,
			fScreenScale,
			0xFFFFFFFF,
			ASURA_TEXTFORMAT_LEFTJUSTIFIED
		);
	}
}

/// End of function 'Asura_PSP_SCEUtility_OSKHandler::Render'. ///


/// End of file. ///
