8#include <unordered_map>
11#include "io/Serializable.h"
12#include "input/KeyboardKey.hpp"
13#include "gccontainers/GcVector.hpp"
14#include "game/callbacks/NodeNotificationBroadcaster.hpp"
19#include "Node.generated.h"
21namespace ne RNAMESPACE() {
33 enum class TickGroup { FIRST, SECOND };
66 static size_t getAliveNodeCount();
78 Node(
const std::string& sName);
81 Node& operator=(
const Node&) =
delete;
86 virtual ~Node()
override;
96 static std::variant<sgc::GcPtr<Node>,
Error>
97 deserializeNodeTree(
const std::filesystem::path& pathToFile);
104 void setNodeName(
const std::string& sName);
116 void detachFromParentAndDespawn();
141 const sgc::GcPtr<Node>& pNode,
142 AttachmentRule locationRule = AttachmentRule::KEEP_WORLD,
143 AttachmentRule rotationRule = AttachmentRule::KEEP_WORLD,
144 AttachmentRule scaleRule = AttachmentRule::KEEP_WORLD);
151 void setSerialize(
bool bSerialize);
172 [[nodiscard]] std::optional<Error>
173 serializeNodeTree(
const std::filesystem::path& pathToFile,
bool bEnableBackup);
180 std::string getNodeName()
const;
188 sgc::GcPtr<Node> getWorldRootNode();
204 std::pair<std::recursive_mutex, sgc::GcPtr<Node>>* getParentNode();
219 std::pair<std::recursive_mutex, sgc::GcVector<sgc::GcPtr<Node>>>* getChildNodes();
235 template <
typename NodeType>
236 requires std::derived_from<NodeType, Node>
237 sgc::GcPtr<NodeType> getParentNodeOfType(
const std::string& sParentNodeName =
"");
253 template <
typename NodeType>
254 requires std::derived_from<NodeType, Node>
255 sgc::GcPtr<NodeType> getChildNodeOfType(
const std::string& sChildNodeName =
"");
269 TickGroup getTickGroup()
const;
278 std::optional<size_t> getNodeId()
const;
285 bool isCalledEveryFrame();
292 bool isReceivingInput();
309 bool isParentOf(
Node* pNode);
319 bool isChildOf(
Node* pNode);
327 bool isSerialized()
const;
338 void setIsCalledEveryFrame(
bool bEnable);
359 void setTickGroup(TickGroup tickGroup);
372 void setIsReceivingInput(
bool bEnable);
399 Timer* createTimer(
const std::string& sTimerName);
431 template <
typename FunctionType>
433 std::scoped_lock guard(mtxIsSpawned.first, mtxCreatedBroadcasters.first);
436 auto pNewBroadcaster = std::unique_ptr<NodeNotificationBroadcaster<FunctionType>>(
438 const auto pRawBroadcaster = pNewBroadcaster.get();
441 mtxCreatedBroadcasters.second.push_back(std::move(pNewBroadcaster));
443 if (mtxIsSpawned.second) {
445 if (!iNodeId.has_value()) [[unlikely]] {
446 Error error(std::format(
447 "node \"{}\" created a new broadcaster while being spawned but node's ID is empty",
454 pRawBroadcaster->onOwnerNodeSpawning(
this);
457 return pRawBroadcaster;
483 std::recursive_mutex,
485 getActionEventBindings();
512 std::recursive_mutex,
513 std::unordered_map<
unsigned int, std::function<void(
KeyboardModifiers,
float)>>>*
514 getAxisEventBindings();
523 std::recursive_mutex* getSpawnDespawnMutex();
653 const std::string& sObjectUniqueId,
654 const std::unordered_map<std::string, std::string>& customAttributes = {},
656 sgc::GcPtr<Node> pDeserializedOriginalObject =
nullptr)
658 this->pDeserializedOriginalObject = pDeserializedOriginalObject;
662 sgc::GcPtr<Node> pDeserializedOriginalObject =
nullptr;
675 bool enableTimer(
Timer* pTimer,
bool bEnable);
688 void onInputActionEvent(
unsigned int iActionId,
KeyboardModifiers modifiers,
bool bIsPressedDown);
701 void onInputAxisEvent(
unsigned int iAxisEventId,
KeyboardModifiers modifiers,
float input);
715 void notifyAboutAttachedToNewParent(
bool bThisNodeBeingAttached);
723 void notifyAboutDetachingFromParent(
bool bThisNodeBeingDetached);
732 World* findValidWorld();
744 std::variant<std::vector<SerializableObjectInformationWithGcPointer>,
Error>
745 getInformationForSerialization(
size_t& iId, std::optional<size_t> iParentId);
757 bool isTreeDeserializedFromOneFile(
const std::string& sPathRelativeToRes);
775 void unlockChildren();
779 std::
string sNodeName;
782 std::pair<std::recursive_mutex, sgc::GcVector<sgc::GcPtr<
Node>>> mtxChildNodes;
785 std::pair<std::recursive_mutex, sgc::GcPtr<
Node>> mtxParentNode;
789 std::recursive_mutex,
791 mtxBindedActionEvents;
795 std::recursive_mutex,
809 std::pair<std::recursive_mutex, std::vector<std::unique_ptr<
Timer>>> mtxCreatedTimers;
820 mtxCreatedBroadcasters;
823 std::pair<std::recursive_mutex,
bool> mtxIsSpawned;
826 std::pair<std::recursive_mutex,
bool> mtxIsCalledEveryFrame;
832 std::pair<std::recursive_mutex,
bool> mtxIsReceivingInput;
842 TickGroup tickGroup = TickGroup::FIRST;
845 std::optional<
size_t> iNodeId;
851 bool bSerialize = true;
854 static inline const auto sParentNodeIdAttributeName = "parent_node_id";
857 static inline const auto sExternalNodeTreePathAttributeName =
858 "external_node_tree_path_relative_to_res";
863 template <typename NodeType>
864 requires std::derived_from<NodeType,
Node>
865 sgc::GcPtr<NodeType>
Node::getParentNodeOfType(const std::
string& sParentNodeName) {
866 std::scoped_lock guard(mtxParentNode.first);
869 if (mtxParentNode.second ==
nullptr) {
874 sgc::GcPtr<NodeType> pCastedParentNode =
dynamic_cast<NodeType*
>(mtxParentNode.second.get());
875 if (pCastedParentNode !=
nullptr &&
876 (sParentNodeName.empty() || mtxParentNode.second->getNodeName() == sParentNodeName)) {
878 return pCastedParentNode;
882 return mtxParentNode.second->getParentNodeOfType<NodeType>(sParentNodeName);
885 template <
typename NodeType>
886 requires std::derived_from<NodeType, Node>
887 sgc::GcPtr<NodeType> Node::getChildNodeOfType(
const std::string& sChildNodeName) {
888 std::scoped_lock guard(mtxChildNodes.first);
891 for (
auto& pChildNode : mtxChildNodes.second) {
893 sgc::GcPtr<NodeType> pCastedChildNode =
dynamic_cast<NodeType*
>(pChildNode.get());
894 if (pCastedChildNode !=
nullptr &&
895 (sChildNodeName.empty() || pChildNode->getNodeName() == sChildNodeName)) {
897 return pCastedChildNode;
901 const auto pNode = pChildNode->getChildNodeOfType<NodeType>(sChildNodeName);
902 if (pNode ==
nullptr) {
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
Definition: GameInstance.h:32
Definition: GameManager.h:34
Definition: GuidProperty.h:30
Definition: KeyboardKey.hpp:10
Definition: NodeNotificationBroadcaster.hpp:16
Definition: NodeNotificationBroadcaster.hpp:45
AttachmentRule
Definition: Node.h:51
virtual void onBeforeDetachedFromParent(bool bThisNodeBeingDetached)
Definition: Node.h:620
virtual void onMouseMove(double xOffset, double yOffset)
Definition: Node.h:536
NodeNotificationBroadcaster< FunctionType > * createNotificationBroadcaster()
Definition: Node.h:432
virtual void onBeforeNewFrame(float timeSincePrevFrameInSec)
Definition: Node.h:560
virtual void onChildNodesSpawned()
Definition: Node.h:589
virtual void onDespawning()
Definition: Node.h:602
virtual void onAfterAttachedToNewParent(bool bThisNodeBeingAttached)
Definition: Node.h:635
virtual void onSpawning()
Definition: Node.h:576
virtual void onMouseScrollMove(int iOffset)
Definition: Node.h:546
Definition: Serializable.h:113
Definition: SerializeProperty.h:42