changeset 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 b60cbf3fa894
children 2caa7c7e2cb5
files Assets/UI/Background03.PSD Assets/UI/PanelA.PSD Assets/UI/PanelB.PSD LightClone/Source/Bot.cpp LightClone/Source/Bot.h LightClone/Source/CameraController.h LightClone/Source/CodePanel.cpp LightClone/Source/CodePanel.h LightClone/Source/CodeSlot.cpp LightClone/Source/CodeSlot.h LightClone/Source/GuiElement.cpp LightClone/Source/GuiElement.h LightClone/Source/Loader.cpp LightClone/Source/Loader.h LightClone/Source/Program.cpp LightClone/Source/Program.h LightClone/Source/World.cpp LightClone/Source/World.h
diffstat 18 files changed, 233 insertions(+), 156 deletions(-) [+]
line wrap: on
line diff
Binary file Assets/UI/Background03.PSD has changed
Binary file Assets/UI/PanelA.PSD has changed
Binary file Assets/UI/PanelB.PSD has changed
--- a/LightClone/Source/Bot.cpp	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/Bot.cpp	Wed Sep 21 20:30:29 2011 -0700
@@ -8,6 +8,7 @@
 
 /*
  * DirectionAngle
+ *	Angles for the different directions the bot could be facing
  */
 const float DirectionAngle[] = {-1.0f * D3DX_PI / 2.0f, -4.0f * D3DX_PI / 2.0f, -3.0f * D3DX_PI / 2.0f, -2.0f * D3DX_PI / 2.0f};
 
@@ -92,6 +93,22 @@
 	kMachine.Reset();
 }
 
+/*
+ * SetPosition
+ */
+void Bot::SetPosition(const Position& kValue)
+{
+	kPosition = kValue;
+}
+
+/*
+ * SetDirection
+ */
+void Bot::SetDirection(uint32 nValue)
+{
+	kDirection = nValue;
+}
+
 /* 
  * GetWorldPosition
  */
@@ -159,14 +176,17 @@
 /*
  * AddFunction
  */
+/*
 void Bot::AddFunction(uint32 nFunction, uint32 nSize)
 {
 	kMachine.AddFunction((uint8)nFunction, (uint8)nSize * 2);
 }
+*/
 
 /*
  * Upload
  */
+/*
 ErrorCode Bot::Upload(Code* pCode, uint32 nCount)
 {
 	ErrorCode eCode = Error_Success;
@@ -194,6 +214,15 @@
 
 	return eCode;
 }
+*/
+
+/*
+ * GetVirtualMachine
+ */
+VirtualMachine& Bot::GetVirtualMachine()
+{
+	return kMachine;
+}
 
 /*
  * Update
@@ -396,6 +425,7 @@
 /*
  * Compile
  */
+/*
 ErrorCode Bot::Compile(Code* pInstance, uint8* pData, uint32 nSize)
 {
 	const uint32 nLength = pInstance->GetLength();
@@ -430,6 +460,7 @@
 
 	return Error_Success;
 }
+*/
 
 /*
  * SetupVertexBuffer
--- a/LightClone/Source/Bot.h	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/Bot.h	Wed Sep 21 20:30:29 2011 -0700
@@ -120,8 +120,6 @@
 	 */
 	BotSequencer kSequencer;
 
-public:
-
 	/*
 	 * kPosition
 	 */
@@ -169,6 +167,16 @@
 	 */
 	void Reset();
 
+	/*
+	 * SetPosition
+	 */
+	void SetPosition(const Position& kPosition);
+
+	/*
+	 * SetDirection
+	 */
+	void SetDirection(uint32 nDirection);
+
 	/* 
 	 * GetWorldPosition
 	 */
@@ -182,12 +190,17 @@
 	/*
 	 * AddFunction
 	 */
-	void AddFunction(uint32 nFunction, uint32 nSize);
+	//void AddFunction(uint32 nFunction, uint32 nSize);
 
 	/*
 	 * Upload
 	 */
-	ErrorCode Upload(Code* pCode, uint32 nCount);
+	//ErrorCode Upload(Code* pCode, uint32 nCount);
+
+	/*
+	 * GetVirtualMachine
+	 */
+	VirtualMachine& GetVirtualMachine();
 
 	/*
 	 * Update
@@ -204,7 +217,7 @@
 	/*
 	 * Compile
 	 */
-	ErrorCode Compile(Code* pCode, uint8* pData, uint32 nSize);
+	//ErrorCode Compile(Code* pCode, uint8* pData, uint32 nSize);
 
 	/*
 	 * SetupVertexBuffer
--- a/LightClone/Source/CameraController.h	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/CameraController.h	Wed Sep 21 20:30:29 2011 -0700
@@ -64,7 +64,7 @@
 	/*
 	 * CameraController
 	 */
-	CameraController(float fInitialDistance = 16.0f, float fInitialYaw = 0.306f, float fInitialPitch = 0.875f);
+	CameraController(float fInitialDistance = 10.0f, float fInitialYaw = 0.150f, float fInitialPitch = 0.575f);
 
 	/*
 	 * SetMode
--- a/LightClone/Source/CodePanel.cpp	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/CodePanel.cpp	Wed Sep 21 20:30:29 2011 -0700
@@ -5,11 +5,16 @@
 #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];
+	pSlot = new CodeSlot[nSizeX * nSizeY];
 }
 
 /*
@@ -31,13 +36,17 @@
 		{
 			for(uint32 j = 0; j < nSizeX; ++j)
 			{
-				pSlot[i * nSizeX + j] = new CodeSlot();
-				pSlot[i * nSizeX + j]->Initialize(pResourceManager);
-				pSlot[i * nSizeX + j]->SetTexture("Data\\Textures\\Slot.tga");
-				pSlot[i * nSizeX + j]->SetDimensions(fSlotSizeX, fSlotSizeY);
-				pSlot[i * nSizeX + j]->SetPosition(fSlotPadding + (fSlotSizeX + fSlotSpacing) * j, fSlotPadding + (fSlotSizeY + fSlotSpacing) * i);
+				const uint32 nIndex = i * nSizeX + j;
 
-				Add(pSlot[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]);
 			}
 		}
 	}
@@ -50,7 +59,7 @@
  */
 void CodePanel::SetSlot(uint32 nSlot, uint32 nAction)
 {
-	pSlot[nSlot]->SetAction(nAction);
+	pSlot[nSlot].SetAction(nAction);
 }
 
 /*
@@ -62,7 +71,7 @@
 
 	for(uint32 i = 0; i < nCount; ++i)
 	{
-		pSlot[i]->SetAction(Action_Default);
+		pSlot[i].SetAction(Action_Default);
 	}
 }
 
@@ -75,6 +84,24 @@
 
 	for(uint32 i = 0; i < nCount; ++i)
 	{
-		pSlot[i]->SetPermanent(bPermanent);
+		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);
 }
\ No newline at end of file
--- a/LightClone/Source/CodePanel.h	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/CodePanel.h	Wed Sep 21 20:30:29 2011 -0700
@@ -9,14 +9,41 @@
 #include "CodeSlot.h"
 
 /*
+ * ActionArguments
+ */
+struct ActionArguments : public GuiEventArguments
+{
+	/*
+	 * nSlot
+	 */
+	uint32 nSlot;
+
+	/*
+	 * ActionArguments
+	 */
+	ActionArguments(GuiElement* pSource, uint32 nCodeSlot) : GuiEventArguments(pSource), nSlot(nCodeSlot)
+	{
+	}
+};
+
+/*
  * CodePanel
  */
 class CodePanel : public GuiImage
 {
+public:
+
+	/*
+	 * EventAction
+	 */
+	static const char* EventAction;
+
+private:
+
 	/*
 	 * pSlot
 	 */
-	CodeSlot** pSlot;
+	CodeSlot* pSlot;
 
 	/*
 	 * nSizeX
@@ -59,6 +86,13 @@
 	 * SetPermanent
 	 */
 	void SetPermanent(bool bPermanent);
+
+private:
+
+	/*
+	 * OnActionDropped
+	 */
+	void OnActionDropped(GuiEventArguments& kArguments);
 };
 
 #endif //__CODEPANEL_H__
--- a/LightClone/Source/CodeSlot.cpp	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/CodeSlot.cpp	Wed Sep 21 20:30:29 2011 -0700
@@ -50,6 +50,22 @@
 }
 
 /*
+ * SetSlot
+ */
+void CodeSlot::SetSlot(uint32 nValue)
+{
+	nSlot = nValue;
+}
+
+/*
+ * GetSlot
+ */
+uint32 CodeSlot::GetSlot() const
+{
+	return nSlot;
+}
+
+/*
  * SetAction
  */
 void CodeSlot::SetAction(uint32 nValue)
@@ -136,5 +152,9 @@
  */
 void CodeSlot::OnDrop(GuiElement* pSource, float fX, float fY)
 {
+	//TODO: Need to make sure pSource is actually a CodeSlot
 	SetAction(((CodeSlot*)pSource)->nAction);
+
+	GuiEventArguments kArguments(this);
+	Fire(CodeSlot::EventDrop, kArguments);
 }
--- a/LightClone/Source/CodeSlot.h	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/CodeSlot.h	Wed Sep 21 20:30:29 2011 -0700
@@ -14,6 +14,11 @@
 class CodeSlot : public GuiImage
 {
 	/*
+	 * nSlot
+	 */
+	uint32 nSlot;
+
+	/*
 	 * nAction
 	 */
 	uint32 nAction;
@@ -41,6 +46,16 @@
 	virtual void Terminate();
 
 	/*
+	 * SetSlot
+	 */
+	void SetSlot(uint32 nSlot);
+
+	/*
+	 * GetSlot
+	 */
+	uint32 GetSlot() const;
+
+	/*
 	 * SetAction
 	 */
 	void SetAction(uint32 nAction);
--- a/LightClone/Source/GuiElement.cpp	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/GuiElement.cpp	Wed Sep 21 20:30:29 2011 -0700
@@ -5,6 +5,11 @@
 #include "GuiElement.h"
 
 /*
+ * EventDrop
+ */
+const char* GuiElement::EventDrop = "GuiElement:EventDrop";
+
+/*
  * GuiElement
  */
 GuiElement::GuiElement() : pContainer(NULL), kPosition(0.0f, 0.0f), kDimensions(0.0f, 0.0f), kColor(0xFFFFFFFF), nFlags(GuiElementFlag_Visible)
--- a/LightClone/Source/GuiElement.h	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/GuiElement.h	Wed Sep 21 20:30:29 2011 -0700
@@ -34,6 +34,11 @@
 public:
 
 	/*
+	 * EventDrop
+	 */
+	static const char* EventDrop;
+
+	/*
 	 * List
 	 */
 	typedef ArrayList<GuiElement*> List;
--- a/LightClone/Source/Loader.cpp	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/Loader.cpp	Wed Sep 21 20:30:29 2011 -0700
@@ -17,8 +17,6 @@
 	kPosition.X		= 0;
 	kPosition.Y		= 0;
 	kDirection		= Direction_North;
-	nFunctionCount	= 0;
-	pFunctionLength	= 0;
 }
 
 /*
@@ -37,12 +35,6 @@
 		delete[] pHeight;
 		pHeight = 0;
 	}
-
-	if(pFunctionLength)
-	{
-		delete[] pFunctionLength;
-		pFunctionLength = 0;
-	}
 }
 
 /*
@@ -62,13 +54,9 @@
 		pHeight = 0;
 	}
 
-	if(pFunctionLength)
-	{
-		delete[] pFunctionLength;
-		pFunctionLength = 0;
-	}
+	Buffer kBuffer = LoadFile(pName);
 
-	Buffer kBuffer = LoadFile(pName);
+	uint32 nTemp;
 
 	if(kBuffer.Read(&kSize.X) != Error_Success)
 		return Error_Fail;
@@ -85,7 +73,7 @@
 	if(kBuffer.Read(&kDirection) != Error_Success)
 		return Error_Fail;
 
-	if(kBuffer.Read(&nFunctionCount) != Error_Success)
+	if(kBuffer.Read(&nTemp) != Error_Success)
 		return Error_Fail;
 
 	if(kSize.X == 0 || kSize.Y == 0)
@@ -93,11 +81,11 @@
 
 	pType			= new uint32[kSize.X * kSize.Y];
 	pHeight			= new uint32[kSize.X * kSize.Y];
-	pFunctionLength	= new uint32[nFunctionCount];
 
-	for(uint32 i = 0; i < nFunctionCount; ++i)
+	for(uint32 i = 0; i < nTemp; ++i)
 	{
-		if(kBuffer.Read(pFunctionLength + i) != Error_Success)
+		uint32 nTemp2 = 0;
+		if(kBuffer.Read(&nTemp2) != Error_Success)
 			return Error_Fail;
 	}
 
@@ -157,19 +145,3 @@
 {
 	return kDirection;
 }
-
-/*
- * GetFunctionCount
- */
-uint32 Loader::GetFunctionCount() const
-{
-	return nFunctionCount;
-}
-
-/*
- * GetFunctionLength
- */
-uint32 Loader::GetFunctionLength(uint32 nIndex) const
-{
-	return pFunctionLength[nIndex];
-}
--- a/LightClone/Source/Loader.h	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/Loader.h	Wed Sep 21 20:30:29 2011 -0700
@@ -37,16 +37,6 @@
 	 */
 	Direction kDirection;
 
-	/*
-	 * nFunctionCount
-	 */
-	uint32 nFunctionCount;
-
-	/*
-	 * pFunctionLength
-	 */
-	uint32* pFunctionLength;
-
 public:
 
 	/*
@@ -88,16 +78,6 @@
 	 * GetInitialDirection
 	 */
 	const Direction& GetInitialDirection() const;
-
-	/*
-	 * GetFunctionCount
-	 */
-	uint32 GetFunctionCount() const;
-
-	/*
-	 * GetFunctionLength
-	 */
-	uint32 GetFunctionLength(uint32 nIndex) const;
 };
 
 #endif //__LOADER_H__
--- a/LightClone/Source/Program.cpp	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/Program.cpp	Wed Sep 21 20:30:29 2011 -0700
@@ -30,6 +30,28 @@
 }
 
 /*
+ * Initialize
+ */
+ErrorCode Program::Initialize()
+{
+	return Error_Success;
+}
+
+/*
+ * Terminate
+ */
+void Program::Terminate()
+{
+}
+
+/*
+ * Clear
+ */
+void Program::Clear()
+{
+}
+
+/*
  * Upload
  */
 ErrorCode Program::Upload(VirtualMachine& kMachine)
--- a/LightClone/Source/Program.h	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/Program.h	Wed Sep 21 20:30:29 2011 -0700
@@ -58,6 +58,21 @@
 	Program();
 
 	/*
+	 * Initialize
+	 */
+	ErrorCode Initialize();
+
+	/*
+	 * Terminate
+	 */
+	void Terminate();
+
+	/*
+	 * Clear
+	 */
+	void Clear();
+
+	/*
 	 * Upload
 	 */
 	ErrorCode Upload(VirtualMachine& kMachine);
--- a/LightClone/Source/World.cpp	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/World.cpp	Wed Sep 21 20:30:29 2011 -0700
@@ -13,10 +13,9 @@
  */
 World::World()
 {
-	nGameState				= GameState_Active;
-	nSimulationState		= SimulationState_Idle;
-	pFunction				= 0;
-	nCurrentLevel			= 0;
+	nGameState			= GameState_Active;
+	nSimulationState	= SimulationState_Idle;
+	nCurrentLevel		= 0;
 }
 
 /*
@@ -37,7 +36,7 @@
 			eCode = kBot.Initialize(pResourceManager);
 			if(eCode == Error_Success)
 			{
-				//eCode = kProgram.Initialize(pResourceManager);
+				eCode = kProgram.Initialize();
 				if(eCode == Error_Success)
 				{
 					eCode = InitializeInterface(pResourceManager);				
@@ -60,7 +59,7 @@
 void World::Terminate()
 {
 	kInterface.Terminate();
-	//kProgram.Terminate();
+	kProgram.Terminate();
 	kBot.Terminate();
 	kEnvironment.Terminate();
 }
@@ -97,12 +96,6 @@
 	ErrorCode eCode = kLoader.Load(pName);
 	if(eCode == Error_Success)
 	{
-		if(pFunction)
-		{
-			delete[] pFunction;
-			pFunction = NULL;
-		}
-
 		const Size& kSize = kLoader.GetSize();
 
 		eCode = kEnvironment.Setup(kSize.X, kSize.Y);
@@ -118,28 +111,8 @@
 			}
 
 			kBot.Setup(&kEnvironment);
-			kBot.kPosition	= kLoader.GetInitialPosition();
-			kBot.kDirection	= kLoader.GetInitialDirection();
-
-			const uint32 nCount = 2;//kLoader.GetFunctionCount();
-
-			pFunction = new Code[nCount + 1];
-			pFunction[0].Initialize(MainFunctionLength);
-			pFunction[0].Clear();
-
-			for(uint32 i = 0; i < nCount; ++i)
-			{
-				const uint32 nLength = kLoader.GetFunctionLength(i);
-
-				pFunction[i + 1].Initialize(nLength);
-				pFunction[i + 1].Clear();
-			}
-
-			for(uint32 i = 0; i < nCount; ++i)
-			{
-				//kProgram.Initialize(i, kLoader.GetFunctionLength(i));
-				//kProgram.Clear(i);
-			}
+			kBot.SetPosition(kLoader.GetInitialPosition());
+			kBot.SetDirection(kLoader.GetInitialDirection());
 		}
 	}
 
@@ -147,35 +120,6 @@
 }
 
 /*
- * ResetEnvironment
- */
-void World::ResetEnvironment()
-{
-	kEnvironment.Reset();
-}
-
-/*
- * ResetBot
- */
-void World::ResetBot()
-{
-	kBot.Reset();
-	kBot.kPosition	= kLoader.GetInitialPosition();
-	kBot.kDirection	= kLoader.GetInitialDirection();
-}
-
-/*
- * ResetCode
- */
-void World::ClearCode()
-{
-	for(uint32 i = 0; i < kLoader.GetFunctionCount(); ++i)
-	{
-		pFunction[i].Clear();
-	}
-}
-
-/*
  * Update
  */
 void World::Update(float fElapsed)
@@ -478,10 +422,13 @@
 {
 	if(nSimulationState == SimulationState_Idle)
 	{
-		ResetEnvironment();
-		ResetBot();
+		kEnvironment.Reset();
 
-		kBot.Upload(pFunction, kLoader.GetFunctionCount() + 1);
+		kBot.Reset();
+		kBot.SetPosition(kLoader.GetInitialPosition());
+		kBot.SetDirection(kLoader.GetInitialDirection());
+
+		kProgram.Upload(kBot.GetVirtualMachine());
 
 		nSimulationState = SimulationState_Active;
 	}
@@ -494,8 +441,11 @@
 {
 	if(nSimulationState == SimulationState_Active)
 	{
-		ResetEnvironment();
-		ResetBot();
+		kEnvironment.Reset();
+
+		kBot.Reset();
+		kBot.SetPosition(kLoader.GetInitialPosition());
+		kBot.SetDirection(kLoader.GetInitialDirection());
 
 		nSimulationState = SimulationState_Idle;
 	}
@@ -506,6 +456,8 @@
  */
 void World::OnReset(GuiEventArguments& kArguments)
 {
+	kProgram.Clear();
+
 	pMain->Clear();
 	pFunctionA->Clear();
 	pFunctionB->Clear();
--- a/LightClone/Source/World.h	Wed Sep 21 12:18:15 2011 -0700
+++ b/LightClone/Source/World.h	Wed Sep 21 20:30:29 2011 -0700
@@ -19,6 +19,7 @@
 #include "GuiImage.h"
 #include "GuiButton.h"
 #include "CodePanel.h"
+#include "Program.h"
 
 /*
  * World
@@ -51,9 +52,9 @@
 	Bot kBot;
 
 	/*
-	 * kFunction
+	 * kProgram
 	 */
-	Code* pFunction;
+	Program kProgram;
 
 	/*
 	 * nGameState
@@ -183,21 +184,6 @@
 	bool Load(const char* pName);
 
 	/*
-	 * ResetEnvironment
-	 */
-	void ResetEnvironment();
-
-	/*
-	 * ResetBot
-	 */
-	void ResetBot();
-
-	/*
-	 * ResetCode
-	 */
-	void ClearCode();
-
-	/*
 	 * Update
 	 */
 	void Update(float fElapsed);