SP3-107 SP3-108
Initial port from 200 Removed unused classes Changing Event package for better ease of use
This commit is contained in:
parent
5f56344a74
commit
23a599abe8
|
@ -102,6 +102,7 @@
|
|||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\Common\SHCommonTypes.h" />
|
||||
<ClInclude Include="src\ECS_Base\Components\SHComponent.h" />
|
||||
<ClInclude Include="src\ECS_Base\Components\SHComponentGroup.h" />
|
||||
<ClInclude Include="src\ECS_Base\Entity\SHEntity.h" />
|
||||
|
@ -116,6 +117,9 @@
|
|||
<ClInclude Include="src\ECS_Base\System\SHSystem.h" />
|
||||
<ClInclude Include="src\ECS_Base\System\SHSystemManager.h" />
|
||||
<ClInclude Include="src\Engine\SHEngine.h" />
|
||||
<ClInclude Include="src\Events\SHEvent.h" />
|
||||
<ClInclude Include="src\Events\SHEventManager.h" />
|
||||
<ClInclude Include="src\Events\SHEventReceiver.h" />
|
||||
<ClInclude Include="src\Filesystem\SHFileSystem.h" />
|
||||
<ClInclude Include="src\Graphics\Buffers\SHVkBuffer.h" />
|
||||
<ClInclude Include="src\Graphics\Commands\SHCommandPoolResetMode.h" />
|
||||
|
@ -200,6 +204,8 @@
|
|||
<ClCompile Include="src\ECS_Base\System\SHEntityManager.cpp" />
|
||||
<ClCompile Include="src\ECS_Base\System\SHSystemManager.cpp" />
|
||||
<ClCompile Include="src\Engine\SHEngine.cpp" />
|
||||
<ClCompile Include="src\Events\SHEvent.cpp" />
|
||||
<ClCompile Include="src\Events\SHEventManager.cpp" />
|
||||
<ClCompile Include="src\Filesystem\SHFileSystem.cpp" />
|
||||
<ClCompile Include="src\Graphics\Buffers\SHVkBuffer.cpp" />
|
||||
<ClCompile Include="src\Graphics\Commands\SHVkCommandBuffer.cpp" />
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Common">
|
||||
<UniqueIdentifier>{8EEA3EAC-7A8C-6982-6347-7DD64F88F0D2}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="ECS_Base">
|
||||
<UniqueIdentifier>{1AB26817-067F-C322-2F98-B1CA1BC4F8B0}</UniqueIdentifier>
|
||||
</Filter>
|
||||
|
@ -19,6 +22,9 @@
|
|||
<Filter Include="Engine">
|
||||
<UniqueIdentifier>{DBC7D3B0-C769-FE86-B024-12DB9C6585D7}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Events">
|
||||
<UniqueIdentifier>{3A8963B1-262B-8E87-0FE6-A1DBFB2615D8}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Filesystem">
|
||||
<UniqueIdentifier>{8A8E2B37-7646-6D84-DF4D-46E0CB240875}</UniqueIdentifier>
|
||||
</Filter>
|
||||
|
@ -117,6 +123,9 @@
|
|||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\Common\SHCommonTypes.h">
|
||||
<Filter>Common</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\ECS_Base\Components\SHComponent.h">
|
||||
<Filter>ECS_Base\Components</Filter>
|
||||
</ClInclude>
|
||||
|
@ -159,6 +168,15 @@
|
|||
<ClInclude Include="src\Engine\SHEngine.h">
|
||||
<Filter>Engine</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Events\SHEvent.h">
|
||||
<Filter>Events</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Events\SHEventManager.h">
|
||||
<Filter>Events</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Events\SHEventReceiver.h">
|
||||
<Filter>Events</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Filesystem\SHFileSystem.h">
|
||||
<Filter>Filesystem</Filter>
|
||||
</ClInclude>
|
||||
|
@ -405,6 +423,12 @@
|
|||
<ClCompile Include="src\Engine\SHEngine.cpp">
|
||||
<Filter>Engine</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Events\SHEvent.cpp">
|
||||
<Filter>Events</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Events\SHEventManager.cpp">
|
||||
<Filter>Events</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Filesystem\SHFileSystem.cpp">
|
||||
<Filter>Filesystem</Filter>
|
||||
</ClCompile>
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
/************************************************************************************//*!
|
||||
\file SHCommonTypes.h
|
||||
\author Tng Kah Wei, kahwei.tng, 390009620
|
||||
\par email: kahwei.tng\@digipen.edu
|
||||
\date Sep 8, 2022
|
||||
\brief Contains the definitions of type alias for commonly used units for
|
||||
clarity and convenience.
|
||||
|
||||
|
||||
Copyright (C) 2022 DigiPen Institute of Technology.
|
||||
Reproduction or disclosure of this file or its contents without the prior written consent
|
||||
of DigiPen Institute of Technology is prohibited.
|
||||
*//*************************************************************************************/
|
||||
#pragma once
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Type Definitions */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/***********************************************************************************/
|
||||
/*!
|
||||
\brief
|
||||
Type used to mark a value that is supposed to represent a size in bytes.
|
||||
*/
|
||||
/***********************************************************************************/
|
||||
using Byte = size_t;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/******************************************************************************
|
||||
* \file SHEvent.cpp
|
||||
* \author Loh Xiao Qi
|
||||
* \brief Implementation of SHEvent.h Initialise data member of smart pointer
|
||||
* to package data. Return the package pointer and package type
|
||||
* accordingly.
|
||||
*
|
||||
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
|
||||
* or disclosure of this file or its contents without the prior
|
||||
* written consent of Digipen Institute of Technology is prohibited
|
||||
******************************************************************************/
|
||||
#include "SHpch.h"
|
||||
#include "SHEvent.h"
|
||||
#include <utility>
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
/****************************************************************************
|
||||
* \param PackagePtr - smart pointer to package data.
|
||||
|
||||
* \brief Constructor for Event. User needs to dynamically allocate memory
|
||||
* for package data and pass in the smart pointer so the data member
|
||||
* can be initialised properly.
|
||||
****************************************************************************/
|
||||
SHEvent::SHEvent(PackagePtr pkg)
|
||||
: package{ std::move(pkg) }
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Get pointer to data package.
|
||||
*
|
||||
* \return Smart pointer to Package Base, needs to be cast to correct
|
||||
* derived class.
|
||||
****************************************************************************/
|
||||
ConstPackagePtr& SHEvent::GetData() const
|
||||
{
|
||||
return package;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Gets the type of package.
|
||||
****************************************************************************/
|
||||
SHPackageType SHEvent::GetType() const
|
||||
{
|
||||
return package->GetPackageType();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/******************************************************************************
|
||||
* \file SHEventBase.h
|
||||
* \author Loh Xiao Qi
|
||||
* \brief Event base abstract class declaration
|
||||
*
|
||||
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
|
||||
* or disclosure of this file or its contents without the prior written consent
|
||||
* of Digipen Institute of Technology is prohibited.
|
||||
******************************************************************************/
|
||||
#ifndef SH_EVENT_BASE_H
|
||||
#define SH_EVENT_BASE_H
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
class SHEvent
|
||||
{
|
||||
public:
|
||||
/****************************************************************************
|
||||
* \param PackagePtr - smart pointer to package data.
|
||||
|
||||
* \brief Constructor for Event. User needs to dynamically allocate memory
|
||||
* for package data and pass in the smart pointer so the data member
|
||||
* can be initialised properly.
|
||||
****************************************************************************/
|
||||
SHEvent(PackagePtr);
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Get pointer to data package.
|
||||
*
|
||||
* \return Smart pointer to Package Base, needs to be cast to correct
|
||||
* derived class.
|
||||
****************************************************************************/
|
||||
PackagePtr const& GetData() const;
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Gets the type of package.
|
||||
****************************************************************************/
|
||||
SHPackageType GetType() const;
|
||||
private:
|
||||
PackagePtr package;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif // !SH_EVENT_BASE_H
|
|
@ -0,0 +1,144 @@
|
|||
/******************************************************************************
|
||||
* \file SHEventManager.cpp
|
||||
* \author Loh Xiao Qi
|
||||
* \brief Function Implmentations for SHEventManager
|
||||
*
|
||||
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
|
||||
* or disclosure of this file or its contents without the prior
|
||||
* written consent of Digipen Institute of Technology is prohibited
|
||||
******************************************************************************/
|
||||
#include "SHpch.h"
|
||||
#include "SHEventManager.h"
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
SHEventManager* SHEventManager::instance;
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Ctor and Dtor. Dtor will delete instance of self stored at
|
||||
* data member instance.
|
||||
****************************************************************************/
|
||||
SHEventManager::SHEventManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Exit function to terminate the manager properly and deallocate
|
||||
* memory.
|
||||
****************************************************************************/
|
||||
void SHEventManager::Exit()
|
||||
{
|
||||
delete instance;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Returns singleton instance of event manager in heap memory. If
|
||||
* instance does not exist, one is created immediately.
|
||||
****************************************************************************/
|
||||
SHEventManager& SHEventManager::GetEventManagerInstance()
|
||||
{
|
||||
// Create instance of event manager and return
|
||||
if (!instance)
|
||||
{
|
||||
instance = new SHEventManager();
|
||||
}
|
||||
|
||||
return *instance;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Returns function pointer to entry point for events.
|
||||
****************************************************************************/
|
||||
EventManagerListener SHEventManager::GetListenerFunction()
|
||||
{
|
||||
return &(instance->CatchEvent);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \param ListenerConstPtr - Const pointer to listener that sent event.
|
||||
* \param EventType - Templated type for every type of event
|
||||
|
||||
* \brief Receives event from the listeners.
|
||||
****************************************************************************/
|
||||
void SHEventManager::CatchEvent(SHEvent event)
|
||||
{
|
||||
|
||||
// Do something with the event
|
||||
|
||||
instance->Broadcast(event);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \param ResponseFunction - function pointer from receiver to be passed
|
||||
* into event manager to be called when events are broadcasted.
|
||||
* \param SHPackageType - package type that corresponding subscriber is
|
||||
* subscribing to.
|
||||
|
||||
* \brief Links a function pointer from a subscriber to a particular
|
||||
* package type
|
||||
****************************************************************************/
|
||||
void SHEventManager::SubscribeTo(SHPackageType pkgType, ReceiverPtr receiver)
|
||||
{
|
||||
SHEventManager::GetEventManagerInstance().RegisterReceiverToType(pkgType, receiver);
|
||||
}
|
||||
|
||||
void SHEventManager::StaticSubscribeTo(SHPackageType pkgType, ResponseFunction func)
|
||||
{
|
||||
SHEventManager::GetEventManagerInstance().RegisterStaticReceiverToType(pkgType, func);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \param ReceiverPtr - Pointer to receiver
|
||||
* \param ListenerConstPtr - Const pointer to listener that receiver is
|
||||
* subscribing to.
|
||||
|
||||
* \brief Registers receiver as a subscriber to listener in the registry.
|
||||
****************************************************************************/
|
||||
void SHEventManager::RegisterReceiverToType(
|
||||
SHPackageType pkgType, ReceiverPtr receiver)
|
||||
{
|
||||
if (packageReceiverRegistry.contains(pkgType))
|
||||
{
|
||||
packageReceiverRegistry[pkgType].emplace_back(receiver);
|
||||
}
|
||||
else
|
||||
{
|
||||
packageReceiverRegistry.emplace(pkgType, std::vector{ receiver });
|
||||
}
|
||||
}
|
||||
|
||||
void SHEventManager::RegisterStaticReceiverToType(SHPackageType pkgType, ResponseFunction func)
|
||||
{
|
||||
if (staticPackageReceiverRegistry.contains(pkgType))
|
||||
{
|
||||
staticPackageReceiverRegistry[pkgType].emplace_back(func);
|
||||
}
|
||||
else
|
||||
{
|
||||
staticPackageReceiverRegistry.emplace(pkgType, std::vector{ func });
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \param ListenerConstPtr - Const pointer to listener that sent event.
|
||||
* \param EventType - Event data
|
||||
|
||||
* \brief Broadcast event to all receivers that are subscribed to this
|
||||
* listener.
|
||||
****************************************************************************/
|
||||
void SHEventManager::Broadcast(SHEvent const& event)
|
||||
{
|
||||
ResponseVec& receivers{ packageReceiverRegistry[event.GetType()] };
|
||||
for (auto& receiver : receivers)
|
||||
{
|
||||
receiver->Receive(event.GetData());
|
||||
}
|
||||
|
||||
auto& funcs{ staticPackageReceiverRegistry[event.GetType()] };
|
||||
for (auto func : funcs)
|
||||
{
|
||||
func(event.GetData());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
/******************************************************************************
|
||||
* \file SHEventManager.h
|
||||
* \author Loh Xiao Qi
|
||||
* \brief Class declaration for event manager.
|
||||
*
|
||||
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
|
||||
* or disclosure of this file or its contents without the prior written consent
|
||||
* of Digipen Institute of Technology is prohibited.
|
||||
******************************************************************************/
|
||||
#ifndef SH_EVENT_MANAGER_H
|
||||
#define SH_EVENT_MANAGER_H
|
||||
|
||||
#include "SHEvent.h"
|
||||
#include "SHEventReceiver.h"
|
||||
#include <list>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
using ResponseFunction = std::function<void(ConstPackagePtr&)>;
|
||||
using ReceiverPtr = std::shared_ptr<SHEventReceiver>;
|
||||
using ResponseVec = std::vector<ReceiverPtr>;
|
||||
using StaticResponseVec = std::vector<ResponseFunction>;
|
||||
|
||||
using EventManagerListener = std::function<void(SHEvent)>;
|
||||
|
||||
class SHEventManager
|
||||
{
|
||||
public:
|
||||
/****************************************************************************
|
||||
* \brief Ctor and Dtor. Dtor will delete instance of self stored at
|
||||
* data member instance.
|
||||
****************************************************************************/
|
||||
SHEventManager();
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Returns singleton instance of event manager in heap memory. If
|
||||
* instance does not exist, one is created immediately.
|
||||
****************************************************************************/
|
||||
static SHEventManager& GetEventManagerInstance();
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Returns function pointer to entry point for events.
|
||||
****************************************************************************/
|
||||
static EventManagerListener GetListenerFunction();
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Exit function to terminate the manager properly and deallocate
|
||||
* memory.
|
||||
****************************************************************************/
|
||||
static void Exit();
|
||||
|
||||
/****************************************************************************
|
||||
* \param ListenerConstPtr - Const pointer to listener that sent event.
|
||||
* \param EventType - Templated type for every type of event
|
||||
|
||||
* \brief Receives event from the listeners.
|
||||
****************************************************************************/
|
||||
static void CatchEvent(SHEvent);
|
||||
|
||||
/****************************************************************************
|
||||
* \param ResponseFunction - function pointer from receiver to be passed
|
||||
* into event manager to be called when events are broadcasted.
|
||||
* \param SHPackageType - package type that corresponding subscriber is
|
||||
* subscribing to.
|
||||
|
||||
* \brief Links a function pointer from a subscriber to a particular
|
||||
* package type
|
||||
****************************************************************************/
|
||||
void SubscribeTo(SHPackageType, ReceiverPtr);
|
||||
|
||||
void StaticSubscribeTo(SHPackageType, ResponseFunction);
|
||||
private:
|
||||
// Singleton instance
|
||||
static SHEventManager* instance;
|
||||
|
||||
// Registry for broadcasters and subscribers
|
||||
std::unordered_map<
|
||||
SHPackageType,
|
||||
ResponseVec> packageReceiverRegistry;
|
||||
|
||||
// Registry for static broadcasters and subscribers
|
||||
std::unordered_map<
|
||||
SHPackageType,
|
||||
StaticResponseVec> staticPackageReceiverRegistry;
|
||||
|
||||
/****************************************************************************
|
||||
* \param ListenerConstPtr - Const pointer to listener that sent event.
|
||||
* \param EventType - Event data
|
||||
|
||||
* \brief Broadcast event to all receivers that are subscribed to this
|
||||
* listener.
|
||||
****************************************************************************/
|
||||
void Broadcast(SHEvent const&);
|
||||
|
||||
/****************************************************************************
|
||||
* \param ReceiverPtr - Pointer to receiver
|
||||
* \param ListenerConstPtr - Const pointer to listener that receiver is
|
||||
* subscribing to.
|
||||
|
||||
* \brief Registers receiver as a subscriber to listener in the registry.
|
||||
****************************************************************************/
|
||||
void RegisterReceiverToType(SHPackageType, ReceiverPtr);
|
||||
|
||||
void RegisterStaticReceiverToType(SHPackageType, ResponseFunction);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // !SH_EVENTS_MANAGER_H
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef SH_EVENT_RECEIVER_H
|
||||
#define SH_EVENT_RECEIVER_H
|
||||
|
||||
#include "SHEvent.h"
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
class SHEventReceiver
|
||||
{
|
||||
private:
|
||||
public:
|
||||
virtual void Receive(PackagePtr) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class SHEventReceiverSpec : public SHEventReceiver
|
||||
{
|
||||
private:
|
||||
T* object;
|
||||
void(T::*callback)(PackagePtr);
|
||||
|
||||
public:
|
||||
SHEventReceiverSpec(T* obj, void(T::*cb)(PackagePtr))
|
||||
:SHEventReceiver(), object{obj}, callback{ cb }
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Receive(PackagePtr evt)
|
||||
{
|
||||
(object->*callback)(evt);
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif // !SH_EVENT_RECEIVER_H
|
|
@ -30,3 +30,6 @@
|
|||
#include <functional>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <cstddef>
|
||||
|
||||
#include "Common/SHCommonTypes.h"
|
||||
|
|
Loading…
Reference in New Issue