view LightClone/Source/CodePanel.cpp @ 37:58a16d529d95

Refactoring code and adding events for drag and drop
author koryspansel <koryspansel@bendbroadband.com>
date Wed, 21 Sep 2011 20:30:29 -0700
parents 7f5feeb3f604
children 2caa7c7e2cb5
line wrap: on
line source

/*
 * CodePanel
 */

#include "CodePanel.h"

/*
 * EventAction
 */
const char* CodePanel::EventAction = "CodePanel:EventAction";

/*
 * CodePanel
 */
CodePanel::CodePanel(uint32 nWidth, uint32 nHeight) : GuiImage(), nSizeX(nWidth), nSizeY(nHeight)
{
	pSlot = new CodeSlot[nSizeX * nSizeY];
}

/*
 * Initialize
 */
ErrorCode CodePanel::Initialize(ResourceManager* pResourceManager)
{
	const float fSlotSizeX		= 48.0f;
	const float fSlotSizeY		= 48.0f;
	const float fSlotPadding	= 16.0f;
	const float fSlotSpacing	= 8.0f;

	ErrorCode eCode = GuiImage::Initialize(pResourceManager);
	if(eCode == Error_Success)
	{
		SetDimensions(2.0f * fSlotPadding + (fSlotSizeX + fSlotSpacing) * nSizeX - fSlotSpacing, 2.0f * fSlotPadding + (fSlotSizeY + fSlotSpacing) * nSizeY - fSlotSpacing);

		for(uint32 i = 0; i < nSizeY; ++i)
		{
			for(uint32 j = 0; j < nSizeX; ++j)
			{
				const uint32 nIndex = i * nSizeX + j;

				pSlot[nIndex].Initialize(pResourceManager);
				pSlot[nIndex].SetTexture("Data\\Textures\\Slot.tga");
				pSlot[nIndex].SetDimensions(fSlotSizeX, fSlotSizeY);
				pSlot[nIndex].SetPosition(fSlotPadding + (fSlotSizeX + fSlotSpacing) * j, fSlotPadding + (fSlotSizeY + fSlotSpacing) * i);
				pSlot[nIndex].Subscribe(CodeSlot::EventDrop, &CodePanel::OnActionDropped, this);
				pSlot[nIndex].SetSlot(nIndex);
				pSlot[nIndex].SetAction(Action_Default);

				Add(&pSlot[nIndex]);
			}
		}
	}

	return eCode;
}

/*
 * SetSlot
 */
void CodePanel::SetSlot(uint32 nSlot, uint32 nAction)
{
	pSlot[nSlot].SetAction(nAction);
}

/*
 * Clear
 */
void CodePanel::Clear()
{
	const uint32 nCount = nSizeX * nSizeY;

	for(uint32 i = 0; i < nCount; ++i)
	{
		pSlot[i].SetAction(Action_Default);
	}
}

/*
 * SetPermanent
 */
void CodePanel::SetPermanent(bool bPermanent)
{
	const uint32 nCount = nSizeX * nSizeY;

	for(uint32 i = 0; i < nCount; ++i)
	{
		pSlot[i].SetPermanent(bPermanent);
	}
}

/*
 * OnActionDropped
 */
void CodePanel::OnActionDropped(GuiEventArguments& kArguments)
{
	CodeSlot* pSlot = (CodeSlot*)kArguments.pSource;
	//ASSERT(pSlot != NULL);

	const uint32 nIndex = pSlot->GetSlot();
	//if(nIndex < nActionSlots)
	{
		ActionArguments kActionArguments(this, nIndex);
		Fire(CodePanel::EventAction, kActionArguments);
	}

	Fire(CodePanel::EventDrop, kArguments);
}