view LightClone/Source/GuiInterface.cpp @ 26:3a63df04f3c0

Several Gui enhancements; Drag and drop should work; Added resource caching
author koryspansel
date Fri, 16 Sep 2011 15:28:15 -0700
parents eae13b04b06f
children 0b729faa4e73
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);
	}

	if(pDragElement)
	{
		if(pInputManager->IsButtonReleased(nDragButton))
		{
			EndDrag(Pick(fMouseX, fMouseY), fMouseX, fMouseY);
		}
		else
		{
			for(uint32 nButton = MouseButton_Left; nButton < MouseButton_Count; ++nButton)
			{
				if(pInputManager->IsButtonPressed(nButton) || pInputManager->IsButtonReleased(nButton))
				{
					EndDrag(NULL, fMouseX, fMouseY);
				}
			}
		}
	}
	else
	{
		GuiElement* pElement = Pick(fMouseX, fMouseY);

		if(pFocusElement != pElement)
		{
			if(!pCaptureElement)
			{
				if(pFocusElement)
				{
					pFocusElement->OnMouseLeave();
				}

				if(pElement)
				{
					pElement->OnMouseEnter();
				}
			}

			pFocusElement = pElement;
		}

		for(uint32 nButton = MouseButton_Left; nButton < MouseButton_Count; ++nButton)
		{
			// button down
			if(pInputManager->IsButtonPressed(nButton))
			{
				if(pCaptureElement)
				{
					pElement->OnMouseDown(nButton, fMouseX, fMouseY);
				}
				else

				if(pElement)
				{
					pElement->OnMouseDown(nButton, fMouseX, fMouseY);
				}
			}
			else

			// button up
			if(pInputManager->IsButtonReleased(nButton))
			{
				if(pCaptureElement)
				{
					pCaptureElement->OnMouseUp(nButton, fMouseX, fMouseY);
				}
				else

				if(pElement)
				{
					pElement->OnMouseUp(nButton, fMouseX, fMouseY);
				}
			}
		}

		// mouse moved
		if(fDeltaX != 0.0f || fDeltaY != 0.0f)
		//if(pInputManager->MouseMoved())
		{
			if(pCaptureElement)
			{
				pCaptureElement->OnMouseMove(fMouseX, fMouseY);
			}
			else

			if(pElement)
			{
				pElement->OnMouseMove(fMouseX, fMouseY);
			}
		}
	}
}

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

	if(nFlags & GuiElementFlag_Visible)
	{
		//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;
}

/*
 * IsCursorAcquiredBy
 */
bool GuiInterface::IsCursorAcquiredBy(GuiElement* pElement) const
{
	return pDragElement == pElement;
}

/*
 * BeginDrag
 */
void GuiInterface::BeginDrag(GuiElement* pSource, uint32 nButton)
{
	//ASSERT(pDragElement == NULL);

	pDragElement	= pSource;
	nDragButton		= nButton;
}

/*
 * EndDrag
 */
void GuiInterface::EndDrag(GuiElement* pTarget, float fX, float fY)
{
	if(pTarget)
	{
		pTarget->OnDrop(pDragElement, fX, fY);
	}

	pDragElement = NULL;
}