ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
scene_system.hpp
Go to the documentation of this file.
1
10
16
17#ifndef TOYMAKERENGINE_SCENESYSTEM_H
18#define TOYMAKERENGINE_SCENESYSTEM_H
19
20#include <vector>
21#include <memory>
22#include <unordered_map>
23#include <type_traits>
24
25#include <glm/glm.hpp>
26#include <glm/gtc/quaternion.hpp>
27
29#include "core/ecs_world.hpp"
31#include "scene_components.hpp"
32#include "render_system.hpp"
33#include "texture.hpp"
35#include "signals.hpp"
36
37namespace ToyMaker {
38
39 class SceneNodeCore;
40 class SceneNode;
41 class ViewportNode;
42 class SceneSystem;
43
53 enum class RelativeTo : uint8_t {
54 PARENT=0, //< Compute relative to/on top of this node's parent's transform.
55 // WORLD=1,
56 // CAMERA=2,
57 };
58
60 NLOHMANN_JSON_SERIALIZE_ENUM(RelativeTo, {
61 {RelativeTo::PARENT, "parent"},
62 });
63
71 ENTITY_NULL = kMaxEntities,
72 };
73
79 extern const std::string kSceneRootName;
80
86 class SceneNodeCore: public std::enable_shared_from_this<SceneNodeCore> {
87 public:
95 static void SceneNodeCore_del_(SceneNodeCore* sceneNode);
96
103 virtual ~SceneNodeCore()=default;
104
112 template <typename TComponent>
113 void addComponent(const TComponent& component, const bool bypassSceneActivityCheck=false);
114
121 void addComponent(const nlohmann::json& jsonComponent, const bool bypassSceneActivityCheck=false);
122
131 template <typename TComponent>
132 TComponent getComponent(const float simulationProgress=1.f) const;
133
141 template <typename TComponent>
142 bool hasComponent() const;
143
151 bool hasComponent(const std::string& type) const;
152
159 template <typename TComponent>
160 void updateComponent(const TComponent& component);
161
167 void updateComponent(const nlohmann::json& component);
168
176 template <typename TComponent>
177 void addOrUpdateComponent(const TComponent& component, const bool bypassSceneActivityCheck=false);
178
185 void addOrUpdateComponent(const nlohmann::json& component, const bool bypassSceneActivityCheck=false);
186
192 template <typename TComponent>
193 void removeComponent();
194
203 template <typename TSystem>
204 void setEnabled(bool state);
205
213 template <typename TSystem>
214 bool getEnabled() const;
215
221 EntityID getEntityID() const;
222
228 WorldID getWorldID() const;
229
236
242 std::weak_ptr<ECSWorld> getWorld() const;
243
252 bool inScene() const;
253
264 bool isActive() const;
265
273 bool isAncestorOf(std::shared_ptr<const SceneNodeCore> sceneNode) const;
274
283 bool hasNode(const std::string& pathToChild) const;
284
291 void addNode(std::shared_ptr<SceneNodeCore> node, const std::string& where);
292
298 std::vector<std::shared_ptr<SceneNodeCore>> getChildren();
299
305 std::vector<std::shared_ptr<const SceneNodeCore>> getChildren() const;
306
312 std::vector<std::shared_ptr<SceneNodeCore>> getDescendants();
313
321 template <typename TObject=std::shared_ptr<SceneNode>>
322 TObject getByPath(const std::string& where);
323
331 template <typename TSceneNode=SceneNode>
332 std::shared_ptr<TSceneNode> getNodeByID(EntityID entityID);
333
340 std::string getPathFromAncestor(std::shared_ptr<const SceneNodeCore> ancestor) const;
341
347 virtual std::shared_ptr<ViewportNode> getLocalViewport();
348
354 virtual std::shared_ptr<const ViewportNode> getLocalViewport() const;
355
362 std::shared_ptr<SceneNodeCore> getNode(const std::string& where);
363
369 std::shared_ptr<SceneNodeCore> getParentNode();
370
376 std::shared_ptr<const SceneNodeCore> getParentNode() const;
377
384 std::shared_ptr<SceneNodeCore> removeNode(const std::string& where);
385
391 std::vector<std::shared_ptr<SceneNodeCore>> removeChildren();
392
398 std::string getName() const;
399
405 void setName(const std::string& name);
406
414 std::string getViewportLocalPath() const;
415
425 inline void setPrototype_(std::shared_ptr<SceneNodeCore> prototype) { mPrototype=prototype; }
426
427 protected:
428
434 virtual void joinWorld(ECSWorld& world);
435
443 static std::shared_ptr<SceneNodeCore> copy(const std::shared_ptr<const SceneNodeCore> other);
444
453 template<typename ...TComponents>
454 SceneNodeCore(const Placement& placement, const std::string& name, TComponents...components);
455
462 SceneNodeCore(const nlohmann::json& jsonSceneNode);
463
469 SceneNodeCore(const SceneNodeCore& sceneObject);
470
471 // /**
472 // * @brief Copy assignment operator.
473 // *
474 // * @todo Sit down and figure out whether this operator will ever actually need to be used.
475 // */
476 // BaseSceneNode& operator=(const BaseSceneNode& sceneObject);
477
482 virtual void onCreated();
483
488 virtual void onActivated();
489
494 virtual void onDeactivated();
495
502 virtual void onDestroyed();
503
511 static void validateName(const std::string& nodeName);
512
513 private:
518 struct Key {};
519
528 template<typename ...TComponents>
529 SceneNodeCore(const Key&, const Placement& placement, const std::string& name, TComponents...components);
530
535 enum StateFlags: uint8_t {
536 ENABLED=0x1, //< This node is intended to be made active as soon as its added to the SceneSystem's scene tree.
537 ACTIVE=0x2, //< This node is presently active on the SceneSystem's scene tree.
538 };
539
546 template <typename TObject, typename Enable=void>
548 static TObject get(std::shared_ptr<SceneNodeCore> rootNode, const std::string& where);
549 static constexpr bool s_valid { false };
550 };
551
557 virtual std::shared_ptr<SceneNodeCore> clone() const;
558
565 void copyDescendants(const SceneNodeCore& other);
566
575 static void setParentViewport(std::shared_ptr<SceneNodeCore> node, std::shared_ptr<ViewportNode> newViewport);
576
583 static std::shared_ptr<SceneNodeCore> disconnectNode(std::shared_ptr<SceneNodeCore> node);
584
592 static bool detectCycle(std::shared_ptr<SceneNodeCore> node);
593
603 static std::tuple<std::string, std::string> nextInPath(const std::string& where);
604
610 void copyAndReplaceAttributes(const SceneNodeCore& other);
611
617
622 std::string mName {};
623
630 uint8_t mStateFlags { 0x00 | StateFlags::ENABLED };
631
636 RelativeTo mRelativeTo{ RelativeTo::PARENT };
637
642 std::shared_ptr<Entity> mEntity { nullptr };
643
648 std::weak_ptr<SceneNodeCore> mParent {};
649
654 std::weak_ptr<ViewportNode> mParentViewport {};
655
662 std::unordered_map<std::string, std::size_t> mChildNameToNode {};
663
668 std::vector<std::shared_ptr<SceneNodeCore>> mChildren {};
669
674 std::shared_ptr<SceneNodeCore> mPrototype { nullptr };
675
683
684 friend class SceneSystem;
685 template<typename TSceneNode>
686 friend class BaseSceneNode;
687 friend class ViewportNode;
688 };
689
690
697 template <typename TSceneNode>
698 class BaseSceneNode: public SceneNodeCore {
699 public:
700
701 protected:
713 template <typename ...TComponents>
714 static std::shared_ptr<TSceneNode> create(const Key&, const Placement& placement, const std::string& name, TComponents...components);
715
727 template <typename ...TComponents>
728 static std::shared_ptr<TSceneNode> create(const Placement& placement, const std::string& name, TComponents...components);
729
738 static std::shared_ptr<TSceneNode> create(const nlohmann::json& sceneNodeDescription);
739
740
750 static std::shared_ptr<TSceneNode> copy(const std::shared_ptr<const TSceneNode> sceneNode);
751 template <typename...TComponents>
752
761 BaseSceneNode(const Key& key, const Placement& placement, const std::string& name, TComponents...components):
762 SceneNodeCore{ key, placement, name, components... }
763 {}
764
773 template<typename ...TComponents>
774 BaseSceneNode(const Placement& placement, const std::string& name, TComponents...components):
775 SceneNodeCore{ placement, name, components... }
776 {}
777
783 BaseSceneNode(const nlohmann::json& nodeDescription) : SceneNodeCore { nodeDescription } {}
784
790 BaseSceneNode(const SceneNodeCore& other): SceneNodeCore{ other } {}
791
792 friend class SceneNodeCore;
793 };
794
802 class SceneNode: public BaseSceneNode<SceneNode>, public Resource<SceneNode> {
803 public:
804 template <typename ...TComponents>
805 static std::shared_ptr<SceneNode> create(const Placement& placement, const std::string& name, TComponents...components);
806 static std::shared_ptr<SceneNode> create(const nlohmann::json& sceneNodeDescription);
807 static std::shared_ptr<SceneNode> copy(const std::shared_ptr<const SceneNode> other);
808
809 static inline std::string getResourceTypeName() { return "SceneNode"; }
810
811 protected:
812 template<typename ...TComponents>
813 SceneNode(const Placement& placement, const std::string& name, TComponents...components):
814 BaseSceneNode<SceneNode>{placement, name, components...},
816 {}
817
818 SceneNode(const nlohmann::json& jsonSceneNode):
819 BaseSceneNode<SceneNode>{jsonSceneNode},
821 {}
822
823 SceneNode(const SceneNode& sceneObject):
824 BaseSceneNode<SceneNode>{sceneObject},
826 {}
827 friend class BaseSceneNode<SceneNode>;
828 };
829
843 class ViewportNode: public BaseSceneNode<ViewportNode>, public Resource<ViewportNode>, public SignalTracker {
844 public:
854 enum class ResizeType: uint8_t {
855 OFF=0, //< No resize, render texture is rendered as is with no scaling.
856 VIEWPORT_DIMENSIONS, //< Viewport transform configured per stretch mode and requested dimensions
857 TEXTURE_DIMENSIONS, //< Texture result rendered in base dimensions, and then warped to fit request dimensions
858 };
859
864 enum class ResizeMode: uint8_t {
865 FIXED_ASPECT=0, //< both, while retaining aspect ratio.
866 EXPAND_VERTICALLY, //< Expand vertically if permitted by target dimensions, otherwise constrain to aspect.
867 EXPAND_HORIZONTALLY, //< Expand horiontally if possible by target dimensions, otherwise constrain to aspect.
868 EXPAND_FILL, //< No constraint in either dimension, expand to fill target dimensions always.
869 };
870
875 enum class UpdateMode: uint8_t {
876 NEVER=0, //< No rerender takes place even when render is called for this viewport.
877 ONCE, //< Update on next render frame, then set to UpdateMode::NEVER
878 ON_FETCH, //< Update whenever a request for the texture is made, where frequency is entirely dependent on caller.
879 ON_FETCH_CAP_FPS, //< Update on request, but ignore requests exceeding FPS cap. Final frequency is constrained to the FPS cap configured for this viewport, but may be lower than it.
880 ON_RENDER, //< Update every render call with no constraint, so frequency depends entirely on the render frequency of the application loop.
881 ON_RENDER_CAP_FPS, //< Update on render call when fps cap isn't exceeded. Final FPS may be lower than specified as a cap.
882 };
883
889
894 ResizeType mResizeType { ResizeType::VIEWPORT_DIMENSIONS };
899 ResizeMode mResizeMode { ResizeMode::EXPAND_HORIZONTALLY };
900
905 RenderType mRenderType { RenderType::BASIC_3D };
906
911 glm::u16vec2 mBaseDimensions { 800, 600 };
912
917 glm::u16vec2 mComputedDimensions { 800, 600 };
918
923 glm::u16vec2 mRequestedDimensions { 800, 600 };
924
935 float mRenderScale { 1.f };
936
941 UpdateMode mUpdateMode { UpdateMode::ON_RENDER_CAP_FPS };
942
948 float mFPSCap { 60.f };
949 };
950
961 static std::shared_ptr<ViewportNode> create(const std::string& name, bool inheritsWorld, bool allowActionFlowThrough, const RenderConfiguration& renderConfiguration, std::shared_ptr<Texture> skybox);
962
974 static std::shared_ptr<ViewportNode> create(const nlohmann::json& sceneNodeDescription);
975
982 static std::shared_ptr<ViewportNode> copy(const std::shared_ptr<const ViewportNode> other);
983
989 static inline std::string getResourceTypeName() { return "ViewportNode"; }
990
997
1005 void updateExposure(float newExposure);
1006
1015 void updateGamma(float newGamma);
1016
1022 float getExposure();
1023
1029 float getGamma();
1030
1036 std::shared_ptr<ViewportNode> getLocalViewport() override;
1037
1043 virtual std::shared_ptr<const ViewportNode> getLocalViewport() const override;
1044
1051 std::shared_ptr<Texture> fetchRenderResult(float simulationProgress);
1052
1058 void setActiveCamera(const std::string& cameraPath);
1059
1065 void setActiveCamera(std::shared_ptr<SceneNodeCore> cameraNode);
1066
1072 std::shared_ptr<const SceneNodeCore> getActiveCamera() const;
1073
1079 RenderConfiguration getRenderConfiguration() const;
1080
1086 void setRenderConfiguration(const RenderConfiguration& renderConfiguration);
1087
1097 void setSkybox(std::shared_ptr<Texture> skybox);
1098
1105
1112
1118 void setRenderScale(float renderScale);
1119
1126
1132 void setFPSCap(float fpsCap);
1133
1141 void requestDimensions(glm::u16vec2 requestedDimensions);
1142
1149
1157 bool handleAction(std::pair<ActionDefinition, ActionData> pendingAction);
1158
1166
1171 ~ViewportNode() override;
1172
1180 inline uint32_t getViewportLoadOrdinal() const { return mViewportLoadOrdinal; }
1181
1186 Signal<RenderConfiguration> mRenderConfigurationUpdated {
1187 *this, "RenderConfigurationUpdated"
1188 };
1189
1190 protected:
1191 ViewportNode(const Placement& placement, const std::string& name):
1192 BaseSceneNode<ViewportNode>{placement, name},
1193 Resource<ViewportNode>{0},
1194 mViewportLoadOrdinal { SDL_GetTicks() }
1195 {}
1196 ViewportNode(const nlohmann::json& jsonSceneNode):
1197 BaseSceneNode<ViewportNode>{jsonSceneNode},
1199 mViewportLoadOrdinal { SDL_GetTicks() }
1200 {}
1201 ViewportNode(const ViewportNode& sceneObject):
1202 BaseSceneNode<ViewportNode>{sceneObject},
1204 mViewportLoadOrdinal { SDL_GetTicks() }
1205 {}
1206
1211 std::shared_ptr<ECSWorld> mOwnWorld { nullptr };
1212
1217 void onActivated() override;
1218
1223 void onDeactivated() override;
1224
1230 void joinWorld(ECSWorld& world) override;
1231
1232 private:
1243 static std::shared_ptr<ViewportNode> create(const Key& key, const std::string& name, bool inheritsWorld, const RenderConfiguration& renderConfiguration, std::shared_ptr<Texture> skybox);
1244
1252 ViewportNode(const Key& key, const Placement& placement, const std::string& name):
1253 BaseSceneNode<ViewportNode>{key, Placement{}, name},
1255 {(void)placement; /*prevent unused parameter warnings*/}
1256
1262 std::shared_ptr<SceneNodeCore> clone() const override;
1263
1268 void createAndJoinWorld();
1269
1275 void registerDomainCamera(std::shared_ptr<SceneNodeCore> cameraNode);
1276
1282 void unregisterDomainCamera(std::shared_ptr<SceneNodeCore> cameraNode);
1283 std::shared_ptr<SceneNodeCore> findFallbackCamera();
1284
1291 void resizeDomainCameras(const glm::vec2& computedDimensions);
1292
1298 std::vector<std::shared_ptr<ViewportNode>> getActiveDescendantViewports();
1299
1305 std::vector<std::weak_ptr<ECSWorld>> getActiveDescendantWorlds();
1306
1316 uint32_t render(float simulationProgress, uint32_t variableStep);
1317
1324 void render_(float simulationProgress);
1325
1332 inline SDL_Rect getCenteredViewportCoordinates() const {
1333 return {
1334 mRenderConfiguration.mRequestedDimensions.x/2 - mRenderConfiguration.mComputedDimensions.x/2, mRenderConfiguration.mRequestedDimensions.y/2 - mRenderConfiguration.mComputedDimensions.y/2,
1335 mRenderConfiguration.mComputedDimensions.x, mRenderConfiguration.mComputedDimensions.y
1336 };
1337 }
1338
1344 bool operator() (const std::shared_ptr<ViewportNode>& one, const std::shared_ptr<ViewportNode>& two) const {
1345 return one->getViewportLoadOrdinal() < two->getViewportLoadOrdinal();
1346 }
1347 };
1348
1353 uint64_t mViewportLoadOrdinal { std::numeric_limits<uint64_t>::max() };
1354
1360
1366
1371 bool mActionFlowthrough { false };
1372
1378
1383 std::set<std::shared_ptr<ViewportNode>, ViewportChildComp_> mChildViewports {};
1384
1389 std::shared_ptr<SceneNodeCore> mActiveCamera { nullptr };
1390
1395 std::set<std::shared_ptr<SceneNodeCore>, std::owner_less<std::shared_ptr<SceneNodeCore>>> mDomainCameras {};
1396
1401 RenderSetID mRenderSet;
1402
1409 std::shared_ptr<Texture> mTextureResult { nullptr };
1410
1416
1421 uint32_t mTimeSinceLastRender { static_cast<uint32_t>(1000/mRenderConfiguration.mFPSCap) };
1422
1423 friend class BaseSceneNode<ViewportNode>;
1424 friend class SceneNodeCore;
1425 friend class SceneSystem;
1426 };
1427
1435 class SceneSystem: public System<SceneSystem, std::tuple<>, std::tuple<Placement, SceneHierarchyData, Transform>> {
1436 public:
1442 explicit SceneSystem(std::weak_ptr<ECSWorld> world):
1443 System<SceneSystem, std::tuple<>, std::tuple<Placement, SceneHierarchyData, Transform>> { world }
1444 {}
1445
1451 static std::string getSystemTypeName() { return "SceneSystem"; }
1452
1461 bool isSingleton() const override { return true; }
1462
1470 template<typename TObject=std::shared_ptr<SceneNode>>
1471 TObject getByPath(const std::string& where);
1472
1480 template <typename TSceneNode>
1481 std::shared_ptr<TSceneNode> getNodeByID(const UniversalEntityID& universalEntityID);
1482
1489 std::vector<std::shared_ptr<SceneNodeCore>> getNodesByID(const std::vector<UniversalEntityID>& universalEntityIDs);
1490
1497 std::shared_ptr<SceneNodeCore> getNode(const std::string& where);
1498
1505 std::shared_ptr<SceneNodeCore> removeNode(const std::string& where);
1506
1513 void addNode(std::shared_ptr<SceneNodeCore> node, const std::string& where);
1514
1520 std::weak_ptr<ECSWorld> getRootWorld() const;
1521
1528
1537 void onApplicationInitialize(const ViewportNode::RenderConfiguration& rootViewportRenderConfiguration);
1538
1543 void onApplicationStart();
1544
1549 void onApplicationEnd();
1550
1558 void simulationStep(uint32_t simStepMillis, std::vector<std::pair<ActionDefinition, ActionData>> triggeredActions={});
1559
1571 void variableStep(float simulationProgress, uint32_t simulationLagMillis, uint32_t variableStepMillis, std::vector<std::pair<ActionDefinition, ActionData>> triggeredActions={});
1572
1577 void transformStep(uint32_t timestepMillis);
1578
1584
1592 uint32_t render(float simulationProgress, uint32_t variableStep);
1593
1594 private:
1604 class PlacementUpdateReporter: public System<PlacementUpdateReporter, std::tuple<Placement>, std::tuple<Transform, SceneHierarchyData>> {
1605 public:
1606 explicit PlacementUpdateReporter(std::weak_ptr<ECSWorld> world):
1607 System<PlacementUpdateReporter, std::tuple<Placement>, std::tuple<Transform, SceneHierarchyData>> { world }
1608 {}
1609 static std::string getSystemTypeName() { return "SceneSystem::PlacementUpdateReporter"; }
1610 private:
1611
1616 std::set<EntityID> mReportedEntities {};
1617
1622 void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override;
1623
1629 inline void clearReportList() { mReportedEntities.clear(); }
1630
1631 friend class SceneSystem;
1632 };
1633
1634
1644 class TransformUpdateReporter: public System<TransformUpdateReporter, std::tuple<Transform>, std::tuple<Placement, SceneHierarchyData>> {
1645 public:
1646 explicit TransformUpdateReporter(std::weak_ptr<ECSWorld> world):
1647 System<TransformUpdateReporter, std::tuple<Transform>, std::tuple<Placement, SceneHierarchyData>> { world }
1648 {}
1649 static std::string getSystemTypeName() { return "SceneSystem::TransformUpdateReporter"; }
1650 private:
1655 std::set<EntityID> mReportedEntities {};
1656
1657
1662 void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override;
1663
1669 inline void clearReportList() { mReportedEntities.clear(); }
1670
1671 friend class SceneSystem;
1672 };
1673
1682 template <typename TSceneNode, typename Enable=void>
1684 static std::shared_ptr<TSceneNode> get(const UniversalEntityID& universalEntityID, SceneSystem& sceneSystem);
1685 };
1686
1694 bool isActive(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1695
1704
1712 bool inScene(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1713
1721 bool inScene(UniversalEntityID universalEntityID) const;
1722
1728 void markDirtyTransform(UniversalEntityID universalEntityID);
1729
1736 void markDirtyPlacement(UniversalEntityID universalEntityID);
1737
1743 std::vector<std::shared_ptr<ViewportNode>> getActiveViewports();
1744
1750 std::vector<std::weak_ptr<ECSWorld>> getActiveWorlds();
1751
1758 std::weak_ptr<ECSWorld> getWorld(WorldID world);
1759
1766 Transform getLocalTransform(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1767
1774 Transform getCachedWorldTransform(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1775
1789 Transform getInheritedTransform(std::shared_ptr<const SceneNodeCore> sceneNode) const;
1790
1796 void updateHierarchyDataInsertion(std::shared_ptr<SceneNodeCore> insertedNode);
1797
1803 void updateHierarchyDataRemoval(std::shared_ptr<SceneNodeCore> removedNode);
1804
1810 void nodeAdded(std::shared_ptr<SceneNodeCore> sceneNode);
1811
1817 void nodeRemoved(std::shared_ptr<SceneNodeCore> sceneNode);
1818
1825 void nodeActivationChanged(std::shared_ptr<SceneNodeCore> sceneNode, bool state);
1826
1832 void activateSubtree(std::shared_ptr<SceneNodeCore> sceneNode);
1833
1839 void deactivateSubtree(std::shared_ptr<SceneNodeCore> sceneNode);
1840
1847
1855 void onWorldTransformUpdate(UniversalEntityID universalEntityID);
1856
1863 std::shared_ptr<SceneNodeCore> getNodeByID(const UniversalEntityID& universalEntityID);
1864
1869 std::shared_ptr<ViewportNode> mRootNode{ nullptr };
1870
1875 std::map<UniversalEntityID, std::weak_ptr<SceneNodeCore>, std::less<UniversalEntityID>> mEntityToNode {};
1876
1881 std::set<UniversalEntityID, std::less<UniversalEntityID>> mActiveEntities {};
1882
1887 std::set<UniversalEntityID, std::less<UniversalEntityID>> mComputeTransformQueue {};
1888
1895 std::set<UniversalEntityID, std::less<UniversalEntityID>> mComputePlacementQueue {};
1896
1897 friend class SceneNodeCore;
1898 };
1899
1900
1901 template <typename TSceneNode>
1902 template <typename ...TComponents>
1903 std::shared_ptr<TSceneNode> BaseSceneNode<TSceneNode>::create(const Placement& placement, const std::string& name, TComponents...components) {
1904 std::shared_ptr<SceneNodeCore> newNode ( new TSceneNode(placement, name, components...), &SceneNodeCore_del_);
1905 newNode->onCreated();
1906 return std::static_pointer_cast<TSceneNode>(newNode);
1907 }
1908
1909 template <typename TSceneNode>
1910 template <typename ...TComponents>
1911 std::shared_ptr<TSceneNode> BaseSceneNode<TSceneNode>::create(const Key& key, const Placement& placement, const std::string& name, TComponents...components) {
1912 std::shared_ptr<SceneNodeCore> newNode( new TSceneNode(key, placement, name, components...), &SceneNodeCore_del_);
1913 newNode->onCreated();
1914 return std::static_pointer_cast<TSceneNode>(newNode);
1915 }
1916
1917 template <typename TSceneNode>
1918 std::shared_ptr<TSceneNode> BaseSceneNode<TSceneNode>::create(const nlohmann::json& sceneNodeDescription) {
1919 std::shared_ptr<SceneNodeCore> newNode{ new TSceneNode{ sceneNodeDescription }, &SceneNodeCore_del_};
1920 newNode->onCreated();
1921 return std::static_pointer_cast<TSceneNode>(newNode);
1922 }
1923
1924 template <typename TSceneNode>
1925 std::shared_ptr<TSceneNode> BaseSceneNode<TSceneNode>::copy(const std::shared_ptr<const TSceneNode> sceneNode) {
1926 std::shared_ptr<SceneNodeCore> newNode { SceneNodeCore::copy(sceneNode) };
1927 newNode->onCreated();
1928 return std::static_pointer_cast<TSceneNode>(newNode);
1929 }
1930
1931 template<typename ...TComponents>
1932 std::shared_ptr<SceneNode> SceneNode::create(const Placement& placement, const std::string& name, TComponents...components) {
1933 return BaseSceneNode<SceneNode>::create<TComponents...>(placement, name, components...);
1934 }
1935
1936 template <typename TObject>
1937 TObject SceneNodeCore::getByPath(const std::string& where) {
1938 return getByPath_Helper<TObject>::get(shared_from_this(), where);
1939 }
1940
1941 template <typename TObject>
1942 TObject SceneSystem::getByPath(const std::string& where) {
1943 return mRootNode->getByPath<TObject>(where);
1944 }
1945
1946 template <typename TSceneNode>
1947 inline std::shared_ptr<TSceneNode> SceneSystem::getNodeByID(const UniversalEntityID& universalEntityID) {
1948 return getNodeByID_Helper<TSceneNode>::get(universalEntityID, *this);
1949 }
1950
1951 // Fail retrieval in cases where no explicitly defined object by path
1952 // method exists
1953 template <typename TObject, typename Enable>
1954 inline TObject SceneNodeCore::getByPath_Helper<TObject, Enable>::get(std::shared_ptr<SceneNodeCore> rootNode, const std::string& where) {
1955 static_assert(false && "No Object-by-Path method for this type exists");
1956 return TObject{}; // this is just to shut the compiler up about no returned value
1957 }
1958
1959 template <typename TSceneNode, typename Enable>
1960 inline std::shared_ptr<TSceneNode> SceneSystem::getNodeByID_Helper<TSceneNode, Enable>::get(const UniversalEntityID& universalEntityID, SceneSystem& sceneSystem) {
1961 static_assert(false && "No scene node of this type exists");
1962 return std::shared_ptr<TSceneNode>{};
1963 }
1964
1965 template <typename TSceneNode>
1966 struct SceneSystem::getNodeByID_Helper<TSceneNode, typename std::enable_if_t<std::is_base_of<SceneNodeCore, TSceneNode>::value>> {
1967 static std::shared_ptr<TSceneNode> get(const UniversalEntityID& universalEntityID, SceneSystem& sceneSystem) {
1968 return std::static_pointer_cast<TSceneNode>(sceneSystem.getNodeByID(universalEntityID));
1969 }
1970 };
1971
1972 template <typename TObject>
1973 struct SceneNodeCore::getByPath_Helper<std::shared_ptr<TObject>, typename std::enable_if_t<std::is_base_of<SceneNodeCore, TObject>::value>> {
1974 static std::shared_ptr<TObject> get(std::shared_ptr<SceneNodeCore> rootNode, const std::string& where) {
1975 return std::static_pointer_cast<TObject>(rootNode->getNode(where));
1976 }
1977 static constexpr bool s_valid { true };
1978 };
1979
1980 template <typename ...TComponents>
1981 SceneNodeCore::SceneNodeCore(const Placement& placement, const std::string& name, TComponents...components) {
1982 validateName(name);
1983 mName = name;
1984 mEntity = std::make_shared<Entity>(
1986 placement,
1988 Transform{
1989 .mModelMatrix { glm::mat4{1.f} },
1990 .mInheritedComponents { placement.mInheritedComponents },
1991 .mInheritMode { placement.mInheritMode },
1992 },
1994 components...
1995 )
1996 );
1999 }
2000 }
2001
2002 template <typename ...TComponents>
2003 SceneNodeCore::SceneNodeCore(const Key&, const Placement& placement, const std::string& name, TComponents...components) {
2004 mName = name;
2005 mEntity = std::make_shared<Entity>(
2007 placement,
2009 Transform{
2010 .mModelMatrix { glm::mat4{1.f} },
2011 .mInheritedComponents { placement.mInheritedComponents },
2012 .mInheritMode { placement.mInheritMode },
2013 },
2015 components...
2016 )
2017 );
2020 }
2021 }
2022
2023 template <typename TComponent>
2024 void SceneNodeCore::addComponent(const TComponent& component, bool bypassSceneActivityCheck) {
2025 mEntity->addComponent<TComponent>(component);
2026
2027 // NOTE: required because even though this node's entity's signature changes, it
2028 // is disabled by default on any systems it is eligible for. We need to activate
2029 // the node according to its system mask
2030 if(!bypassSceneActivityCheck && isActive()) {
2031 mEntity->enableSystems(mSystemMask);
2032 }
2033 // NOTE: no removeComponent() equivalent required, as systems that depend on the removed
2034 // component will automatically have this entity removed from their list, and hence
2035 // be disabled
2036 }
2037
2038 template <typename TComponent>
2039 TComponent SceneNodeCore::getComponent(const float simulationProgress) const {
2040 return mEntity->getComponent<TComponent>(simulationProgress);
2041 }
2042
2043 template <typename TComponent>
2045 return mEntity->hasComponent<TComponent>();
2046 }
2047
2048 template <typename TComponent>
2049 void SceneNodeCore::updateComponent(const TComponent& component) {
2050 mEntity->updateComponent<TComponent>(component);
2051 }
2052
2053 template <typename TComponent>
2054 void SceneNodeCore::addOrUpdateComponent(const TComponent& component, const bool bypassSceneActivityCheck) {
2056 addComponent<TComponent>(component, bypassSceneActivityCheck);
2057 return;
2058 }
2059 updateComponent<TComponent>(component);
2060 }
2061
2062 template <typename TComponent>
2064 mEntity->removeComponent<TComponent>();
2065 }
2066
2067 template <typename TSystem>
2069 return mEntity->isEnabled<TSystem>();
2070 }
2071
2072 template <typename TSystem>
2073 void SceneNodeCore::setEnabled(bool state) {
2074 const SystemType systemType { mEntity->getWorld().lock()->getSystemType<TSystem>() };
2075 mSystemMask.set(systemType, state);
2076
2077 // since the system mask has been changed, we'll want the scene
2078 // to talk to ECS and make this node visible to the system that
2079 // was enabled, if eligible
2080 if(state == true && isActive()){
2081 mEntity->enableSystems(mSystemMask);
2082 }
2083 }
2084
2085 // Specialization for when the scene system itself is marked
2086 // enabled or disabled
2087 template <>
2088 inline void SceneNodeCore::setEnabled<SceneSystem>(bool state) {
2089 const SystemType systemType { mEntity->getWorld().lock()->getSystemType<SceneSystem>() };
2090 // TODO: enabled entities are tracked in both SceneSystem's
2091 // mActiveNodes and ECS getEnabledEntities, which is
2092 // redundant and may eventually cause errors
2093 mSystemMask.set(systemType, state);
2094 //TODO: More redundancy. Why?
2095 mStateFlags = state? (mStateFlags | SceneNodeCore::StateFlags::ENABLED): (mStateFlags & ~SceneNodeCore::StateFlags::ENABLED);
2096 mEntity->getWorld().lock()->getSystem<SceneSystem>()->nodeActivationChanged(
2097 shared_from_this(),
2098 state
2099 );
2100 }
2101
2102 // Prevent removal of components essential to a scene node
2103 template <>
2105 assert(false && "Cannot remove a scene node's Placement component");
2106 }
2107
2108 template <>
2110 assert(false && "Cannot remove a scene node's Transform component");
2111 }
2112
2113 template <typename TSceneNode>
2114 std::shared_ptr<TSceneNode> SceneNodeCore::getNodeByID(EntityID entityID) {
2115 return getWorld().lock()->getSystem<SceneSystem>()->getNodeByID<TSceneNode>({getWorldID(), entityID});
2116 }
2117
2119 NLOHMANN_JSON_SERIALIZE_ENUM(ViewportNode::RenderConfiguration::ResizeType, {
2120 {ViewportNode::RenderConfiguration::ResizeType::OFF, "off"},
2121 {ViewportNode::RenderConfiguration::ResizeType::VIEWPORT_DIMENSIONS, "viewport-dimensions"},
2122 {ViewportNode::RenderConfiguration::ResizeType::TEXTURE_DIMENSIONS, "texture-dimensions"},
2123 });
2124
2126 NLOHMANN_JSON_SERIALIZE_ENUM(ViewportNode::RenderConfiguration::ResizeMode, {
2127 {ViewportNode::RenderConfiguration::ResizeMode::FIXED_ASPECT,"fixed-aspect"},
2128 {ViewportNode::RenderConfiguration::ResizeMode::EXPAND_VERTICALLY, "expand-vertically"},
2129 {ViewportNode::RenderConfiguration::ResizeMode::EXPAND_HORIZONTALLY, "expand-horizontally"},
2130 {ViewportNode::RenderConfiguration::ResizeMode::EXPAND_FILL, "expand-fill"},
2131 });
2132
2134 NLOHMANN_JSON_SERIALIZE_ENUM(ViewportNode::RenderConfiguration::UpdateMode, {
2135 {ViewportNode::RenderConfiguration::UpdateMode::NEVER, "never"},
2136 {ViewportNode::RenderConfiguration::UpdateMode::ONCE, "once"},
2137 {ViewportNode::RenderConfiguration::UpdateMode::ON_FETCH, "on-fetch"},
2138 {ViewportNode::RenderConfiguration::UpdateMode::ON_RENDER, "on-render"},
2139 {ViewportNode::RenderConfiguration::UpdateMode::ON_RENDER_CAP_FPS, "on-render-cap-fps"},
2140 });
2141
2143 NLOHMANN_JSON_SERIALIZE_ENUM(ViewportNode::RenderConfiguration::RenderType, {
2144 {ViewportNode::RenderConfiguration::RenderType::BASIC_3D, "basic-3d"},
2145 {ViewportNode::RenderConfiguration::RenderType::ADDITION, "addition"},
2146 });
2147
2149 inline void to_json(nlohmann::json& json, const ViewportNode::RenderConfiguration& renderConfiguration) {
2150 json = {
2151 {"base_dimensions", nlohmann::json::array({renderConfiguration.mBaseDimensions.x, renderConfiguration.mBaseDimensions.y})},
2152 {"update_mode", renderConfiguration.mUpdateMode},
2153 {"resize_type", renderConfiguration.mResizeType},
2154 {"resize_mode", renderConfiguration.mResizeMode},
2155 {"render_scale", renderConfiguration.mRenderScale},
2156 {"render_type", renderConfiguration.mRenderType},
2157 {"fps_cap", renderConfiguration.mFPSCap},
2158 };
2159 }
2160
2162 inline void from_json(const nlohmann::json& json, ViewportNode::RenderConfiguration& renderConfiguration) {
2163 assert(json.find("base_dimensions") != json.end() && "Viewport descriptions must contain the \"base_dimensions\" size 2 array of Numbers attribute");
2164 json.at("base_dimensions")[0].get_to(renderConfiguration.mBaseDimensions.x);
2165 json.at("base_dimensions")[1].get_to(renderConfiguration.mBaseDimensions.y);
2166 json.at("render_type").get_to(renderConfiguration.mRenderType);
2167 renderConfiguration.mRequestedDimensions = renderConfiguration.mBaseDimensions;
2168 renderConfiguration.mComputedDimensions = renderConfiguration.mBaseDimensions;
2169 assert(renderConfiguration.mBaseDimensions.x > 0 && renderConfiguration.mBaseDimensions.y > 0 && "Base dimensions cannot include a 0 in either dimension");
2170
2171 assert(json.find("update_mode") != json.end() && "Viewport render configuration must include the \"update_mode\" enum attribute");
2172 json.at("update_mode").get_to(renderConfiguration.mUpdateMode);
2173
2174 assert(json.find("resize_type") != json.end() && "Viewport render configuration must include the \"resize_type\" enum attribute");
2175 json.at("resize_type").get_to(renderConfiguration.mResizeType);
2176
2177 assert(json.find("resize_mode") != json.end() && "Viewport render configuration must include the \"resize_mode\" enum attribute");
2178 json.at("resize_mode").get_to(renderConfiguration.mResizeMode);
2179
2180 assert(json.find("render_scale") != json.end() && "Viewport render configuration must include the \"render_scale\" float attribute");
2181 json.at("render_scale").get_to(renderConfiguration.mRenderScale);
2182 assert(renderConfiguration.mRenderScale > 0.f && "Render scale must be a positive non-zero decimal number");
2183
2184 assert(json.find("fps_cap") != json.end() && "Viewport must include the \"fps_cap\" float attribute");
2185 json.at("fps_cap").get_to(renderConfiguration.mFPSCap);
2186 assert(renderConfiguration.mFPSCap > 0.f && "FPS cap must be a positive non-zero decimal number");
2187 }
2188
2189}
2190
2191#endif
An object responsible for tracking action listeners for a given project.
Definition input_system.hpp:586
An object containing a coarse simplified representation, AABB, of spatially queryable objects.
Definition types.hpp:1322
A CRTP template for all the scene node types present in the project.
Definition scene_system.hpp:698
BaseSceneNode(const Key &key, const Placement &placement, const std::string &name, TComponents...components)
Constructor for a single node of a subclass.
Definition scene_system.hpp:761
static std::shared_ptr< TSceneNode > copy(const std::shared_ptr< const TSceneNode > sceneNode)
Creates a scene node of a specific type based on another node of that type.
Definition scene_system.hpp:1925
BaseSceneNode(const SceneNodeCore &other)
Constructs a new node of a certain type as a copy of another node.
Definition scene_system.hpp:790
BaseSceneNode(const nlohmann::json &nodeDescription)
Constructs a new node of a certain type based on its json description.
Definition scene_system.hpp:783
static std::shared_ptr< TSceneNode > create(const Key &, const Placement &placement, const std::string &name, TComponents...components)
A (private) method for the creation of a new scene node for a particular type.
Definition scene_system.hpp:1911
BaseSceneNode(const Placement &placement, const std::string &name, TComponents...components)
General constructor for a single node of a subclass.
Definition scene_system.hpp:774
A class that represents a set of systems, entities, and components, that are all interrelated,...
Definition ecs_world.hpp:1462
static Entity createEntityPrototype(TComponents...components)
Create a prototype entity object.
Definition ecs_world.hpp:2805
The base class for any type whose creation and storage should be managed by the ResourceDatabase.
Definition resource_database.hpp:369
Resource(int explicitlyInitializeMe)
Definition resource_database.hpp:386
The core of a node in the SceneSystem, a set of components and methods overridable or usable by all t...
Definition scene_system.hpp:86
virtual void onCreated()
Scene node lifecycle hook for when a node is created.
Definition scene_system.cpp:21
std::shared_ptr< SceneNodeCore > removeNode(const std::string &where)
Removes a node from the tree present at the path specified.
Definition scene_system.cpp:350
virtual std::shared_ptr< SceneNodeCore > clone() const
Virtual method which each type of scene node with special members should implement (in lieu of copy c...
Definition scene_system.cpp:42
std::unordered_map< std::string, std::size_t > mChildNameToNode
A mapping of names of this node's child nodes to the indices of the nodes themselves.
Definition scene_system.hpp:662
void addComponent(const TComponent &component, const bool bypassSceneActivityCheck=false)
Adds a component of type TComponent to the node.
Definition scene_system.hpp:2024
std::shared_ptr< TSceneNode > getNodeByID(EntityID entityID)
Gets a pointer to a node by its EntityID, assuming that node and this one belong to the same ECSWorld...
Definition scene_system.hpp:2114
bool isAncestorOf(std::shared_ptr< const SceneNodeCore > sceneNode) const
Tests whether a particular scene node is the ancestor of this one.
Definition scene_system.cpp:169
RelativeTo mRelativeTo
A marker indicating how this node's transform component should be computed.
Definition scene_system.hpp:636
std::weak_ptr< ECSWorld > getWorld() const
Gets a reference to the ECSWorld this node belongs to.
Definition scene_system.cpp:407
virtual std::shared_ptr< ViewportNode > getLocalViewport()
Returns the viewport node which is in the same ECSWorld as and is the closest ancestor of (or the sam...
Definition scene_system.cpp:311
SceneNodeCore(const Placement &placement, const std::string &name, TComponents...components)
Constructs a scene node object from essential and extra components.
Definition scene_system.hpp:1981
static std::shared_ptr< SceneNodeCore > disconnectNode(std::shared_ptr< SceneNodeCore > node)
Disconnects this node from its parent node if it has one.
Definition scene_system.cpp:334
void recomputeChildNameIndexMapping()
Utility function for updating SceneHierarchyData components belonging to a single ECSWorld in the Sce...
Definition scene_system.cpp:82
void setPrototype_(std::shared_ptr< SceneNodeCore > prototype)
A reference to the node which was used in order to construct this one.
Definition scene_system.hpp:425
std::string mName
The name of this scene node.
Definition scene_system.hpp:622
std::string getPathFromAncestor(std::shared_ptr< const SceneNodeCore > ancestor) const
Gets the path from a node (assumed to be an ancestor) to this node.
Definition scene_system.cpp:385
std::vector< std::shared_ptr< SceneNodeCore > > getChildren()
Returns a list of all of this node's immediate children scene nodes.
Definition scene_system.cpp:248
virtual void onDeactivated()
Scene node lifecycle hook for when a node is deactivated on the SceneSystem.
Definition scene_system.cpp:23
virtual ~SceneNodeCore()=default
Destroys SceneNodeCore.
static std::tuple< std::string, std::string > nextInPath(const std::string &where)
Strips the root-most part of the path to a node.
Definition scene_system.cpp:192
std::vector< std::shared_ptr< SceneNodeCore > > mChildren
A list of this node's child nodes.
Definition scene_system.hpp:668
virtual void joinWorld(ECSWorld &world)
Removes this node's entity from its current ECSWorld and adds it to a new ECSWorld.
Definition scene_system.cpp:410
static void setParentViewport(std::shared_ptr< SceneNodeCore > node, std::shared_ptr< ViewportNode > newViewport)
Sets a node as the parent viewport of another one.
Definition scene_system.cpp:273
std::shared_ptr< SceneNodeCore > getNode(const std::string &where)
Gets a reference to a scene node (of any valid type) based on its path relative to this node.
Definition scene_system.cpp:259
void addOrUpdateComponent(const TComponent &component, const bool bypassSceneActivityCheck=false)
A method for adding a component or updating a component if that component is already present on this ...
Definition scene_system.hpp:2054
std::string getName() const
Returns the name string for this node.
Definition scene_system.cpp:184
std::string getViewportLocalPath() const
Gets the path of this node relative to its local viewport node.
Definition scene_system.cpp:188
static void validateName(const std::string &nodeName)
Tests whether a given name is actually valid, throwing an error when it is not.
Definition scene_system.cpp:425
bool isActive() const
Returns whether this node is present as part of the SceneSystem's scene tree, AND is active there as ...
Definition scene_system.cpp:165
static void SceneNodeCore_del_(SceneNodeCore *sceneNode)
Deleter for a managed pointer to a scene node which ensures its onDestroyed virtual function gets cal...
Definition scene_system.cpp:15
void copyDescendants(const SceneNodeCore &other)
Copies descendant nodes belonging to another node, attaches the copies to this node.
Definition scene_system.cpp:105
virtual void onActivated()
Scene node lifecycle hook for when a node is made an active part of the SceneSystem.
Definition scene_system.cpp:22
void updateComponent(const TComponent &component)
Updates the value of a component of this node (to what it should be at the start of the next simulati...
Definition scene_system.hpp:2049
std::shared_ptr< Entity > mEntity
The ECSWorld entity which this node is a wrapper over.
Definition scene_system.hpp:642
TComponent getComponent(const float simulationProgress=1.f) const
Retrieves a component belonging to this node.
Definition scene_system.hpp:2039
std::shared_ptr< SceneNodeCore > getParentNode()
Gets the parent node of this node, if one is present.
Definition scene_system.cpp:318
void copyAndReplaceAttributes(const SceneNodeCore &other)
Copies component values from another node and replaces the values on node's components with them.
Definition scene_system.cpp:89
bool hasComponent() const
Tests whether this node has a component of a specific type.
Definition scene_system.hpp:2044
std::vector< std::shared_ptr< SceneNodeCore > > getDescendants()
Gets all of the descendant nodes belonging to this scene node.
Definition scene_system.cpp:375
virtual void onDestroyed()
Scene node lifecycle hook for when a node (and possibly its descendants) are about to be destroyed.
Definition scene_system.cpp:24
void addNode(std::shared_ptr< SceneNodeCore > node, const std::string &where)
Adds a node (or a tree of them) as a child of the node specified by the path in the argument.
Definition scene_system.cpp:225
uint8_t mStateFlags
Flags indicating the state of this scene node in the scene system.
Definition scene_system.hpp:630
Signature mSystemMask
A bitset, each position of which indicates whether a system should influence this node when it is par...
Definition scene_system.hpp:682
void setName(const std::string &name)
Sets the name of this node.
Definition scene_system.cpp:179
std::vector< std::shared_ptr< SceneNodeCore > > removeChildren()
Disconnects and removes all the child nodes attached to this node.
Definition scene_system.cpp:365
std::weak_ptr< ViewportNode > mParentViewport
A reference to this node's parent viewport (whose meaning changes depending on whether this node is a...
Definition scene_system.hpp:654
UniversalEntityID getUniversalEntityID() const
Gets the UniversalEntityID aka the world-entity-id pair associated with this node.
Definition scene_system.cpp:404
TObject getByPath(const std::string &where)
Gets a reference to a node or related object by its path.
Definition scene_system.hpp:1937
std::shared_ptr< SceneNodeCore > mPrototype
Allows a prototype scene node to be retained as a resource so long as this node is present in memory ...
Definition scene_system.hpp:674
bool hasNode(const std::string &pathToChild) const
Tests whether a node specified by some path relative to this node is a real descendant of this node.
Definition scene_system.cpp:208
void setEnabled(bool state)
Sets whether or not a given system should be able to influence this scene object.
Definition scene_system.hpp:2073
std::weak_ptr< SceneNodeCore > mParent
A reference to this node's parent scene node.
Definition scene_system.hpp:648
static std::shared_ptr< SceneNodeCore > copy(const std::shared_ptr< const SceneNodeCore > other)
Creates a new scene tree by copying another scene node and its descendants.
Definition scene_system.cpp:26
EntityID getEntityID() const
Returns the entity id associated with these scene node.
Definition scene_system.cpp:398
static bool detectCycle(std::shared_ptr< SceneNodeCore > node)
Tests whether there are any cycles in the path up to the node's oldest ancestor.
Definition scene_system.cpp:147
WorldID getWorldID() const
Returns the ID of the ECSWorld this node belongs to.
Definition scene_system.cpp:401
void removeComponent()
Removes a component present on this node.
Definition scene_system.hpp:2063
StateFlags
Flags that indicate whether this node is enabled and active for the SceneSystem.
Definition scene_system.hpp:535
bool inScene() const
Returns whether this node is present as part of the SceneSystem's scene tree.
Definition scene_system.cpp:161
bool getEnabled() const
Returns whether a particular system has been enabled for this node.
Definition scene_system.hpp:2068
The most basic vanilla flavour of scene node comprised of no more than a name and some components.
Definition scene_system.hpp:802
void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override
Notifies scene system that a placement component in its world has been updated.
Definition scene_system.cpp:1544
void clearReportList()
Called at the end of the transform update to clear report list in preparation for the next time step'...
Definition scene_system.hpp:1629
std::set< EntityID > mReportedEntities
Entities whose placement updates this system has reported.
Definition scene_system.hpp:1616
void clearReportList()
Called at the end of the placement update to clear report list in preparation for the next time step'...
Definition scene_system.hpp:1669
void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override
Notifies the scene system that an entity in its world has had its transform updated.
Definition scene_system.cpp:1555
std::set< EntityID > mReportedEntities
Entities whose transform updates this system has reported.
Definition scene_system.hpp:1655
The SceneSystem, a singleton System, responsible for tracking all objects in the scene,...
Definition scene_system.hpp:1435
bool isSingleton() const override
Informs this System's ECSWorld that the SceneSystem is a singleton, i.e., there should not be more th...
Definition scene_system.hpp:1461
bool inScene(std::shared_ptr< const SceneNodeCore > sceneNode) const
Returns whether a particular scene node is in the SceneSystem's scene tree, even if it is inactive.
Definition scene_system.cpp:1132
std::set< UniversalEntityID, std::less< UniversalEntityID > > mComputePlacementQueue
Nodes whose transforms were updated this variable or simulation step should have their placements rec...
Definition scene_system.hpp:1895
std::vector< std::weak_ptr< ECSWorld > > getActiveWorlds()
Returns the ECSWorlds owned by ViewportNodes active in the scene tree.
Definition scene_system.cpp:1164
std::weak_ptr< ECSWorld > getRootWorld() const
Gets the world associated with the root ViewportNode of the scene system.
Definition scene_system.cpp:1156
ViewportNode & getRootViewport() const
Gets a reference to the root viewport of the SceneSystem, usually used to adjust its rendering config...
Definition scene_system.cpp:1177
std::vector< std::shared_ptr< SceneNodeCore > > getNodesByID(const std::vector< UniversalEntityID > &universalEntityIDs)
Gets nodes by their world-entity ID pair.
Definition scene_system.cpp:1572
std::set< UniversalEntityID, std::less< UniversalEntityID > > mActiveEntities
A list of world-entity IDs associated with all the active nodes known by the SceneSystem.
Definition scene_system.hpp:1881
void markDirtyPlacement(UniversalEntityID universalEntityID)
Marks a node as in need of a placement update based on its universal entity id.
Definition scene_system.cpp:1517
uint32_t render(float simulationProgress, uint32_t variableStep)
Runs the render step for the SceneSystem's root viewport and its descendants.
Definition scene_system.cpp:1108
void deactivateSubtree(std::shared_ptr< SceneNodeCore > sceneNode)
Deactivates this node and its descendants.
Definition scene_system.cpp:1349
void activateSubtree(std::shared_ptr< SceneNodeCore > sceneNode)
Activates this node and its descendants.
Definition scene_system.cpp:1333
std::set< UniversalEntityID, std::less< UniversalEntityID > > mComputeTransformQueue
Nodes which were updated during this variable or simulation step scheduled for a Transform update.
Definition scene_system.hpp:1887
void simulationStep(uint32_t simStepMillis, std::vector< std::pair< ActionDefinition, ActionData > > triggeredActions={})
Runs a single step for the root viewport and its descendants, propagating any actions generated by th...
Definition scene_system.cpp:1012
void nodeRemoved(std::shared_ptr< SceneNodeCore > sceneNode)
Plays any side effects related to a node being removed from the SceneSystem's scene tree.
Definition scene_system.cpp:1301
std::shared_ptr< TSceneNode > getNodeByID(const UniversalEntityID &universalEntityID)
Gets a scene node by its world-entity ID pair.
Definition scene_system.hpp:1947
void updateHierarchyDataInsertion(std::shared_ptr< SceneNodeCore > insertedNode)
Updates a node's scene hierarchy data per its location in the scene tree.
Definition scene_system.cpp:1185
void onApplicationInitialize(const ViewportNode::RenderConfiguration &rootViewportRenderConfiguration)
A method intended to be used at the start of the application to configure the SceneSystem's root view...
Definition scene_system.cpp:1530
std::shared_ptr< SceneNodeCore > removeNode(const std::string &where)
Removes a node present at the path specified in the call.
Definition scene_system.cpp:1151
Transform getCachedWorldTransform(std::shared_ptr< const SceneNodeCore > sceneNode) const
Returns the Transform of a node based on both its local Placement and its hierarchical transforms.
Definition scene_system.cpp:1456
void onApplicationEnd()
Clean up tasks the SceneSystem should perform before the application is terminated.
Definition scene_system.cpp:1127
void variableStep(float simulationProgress, uint32_t simulationLagMillis, uint32_t variableStepMillis, std::vector< std::pair< ActionDefinition, ActionData > > triggeredActions={})
Runs the variable step for the SceneSystem's root viewport and its descendants.
Definition scene_system.cpp:1067
static std::string getSystemTypeName()
The system type string associated with the SceneSystem.
Definition scene_system.hpp:1451
void nodeAdded(std::shared_ptr< SceneNodeCore > sceneNode)
Plays any side effects associated with a node being added to the SceneSystem's scene tree.
Definition scene_system.cpp:1262
void onWorldPlacementUpdate(UniversalEntityID UniversalEntityID)
A callback used by this system's subsystem to notify the SceneSystem that an entity's placement has b...
Definition scene_system.cpp:1522
std::weak_ptr< ECSWorld > getWorld(WorldID world)
Returns a reference to the world with a particular ID.
Definition scene_system.cpp:1168
void updateHierarchyDataRemoval(std::shared_ptr< SceneNodeCore > removedNode)
Removes a node's scene hierarchy data before it's removed from the hierarchy.
Definition scene_system.cpp:1226
void updateTransformsPlacements()
Updates transforms of objects in the scene per changes in those object's Placement component.
Definition scene_system.cpp:1365
SceneSystem(std::weak_ptr< ECSWorld > world)
Constructs a new SceneSystem object.
Definition scene_system.hpp:1442
std::vector< std::shared_ptr< ViewportNode > > getActiveViewports()
Returns a list of active viewports including the root viewport of the scene tree.
Definition scene_system.cpp:1160
void nodeActivationChanged(std::shared_ptr< SceneNodeCore > sceneNode, bool state)
"Activates" or deactivates a node and its descendants on various ECS systems, per the node's system m...
Definition scene_system.cpp:1317
void transformStep(uint32_t timestepMillis)
Updates transforms and placements as needed after a system update step.
Definition scene_system.cpp:1088
void onWorldTransformUpdate(UniversalEntityID universalEntityID)
A callback used by this system's subsystem to notify it that an entity's Transform has been updated.
Definition scene_system.cpp:1526
void addNode(std::shared_ptr< SceneNodeCore > node, const std::string &where)
Adds a node to the SceneSystem's scene tree as a child of the node specified by its path.
Definition scene_system.cpp:1181
Transform getInheritedTransform(std::shared_ptr< const SceneNodeCore > sceneNode) const
Gets the transform premultiplied with this object's local transform based on its transform settings.
Definition scene_system.cpp:1462
Transform getLocalTransform(std::shared_ptr< const SceneNodeCore > sceneNode) const
Gets the transform of a node solely based on its Placement component and independent of its position ...
Definition scene_system.cpp:1443
std::shared_ptr< SceneNodeCore > getNode(const std::string &where)
Gets a node by its scene node path.
Definition scene_system.cpp:1146
TObject getByPath(const std::string &where)
Gets an object of a specific type based on a valid path to that object belonging to the scene system ...
Definition scene_system.hpp:1942
void markDirtyTransform(UniversalEntityID universalEntityID)
Marks a node as in need of a transform update based on its universal entity id.
Definition scene_system.cpp:1511
void onApplicationStart()
Method to be called by main to initialize the SceneSystem as a whole.
Definition scene_system.cpp:1539
std::shared_ptr< ViewportNode > mRootNode
The root node of the SceneSystem, alive and active throughout the lifetime of the application.
Definition scene_system.hpp:1869
std::map< UniversalEntityID, std::weak_ptr< SceneNodeCore >, std::less< UniversalEntityID > > mEntityToNode
A mapping from the world-entity IDs of active nodes in the SceneSystem to the nodes themselves.
Definition scene_system.hpp:1875
bool isActive(std::shared_ptr< const SceneNodeCore > sceneNode) const
Returns whether a particular scene node is an active member of the SceneSystem's scene tree.
Definition scene_system.cpp:1139
SignalTracker()
Constructs a new SignalTracker object.
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1085
A type of node capable of and responsible for interacting sensibly with the engine's RenderSystem and...
Definition scene_system.hpp:843
std::set< std::shared_ptr< SceneNodeCore >, std::owner_less< std::shared_ptr< SceneNodeCore > > > mDomainCameras
The set of all active cameras that belong to the domain owned by this viewport.
Definition scene_system.hpp:1395
ActionDispatch & getActionDispatch()
Gets the action dispatch object for this viewport, which is the central location from which all actio...
Definition scene_system.cpp:971
bool mActionFlowthrough
Whether or not handled actions are propagated to this viewport's descendant viewports.
Definition scene_system.hpp:1371
void unregisterDomainCamera(std::shared_ptr< SceneNodeCore > cameraNode)
Removes a camera from this viewport's domain.
Definition scene_system.cpp:779
std::shared_ptr< Texture > fetchRenderResult(float simulationProgress)
Fetches the render result for the most recently computed render frame.
Definition scene_system.cpp:819
void requestDimensions(glm::u16vec2 requestedDimensions)
Target dimensions that another part of the program specifies for this viewport.
Definition scene_system.cpp:644
void updateExposure(float newExposure)
Updates the exposure of this viewport's RenderSet.
Definition scene_system.cpp:596
uint64_t mViewportLoadOrdinal
Number dictating when this viewport should be computed relative to other viewports,...
Definition scene_system.hpp:1353
std::shared_ptr< const SceneNodeCore > getActiveCamera() const
Gets the active camera for this viewport.
Definition scene_system.cpp:640
void createAndJoinWorld()
Creates and joins its own ECSWorld.
Definition scene_system.cpp:559
uint32_t mNLifetimeChildrenAdded
A number that is incremented whenever a child viewport is added to this viewport, guaranteeing unique...
Definition scene_system.hpp:1359
std::shared_ptr< ViewportNode > getLocalViewport() override
Returns this viewport instead of base class return value.
Definition scene_system.cpp:975
static std::shared_ptr< ViewportNode > copy(const std::shared_ptr< const ViewportNode > other)
Copies the properties and components of another viewport and uses them to construct a new one.
Definition scene_system.cpp:530
std::set< std::shared_ptr< ViewportNode >, ViewportChildComp_ > mChildViewports
This viewports children viewport, in the order they were added to this viewport.
Definition scene_system.hpp:1383
uint32_t render(float simulationProgress, uint32_t variableStep)
Requests execution of the render pipeline.
Definition scene_system.cpp:852
bool handleAction(std::pair< ActionDefinition, ActionData > pendingAction)
Handles an action received by this viewport, generally by dispatching it to subscribed listeners and ...
Definition scene_system.cpp:931
void joinWorld(ECSWorld &world) override
Joins this node's entity to a world (either its own or its parent viewport's).
Definition scene_system.cpp:413
void setFPSCap(float fpsCap)
If an FPS capped update mode is selected, sets what that cap actually is.
Definition scene_system.cpp:763
RenderConfiguration mRenderConfiguration
The render configuration associated with this viewport.
Definition scene_system.hpp:1415
bool disallowsHandledActionPropagation() const
Returns whether an action handled by one of this viewport's (high precedence) child viewports,...
Definition scene_system.hpp:1165
~ViewportNode() override
Destroys this viewport object.
Definition scene_system.cpp:437
static std::string getResourceTypeName()
Gets the resource type string associated with the ViewportNode.
Definition scene_system.hpp:989
uint32_t getViewportLoadOrdinal() const
(When this viewport is the immediate descendant of a RenderSet::RenderType::ADDITION viewport) The pr...
Definition scene_system.hpp:1180
void setActiveCamera(const std::string &cameraPath)
Sets the active camera for this viewport's RenderSet via path to the camera node.
Definition scene_system.cpp:619
void setRenderConfiguration(const RenderConfiguration &renderConfiguration)
Sets the render configuration for this viewport.
Definition scene_system.cpp:549
bool mPreventHandledActionPropagation
When a child viewport handles an action, determines whether the action is sent along to this viewport...
Definition scene_system.hpp:1377
RenderSetID mRenderSet
The ID of the RenderSet registered with this viewport's RenderSystem corresponding to this ViewportNo...
Definition scene_system.hpp:1401
std::shared_ptr< SceneNodeCore > mActiveCamera
The active camera node associated with this viewport.
Definition scene_system.hpp:1389
float getGamma()
Gets the gamma value used by this viewport's RenderSet.
Definition scene_system.cpp:613
SDL_Rect getCenteredViewportCoordinates() const
Gets the region of this viewport's target texture that the rendered texture should be mapped to.
Definition scene_system.hpp:1332
void viewNextDebugTexture()
Sets the next debug texture listed in this viewport's render set to the texture considered "active" b...
Definition scene_system.cpp:591
void registerDomainCamera(std::shared_ptr< SceneNodeCore > cameraNode)
Registers a camera that belongs to this viewport, which is a descendant of it and not a descendant of...
Definition scene_system.cpp:774
std::vector< std::shared_ptr< ViewportNode > > getActiveDescendantViewports()
Gets active descendant viewports (in DFS order) under this Viewport.
Definition scene_system.cpp:983
void setSkybox(std::shared_ptr< Texture > skybox)
Sets the skybox texture for this object's RenderSystem.
Definition scene_system.cpp:587
std::vector< std::weak_ptr< ECSWorld > > getActiveDescendantWorlds()
Gets weak references to ECSWorlds belonging to descendant viewports.
Definition scene_system.cpp:997
float getExposure()
Gets the exposure value used by this viewport's RenderSet.
Definition scene_system.cpp:607
void onDeactivated() override
Deactivates this viewports ECSWorld (if there is one) when this ViewportNode is retired.
Definition scene_system.cpp:583
void setResizeType(RenderConfiguration::ResizeType type)
Sets this viewport's behaviour when request dimensions (or target dimensions) are changed.
Definition scene_system.cpp:746
void resizeDomainCameras(const glm::vec2 &computedDimensions)
Readjusts all viewport cameras to match the size of the viewport.
Definition scene_system.cpp:785
std::shared_ptr< ECSWorld > mOwnWorld
The ECS world owned by this viewport (if any), as well as the world this node is a member of.
Definition scene_system.hpp:1211
static std::shared_ptr< ViewportNode > create(const std::string &name, bool inheritsWorld, bool allowActionFlowThrough, const RenderConfiguration &renderConfiguration, std::shared_ptr< Texture > skybox)
Creates a viewport node with components essential to it.
Definition scene_system.cpp:469
void setResizeMode(RenderConfiguration::ResizeMode mode)
When resize is enabled, determines how resized render dimensions are computed.
Definition scene_system.cpp:752
std::shared_ptr< Texture > mTextureResult
The result of rendering from running the rendering pipeline associated with this viewport.
Definition scene_system.hpp:1409
void updateGamma(float newGamma)
Updates the gamma value of this viewport's RenderSet.
Definition scene_system.cpp:601
RenderConfiguration getRenderConfiguration() const
Gets the render configuration for this viewport.
Definition scene_system.cpp:555
void onActivated() override
Override which for a viewport sets its active camera, and initializes the ECSWorld owned by this view...
Definition scene_system.cpp:565
Signal< RenderConfiguration > mRenderConfigurationUpdated
Signal emitted whenever this viewport's render configuration is updated.
Definition scene_system.hpp:1186
ViewportNode(const Key &key, const Placement &placement, const std::string &name)
Constructs a new ViewportNode using a simplified constructor.
Definition scene_system.hpp:1252
ActionDispatch mActionDispatch
Dispatcher for received actions to their action handlers within the domain of this viewport.
Definition scene_system.hpp:1365
void render_(float simulationProgress)
Implementation responsible for actually computing a new render frame.
Definition scene_system.cpp:893
void setRenderScale(float renderScale)
Sets the scale relative to computed and design dimensions for the render pipeline target.
Definition scene_system.cpp:768
uint32_t mTimeSinceLastRender
The time, in milliseconds, since the last time a render request was honoured by this viewport.
Definition scene_system.hpp:1421
std::shared_ptr< SceneNodeCore > clone() const override
Creates a new ViewportNode using itself as a template.
Definition scene_system.cpp:535
void setUpdateMode(RenderConfiguration::UpdateMode updateMode)
Sets the behaviour for frequency of render updates w.r.t render requests.
Definition scene_system.cpp:758
ToyMaker Engine's implementation of an ECS system.
ECSType ComponentType
An unsigned integer representing the type of a component.
Definition ecs_world.hpp:101
std::bitset< kMaxComponents > Signature
A 255 bit number, where each enabled bit represents a relationship between an entity and some ECS rel...
Definition ecs_world.hpp:152
ECSType SystemType
An unsigned integer representing the type of a system.
Definition ecs_world.hpp:110
constexpr EntityID kMaxEntities
A user-set constant which limits the number of creatable entities in a single ECS system.
Definition ecs_world.hpp:117
std::pair< WorldID, EntityID > UniversalEntityID
An ID that uniquely identifies an entity.
Definition ecs_world.hpp:83
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:66
std::uint64_t WorldID
An unsigned integer representing the name of an ECS world.
Definition ecs_world.hpp:74
SpecialEntity
(Perhaps unused) Special "reserved" entity IDs which the scene system might use.
Definition scene_system.hpp:70
RelativeTo
(Presently unused) A marker to indicate how transforms should be computed for a given scene node.
Definition scene_system.hpp:53
const std::string kSceneRootName
Special name for the scene root, unusable by any other scene object.
Definition scene_system.cpp:13
@ PARENT
Transform components are inherited from this object's parent.
Definition scene_components.hpp:58
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:26
STL namespace.
Contains definitions relating to the render system defined for this object.
Headers relating to resources and their management for a given project.
Stores structs and classes for common components used by the SceneSystem and other related Systems.
Classes relating to this engine's implementation of signals. Contains template classes used to define...
Classes and structs representing data related to the engine's spatial query system (the precursor to ...
A component representing the position, rotation, and scale of an entity.
Definition scene_components.hpp:73
TransformInheritMode mInheritMode
Field specifying which coordinate system this object's transform is specified relative to.
Definition scene_components.hpp:105
TransformComponentSet mInheritedComponents
Field specifying which aspects of its parent's transform this object's transform is specified relativ...
Definition scene_components.hpp:99
RenderType
Enum listing the different rendering pipelines available.
Definition render_system.hpp:59
Component representing hierarchical information related to this entity.
Definition scene_components.hpp:212
A private struct to limit certain sensitive functions to this class and other closely coupled classes...
Definition scene_system.hpp:518
A helper intended to get scene nodes and related objects attached to the scene tree.
Definition scene_system.hpp:547
Helper struct for retrieving nodes based on their UniversalEntityIDs.
Definition scene_system.hpp:1683
The transform component, which moves the vertices of a model to their world space coordinates during ...
Definition scene_components.hpp:150
A collection of data that specifies the behaviour and properties of the RenderSystem and target textu...
Definition scene_system.hpp:849
glm::u16vec2 mComputedDimensions
The dimensions finally computed for this viewport, per request from other parts of the application.
Definition scene_system.hpp:917
RenderSet::RenderType RenderType
Specifies the type of render pipeline requested by this viewport.
Definition scene_system.hpp:888
ResizeType
Different resize configurations available for this Viewport node that dictate how render textures (fr...
Definition scene_system.hpp:854
glm::u16vec2 mRequestedDimensions
The dimensions requested by other parts of the application, to which this viewport's render texture m...
Definition scene_system.hpp:923
ResizeMode
Determines which dimensions the end result of the viewport is allowed to expand on.
Definition scene_system.hpp:864
UpdateMode mUpdateMode
The frequency of rendering updates in real time made on this viewport.
Definition scene_system.hpp:941
ResizeMode mResizeMode
The resizing/scaling behaviour from render-> target texture for this viewport.
Definition scene_system.hpp:899
RenderType mRenderType
The type of render pipelien requested by this viewport.
Definition scene_system.hpp:905
glm::u16vec2 mBaseDimensions
The design dimensions for this viewport, specified at the time of its development.
Definition scene_system.hpp:911
float mFPSCap
If an FPS capped update mode is used, specifies the value of that cap.
Definition scene_system.hpp:948
ResizeType mResizeType
The type of resizing/scaling behaviour from render->target texture for this viewport.
Definition scene_system.hpp:894
float mRenderScale
A multiplier applied (in case resizing is enabled) determining multiplier to the base or computed dim...
Definition scene_system.hpp:935
UpdateMode
Configuration value determining when and how often render updates take place for this viewport.
Definition scene_system.hpp:875
Comparator used for determining priority of descendant viewports owned by a RenderSet::RenderType::AD...
Definition scene_system.hpp:1343
Header containing definitions of classes and functions related to loading and using Texture resources...