WIP ECS GUI

This commit is contained in:
2014-05-12 22:33:59 +02:00
parent b6372d44b8
commit ad19848c1b
9 changed files with 172 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
#ifndef Events_CastRay_h__
#define Events_CastRay_h__
#include "EventBroker.h"
namespace Events
{
struct CastRay : Event
{
glm::vec3 Direction;
};
}
#endif // Events_CastRay_h__
+1
View File
@@ -9,6 +9,7 @@ namespace Events
struct MousePress : Event
{
int Button;
double X, Y;
};
}
+1
View File
@@ -9,6 +9,7 @@ namespace Events
struct MouseRelease : Event
{
int Button;
double X, Y;
};
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef Events_RayIntersection_h__
#define Events_RayIntersection_h__
#include "EventBroker.h"
#include "Entity.h"
namespace Events
{
struct RayIntersection : Event
{
EntityID Entity;
};
}
#endif // Events_RayIntersection_h__
+27
View File
@@ -6,6 +6,9 @@
#include "Util/Rectangle.h"
#include "EventBroker.h"
// HACK: Decouple renderer plz
#include "Renderer.h"
namespace GUI
{
@@ -31,17 +34,41 @@ public:
{ SetParent(parent); Initialize(); }
virtual void Initialize() { }
std::shared_ptr<Frame> Parent() const { return m_Parent; }
void SetParent(std::shared_ptr<Frame> parent)
{
parent->AddChild(std::shared_ptr<Frame>(this));
m_Parent = parent;
EventBroker = parent->EventBroker;
}
void AddChild(std::shared_ptr<Frame> child)
{
m_Children.push_back(child);
if (m_Parent != nullptr)
{
m_Parent->AddChild(child);
}
}
typedef std::list<std::shared_ptr<Frame>>::const_iterator FrameChildrenIterator;
FrameChildrenIterator begin()
{
return m_Children.begin();
}
FrameChildrenIterator end()
{
return m_Children.end();
}
virtual void Update(double dt) { }
virtual void Draw(Renderer* renderer) { }
protected:
std::shared_ptr<::EventBroker> EventBroker;
std::shared_ptr<Frame> m_Parent;
std::list<std::shared_ptr<Frame>> m_Children;
};
}
+6
View File
@@ -36,17 +36,23 @@ void InputManager::Update(double dt)
m_CurrentMouseState[i] = glfwGetMouseButton(m_GLFWWindow, i);
if (m_CurrentMouseState[i] != m_LastMouseState[i])
{
double x, y;
glfwGetCursorPos(m_GLFWWindow, &x, &y);
// Publish mouse button events
if (m_CurrentMouseState[i])
{
Events::MousePress e;
e.Button = i;
e.X = x;
e.Y = y;
m_EventBroker->Publish<Events::MousePress>(e);
}
else
{
Events::MouseRelease e;
e.Button = i;
e.X = x;
e.Y = y;
m_EventBroker->Publish<Events::MouseRelease>(e);
}
}
+25
View File
@@ -0,0 +1,25 @@
#include "PrecompiledHeader.h"
#include "RaySystem.h"
#include "World.h"
void Systems::RaySystem::Initialize()
{
// Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_ECastRay, &Systems::RaySystem::OnCastRay);
}
void Systems::RaySystem::Update(double dt)
{
}
void Systems::RaySystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
}
bool Systems::RaySystem::OnCastRay(const Events::CastRay &event)
{
Ray r;
return true;
}
+35
View File
@@ -0,0 +1,35 @@
#ifndef DebugSystem_h__
#define DebugSystem_h__
#include "System.h"
#include "Events/CastRay.h"
namespace Systems
{
class RaySystem : public System
{
public:
RaySystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
void Initialize() override;
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
struct Ray
{
glm::vec3 Direction;
};
EventRelay<Events::CastRay> m_ECastRay;
bool OnCastRay(const Events::CastRay &event);
std::list<Ray> m_UnresolvedRays;
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
};
}
#endif // DebugSystem_h__
+44
View File
@@ -0,0 +1,44 @@
#include <boost/any.hpp>
#include "Entity.h"
#include "World.h"
#include "EventBroker.h"
// A slower wrapper class for EntityID to make
// creation of pre-defined entity hierarchies easier
class EntityGroup
{
public:
EntityGroup(World* world, EntityID parent = 0)
: World(world)
, EventBroker(world->EventBroker())
{
m_ID = World->CreateEntity(parent);
Initialize();
World->CommitEntity(m_ID);
}
~EntityGroup()
{
World->RemoveEntity(m_ID);
}
virtual void Initialize() { }
template <class T>
std::shared_ptr<T> AddComponent(std::string componentType)
{
return World->AddComponent<T>(m_ID, componentType);
}
std::shared_ptr<Component> AddComponent(std::string componentType)
{
return World->AddComponent(m_ID, componentType);
}
operator EntityID () const { return m_ID; }
private:
EntityID m_ID;
::World* World;
std::shared_ptr<::EventBroker> EventBroker;
};