view LightClone/Source/CodeSlot.cpp @ 33:06b151afc8d0

Cleaned up UI art; Completed drag and drop; Centered grid
author koryspansel <koryspansel@bendbroadband.com>
date Wed, 21 Sep 2011 00:50:20 -0700
parents c227be6a15fe
children 58a16d529d95
line wrap: on
line source

/*
 * CodeSlot
 */

#include "CodeSlot.h"
#include "GuiInterface.h"
#include "InputManager.h"

/*
 * ActionTextureName
 */
const char* ActionTextureName[] = 
{
	"Data\\Textures\\Forward.tga",
	"Data\\Textures\\RotateCW.tga",
	"Data\\Textures\\RotateCCW.tga",
	"Data\\Textures\\Jump.tga",
	"Data\\Textures\\Light.tga",
	"Data\\Textures\\Function1.tga",
	"Data\\Textures\\Function2.tga",
};

/*
 * CodeSlot
 */
CodeSlot::CodeSlot() : GuiImage(), nAction(Action_Default), bPermanent(false)
{
	SetFlag(GuiElementFlag_Pickable);
}

/*
 * Initialize
 */
ErrorCode CodeSlot::Initialize(ResourceManager* pManager)
{
	ErrorCode eCode = GuiImage::Initialize(pManager);
	if(eCode == Error_Success)
	{
		SetTexture("Data\\Textures\\Slot.tga");
	}

	return eCode;
}

/*
 * Terminate
 */
void CodeSlot::Terminate()
{
}

/*
 * SetAction
 */
void CodeSlot::SetAction(uint32 nValue)
{
	nAction = nValue;

	SetTexture(nAction == Action_Default ? "Data\\Textures\\Slot.tga" : ActionTextureName[nAction - Action_Forward]);
}

/*
 * GetAction
 */
uint32 CodeSlot::GetAction() const
{
	return nAction;
}

/*
 * SetPermanent
 */
void CodeSlot::SetPermanent(bool bValue)
{
	bPermanent = bValue;
}

/*
 * OnMouseDown
 */
void CodeSlot::OnMouseDown(uint32 nButton, float fX, float fY)
{
	if(nButton == MouseButton_Left)
	{
		pInterface->AcquireCursor(this);
	}
}

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

/*
 * OnMouseMove
 */
void CodeSlot::OnMouseMove(float fX, float fY)
{
	if(pInterface->IsCursorAcquiredBy(this))
	{
		pInterface->ReleaseCursor();

		if(nAction != Action_Default)
		{
			//TODO: Figure out some other way of doing this

			// create a new element to represent the object being dragged
			CodeSlot* pInstance = new CodeSlot();
			pInstance->Initialize(pResourceManager);
			pInstance->SetAction(nAction);
			pInstance->SetDimensions(kDimensions);
			pInstance->SetPosition(kPosition);
			pInstance->SetInterface(pInterface);

			// let the interface and cursor manage the drag object
			pInterface->BeginDrag(pInstance, MouseButton_Left);

			// if this slot is always suppose to have a valid action, do
			// not clear out the current action
			if(!bPermanent)
			{
				SetAction(Action_Default);
			}
		}
	}
}

/*
 * OnDrop
 */
void CodeSlot::OnDrop(GuiElement* pSource, float fX, float fY)
{
	SetAction(((CodeSlot*)pSource)->nAction);
}