changeset 15:ee1c2510096d

Work on GUI system
author koryspansel <koryspansel@bendbroadband.com>
date Wed, 14 Sep 2011 11:04:18 -0700
parents 7081e8e6008c
children 88f7c22e7b45
files LightClone/Source/ArrayList.h LightClone/Source/Event.h LightClone/Source/EventSink.h LightClone/Source/EventSystem.cpp LightClone/Source/EventSystem.h LightClone/Source/InputManager.cpp LightClone/Source/InputManager.h LightClone/Source/Interface.cpp LightClone/Source/Interface.h LightClone/Source/Mediator.cpp LightClone/Source/World.cpp
diffstat 11 files changed, 176 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/LightClone/Source/ArrayList.h	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/ArrayList.h	Wed Sep 14 11:04:18 2011 -0700
@@ -68,14 +68,14 @@
 
 		if(nIndex < nSize)
 		{
-			--nCount;
+			--nSize;
 
-			for(uint32 i = nIndex; i < nCount; ++i)
+			for(uint32 i = nIndex; i < nSize; ++i)
 			{
 				pList[i] = pList[i + 1];
 			}
 
-			eCode = Error_Success
+			eCode = Error_Success;
 		}
 
 		return eCode;
--- a/LightClone/Source/Event.h	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/Event.h	Wed Sep 14 11:04:18 2011 -0700
@@ -22,6 +22,10 @@
  */
 struct Event
 {
+	/*
+	 * nType
+	 */
+	uint32 nType;
 };
 
 #endif //__EVENT_H__
--- a/LightClone/Source/EventSink.h	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/EventSink.h	Wed Sep 14 11:04:18 2011 -0700
@@ -7,6 +7,7 @@
 
 #include "Core.h"
 #include "ArrayList.h"
+#include "Event.h"
 
 /*
  * EventSink
@@ -23,7 +24,7 @@
 	/*
 	 * ProcessEvent
 	 */
-	virtual int32 ProcessEvent() = 0;
+	virtual int32 ProcessEvent(const Event& kEvent) = 0;
 };
 
 /*
--- a/LightClone/Source/EventSystem.cpp	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/EventSystem.cpp	Wed Sep 14 11:04:18 2011 -0700
@@ -12,13 +12,45 @@
 }
 
 /*
+ * AddSource
+ */
+ErrorCode EventSystem::AddSource(EventSource* pSource)
+{
+	return kSources.Add(pSource);
+}
+
+/*
+ * AddSink
+ */
+ErrorCode EventSystem::AddSink(EventSink* pSink)
+{
+	return kSinks.Add(pSink);
+}
+
+/*
+ * RemoveSink
+ */
+ErrorCode EventSystem::RemoveSink(EventSink* pSink)
+{
+	ErrorCode eCode = Error_Fail;
+
+	int32 nIndex = kSinks.Find(pSink);
+	if(nIndex >= 0)
+	{
+		eCode = kSinks.Remove(nIndex);
+	}
+
+	return eCode;
+}
+
+/*
  * Post
  */
 void EventSystem::Post(const Event& kEvent)
 {
 	for(uint32 i = 0; i < kSinks.GetSize(); ++i)
 	{
-		if(kSinks[i]->ProcessEvent() == EventResult_Stop)
+		if(kSinks[i]->ProcessEvent(kEvent) == EventResult_Stop)
 		{
 			break;
 		}
--- a/LightClone/Source/EventSystem.h	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/EventSystem.h	Wed Sep 14 11:04:18 2011 -0700
@@ -10,8 +10,6 @@
 #include "EventSink.h"
 #include "Event.h"
 
-//TODO: Support registering sinks for specific events
-
 /*
  * EventSystem
  *	The system ties together event sources and event sinks, and provides an
@@ -42,6 +40,17 @@
 	ErrorCode AddSource(EventSource* pSource);
 
 	/*
+	 * AddSink
+	 *	TODO: This should support specifying a specific event
+	 */
+	ErrorCode AddSink(EventSink* pSink);
+
+	/*
+	 * RemoveSink
+	 */
+	ErrorCode RemoveSink(EventSink* pSink);
+
+	/*
 	 * Post
 	 */
 	void Post(const Event& kEvent);
--- a/LightClone/Source/InputManager.cpp	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/InputManager.cpp	Wed Sep 14 11:04:18 2011 -0700
@@ -3,6 +3,7 @@
  */
 
 #include "InputManager.h"
+#include "EventSystem.h"
 
 /*
  * fMouseSensitivity
@@ -126,34 +127,34 @@
 			if(SUCCEEDED(hResult))
 			{
 				hResult = pKeyboard->GetDeviceState(sizeof(kCurrentKeyboardState), kCurrentKeyboardState);
-
-				for(uint32 i = 0; i < sizeof(kCurrentKeyboardState) / sizeof(kCurrentKeyboardState[0]); ++i)
-				{
-					// check for key up events
-					if(kPreviousKeyboardState[i] & 0x80)
-					{
-						if(!(kCurrentKeyboardState[i] & 0x80))
-						{
-							InputEvent kEvent;
-							kEvent.nType		= InputEventType_KeyUp;
-							kEvent.nKey			= i;
-							kEvent.nDuration	= 0;
+			}
+		}
 
-							pEventSystem->Post(kEvent);
-						}
-					}
-					else
-					{
-						if(kCurrentKeyboardState[i] & 0x80)
-						{
-							InputEvent kEvent;
-							kEvent.nType		= InputEventType_KeyDown;
-							kEvent.nKey			= i;
-							kEvent.nDuration	= 0;
+		for(uint32 i = 0; i < sizeof(kCurrentKeyboardState) / sizeof(kCurrentKeyboardState[0]); ++i)
+		{
+			// check for key up events
+			if(kPreviousKeyboardState[i] & 0x80)
+			{
+				if(!(kCurrentKeyboardState[i] & 0x80))
+				{
+					InputEvent kEvent;
+					kEvent.nType		= InputEventType_KeyUp;
+					kEvent.nKey			= i;
+					kEvent.nDuration	= 0;
 
-							pEventSystem->Post(kEvent);
-						}
-					}
+					pEventSystem->Post(kEvent);
+				}
+			}
+			else
+			{
+				if(kCurrentKeyboardState[i] & 0x80)
+				{
+					InputEvent kEvent;
+					kEvent.nType		= InputEventType_KeyDown;
+					kEvent.nKey			= i;
+					kEvent.nDuration	= 0;
+
+					pEventSystem->Post(kEvent);
 				}
 			}
 		}
@@ -175,6 +176,44 @@
 
 		fMouseX = Clamp(fMouseX + fMouseSensitivity * kCurrentMouseState.lX, kMouseBounds.X, kMouseBounds.X + kMouseBounds.Width - 1.0f);
 		fMouseY = Clamp(fMouseY + fMouseSensitivity * kCurrentMouseState.lY, kMouseBounds.Y, kMouseBounds.Y + kMouseBounds.Height - 1.0f);
+
+		for(uint32 i = 0; i < sizeof(kCurrentMouseState.rgbButtons) / sizeof(kCurrentMouseState.rgbButtons[0]); ++i)
+		{
+			if(kPreviousMouseState.rgbButtons[i] & 0x80)
+			{
+				if(!(kCurrentMouseState.rgbButtons[i] & 0x80))
+				{
+					InputEvent kEvent;
+					kEvent.nType		= InputEventType_MouseUp;
+					kEvent.nButton		= i;
+					kEvent.nDuration	= 0;
+
+					pEventSystem->Post(kEvent);
+				}
+			}
+			else
+			{
+				if(kCurrentMouseState.rgbButtons[i] & 0x80)
+				{
+					InputEvent kEvent;
+					kEvent.nType		= InputEventType_MouseDown;
+					kEvent.nButton		= i;
+					kEvent.nDuration	= 0;
+
+					pEventSystem->Post(kEvent);
+				}
+			}
+		}
+
+		if(kCurrentMouseState.lX != 0 || kCurrentMouseState.lY != 0)
+		{
+			InputEvent kEvent;
+			kEvent.nType		= InputEventType_MouseMove;
+			kEvent.fX			= fMouseX;
+			kEvent.fY			= fMouseY;
+
+			pEventSystem->Post(kEvent);
+		}
 	}
 }
 
--- a/LightClone/Source/InputManager.h	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/InputManager.h	Wed Sep 14 11:04:18 2011 -0700
@@ -8,6 +8,7 @@
 #define DIRECTINPUT_VERSION 0x0800
 #include <dinput.h>
 #include "Core.h"
+#include "Event.h"
 #include "EventSource.h"
 
 //TODO: Remove the need to pass InputManager to world objects during
@@ -29,13 +30,8 @@
 /*
  * InputEvent
  */
-struct InputEvent
+struct InputEvent : public Event
 {
-	/*
-	 * nType
-	 */
-	uint32 nType;
-
 	union
 	{
 		struct
--- a/LightClone/Source/Interface.cpp	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/Interface.cpp	Wed Sep 14 11:04:18 2011 -0700
@@ -4,6 +4,7 @@
 
 #include "Interface.h"
 #include "VertexTypes.h"
+#include "InputManager.h"
 
 /*
  * pActionTextureName
@@ -59,7 +60,8 @@
 /*
  * Initialize
  */
-ErrorCode Interface::Initialize(ResourceManager* pManager)
+//ErrorCode Interface::Initialize(EventSystem* pSystem, ResourceManager* pManager)
+ErrorCode Interface::Initialize(InputManager* pInput, ResourceManager* pManager);
 {
 	ErrorCode eCode = pManager->CreateEffectFromFile("Data\\Shaders\\TexturedQuad.fx", &pEffect);
 	if(eCode != Error_Success)
@@ -140,6 +142,9 @@
 		return Error_Fail;
 	}
 
+	//pEventSystem = pSystem;
+	//pEventSystem->AddSink(this);
+
 	return SetupVertexBuffer();
 }
 
@@ -148,6 +153,9 @@
  */
 void Interface::Terminate()
 {
+	//pEventSystem->RemoveSink(this);
+	//pEventSystem = NULL;
+
 	if(pCursorTexture)
 	{
 		pCursorTexture->Release();
@@ -284,6 +292,36 @@
 }
 
 /*
+ * ProcessEvent
+ */
+int32 Interface::ProcessEvent(const Event& kEvent)
+{
+	//HACK: Only have one event struct at the moment
+	const InputEvent& kInputEvent = (const InputEvent&)kEvent;
+
+	if(kEvent.nType == InputEventType_KeyDown)
+	{
+		if(kInputEvent.nKey == DIK_SPACE)
+		{
+			OutputDebugStringA("Events!  Yay!\n");
+			return EventResult_Stop;
+		}
+	}
+	else
+
+	if(kEvent.nType == InputEventType_MouseMove)
+	{
+		char kBuffer[64];
+		sprintf_s(kBuffer, "%d %d\n", (int)kInputEvent.fX, (int)kInputEvent.fY);
+
+		OutputDebugStringA(kBuffer);
+		return EventResult_Stop;
+	}
+
+	return EventResult_Continue;
+}
+
+/*
  * SetupVertexBuffer
  */
 ErrorCode Interface::SetupVertexBuffer()
--- a/LightClone/Source/Interface.h	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/Interface.h	Wed Sep 14 11:04:18 2011 -0700
@@ -9,15 +9,21 @@
 #include "RenderContext.h"
 #include "ResourceManager.h"
 #include "GuiContainer.h"
+#include "EventSystem.h"
 
 /*
  * Interface
  */
-class Interface
+class Interface : public EventSink
 {
 private:
 
 	/*
+	 * pEventSystem
+	 */
+	EventSystem* pEventSystem;
+
+	/*
 	 * pEffect
 	 */
 	ID3DXEffect* pEffect;
@@ -77,7 +83,8 @@
 	/*
 	 * Initialize
 	 */
-	ErrorCode Initialize(ResourceManager* pManager);
+	//ErrorCode Initialize(EventSystem* pSystem, ResourceManager* pManager);
+	ErrorCode Initialize(InputManager* pInput, ResourceManager* pManager);
 
 	/* 
 	 * Terminate
@@ -89,6 +96,11 @@
 	 */
 	void Render(RenderContext& kContext, Camera& kCamera);
 
+	/*
+	 * ProcessEvent
+	 */
+	virtual int32 ProcessEvent(const Event& kEvent);
+
 private:
 
 	/*
--- a/LightClone/Source/Mediator.cpp	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/Mediator.cpp	Wed Sep 14 11:04:18 2011 -0700
@@ -148,6 +148,8 @@
  */
 void Mediator::Update(float fElapsed)
 {
+	kEventSystem.Update(fElapsed);
+
 	kWorld.Update(fElapsed);
 }
 
--- a/LightClone/Source/World.cpp	Tue Sep 13 23:01:45 2011 -0700
+++ b/LightClone/Source/World.cpp	Wed Sep 14 11:04:18 2011 -0700
@@ -274,7 +274,7 @@
  */
 ErrorCode World::InitializeInterface(ResourceManager* pResourceManager)
 {
-	return kInterface.Initialize(pResourceManager);
+	return kInterface.Initialize(pEventSystem, pResourceManager);
 
 	/*
 	GuiElement* pRoot = kInterface.GetRoot();