#include "Input.h"
#include "menumanager.h"
#include "console.h"

Input::Input(MenuManager*manager,DWORD id) : GUIElement(manager,id)
{
	m_Panel = new Panel(GetManager(),0);
	AddChild(m_Panel);
	m_Text = new Label(GetManager(),0);
	m_Text->SetTextAlign(AL_LEFTCENTER);
	AddChild(m_Text);
	m_CursorChar = '_';
	m_CursorPos = 0;
	m_SelSize = 0;
	m_ShowCursor = false;
	m_Multiline = false;

	m_Panel->SetEventHandler(GE_BUTTONCLICK, OnFwdEvent);
	m_Text->SetEventHandler(GE_BUTTONCLICK, OnFwdEvent);
}

Input::~Input(void)
{
}

void Input::OnUpdate(void)
{
}

void Input::OnKeyPress(BYTE key)
{
	if(key == DIK_DELETE)
	{
		if(m_CursorPos < m_Text->GetText().getlen())
			m_Text->SetText(m_Text->GetText().Left(m_CursorPos) + m_Text->GetText().Right(m_CursorPos + 1- m_Text->GetText().getlen()));
	}
	else if(key == DIK_BACKSPACE)
	{
		if(m_CursorPos > 0)
		{
			m_Text->SetText(m_Text->GetText().Left(m_CursorPos-1) + m_Text->GetText().Right(m_CursorPos - m_Text->GetText().getlen()));
			m_CursorPos--;
		}
	}
	else if(key == DIK_LEFT)
	{
		if(m_CursorPos > 0)
			m_CursorPos--;
	}
	else if(key == DIK_RIGHT)
	{
		if(m_CursorPos < m_Text->GetText().getlen())
			m_CursorPos++;
	}
	else
	{
		XString toins = GetManager()->GetInputDevice()->KeyToXString(key);
		XString text = m_Text->GetText();
		text.insertat(m_CursorPos,toins);
		m_Text->SetText(text);
		m_CursorPos += toins.getlen();
	}
}

void Input::SetSize(const float xl, const float yl)
{
	GUIElement::SetSize(xl,yl);

	m_Text->SetSize(GetXl() - m_ClientBorder*2, GetYl() - m_ClientBorder*2);
	m_Panel->SetSize(GetXl(),GetYl());
}