changeset 18:33cb6979ac51

More work on GUI system
author koryspansel
date Wed, 14 Sep 2011 17:24:49 -0700
parents 4951acfe92fc
children 51718795f019
files LightClone/LightClone.vcproj LightClone/Source/CodePanel.cpp LightClone/Source/CodePanel.h LightClone/Source/GuiButton.cpp LightClone/Source/GuiButton.h LightClone/Source/GuiContainer.cpp LightClone/Source/GuiContainer.h LightClone/Source/GuiElement.cpp LightClone/Source/GuiElement.h LightClone/Source/GuiImage.cpp LightClone/Source/GuiImage.h LightClone/Source/GuiInterface.cpp LightClone/Source/GuiLabel.cpp LightClone/Source/GuiLabel.h LightClone/Source/World.cpp
diffstat 15 files changed, 360 insertions(+), 221 deletions(-) [+]
line wrap: on
line diff
--- a/LightClone/LightClone.vcproj	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/LightClone.vcproj	Wed Sep 14 17:24:49 2011 -0700
@@ -201,6 +201,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\CodePanel.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\Core.cpp"
 				>
 			</File>
@@ -249,10 +253,18 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\GuiImage.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\GuiInterface.cpp"
 				>
 			</File>
 			<File
+				RelativePath=".\Source\GuiLabel.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\InputManager.cpp"
 				>
 			</File>
@@ -339,6 +351,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\CodePanel.h"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\Core.h"
 				>
 			</File>
@@ -387,11 +403,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\Source\GuiInterface.h"
+				RelativePath=".\Source\GuiImage.h"
 				>
 			</File>
 			<File
-				RelativePath=".\Source\GuiLabel.cpp"
+				RelativePath=".\Source\GuiInterface.h"
 				>
 			</File>
 			<File
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/CodePanel.cpp	Wed Sep 14 17:24:49 2011 -0700
@@ -0,0 +1,45 @@
+/*
+ * CodePanel
+ */
+
+#include "CodePanel.h"
+#include "GuiImage.h"
+
+/*
+ * CodePanel
+ */
+CodePanel::CodePanel()
+{
+}
+
+/*
+ * Initialize
+ */
+ErrorCode CodePanel::Initialize(ResourceManager* pResourceManager)
+{
+	ErrorCode eCode = GuiContainer::Initialize(pResourceManager);
+	if(eCode == Error_Success)
+	{
+		for(uint32 i = 0; i < 4; ++i)
+		{
+			GuiImage*
+			pImage = new GuiImage();
+			pImage->Initialize(pResourceManager);
+			pImage->SetTexture("Data\\Textures\\Slot.tga");
+			pImage->SetDimensions(48.0f, 48.0f);
+			pImage->SetPosition(48.0f * i, 0.0f);
+
+			Add(pImage);
+		}
+	}
+
+	return eCode;
+}
+
+/*
+ * Update
+ */
+void CodePanel::Update(float fElapsed)
+{
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/CodePanel.h	Wed Sep 14 17:24:49 2011 -0700
@@ -0,0 +1,34 @@
+/*
+ * CodePanel
+ */
+
+#ifndef __CODEPANEL_H__
+#define __CODEPANEL_H__
+
+#include "Core.h"
+#include "GuiContainer.h"
+
+/*
+ * CodePanel
+ */
+class CodePanel : public GuiContainer
+{
+public:
+
+	/*
+	 * CodePanel
+	 */
+	CodePanel();
+
+	/*
+	 * Initialize
+	 */
+	virtual ErrorCode Initialize(ResourceManager* pResourceManager);
+
+	/*
+	 * Update
+	 */
+	virtual void Update(float fElapsed);
+};
+
+#endif //__CODEPANEL_H__
--- a/LightClone/Source/GuiButton.cpp	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiButton.cpp	Wed Sep 14 17:24:49 2011 -0700
@@ -67,7 +67,7 @@
 /*
  * Render
  */
-void GuiButton::Render(RenderContext& kContext)
+void GuiButton::Render(RenderContext& kContext, Camera& kCamera)
 {
 	if(pTexture[nState])
 	{
--- a/LightClone/Source/GuiButton.h	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiButton.h	Wed Sep 14 17:24:49 2011 -0700
@@ -70,7 +70,7 @@
 	/*
 	 * Render
 	 */
-	virtual void Render(RenderContext& kContext);
+	virtual void Render(RenderContext& kContext, Camera& kCamera);
 
 private:
 
--- a/LightClone/Source/GuiContainer.cpp	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiContainer.cpp	Wed Sep 14 17:24:49 2011 -0700
@@ -66,11 +66,11 @@
 /*
  * Render
  */
-void GuiContainer::Render(RenderContext& kContext)
+void GuiContainer::Render(RenderContext& kContext, Camera& kCamera)
 {
 	for(uint32 i = 0; i < kChildren.GetSize(); ++i)
 	{
-		kChildren[i]->Render(kContext);
+		kChildren[i]->Render(kContext, kCamera);
 	}
 }
 
--- a/LightClone/Source/GuiContainer.h	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiContainer.h	Wed Sep 14 17:24:49 2011 -0700
@@ -48,7 +48,7 @@
 	/*
 	 * Render
 	 */
-	virtual void Render(RenderContext& kContext);
+	virtual void Render(RenderContext& kContext, Camera& kCamera);
 
 	/*
 	 * Pick
--- a/LightClone/Source/GuiElement.cpp	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiElement.cpp	Wed Sep 14 17:24:49 2011 -0700
@@ -64,9 +64,9 @@
 /*
  * GetPosition
  */
-const D3DXVECTOR2& GuiElement::GetPosition() const
+const D3DXVECTOR2 GuiElement::GetPosition() const
 {
-	return kPosition;
+	return pContainer ? pContainer->GetPosition() + kPosition : kPosition;
 }
 
 /*
@@ -89,7 +89,7 @@
 /*
  * GetDimensions
  */
-const D3DXVECTOR2& GuiElement::GetDimensions() const
+const D3DXVECTOR2 GuiElement::GetDimensions() const
 {
 	return kDimensions;
 }
@@ -99,136 +99,7 @@
  */
 GuiElement* GuiElement::Pick(float fX, float fY)
 {
-	return Rectangle2(kPosition.y, kPosition.y, kDimensions.x, kDimensions.y).Contains(fX, fY) ? this : NULL;
-}
-
-#if 0
-/*
- * GuiElementList
- */
-GuiElementList::GuiElementList() : pElement(NULL), nSize(0), nCount(0)
-{
-}
-
-/*
- * ~GuiElementList
- */
-GuiElementList::~GuiElementList()
-{
-	delete[] pElement;
-	pElement = NULL;
-}
-
-/*
- * Add
- */
-ErrorCode GuiElementList::Add(GuiElement* pInstance)
-{
-	ErrorCode eCode = Resize(nCount + 1);
-	if(eCode == Error_Success)
-	{
-		pElement[nCount++] = pInstance;
-	}
-
-	return eCode;
-}
-
-/*
- * Remove
- */
-ErrorCode GuiElementList::Remove(uint32 nIndex)
-{
-	if(nIndex < nCount)
-	{
-		--nCount;
-
-		for(uint32 i = nIndex; i < nCount; ++i)
-		{
-			pElement[i] = pElement[i + 1];
-		}
-
-		return Error_Success;
-	}
-
-	return Error_Fail;
-}
-
-/*
- * Clear
- */
-void GuiElementList::Clear()
-{
-	nCount = 0;
-}
+	const D3DXVECTOR2& kLocation = GetPosition();
 
-/*
- * Find
- */
-int32 GuiElementList::Find(GuiElement* pInstance)
-{
-	for(uint32 i = 0; i < nCount; ++i)
-	{
-		if(pElement[i] == pInstance)
-		{
-			return (int32)i;
-		}
-	}
-
-	return -1;
-}
-
-/*
- * GetSize
- */
-uint32 GuiElementList::GetSize() const
-{
-	return nCount;
-}
-
-/*
- * operator []
- */
-GuiElement* GuiElementList::operator[](uint32 nIndex)
-{
-	//ASSERT(nIndex < nCount);
-	return pElement[nIndex];
+	return Rectangle2(kLocation.y, kLocation.y, kDimensions.x, kDimensions.y).Contains(fX, fY) ? this : NULL;
 }
-
-/*
- * operator []
- */
-const GuiElement* GuiElementList::operator[](uint32 nIndex) const
-{
-	//ASSERT(nIndex < nCount);
-	return pElement[nIndex];
-}
-
-/*
- * Resize
- */
-ErrorCode GuiElementList::Resize(uint32 nLength)
-{
-	ErrorCode eCode = Error_Success;
-
-	if(nLength > nSize)
-	{
-		//TODO: Reallocate array and copy
-		GuiElement** pArray = new GuiElement*[2 * nSize + 1];
-
-		if(pElement)
-		{
-			for(uint32 i = 0; i < nCount; ++i)
-			{
-				pArray[i] = pElement[i];
-			}
-
-			delete[] pElement;
-		}
-
-		pElement = pArray;
-		nSize *= 2;
-	}
-
-	return eCode;
-}
-#endif
\ No newline at end of file
--- a/LightClone/Source/GuiElement.h	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiElement.h	Wed Sep 14 17:24:49 2011 -0700
@@ -63,7 +63,7 @@
 	/*
 	 * Render
 	 */
-	virtual void Render(RenderContext& kContext) = 0;
+	virtual void Render(RenderContext& kContext, Camera& kCamera) = 0;
 
 	/*
 	 * Attach
@@ -93,7 +93,7 @@
 	/*
 	 * GetPosition
 	 */
-	const D3DXVECTOR2& GetPosition() const;
+	const D3DXVECTOR2 GetPosition() const;
 
 	/*
 	 * SetDimensions
@@ -108,7 +108,7 @@
 	/*
 	 * GetDimensions
 	 */
-	const D3DXVECTOR2& GetDimensions() const;
+	const D3DXVECTOR2 GetDimensions() const;
 
 	/*
 	 * Pick
@@ -121,78 +121,4 @@
  */
 typedef ArrayList<GuiElement*> GuiElementList;
 
-#if 0
-class GuiElementList
-{
-	/*
-	 * pElement
-	 */
-	GuiElement** pElement;
-
-	/*
-	 * nSize
-	 */
-	uint32 nSize;
-
-	/*
-	 * nCount;
-	 */
-	uint32 nCount;
-
-public:
-
-	/*
-	 * GuiElementList
-	 */
-	GuiElementList();
-
-	/*
-	 * ~GuiElementList
-	 */
-	~GuiElementList();
-
-	/*
-	 * Add
-	 */
-	ErrorCode Add(GuiElement* pInstance);
-
-	/*
-	 * Remove
-	 */
-	ErrorCode Remove(uint32 nIndex);
-
-	/*
-	 * Clear
-	 */
-	void Clear();
-
-	/*
-	 * Find
-	 */
-	int32 Find(GuiElement* pInstance);
-
-	/*
-	 * GetSize
-	 */
-	uint32 GetSize() const;
-
-	/*
-	 * operator []
-	 */
-	GuiElement* operator[](uint32 nIndex);
-
-	/*
-	 * operator []
-	 */
-	const GuiElement* operator[](uint32 nIndex) const;
-
-private:
-
-	/*
-	 * Resize
-	 */
-	ErrorCode Resize(uint32 nLength);
-};
-#endif
-
 #endif //__GUIELEMENT_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/GuiImage.cpp	Wed Sep 14 17:24:49 2011 -0700
@@ -0,0 +1,144 @@
+/*
+ * GuiImage
+ */
+
+#include "GuiImage.h"
+#include "VertexTypes.h"
+
+/*
+ * GuiImage
+ */
+GuiImage::GuiImage() : pEffect(NULL), pVertexBuffer(NULL), pTexture(NULL)
+{
+}
+
+/*
+ * Initialize
+ */
+ErrorCode GuiImage::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 GuiImage::Terminate()
+{
+	if(pTexture)
+	{
+		pTexture->Release();
+		pTexture = NULL;
+	}
+
+	if(pVertexBuffer)
+	{
+		pVertexBuffer->Release();
+		pVertexBuffer = NULL;
+	}
+
+	if(pEffect)
+	{
+		pEffect->Release();
+		pEffect = NULL;
+	}
+}
+
+/*
+ * Update
+ */
+void GuiImage::Update(float fElapsed)
+{
+}
+
+/*
+ * Render
+ */
+void GuiImage::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 GuiImage::SetTexture(const char* pName)
+{
+	if(pTexture)
+	{
+		pTexture->Release();
+		pTexture = NULL;
+	}
+
+	return pResourceManager->CreateTextureFromFile(pName, &pTexture);
+}
+
+/*
+ * SetupVertexBuffer
+ */
+ErrorCode GuiImage::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/GuiImage.h	Wed Sep 14 17:24:49 2011 -0700
@@ -0,0 +1,76 @@
+/*
+ * GuiImage
+ */
+
+#ifndef __GUIIMAGE_H__
+#define __GUIIMAGE_H__
+
+#include "Core.h"
+#include "GuiElement.h"
+
+/*
+ * GuiImage
+ */
+class GuiImage : public GuiElement
+{
+	/*
+	 * pResourceManager
+	 */
+	ResourceManager* pResourceManager;
+
+	/*
+	 * pEffect
+	 */
+	ID3DXEffect* pEffect;
+
+	/*
+	 * pVertexBuffer
+	 */
+	IDirect3DVertexBuffer9* pVertexBuffer;
+
+	/*
+	 * pTexture
+	 */
+	IDirect3DTexture9* pTexture;
+
+public:
+
+	/*
+	 * GuiImage
+	 */
+	GuiImage();
+
+	/*
+	 * 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);
+
+private:
+
+	/*
+	 * SetupVertexBuffer
+	 */
+	ErrorCode SetupVertexBuffer();
+};
+
+#endif //__GUIIMAGE_H__
--- a/LightClone/Source/GuiInterface.cpp	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiInterface.cpp	Wed Sep 14 17:24:49 2011 -0700
@@ -55,7 +55,7 @@
 	//		the camera and effect at this point.  May also need a wrapper
 	//		around render context (GuiRenderContext)
 
-	pRoot->Render(kContext);
+	pRoot->Render(kContext, kCamera);
 }
 
 /*
--- a/LightClone/Source/GuiLabel.cpp	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiLabel.cpp	Wed Sep 14 17:24:49 2011 -0700
@@ -42,7 +42,7 @@
 /*
  * Render
  */
-void GuiLabel::Render(RenderContext& kContext)
+void GuiLabel::Render(RenderContext& kContext, Camera& kCamera)
 {
 	if(pFont)
 	{
@@ -101,4 +101,20 @@
 	}
 
 	return eCode;
+}
+
+/*
+ * SetFlags
+ */
+ErrorCode GuiLabel::SetFlags(uint32 nValue)
+{
+	return nFlags |= nValue, Error_Success;
+}
+
+/*
+ * ClearFlags
+ */
+ErrorCode GuiLabel::ClearFlags(uint32 nValue)
+{
+	return nFlags &= ~nValue, Error_Success;
 }
\ No newline at end of file
--- a/LightClone/Source/GuiLabel.h	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/GuiLabel.h	Wed Sep 14 17:24:49 2011 -0700
@@ -77,7 +77,7 @@
 	/*
 	 * Render
 	 */
-	virtual void Render(RenderContext& kContext);
+	virtual void Render(RenderContext& kContext, Camera& kCamera);
 
 	/*
 	 * SetFont
@@ -88,6 +88,16 @@
 	 * SetText
 	 */
 	ErrorCode SetText(const char* pText);
+
+	/*
+	 * SetFlags
+	 */
+	ErrorCode SetFlags(uint32 nFlags);
+
+	/*
+	 * ClearFlags
+	 */
+	ErrorCode ClearFlags(uint32 nFlags);
 };
 
 #endif //__GUILABEL_H__
--- a/LightClone/Source/World.cpp	Wed Sep 14 13:33:22 2011 -0700
+++ b/LightClone/Source/World.cpp	Wed Sep 14 17:24:49 2011 -0700
@@ -285,6 +285,7 @@
 	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);
 
 	pRoot->Add(pLabel);