view LightClone/Source/GuiButton.cpp @ 75:57c0ce406a68 tip

Add main menu
author koryspansel <koryspansel@bendbroadband.com>
date Tue, 18 Oct 2011 17:08:17 -0700
parents 3507bd831c7f
children
line wrap: on
line source

/*
 * GuiButton
 */

#include "GuiButton.h"
#include "GuiInterface.h"
#include "VertexTypes.h"

/*
 * EventClick
 */
const char* GuiButton::EventClick = "GuiButton:Click";

/*
 * GuiButton
 */
GuiButton::GuiButton() : GuiElement(), nState(GuiButtonState_Normal), pFont(NULL)
{
	SetFlag(GuiElementFlag_Pickable);

	memset(pTexture, 0, sizeof(pTexture));
}

/*
 * Render
 */
void GuiButton::Render(GuiRenderContext& kContext)
{
	if(nFlags & GuiElementFlag_Visible)
	{
		if(pTexture[nState])
		{
			const D3DXVECTOR2& kLocation = GetPosition();

			kContext.Add(pTexture[nState], kLocation, 1.0f, GetDimensions(), D3DCOLOR_XRGB(255, 255, 255));
			kContext.Flush();

			if(pFont && kText.Length() > 0)
			{
				int32 nOffsetX		= 0;
				int32 nOffsetY		= 0;

				if(nState == GuiButtonState_Down)
				{
					nOffsetX += 1;
					nOffsetY += 2;
				}

				const int32 nX		= (int32)(kLocation.x + 0.5f * kDimensions.x + 1) + nOffsetX;
				const int32 nY		= (int32)(kLocation.y + 0.5f * kDimensions.y + 0) + nOffsetY;

				RECT kRectangle;
				kRectangle.left		= nX;
				kRectangle.top		= nY;
				kRectangle.right	= nX;
				kRectangle.bottom	= nY;

				const uint32 nLength = kText.Length();

				pFont->DrawTextA(NULL, kText, nLength, &kRectangle, DT_CENTER | DT_VCENTER | DT_CALCRECT, kColor);
				pFont->DrawTextA(NULL, kText, nLength, &kRectangle, DT_CENTER | DT_VCENTER, kColor);
			}
		}

		GuiElement::Render(kContext);
	}
}


/*
 * SetTexture
 */
ErrorCode GuiButton::SetTexture(uint32 nState, const char* pName, bool bResize)
{
	ResourceManager* pResourceManager = NULL;

	ErrorCode eCode = pServiceProvider->GetService("ResourceManager", &pResourceManager);
	if(eCode == Error_Success)
	{
		if(nState < GuiButtonState_Count)
		{
			pTexture[nState] = NULL;

			eCode = pResourceManager->CreateTextureFromFile(pName, &pTexture[nState]);
			if(eCode == Error_Success)
			{
				if(bResize)
				{
					D3DSURFACE_DESC kDescriptor;
					pTexture[nState]->GetLevelDesc(0, &kDescriptor);

					kDimensions.x = (float)kDescriptor.Width;
					kDimensions.y = (float)kDescriptor.Height;
				}
			}
		}
	}

	return eCode;
}

/*
 * SetFont
 */
ErrorCode GuiButton::SetFont(const char* pName, uint32 nSize, uint32 nWeight)
{
	pFont = NULL;

	ResourceManager* pResourceManager = NULL;

	ErrorCode eCode = pServiceProvider->GetService("ResourceManager", &pResourceManager);
	if(eCode == Error_Success)
	{
		eCode = pResourceManager->CreateFontFromName(pName, nSize, nWeight, &pFont);
	}

	return eCode;
}

/*
 * SetText
 */
void GuiButton::SetText(const char* pText)
{
	kText = pText;
}

/*
 * OnMouseEnter
 */
void GuiButton::OnMouseEnter()
{
}

/*
 * OnMouseLeave
 */
void GuiButton::OnMouseLeave()
{
	if(!pInterface->IsCursorAcquiredBy(this))
	{
		nState = GuiButtonState_Normal;
	}
}

/*
 * OnMouseDown
 */
void GuiButton::OnMouseDown(uint32 nButton, float fX, float fY)
{
	pInterface->AcquireCursor(this);
	nState = GuiButtonState_Down;
}

/*
 * OnMouseUp
 */
void GuiButton::OnMouseUp(uint32 nButton, float fX, float fY)
{
	if(pInterface->IsCursorAcquiredBy(this))
	{
		pInterface->ReleaseCursor();

		if(Contains(fX, fY))
		{
			nState = GuiButtonState_Hover;

			GuiEventArguments kArguments(this);
			Fire(EventClick, kArguments);
		}
		else
		{
			nState = GuiButtonState_Normal;
		}
	}
}

/*
 * OnMouseMove
 */
void GuiButton::OnMouseMove(float fX, float fY)
{
	if(pInterface->IsCursorAcquiredBy(this))
	{
		if(Contains(fX, fY))
		{
			nState = GuiButtonState_Down;
		}
		else
		{
			nState = GuiButtonState_Hover;
		}
	}
	else
	{
		//NOTE: If we're not capturing the mouse we only get mouse
		//		move events when the cursor is inside the button bounds
		nState = GuiButtonState_Hover;
	}
}