# HG changeset patch # User koryspansel # Date 1315931651 25200 # Node ID d80d06d5ff53f129624233c9ec64d36669a58946 # Parent 292e534f00c2384a2954e174aa1a67f61008e886 Begin working on Gui system diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/LightClone.vcproj --- a/LightClone/LightClone.vcproj Sat Sep 10 23:52:12 2011 -0700 +++ b/LightClone/LightClone.vcproj Tue Sep 13 09:34:11 2011 -0700 @@ -221,6 +221,10 @@ > + + @@ -229,6 +233,10 @@ > + + @@ -331,6 +339,10 @@ > + + @@ -339,6 +351,10 @@ > + + diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/GuiButton.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightClone/Source/GuiButton.cpp Tue Sep 13 09:34:11 2011 -0700 @@ -0,0 +1,124 @@ +/* + * GuiButton + */ + +#include "GuiButton.h" +#include "VertexTypes.h" + +/* + * GuiButton + */ +GuiButton::GuiButton() +{ +} + +/* + * Initialize + */ +ErrorCode GuiButton::Initialize(ResourceManager* pResourceManager) +{ + 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 GuiButton::Terminate() +{ + for(uint32 i = 0; i < GuiButtonState_Count; ++i) + { + if(pTexture[i]) + { + pTexture[i]->Release(); + pTexture[i] = NULL; + } + } + + if(pVertexBuffer) + { + pVertexBuffer->Release(); + pVertexBuffer = NULL; + } + + if(pEffect) + { + pEffect->Release(); + pEffect = NULL; + } +} + +/* + * Update + */ +void GuiButton::Update(float fElapsed) +{ +} + +/* + * Render + */ +void GuiButton::Render(RenderContext& kContext) +{ + if(pTexture[nState]) + { + const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f); + + D3DSURFACE_DESC kDescriptor; + pTexture[nState]->GetLevelDesc(0, &kDescriptor); + + const float fX = ScreenSizeX - (float)kDescriptor.Width; + const float fY = 0.0f; + + D3DXMATRIX kScale; + D3DXMatrixScaling(&kScale, (float)kDescriptor.Width, (float)kDescriptor.Height, 1.0f); + + D3DXMATRIX kTranslate; + //D3DXMatrixTranslation(&kTranslate, -0.5f * ScreenSizeX + fX + 0.5f, 0.5f * ScreenSizeY - fY + 0.5f, 0.0f); + D3DXMatrixTranslation( + + 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"), pBackgroundTexture); + pEffect->CommitChanges(); + + kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace); + } +} + +/* + * SetupVertexBuffer + */ +ErrorCode GuiButton::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; +} diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/GuiButton.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightClone/Source/GuiButton.h Tue Sep 13 09:34:11 2011 -0700 @@ -0,0 +1,83 @@ +/* + * GuiButton + */ + +#ifndef __GUIBUTTON_H__ +#define __GUIBUTTON_H__ + +#include "Core.h" +#include "GuiElement.h" + +/* + * GuiButtonState + */ +enum +{ + GuiButtonState_Normal, + GuiButtonState_Down, + GuiButtonState_Hover, + GuiButtonState_Disabled, + GuiButtonState_Count, +}; + +/* + * GuiButton + */ +class GuiButton +{ + /* + * nState + */ + uint32 nState; + + /* + * pEffect + */ + ID3DXEffect* pEffect; + + /* + * pVertexBuffer + */ + IDirect3DVertexBuffer9* pVertexBuffer; + + /* + * pTexture + */ + IDirect3DTexture9* pTexture[GuiButtonState_Count]; + +public: + + /* + * GuiButton + */ + GuiButton(); + + /* + * Initialize + */ + virtual ErrorCode Initialize(ResourceManager* pResourceManager); + + /* + * Terminate + */ + virtual void Terminate(); + + /* + * Update + */ + virtual void Update(float fElapsed); + + /* + * Render + */ + virtual void Render(RenderContext& kContext); + +private: + + /* + * SetupVertexBuffer + */ + ErrorCode SetupVertexBuffer(); +}; + +#endif //__GUIBUTTON_H__ diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/GuiContainer.cpp --- a/LightClone/Source/GuiContainer.cpp Sat Sep 10 23:52:12 2011 -0700 +++ b/LightClone/Source/GuiContainer.cpp Tue Sep 13 09:34:11 2011 -0700 @@ -12,6 +12,34 @@ } /* + * Initialize + */ +ErrorCode GuiContainer::Initialize(ResourceManager* pResourceManager) +{ + ErrorCode eCode = Error_Success; + + for(uint32 i = 0; i < kChildren.GetSize() && eCode == Error_Success; ++i) + { + eCode = kChildren[i]->Initialize(pResourceManager); + } + + return eCode; +} + +/* + * Terminate + */ +void GuiContainer::Terminate() +{ + for(uint32 i = 0; i < kChildren.GetSize(); ++i) + { + kChildren[i]->Terminate(); + } + + kChildren.Clear(); +} + +/* * Update */ void GuiContainer::Update(float fElapsed) @@ -33,4 +61,26 @@ { kChildren[i]->Render(kContext); } +} + +/* + * Pick + */ +GuiElement* GuiContainer::Pick(float fX, float fY) +{ + GuiElement* pElement = NULL; + + for(uint32 i = 0; i < kChildren.GetSize() && !pElement; ++i) + { + pElement = kChildren[i]->Pick(fX, fY); + } + + return pElement; +} + +/* + * Layout + */ +void GuiContainer::Layout(GuiElementList& kElements) +{ } \ No newline at end of file diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/GuiContainer.h --- a/LightClone/Source/GuiContainer.h Sat Sep 10 23:52:12 2011 -0700 +++ b/LightClone/Source/GuiContainer.h Tue Sep 13 09:34:11 2011 -0700 @@ -26,6 +26,16 @@ GuiContainer(); /* + * Initialize + */ + virtual ErrorCode Initialize(ResourceManager* pResourceManager); + + /* + * Terminate + */ + virtual void Terminate(); + + /* * Update */ virtual void Update(float fElapsed); @@ -35,12 +45,17 @@ */ virtual void Render(RenderContext& kContext); + /* + * Pick + */ + virtual GuiElement* Pick(float fX, float fY); + protected: /* * Layout */ - virtual void Layout(GuiElementList& kElements) = 0; + virtual void Layout(GuiElementList& kElements); }; #endif //__GUICONTAINER_H__ diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/GuiElement.cpp --- a/LightClone/Source/GuiElement.cpp Sat Sep 10 23:52:12 2011 -0700 +++ b/LightClone/Source/GuiElement.cpp Tue Sep 13 09:34:11 2011 -0700 @@ -94,6 +94,14 @@ return kDimensions; } +/* + * Pick + */ +GuiElement* GuiElement::Pick(float fX, float fY) +{ + return Rectangle2(kPosition.y, kPosition.y, kDimensions.x, kDimensions.y).Contains(fX, fY) ? this : NULL; +} + /* * GuiElementList @@ -146,6 +154,14 @@ } /* + * Clear + */ +void GuiElementList::Clear() +{ + nCount = 0; +} + +/* * Find */ int32 GuiElementList::Find(GuiElement* pInstance) diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/GuiElement.h --- a/LightClone/Source/GuiElement.h Sat Sep 10 23:52:12 2011 -0700 +++ b/LightClone/Source/GuiElement.h Tue Sep 13 09:34:11 2011 -0700 @@ -43,6 +43,26 @@ virtual ~GuiElement(); /* + * Initialize + */ + virtual ErrorCode Initialize(ResourceManager* pResourceManager) = 0; + + /* + * Terminate + */ + virtual void Terminate() = 0; + + /* + * Update + */ + virtual void Update(float fElapsed) = 0; + + /* + * Render + */ + virtual void Render(RenderContext& kContext) = 0; + + /* * Attach */ void Attach(GuiElement* pInstance); @@ -88,24 +108,9 @@ const D3DXVECTOR2& GetDimensions() const; /* - * Initialize - */ - virtual ErrorCode Initialize(ResourceManager* pResourceManager) = 0; - - /* - * Terminate + * Pick */ - virtual void Terminate() = 0; - - /* - * Update - */ - virtual void Update(float fElapsed) = 0; - - /* - * Render - */ - virtual void Render(RenderContext& kContext) = 0; + virtual GuiElement* Pick(float fX, float fY); }; /* @@ -151,6 +156,11 @@ ErrorCode Remove(uint32 nIndex); /* + * Clear + */ + void Clear(); + + /* * Find */ int32 Find(GuiElement* pInstance); diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/GuiInterface.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightClone/Source/GuiInterface.cpp Tue Sep 13 09:34:11 2011 -0700 @@ -0,0 +1,70 @@ +/* + * GuiInterface + */ + +#include "GuiInterface.h" + +/* + * GuiInterface + */ +GuiInterface::GuiInterface() +{ + pRoot = new GuiContainer(); +} + +/* + * ~GuiInterface + */ +GuiInterface::~GuiInterface() +{ + delete pRoot; + pRoot = NULL; +} + +/* + * Initialize + */ +ErrorCode GuiInterface::Initialize(ResourceManager* pResourceManager) +{ + return pRoot->Initialize(pResourceManager); +} + +/* + * Terminate + */ +void GuiInterface::Terminate() +{ + pRoot->Terminate(); +} + +/* + * Update + */ +void GuiInterface::Update(float fElapsed) +{ + pRoot->Update(fElapsed); +} + +/* + * Render + */ +void GuiInterface::Render(RenderContext& kContext) +{ + pRoot->Render(kContext); +} + +/* + * Pick + */ +GuiElement* GuiInterface::Pick(float fX, float fY) +{ + return pRoot->Pick(fX, fY); +} + +/* + * GetRoot + */ +GuiContainer* GuiInterface::GetRoot() +{ + return pRoot; +} diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/GuiInterface.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightClone/Source/GuiInterface.h Tue Sep 13 09:34:11 2011 -0700 @@ -0,0 +1,64 @@ +/* + * GuiInterface + */ + +#ifndef __GUIINTERFACE_H__ +#define __GUIINTERFACE_H__ + +#include "Core.h" +#include "GuiContainer.h" + +/* + * GuiInterface + */ +class GuiInterface +{ + /* + * pRoot + */ + GuiContainer* pRoot; + +public: + + /* + * GuiInterface + */ + GuiInterface(); + + /* + * ~GuiInterface + */ + ~GuiInterface(); + + /* + * Initialize + */ + ErrorCode Initialize(ResourceManager* pResourceManager); + + /* + * Terminate + */ + void Terminate(); + + /* + * Update + */ + void Update(float fElapsed); + + /* + * Render + */ + void Render(RenderContext& kContext); + + /* + * Pick + */ + GuiElement* Pick(float fX, float fY); + + /* + * GetRoot + */ + GuiContainer* GetRoot(); +}; + +#endif //__GUIINTERFACE_H__ diff -r 292e534f00c2 -r d80d06d5ff53 LightClone/Source/World.cpp --- a/LightClone/Source/World.cpp Sat Sep 10 23:52:12 2011 -0700 +++ b/LightClone/Source/World.cpp Tue Sep 13 09:34:11 2011 -0700 @@ -309,21 +309,25 @@ pButtonStart = new GuiButton(); pButtonStart->SetTexture("Data\\Textures\\Play.tga"); pButtonStart->SetDimensions(...); + pButtonStart->SetCallback(&World::OnClick, this); //GuiButton* pButtonStop = new GuiButton(); pButtonStop->SetTexture("Data\\Textures\\Stop.tga"); pButtonStop->SetDimensions(...); + pButtonStop->SetCallback(&World::OnClick, this); //GuiButton* pButtonReset = new GuiButton(); pButtonReset->SetTexture("Data\\Textures\\Reset.tga"); pButtonReset->SetDimensions(...); + pButtonReset->SetCallback(&World::OnClick, this); //GuiButton* pButtonExit = new GuiButton(); pButtonExit->SetTexture("Data\\Textures\\Exit.tga"); pButtonExit->SetDimensions(...); + pButtonExit->SetCallback(&World::OnClick, this); //GuiPanel* pControlPanel = new GuiPanel();