Mercurial > LightClone
changeset 14:7081e8e6008c
Working on integrating the EventSystem
author | koryspansel |
---|---|
date | Tue, 13 Sep 2011 23:01:45 -0700 |
parents | 28cf0baf2772 |
children | ee1c2510096d |
files | LightClone/Source/EventSource.cpp LightClone/Source/EventSource.h LightClone/Source/EventSystem.cpp LightClone/Source/EventSystem.h LightClone/Source/InputManager.cpp LightClone/Source/InputManager.h LightClone/Source/Mediator.cpp LightClone/Source/Mediator.h LightClone/Source/World.cpp LightClone/Source/World.h |
diffstat | 10 files changed, 142 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/LightClone/Source/EventSource.cpp Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/EventSource.cpp Tue Sep 13 23:01:45 2011 -0700 @@ -4,3 +4,9 @@ #include "EventSource.h" +/* + * ~EventSource + */ +EventSource::~EventSource() +{ +} \ No newline at end of file
--- a/LightClone/Source/EventSource.h Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/EventSource.h Tue Sep 13 23:01:45 2011 -0700 @@ -27,10 +27,10 @@ virtual ~EventSource(); /* - * Generate + * Update * Give the event source a chance to post events to the system */ - virtual void Generate(EventSystem* pSystem); + virtual void Update(EventSystem* pSystem, float fElapsed) = 0; }; /*
--- a/LightClone/Source/EventSystem.cpp Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/EventSystem.cpp Tue Sep 13 23:01:45 2011 -0700 @@ -26,12 +26,12 @@ } /* - * Process + * Update */ -void EventSystem::Process() +void EventSystem::Update(float fElapsed) { for(uint32 i = 0; i < kSources.GetSize(); ++i) { - kSources[i]->Generate(this); + kSources[i]->Update(this, fElapsed); } } \ No newline at end of file
--- a/LightClone/Source/EventSystem.h Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/EventSystem.h Tue Sep 13 23:01:45 2011 -0700 @@ -8,6 +8,7 @@ #include "Core.h" #include "EventSource.h" #include "EventSink.h" +#include "Event.h" //TODO: Support registering sinks for specific events @@ -36,14 +37,19 @@ EventSystem(); /* + * AddSource + */ + ErrorCode AddSource(EventSource* pSource); + + /* * Post */ void Post(const Event& kEvent); /* - * Process + * Update */ - void Process(); + virtual void Update(float fElapsed); }; #endif //__EVENTSYSTEM_H__
--- a/LightClone/Source/InputManager.cpp Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/InputManager.cpp Tue Sep 13 23:01:45 2011 -0700 @@ -113,7 +113,7 @@ /* * Update */ -void InputManager::Update(float fElapsed) +void InputManager::Update(EventSystem* pEventSystem, float fElapsed) { if(pKeyboard) { @@ -126,6 +126,35 @@ 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; + + pEventSystem->Post(kEvent); + } + } + } } } }
--- a/LightClone/Source/InputManager.h Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/InputManager.h Tue Sep 13 23:01:45 2011 -0700 @@ -8,14 +8,81 @@ #define DIRECTINPUT_VERSION 0x0800 #include <dinput.h> #include "Core.h" +#include "EventSource.h" //TODO: Remove the need to pass InputManager to world objects during // initialization by sending input events instead /* + * InputEventType + */ +enum +{ + InputEventType_KeyDown, + InputEventType_KeyUp, + + InputEventType_MouseDown, + InputEventType_MouseUp, + InputEventType_MouseMove, +}; + +/* + * InputEvent + */ +struct InputEvent +{ + /* + * nType + */ + uint32 nType; + + union + { + struct + { + /* + * nKey + */ + uint32 nKey; + + /* + * nDuration + */ + uint32 nDuration; + }; + + struct + { + /* + * nButton + */ + uint32 nButton; + + /* + * nDuration + */ + uint32 nDuration; + }; + + struct + { + /* + * fX + */ + float fX; + + /* + * fY + */ + float fY; + }; + }; +}; + +/* * InputManager */ -class InputManager +class InputManager : public EventSource { /* * pDirectInput @@ -87,7 +154,7 @@ /* * Update */ - void Update(float fElapsed); + virtual void Update(EventSystem* pEventSystem, float fElapsed); /* * SetBounds
--- a/LightClone/Source/Mediator.cpp Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/Mediator.cpp Tue Sep 13 23:01:45 2011 -0700 @@ -112,7 +112,14 @@ return Error_Fail; } - eCode = kWorld.Initialize(&kResourceManager, &kInputManager); + eCode = kEventSystem.AddSource(&kInputManager); + if(eCode != Error_Success) + { + Terminate(); + return Error_Fail; + } + + eCode = kWorld.Initialize(&kEventSystem, &kResourceManager, &kInputManager); if(eCode != Error_Success) { Terminate();
--- a/LightClone/Source/Mediator.h Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/Mediator.h Tue Sep 13 23:01:45 2011 -0700 @@ -25,6 +25,11 @@ class Mediator : public WindowCallback { /* + * kEventSystem + */ + EventSystem kEventSystem; + + /* * kWindow */ Window kWindow;
--- a/LightClone/Source/World.cpp Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/World.cpp Tue Sep 13 23:01:45 2011 -0700 @@ -67,12 +67,13 @@ /* * Initialize */ -ErrorCode World::Initialize(ResourceManager* pResourceManager, InputManager* pInput) +ErrorCode World::Initialize(EventSystem* pSystem, ResourceManager* pResourceManager, InputManager* pInput) { ErrorCode eCode = Error_Fail; if(pResourceManager && pInput) { + pEventSystem = pSystem; pInputManager = pInput; eCode = kEnvironment.Initialize(pResourceManager); @@ -392,7 +393,7 @@ */ void World::ProcessInput(float fElapsed) { - pInputManager->Update(fElapsed); + //pInputManager->Update(fElapsed); #if defined(_DEBUG) if(pInputManager->IsKeyDown(DIK_LEFT))
--- a/LightClone/Source/World.h Tue Sep 13 19:44:23 2011 -0700 +++ b/LightClone/Source/World.h Tue Sep 13 23:01:45 2011 -0700 @@ -6,6 +6,8 @@ #define __WORLD_H__ #include "Core.h" +#include "ResourceManager.h" +#include "EventSystem.h" #include "RenderContext.h" #include "Environment.h" #include "Bot.h" @@ -13,7 +15,6 @@ #include "Loader.h" #include "CameraController.h" #include "DragController.h" -#include "ResourceManager.h" #include "InputManager.h" #include "ButtonPane.h" #include "Dialog.h" @@ -25,6 +26,11 @@ class World { /* + * pEventSystem + */ + EventSystem* pEventSystem; + + /* * pInputManager */ InputManager* pInputManager; @@ -135,7 +141,7 @@ /* * Initialize */ - ErrorCode Initialize(ResourceManager* pResource, InputManager* pInput); + ErrorCode Initialize(EventSystem* pSystem, ResourceManager* pResource, InputManager* pInput); /* * Terminate