changeset 20:4e9b5299ffdc

Added GuiCursor and fixed picking
author koryspansel <koryspansel@bendbroadband.com>
date Thu, 15 Sep 2011 13:28:10 -0700
parents 51718795f019
children b4dc5d674e22
files LightClone/LightClone.vcproj LightClone/Source/GuiCursor.cpp LightClone/Source/GuiCursor.h LightClone/Source/GuiElement.cpp LightClone/Source/GuiInterface.cpp LightClone/Source/GuiInterface.h LightClone/Source/RenderContext.cpp LightClone/Source/World.cpp
diffstat 8 files changed, 420 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/LightClone/LightClone.vcproj	Thu Sep 15 12:13:40 2011 -0700
+++ b/LightClone/LightClone.vcproj	Thu Sep 15 13:28:10 2011 -0700
@@ -245,6 +245,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\GuiCursor.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\GuiElement.cpp"
 				>
 			</File>
@@ -391,6 +395,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\GuiCursor.h"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\GuiElement.h"
 				>
 			</File>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/GuiCursor.cpp	Thu Sep 15 13:28:10 2011 -0700
@@ -0,0 +1,157 @@
+/*
+ * GuiCursor
+ */
+
+#include "GuiCursor.h"
+#include "VertexTypes.h"
+
+/*
+ * GuiCursor
+ */
+GuiCursor::GuiCursor() : pEffect(NULL), pVertexBuffer(NULL), pTexture(NULL)
+{
+}
+
+/*
+ * Initialize
+ */
+ErrorCode GuiCursor::Initialize(ResourceManager* pManager)
+{
+	pResourceManager = pManager;
+
+	ErrorCode eCode = pResourceManager->CreateEffectFromFile("Data\\Shaders\\TexturedQuad.fx", &pEffect);
+	if(eCode == Error_Success)
+	{
+		eCode = pResourceManager->CreateVertexBuffer(6 * sizeof(Vertex::Quad), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pVertexBuffer);
+		if(eCode == Error_Success)
+		{
+			eCode = SetupVertexBuffer();
+		}
+	}
+
+	return eCode;
+}
+
+/*
+ * Terminate
+ */
+void GuiCursor::Terminate()
+{
+	if(pTexture)
+	{
+		pTexture->Release();
+		pTexture = NULL;
+	}
+
+	if(pVertexBuffer)
+	{
+		pVertexBuffer->Release();
+		pVertexBuffer = NULL;
+	}
+
+	if(pEffect)
+	{
+		pEffect->Release();
+		pEffect = NULL;
+	}
+}
+
+/*
+ * Update
+ */
+void GuiCursor::Update(float fElapsed)
+{
+}
+
+/*
+ * Render
+ */
+void GuiCursor::Render(RenderContext& kContext, Camera& kCamera)
+{
+	if(pTexture)
+	{
+		uint32 nPasses = 0;
+
+		const float fOffsetX = -0.5f * ScreenSizeX;
+		const float fOffsetY = +0.5f * ScreenSizeY;
+
+		const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f);
+
+		kContext.ApplyCameraToEffect(kCamera, pEffect);
+
+		pEffect->SetTechnique(pEffect->GetTechnique(0));
+		pEffect->Begin(&nPasses, 0);
+		pEffect->BeginPass(0);
+
+		D3DXMATRIX kScale;
+		D3DXMatrixScaling(&kScale, kDimensions.x, kDimensions.y, 1.0f);
+
+		D3DXMATRIX kTranslate;
+		D3DXMatrixTranslation(&kTranslate, fOffsetX + kPosition.x + 0.5f, fOffsetY - kPosition.y + 0.5f, 0.0f);
+
+		D3DXMATRIX kWorldMatrix;
+		D3DXMatrixMultiply(&kWorldMatrix, &kScale, &kTranslate);
+
+		pEffect->SetMatrix(pEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
+		pEffect->SetVector(pEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
+		pEffect->SetTexture(pEffect->GetParameterByName(NULL, "kTexture"), pTexture);
+		pEffect->CommitChanges();
+
+		kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace);
+
+		pEffect->EndPass();
+		pEffect->End();
+	}
+}
+
+/*
+ * SetTexture
+ */
+ErrorCode GuiCursor::SetTexture(const char* pName, bool bResize)
+{
+	if(pTexture)
+	{
+		pTexture->Release();
+		pTexture = NULL;
+	}
+
+	ErrorCode eCode = pResourceManager->CreateTextureFromFile(pName, &pTexture);
+	if(eCode == Error_Success)
+	{
+		if(bResize)
+		{
+			D3DSURFACE_DESC kDescriptor;
+			pTexture->GetLevelDesc(0, &kDescriptor);
+
+			kDimensions.x = (float)kDescriptor.Width;
+			kDimensions.y = (float)kDescriptor.Height;
+		}
+	}
+
+	return eCode;
+}
+
+/*
+ * SetupVertexBuffer
+ */
+ErrorCode GuiCursor::SetupVertexBuffer()
+{
+	Vertex::Quad* pVertices = NULL;
+
+	HRESULT hResult = pVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
+	if(FAILED(hResult))
+	{
+		return Error_Fail;
+	}
+
+	pVertices[0]	= Vertex::Quad(+0.0f, -1.0f, 1.0f, 0.0f, 1.0f);
+	pVertices[1]	= Vertex::Quad(+0.0f, +0.0f, 1.0f, 0.0f, 0.0f);
+	pVertices[2]	= Vertex::Quad(+1.0f, +0.0f, 1.0f, 1.0f, 0.0f);
+	pVertices[3]	= Vertex::Quad(+0.0f, -1.0f, 1.0f, 0.0f, 1.0f);
+	pVertices[4]	= Vertex::Quad(+1.0f, +0.0f, 1.0f, 1.0f, 0.0f);
+	pVertices[5]	= Vertex::Quad(+1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
+
+	pVertexBuffer->Unlock();
+
+	return Error_Success;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/GuiCursor.h	Thu Sep 15 13:28:10 2011 -0700
@@ -0,0 +1,76 @@
+/*
+ * GuiCursor
+ */
+
+#ifndef __GUICURSOR_H__
+#define __GUICURSOR_H__
+
+#include "Core.h"
+#include "GuiElement.h"
+
+/*
+ * GuiCursor
+ */
+class GuiCursor : public GuiElement
+{
+	/*
+	 * pResourceManager
+	 */
+	ResourceManager* pResourceManager;
+
+	/*
+	 * pEffect
+	 */
+	ID3DXEffect* pEffect;
+
+	/*
+	 * pVertexBuffer
+	 */
+	IDirect3DVertexBuffer9* pVertexBuffer;
+
+	/*
+	 * pTexture
+	 */
+	IDirect3DTexture9* pTexture;
+
+public:
+
+	/*
+	 * GuiCursor
+	 */
+	GuiCursor();
+
+	/*
+	 * Initialize
+	 */
+	virtual ErrorCode Initialize(ResourceManager* pResourceManager);
+
+	/*
+	 * Terminate
+	 */
+	virtual void Terminate();
+
+	/*
+	 * Update
+	 */
+	virtual void Update(float fElapsed);
+
+	/*
+	 * Render
+	 */
+	virtual void Render(RenderContext& kContext, Camera& kCamera);
+
+	/*
+	 * SetTexture
+	 */
+	ErrorCode SetTexture(const char* pName, bool bResize = true);
+
+private:
+
+	/*
+	 * SetupVertexBuffer
+	 */
+	ErrorCode SetupVertexBuffer();
+};
+
+#endif //__GUICURSOR_H__
--- a/LightClone/Source/GuiElement.cpp	Thu Sep 15 12:13:40 2011 -0700
+++ b/LightClone/Source/GuiElement.cpp	Thu Sep 15 13:28:10 2011 -0700
@@ -7,7 +7,7 @@
 /*
  * GuiElement
  */
-GuiElement::GuiElement()
+GuiElement::GuiElement() : pContainer(NULL), kPosition(0.0f, 0.0f), kDimensions(0.0f, 0.0f)
 {
 }
 
@@ -136,9 +136,31 @@
  */
 GuiElement* GuiElement::Pick(float fX, float fY)
 {
-	const D3DXVECTOR2& kLocation = GetPosition();
+	GuiElement* pElement = NULL;
+
+	for(uint32 i = 0; i < kChildren.Size() && !pElement; ++i)
+	{
+		const D3DXVECTOR2& kLocation	= kChildren[i]->GetPosition();
+		const D3DXVECTOR2& kSize		= kChildren[i]->GetDimensions();
+
+		if(Rectangle2(kLocation.x, kLocation.y, kSize.x, kSize.y).Contains(fX, fY))
+		{
+			pElement = kChildren[i];
+		}
+	}
 
-	return Rectangle2(kLocation.y, kLocation.y, kDimensions.x, kDimensions.y).Contains(fX, fY) ? this : NULL;
+	if(!pElement)
+	{
+		const D3DXVECTOR2& kLocation	= GetPosition();
+		const D3DXVECTOR2& kSize		= GetDimensions();
+
+		if(Rectangle2(kLocation.x, kLocation.y, kSize.x, kSize.y).Contains(fX, fY))
+		{
+			pElement = this;
+		}
+	}
+
+	return pElement;
 }
 
 /*
--- a/LightClone/Source/GuiInterface.cpp	Thu Sep 15 12:13:40 2011 -0700
+++ b/LightClone/Source/GuiInterface.cpp	Thu Sep 15 13:28:10 2011 -0700
@@ -9,6 +9,7 @@
  */
 GuiInterface::GuiInterface() : pInputManager(NULL), pDragSource(NULL)
 {
+	pCursor = new GuiCursor();
 }
 
 /*
@@ -19,8 +20,19 @@
 	ErrorCode eCode = GuiElement::Initialize(pResourceManager);
 	if(eCode == Error_Success)
 	{
-		pInputManager = pInput;
-		//ASSERT(pInputManager);
+		eCode = pCursor->Initialize(pResourceManager);
+		if(eCode == Error_Success)
+		{
+			eCode = pCursor->SetTexture("Data\\Textures\\Arrow.tga");
+			if(eCode == Error_Success)
+			{
+				pInputManager = pInput;
+				//ASSERT(pInputManager);
+
+				pInputManager->SetBounds(0.0f, 0.0f, (float)ScreenSizeX, (float)ScreenSizeY);
+				pInputManager->SetMouse(0.5f * ScreenSizeX, 0.5f * ScreenSizeY);
+			}
+		}
 	}
 
 	return eCode;
@@ -44,6 +56,16 @@
 
 	GuiElement::Update(fElapsed);
 
+	if(pCursor)
+	{
+		char kBuffer[128];
+		sprintf(kBuffer, "<%0.2f, %0.2f>\n", fX, fY);
+		OutputDebugStringA(kBuffer);
+
+		pCursor->SetPosition(fX, fY);
+		pCursor->Update(fElapsed);
+	}
+
 	if(pInputManager->IsButtonDown(MouseButton_Left) && !pInputManager->WasButtonDown(MouseButton_Left))
 	{
 		GuiElement* pElement = Pick(fX, fY);
@@ -84,3 +106,16 @@
 		}
 	}
 }
+
+/*
+ * Render
+ */
+void GuiInterface::Render(RenderContext& kContext, Camera& kCamera)
+{
+	GuiElement::Render(kContext, kCamera);
+
+	if(pCursor)
+	{
+		pCursor->Render(kContext, kCamera);
+	}
+}
--- a/LightClone/Source/GuiInterface.h	Thu Sep 15 12:13:40 2011 -0700
+++ b/LightClone/Source/GuiInterface.h	Thu Sep 15 13:28:10 2011 -0700
@@ -7,6 +7,7 @@
 
 #include "Core.h"
 #include "GuiElement.h"
+#include "GuiCursor.h"
 #include "InputManager.h"
 
 /*
@@ -24,6 +25,11 @@
 	 */
 	GuiElement* pDragSource;
 
+	/*
+	 * pCursor
+	 */
+	GuiCursor* pCursor;
+
 public:
 
 	/*
@@ -46,8 +52,16 @@
 	 */
 	void Update(float fElapsed);
 
+	/*
+	 * Render
+	 */
+	void Render(RenderContext& kContext, Camera& kCamera);
+
 private:
 
+	/*
+	 * Initialize
+	 */
 	using GuiElement::Initialize;
 };
 
--- a/LightClone/Source/RenderContext.cpp	Thu Sep 15 12:13:40 2011 -0700
+++ b/LightClone/Source/RenderContext.cpp	Thu Sep 15 13:28:10 2011 -0700
@@ -128,15 +128,11 @@
 		D3DXHANDLE kHandleProj	= pEffect->GetParameterByName(NULL, pProjection);
 		D3DXHANDLE kHandleView	= pEffect->GetParameterByName(NULL, pView);
 
-		if(kHandleProj && kHandleView)
-		{
-			const D3DXMATRIX& kProjectionMatrix	= kCamera.GetProjection(kViewport);
-			const D3DXMATRIX& kViewMatrix		= kCamera.GetView();
+		if(kHandleProj)
+			pEffect->SetMatrix(kHandleProj, &kCamera.GetProjection(kViewport));
 
-			// apply parameters to shader
-			pEffect->SetMatrix(kHandleProj, &kProjectionMatrix);
-			pEffect->SetMatrix(kHandleView, &kViewMatrix);
-		}
+		if(kHandleView)
+			pEffect->SetMatrix(kHandleView, &kCamera.GetView());
 	}
 }
 
--- a/LightClone/Source/World.cpp	Thu Sep 15 12:13:40 2011 -0700
+++ b/LightClone/Source/World.cpp	Thu Sep 15 13:28:10 2011 -0700
@@ -5,6 +5,7 @@
 #include "World.h"
 #include "VertexTypes.h"
 #include "GuiLabel.h"
+#include "GuiImage.h"
 
 /*
  * World
@@ -278,105 +279,119 @@
  */
 ErrorCode World::InitializeInterface(ResourceManager* pResourceManager)
 {
-	GuiLabel*
-	pLabel = new GuiLabel();
-	pLabel->Initialize(pResourceManager);
-	pLabel->SetFont("Courier New", 16);
-	pLabel->SetText("This is a test!");
-	pLabel->SetFlags(GuiLabelFlag_CenterX | GuiLabelFlag_CenterY);
-	pLabel->SetPosition(0.5f * ScreenSizeX, 0.5f * ScreenSizeY);
+	ErrorCode eCode = kInterface.Initialize(pResourceManager, pInputManager);
+	if(eCode == Error_Success)
+	{
+		/*
+		GuiLabel*
+		pLabel = new GuiLabel();
+		pLabel->Initialize(pResourceManager);
+		pLabel->SetFont("Courier New", 16);
+		pLabel->SetText("This is a test!");
+		pLabel->SetFlags(GuiLabelFlag_CenterX | GuiLabelFlag_CenterY);
+		pLabel->SetPosition(0.5f * ScreenSizeX, 0.5f * ScreenSizeY);
+		*/
 
-	/*
-	//CodePanel* 
-	pToolbar = new CodePanel(8);
-	pToolbar->Initialize(pResourceManager);
-	pToolbar->SetBackgroundTexture("Data\\Textures\\CodePanel.tga");
-	pToolbar->SetSlotTexture("Data\\Textures\\Slot.tga");
+		GuiImage*
+		pImage = new GuiImage();
+		pImage->Initialize(pResourceManager);
+		pImage->SetTexture("Data\\Textures\\Slot.tga");
+		pImage->SetDimensions(48.0f, 48.0f);
+		pImage->SetPosition(0.5f * ScreenSizeX, 0.5f * ScreenSizeY);
 
-	//CodePanel* 
-	pMain = new CodePanel(16);
-	pMain->SetBackgroundTexture("Data\\Textures\\CodePanel.tga");
-	pMain->SetSlotTexture("Data\\Textures\\Slot.tga");
-	pMain->Subscribe(CodePanel::Drop, &World::OnDrop, this);
+		//kInterface.Add(pLabel);
+		kInterface.Add(pImage);
 
-	//CodePanel* 
-	pFunctionA = new CodePanel(16);
-	pFunctionA->SetBackgroundTexture("Data\\Textures\\CodePanel.tga");
-	pFunctionA->SetSlotTexture("Data\\Textures\\Slot.tga");
-	pFunctionA->Subscribe(CodePanel::Drop, &World::OnDrop, this);
+		/*
+		//CodePanel* 
+		pToolbar = new CodePanel(8);
+		pToolbar->Initialize(pResourceManager);
+		pToolbar->SetBackgroundTexture("Data\\Textures\\CodePanel.tga");
+		pToolbar->SetSlotTexture("Data\\Textures\\Slot.tga");
 
-	//CodePanel* 
-	pFunctionB = new CodePanel(16);
-	pFunctionB->SetBackgroundTexture("Data\\Textures\\CodePanel.tga");
-	pFunctionB->SetSlotTexture("Data\\Textures\\Slot.tga");
-	pFunctionB->Subscribe(CodePanel::Drop, &World::OnDrop, this);
+		//CodePanel* 
+		pMain = new CodePanel(16);
+		pMain->SetBackgroundTexture("Data\\Textures\\CodePanel.tga");
+		pMain->SetSlotTexture("Data\\Textures\\Slot.tga");
+		pMain->Subscribe(CodePanel::Drop, &World::OnDrop, this);
 
-	//GuiButton*
-	pButtonStart = new GuiButton();
-	pButtonStart->SetTexture("Data\\Textures\\Play.tga");
-	pButtonStart->SetDimensions(...);
-	pButtonStart->Subscribe(GuiButton::Click, &World::OnClick, this);
+		//CodePanel* 
+		pFunctionA = new CodePanel(16);
+		pFunctionA->SetBackgroundTexture("Data\\Textures\\CodePanel.tga");
+		pFunctionA->SetSlotTexture("Data\\Textures\\Slot.tga");
+		pFunctionA->Subscribe(CodePanel::Drop, &World::OnDrop, this);
 
-	//GuiButton*
-	pButtonStop = new GuiButton();
-	pButtonStop->SetTexture("Data\\Textures\\Stop.tga");
-	pButtonStop->SetDimensions(...);
-	pButtonStart->Subscribe(GuiButton::Click, &World::OnClick, this);
+		//CodePanel* 
+		pFunctionB = new CodePanel(16);
+		pFunctionB->SetBackgroundTexture("Data\\Textures\\CodePanel.tga");
+		pFunctionB->SetSlotTexture("Data\\Textures\\Slot.tga");
+		pFunctionB->Subscribe(CodePanel::Drop, &World::OnDrop, this);
+
+		//GuiButton*
+		pButtonStart = new GuiButton();
+		pButtonStart->SetTexture("Data\\Textures\\Play.tga");
+		pButtonStart->SetDimensions(...);
+		pButtonStart->Subscribe(GuiButton::Click, &World::OnClick, this);
 
-	//GuiButton*
-	pButtonReset = new GuiButton();
-	pButtonReset->SetTexture("Data\\Textures\\Reset.tga");
-	pButtonReset->SetDimensions(...);
-	pButtonStart->Subscribe(GuiButton::Click, &World::OnClick, this);
+		//GuiButton*
+		pButtonStop = new GuiButton();
+		pButtonStop->SetTexture("Data\\Textures\\Stop.tga");
+		pButtonStop->SetDimensions(...);
+		pButtonStart->Subscribe(GuiButton::Click, &World::OnClick, this);
 
-	//GuiButton*
-	pButtonExit = new GuiButton();
-	pButtonExit->SetTexture("Data\\Textures\\Exit.tga");
-	pButtonExit->SetDimensions(...);
-	pButtonStart->Subscribe(GuiButton::Click, &World::OnClick, this);
+		//GuiButton*
+		pButtonReset = new GuiButton();
+		pButtonReset->SetTexture("Data\\Textures\\Reset.tga");
+		pButtonReset->SetDimensions(...);
+		pButtonStart->Subscribe(GuiButton::Click, &World::OnClick, this);
 
-	//GuiPanel*
-	pControlPanel = new GuiPanel();
-	pControlPanel->SetTexture("Data\\Textures\\ControlPanel.tga")
-	pControlPanel->SetDimensions(...);
-	pControlPanel->Add(pButtonStart);
-	pControlPanel->Add(pButtonStop);
-	pControlPanel->Add(pButtonReset);
-	pControlPanel->Add(pButtonExit);
+		//GuiButton*
+		pButtonExit = new GuiButton();
+		pButtonExit->SetTexture("Data\\Textures\\Exit.tga");
+		pButtonExit->SetDimensions(...);
+		pButtonStart->Subscribe(GuiButton::Click, &World::OnClick, this);
 
-	//GuiDialog*
-	pLevelDialog = new GuiDialog();
-	pLevelDialog->AddButton(DialogButton_Ok, "Ok");
-	pLevelDialog->SetVisible(false);
+		//GuiPanel*
+		pControlPanel = new GuiPanel();
+		pControlPanel->SetTexture("Data\\Textures\\ControlPanel.tga")
+		pControlPanel->SetDimensions(...);
+		pControlPanel->Add(pButtonStart);
+		pControlPanel->Add(pButtonStop);
+		pControlPanel->Add(pButtonReset);
+		pControlPanel->Add(pButtonExit);
 
-	//GuiDialog*
-	pGameDialog = new GuiDialog();
-	pGameDialog->AddButton(DialogButton_Ok, "Ok");
-	pGameDialog->SetVisible(false);
+		//GuiDialog*
+		pLevelDialog = new GuiDialog();
+		pLevelDialog->AddButton(DialogButton_Ok, "Ok");
+		pLevelDialog->SetVisible(false);
 
-	//GuiDialog*
-	pConfirmDialog = new GuiDialog();
-	pConfirmDialog->AddButton(DialogButton_Yes, "Yes");
-	pConfirmDialog->AddButton(DialogButton_No, "No");
-	pConfirmDialog->SetVisible(false);
+		//GuiDialog*
+		pGameDialog = new GuiDialog();
+		pGameDialog->AddButton(DialogButton_Ok, "Ok");
+		pGameDialog->SetVisible(false);
+
+		//GuiDialog*
+		pConfirmDialog = new GuiDialog();
+		pConfirmDialog->AddButton(DialogButton_Yes, "Yes");
+		pConfirmDialog->AddButton(DialogButton_No, "No");
+		pConfirmDialog->SetVisible(false);
 
-	//GuiPanel* 
-	pContainerPanel = new GuiPanel();
-	pContainerPanel->SetBackgroundTexture("Data\\Texturse\\ContainerPanel.tga");
-	pContainerPanel->Add(pToolbar);
-	pContainerPanel->Add(pMain);
-	pContainerPanel->Add(pSelectorPanel);
-	pContainerPanel->Add(pControlPanel);
+		//GuiPanel* 
+		pContainerPanel = new GuiPanel();
+		pContainerPanel->SetBackgroundTexture("Data\\Texturse\\ContainerPanel.tga");
+		pContainerPanel->Add(pToolbar);
+		pContainerPanel->Add(pMain);
+		pContainerPanel->Add(pSelectorPanel);
+		pContainerPanel->Add(pControlPanel);
 
-	pRoot->Add(pControlPanel);
-	pRoot->Add(pLevelDialog);
-	pRoot->Add(pGameDialog);
-	pRoot->Add(pConfirmDialog);
-	*/
+		pRoot->Add(pControlPanel);
+		pRoot->Add(pLevelDialog);
+		pRoot->Add(pGameDialog);
+		pRoot->Add(pConfirmDialog);
+		*/
+	}
 
-	kInterface.Add(pLabel);
-
-	return Error_Success;
+	return eCode;
 }
 
 /*