Toast Engine
Loading...
Searching...
No Matches
node_owner.hpp
1
10
11#pragma once
12#include "box.hpp"
13
14#include <cstdint>
15#include <functional>
16#include <memory>
17#include <mutex>
18#include <shared_mutex>
19#include <string_view>
20#include <toast/assets/prefab.hpp>
21#include <toast/export.hpp>
22#include <toast/physics/accumulator.hpp>
24#include <toast/uid.hpp>
25#include <unordered_set>
26#include <vector>
27
28namespace toast {
29class Workspace;
31class Camera;
32enum class NodeOwnerParticipation : uint8_t {
33 render,
34 runtime_input,
35 gameplay_tick,
36};
37
38class TOAST_API INodeOwner {
39public:
40 INodeOwner();
41 virtual ~INodeOwner();
42 virtual auto name() -> std::string = 0;
43
44 virtual void tick() = 0;
45
47 virtual auto asWorkspace() -> Workspace* { return nullptr; }
48
50 [[nodiscard]]
51 virtual auto participatesIn(NodeOwnerParticipation use) const noexcept -> bool = 0;
52
54 [[nodiscard]]
55 virtual auto receivesEvents() const noexcept -> bool {
56 return true;
57 }
58
59 void syncListenerState() noexcept;
60
62 [[nodiscard]]
63 auto isEditing() noexcept -> bool;
64
65 [[nodiscard]]
66 auto treeMutex() const noexcept -> std::shared_mutex& {
67 return m_tree_mutex;
68 }
69
70 virtual void registerDependency(Node& from, Node& to) = 0;
71 virtual void unregisterDependency(Node& from, Node& to) = 0;
72
73 virtual void registerInteraction(Node& /*first*/, Node& /*second*/) { }
74
75 virtual void nodeEnabledChanged(Node& node) noexcept { (void)node; }
76
77 virtual auto findFrom(const Node& origin, std::string_view query) -> Box<Node> = 0;
78 virtual auto findFrom(const Node& origin, const UID& uid) -> Box<Node> = 0;
79 virtual auto searchFrom(const Node& origin, std::string_view query) -> std::vector<Box<Node>> = 0;
80
81 void activateCamera(Camera& camera);
83 void setMainCamera(Camera& camera);
84 void deactivateCamera(Camera& camera);
85 void activateCameraController(CameraController& controller);
86 void deactivateCameraController(CameraController& controller);
87
88 auto requestRuntimeCreate(Node& parent, std::string_view type) -> Box<Node>;
89 auto requestRuntimeSpawn(Node& parent, UID uid) -> Box<Node>;
90 auto requestRuntimeSpawn(Node& parent, std::string_view) -> Box<Node>;
91
92 void reloadScriptsUsing(UID script_uid) noexcept;
93 void refreshNodeInfos() noexcept;
94 void rebindAssetHandles(UID asset_uid) noexcept;
95 static void reapOrphanedControlBoxes() noexcept;
96
98 std::vector<uint64_t> asset_chain;
99 std::function<assets::Handle<assets::Prefab>(toast::UID)> resolver;
100 uint64_t script_group = 0;
101 bool nested = false;
102 };
103
104protected:
105 [[nodiscard]]
106 virtual auto owningPrefabUid() const -> UID {
107 return {0};
108 }
109
110 void seedPrefabContext(InstantiateContext& context, const Node* parent = nullptr) const;
111 static void updateTransforms(Node& root);
112
114 static void generateUid(Node& node);
115
117 static auto stripNamespace(std::string_view type) -> std::string_view;
118
120 static auto uniqueChildName(const Node& parent, std::string_view base) -> std::string;
121
128 auto nodeAllocation(std::string_view type = "toast::Node") noexcept -> Box<Node>;
129
135 auto nodeAllocation(const assets::Prefab::BasicNode& node_data, uint64_t script_group = 0, bool build_scripts = true) noexcept
136 -> Box<Node>;
137
138 void buildLeaves(
139 const assets::Handle<assets::Prefab>& file, uint64_t group, const std::vector<size_t>& leaf_slots,
140 std::vector<Box<Node>>& slots
141 );
142
149 auto buildTree(std::vector<Box<Node>>&& nodes, const assets::Handle<assets::Prefab>& file) -> Box<Node>;
150
162 auto instantiate(const assets::Handle<assets::Prefab>& file, InstantiateContext& ctx) -> Box<Node>;
163
165 void applyFields(Node& node, const assets::Prefab::BasicNode& data);
166
170 void applyLuaOverrides(Node& node, const assets::Prefab::BasicNode& data, const scripting::NodeResolver& find_node);
171
173 void releaseNode(_detail::ControlBox& control) noexcept;
174
176 void reapTombstones() noexcept;
177
178 void findCamera();
179 void findCameraController();
180 void tickActiveCameraController();
181 void beginCameraShutdown() noexcept;
182
183 [[nodiscard]]
184 auto activeCamera() noexcept -> Box<Camera>&;
185
186 [[nodiscard]]
187 auto activeRenderCamera() noexcept -> Camera*;
188 virtual void applyActiveCamera() = 0;
189
190 template<typename Fn>
191 void forEachNode(Fn&& fn) const {
192 for (const auto& cb : nodes) {
193 fn(cb);
194 }
195 }
196
197 std::mutex nodes_mutex;
198 size_t tombstones = 0;
199
200 physics::Accumulator m_accumulator;
201
202private:
203 friend class CameraController;
204
205 mutable std::shared_mutex m_tree_mutex;
206 std::unordered_set<_detail::ControlBox> nodes;
207
208 std::unique_ptr<Camera> m_fallback_camera;
209 Box<Camera> m_active_camera;
210 Box<CameraController> m_active_camera_controller;
211 bool m_has_camera_controller = false;
212 bool m_is_shutting_down = false;
213};
214
215}
pseudo shared pointer for Nodes
Definition accumulator.hpp:30
Intrusive reference-counted handle to a Node.
Definition box.hpp:35
Manages switching between different active cameras.
Definition camera_controller.hpp:39
Definition camera.hpp:25
virtual auto asWorkspace() -> Workspace *
Downcast helper for owners that need Workspace-specific state.
Definition node_owner.hpp:47
virtual auto receivesEvents() const noexcept -> bool
Definition node_owner.hpp:55
virtual auto participatesIn(NodeOwnerParticipation use) const noexcept -> bool=0
Determines if this owner is elegible for runtime systems.
size_t tombstones
short-circuits the reap sweep when there's nothing to clean
Definition node_owner.hpp:198
Single-viewport node owner used by the editor.
Definition workspace.hpp:39
Converts exported Lua script variable values to/from the engine's text encoding.
Definition node_owner.hpp:97
std::vector< uint64_t > asset_chain
UIDs of prefabs currently being instantiated; prevents infinite recursion.
Definition node_owner.hpp:98
std::function< assets::Handle< assets::Prefab >(toast::UID)> resolver
injected loader so tests can swap in a fake
Definition node_owner.hpp:99
Definition uid.hpp:20
Unique identifier for everything that exists in our engine.