view LightClone/Source/GuiInterface.cpp @ 25:eae13b04b06f

Working on Gui drag and drop
author koryspansel <koryspansel@bendbroadband.com>
date Fri, 16 Sep 2011 13:11:35 -0700
parents a785b0aaf004
children 3a63df04f3c0
line wrap: on
line source

/*
 * GuiInterface
 */

#include "GuiInterface.h"

/*
 * GuiInterface
 */
GuiInterface::GuiInterface() : pInputManager(NULL), pDragElement(NULL), pCaptureElement(NULL), pFocusElement(NULL)
{
	pInterface	= this;
	pCursor		= new GuiCursor();
}

/*
 * Initialize
 */
ErrorCode GuiInterface::Initialize(ResourceManager* pResourceManager, InputManager* pInput)
{
	ErrorCode eCode = GuiElement::Initialize(pResourceManager);
	if(eCode == Error_Success)
	{
		eCode = pCursor->Initialize(pResourceManager);
		if(eCode == Error_Success)
		{
			eCode = pCursor->SetTexture("Data\\Textures\\Arrow.tga");
			if(eCode == Error_Success)
			{
				pInputManager = pInput;
				//ASSERT(pInputManager);

				pInputManager->SetBounds(0.0f, 0.0f, (float)ScreenSizeX, (float)ScreenSizeY);
				pInputManager->SetMouse(0.5f * ScreenSizeX, 0.5f * ScreenSizeY);
			}
		}
	}

	return eCode;
}

/*
 * Terminate
 */
void GuiInterface::Terminate()
{
	GuiElement::Terminate();
}

/*
 * Update
 */
void GuiInterface::Update(float fElapsed)
{
	const float fMouseX	= pInputManager->GetMouseX();
	const float fMouseY	= pInputManager->GetMouseY();
	const float fDeltaX	= pInputManager->GetMouseDeltaX();
	const float fDeltaY	= pInputManager->GetMouseDeltaY();

	GuiElement::Update(fElapsed);

	if(pCursor)
	{
		/*
		char kBuffer[128];
		sprintf_s(kBuffer, "<%0.2f, %0.2f>\n", fX, fY);
		OutputDebugStringA(kBuffer);
		*/

		pCursor->SetPosition(fMouseX, fMouseY);
		pCursor->Update(fElapsed);
	}

	GuiElement* pElement = Pick(fMouseX, fMouseY);

	if(fMouseDeltaX != 0.0f || fMouseDeltaY != 0.0f)
	{
		//Mouse moved
		if(pCaptureElement)
		{

		}
	}

	for(uint32 nButton = MouseButton_Left; nButton < MouseButton_Count; ++nButton)
	{
		// button down
		if(pInputManager->IsButtonDown(nButton) && !pInputManager->WasButtonDown(nButton))
		{
			if(pCaptureElement)
			{
				//TODO: Does capture make sense for mouse down events?
				pElement->OnMouseDown(nButton, fMouseX, fMouseY);
			}
			else
			{
				if(pElement)
				{
					pElement->OnMouseDown(nButton, fMouseX, fMouseY);
				}
			}
		}
		else

		// button up
		if(!pInputManager->IsButtonDown(nButton) && pInputManager->WasButtonDown(nButton))
		{
			if(pDragElement)
			{
				GuiElement* pElement = Pick(fMouseX, fMouseY);
				if(pElement)
				{
					pElement->OnDrop(pDragElement, fMouseX, fMouseY);
				}

				pDragElement = NULL;
			}
			else
			{
				if(pCaptureElement)
				{
					pCaptureElement->OnMouseUp(nButton, fMouseX, fMouseY);
				}
				else
				{
					GuiElement* pElement = Pick(fMouseX, fMouseY);
					if(pElement)
					{
						pElement->OnMouseUp(nButton, fMouseX, fMouseY);
					}
				}
			}
		}
	}
}

/*
 * Render
 */
void GuiInterface::Render(RenderContext& kContext, Camera& kCamera)
{
	//TODO: Begin batch
	//kContext.BeginBatch();

	if(bVisible)
	{
		//TODO: Inside Render functions call kContext.AddToBatch(pTexture, kPosition, kDimensions, kColor)
		GuiElement::Render(kContext, kCamera);

		if(pCursor)
		{
			pCursor->Render(kContext, kCamera);

			if(pDragElement)
			{
				//TODO: Move to cursor position
				//pDragElement->Render(kContext, kCamera);
			}
		}
	}

	//TODO: End batch
	//kContext.EndBatch()
}

/*
 * AcquireCursor
 */
ErrorCode GuiInterface::AcquireCursor(GuiElement* pSource)
{
	if(!pCaptureElement)
	{
		return pCaptureElement = pSource, Error_Success;
	}

	return Error_Fail;
}

/*
 * ReleaseCursor
 */
void GuiInterface::ReleaseCursor()
{
	pCaptureElement = NULL;
}

/*
 * BeginDrag
 */
void GuiInterface::BeginDrag(GuiElement* pSource)
{
}

/*
 * EndDrag
 */
void GuiInterface::EndDrag()
{
}