view LightClone/Source/GuiInterface.cpp @ 64:3507bd831c7f

Hook up ServiceProvider
author koryspansel <koryspansel@bendbroadband.com>
date Tue, 04 Oct 2011 13:02:45 -0700
parents 1fe27776627e
children 8e7ebab350e7
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(ServiceProvider* pServiceProvider)
{
	ResourceManager* pResourceManager = NULL;

	ErrorCode eCode = pServiceProvider->GetService("ResourceManager", &pResourceManager);
	if(eCode == Error_Success)
	{
		eCode = kRenderContext.Initialize(pServiceProvider);
		if(eCode == Error_Success)
		{
			eCode = GuiElement::Initialize(pServiceProvider);
			if(eCode == Error_Success)
			{
				eCode = pCursor->Initialize(pServiceProvider);
				if(eCode == Error_Success)
				{
					eCode = pCursor->SetTexture("Data\\Textures\\Arrow.tga");
					if(eCode == Error_Success)
					{
						pInputManager = (InputManager*)pServiceProvider->GetService("InputManager");
						ASSERT(pInputManager != NULL);

						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)
	{
		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)
				{
					pCaptureElement->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)
{
	kRenderContext.Begin(&kContext, &kCamera);

	if(nFlags & GuiElementFlag_Visible)
	{
		//GuiElement::Render(kContext, kCamera);
		GuiElement::Render(kRenderContext);

		if(pCursor)
		{
			if(pDragElement)
			{
				pDragElement->SetPosition(pCursor->GetPosition() - 0.5f * pDragElement->GetDimensions());
				pDragElement->Render(kRenderContext);
			}

			pCursor->Render(kRenderContext);
		}
	}

	kRenderContext.End();
}

/*
 * 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 pCaptureElement == 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);
	}

	delete pDragElement;
	pDragElement = NULL;
}