载入中...
搜索中...
未找到
SceneHost.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/ECS.h"
6#include "common/Identity.h"
7#include "common/Result.h"
8
9#include <cstdint>
10#include <functional>
11#include <string>
12#include <unordered_map>
13#include <utility>
14#include <vector>
15
16#include <glm/mat4x4.hpp>
17
18#include "scene/NodeDesc.h"
19#include "scene/SceneLink.h"
20
21// Only pointers to these cross the interface, so a forward declaration is
22// enough: scene links carry them as void* and the owning module does the casts.
23namespace eve::graphics {
24class Renderable2D;
25class Renderable3D;
26class Camera3D;
27}
28
29namespace eve::physics {
30class Body;
31class Body3D;
32}
33
34namespace eve::audio {
35class Source;
36}
37
38namespace eve::scene {
39
42
46struct SceneNode {
47 std::string id;
50 std::string key;
51 std::string name;
52 std::string space = "3d"; // "2d" | "3d"
53 bool visible = true;
55 std::vector<std::string> tags;
56 int layer = 0;
58 float bminX = 0.f, bminY = 0.f, bminZ = 0.f;
59 float bmaxX = 0.f, bmaxY = 0.f, bmaxZ = 0.f;
60 bool hasBounds = false;
61
62 float x = 0.f, y = 0.f, z = 0.f;
63 float yaw = 0.f, pitch = 0.f, roll = 0.f;
64 float sx = 1.f, sy = 1.f, sz = 1.f;
65
66 bool localDirty = true;
68 bool subtreeDirty = true;
69 glm::mat4 world{1.f};
70
72 std::vector<SceneLink> links;
73
75 uint32_t objectId = 0;
76
77 int firstChild = -1;
78 int nextSibling = -1;
79 int parent = -1;
80};
81
84
89class EVENGINE_API_PLATFORM SceneHost : public ecs::Entity {
90public:
92 ENTITY(SceneHost, ecs::Entity)
93
94
95 void release() override {}
96
98 struct Meta {
99 bool visible = true;
100 int layer = 0;
101 std::string name;
102 uint32_t ownerId = 0;
103 SceneHost *entity = nullptr;
104 };
105
106 struct Tree {
107 bool dirty = true;
108 bool transformDirty = true;
109 bool indexValid = false;
110 std::unordered_map<std::string, int> idIndex;
111 std::vector<SceneNode> nodes;
112 int root = -1;
113 };
114
123 [[nodiscard]] static eve::Result<Tree> buildDetachedTree(const Tree* previous, NodeDesc root);
124
125 COMPONENT(Meta, meta)
126 COMPONENT(Tree, tree)
127
128
135 [[nodiscard]] static eve::Result<SceneHost *> createHost(const std::string &name = "");
136
137 void setName(const std::string &name);
138 const std::string &getName();
139 void setOwnerId(uint32_t ownerId) { meta()->ownerId = ownerId; }
140 uint32_t getOwnerId() { return meta()->ownerId; }
141
143 void setTree(NodeDesc root);
145 bool setTreeReconcile(NodeDesc root);
146
151 [[nodiscard]] eve::Result<SceneNode *> findById(const std::string &id);
156 [[nodiscard]] eve::Result<SceneNode *> findByKey(const std::string &key);
161 [[nodiscard]] eve::Result<SceneNode *> findByName(const std::string &name);
166 [[nodiscard]] eve::Result<SceneNode *> findByPath(const std::string &path);
167 int findIndexById(const std::string &id);
168 int findIndexByKey(const std::string &key);
169 int findIndexByName(const std::string &name);
170 int findIndexByPath(const std::string &path);
171
172 bool hasNode(const std::string &id);
173 int getNodeCount();
174 int getRootIndex();
175 [[nodiscard]] SceneNode *getRoot();
176 [[nodiscard]] SceneNode *getNode(int index);
177
178 int getParentIndex(int nodeIndex);
179 [[nodiscard]] SceneNode *getParent(int nodeIndex);
180 [[nodiscard]] SceneNode *getParentById(const std::string &id);
181 int getChildCount(int nodeIndex);
182 int getChildCountById(const std::string &id);
184 int getChildIndexAt(int parentIndex, int childOrdinal);
185 [[nodiscard]] SceneNode *getChildAt(int parentIndex, int childOrdinal);
186 [[nodiscard]] SceneNode *getChildAtById(const std::string &parentId, int childOrdinal);
187 std::vector<int> getChildIndices(int nodeIndex);
188 std::vector<SceneNode *> getChildren(int nodeIndex);
189 std::vector<std::string> getChildIds(const std::string &parentId);
190
192 std::string getPath(int nodeIndex);
193 std::string getPathById(const std::string &id);
194
195 bool isAncestorOf(int ancestorIndex, int nodeIndex);
196 bool isAncestorOfById(const std::string &ancestorId, const std::string &nodeId);
197 bool isDescendantOf(int nodeIndex, int ancestorIndex);
198 bool isDescendantOfById(const std::string &nodeId, const std::string &ancestorId);
199
200 void markDirty() { tree()->dirty = true; }
201 void markTransformDirty() { tree()->transformDirty = true; }
203 void markSubtreeDirty(int nodeIndex);
204 void markSubtreeDirtyById(const std::string &nodeId) {
205 markSubtreeDirty(findIndexById(nodeId));
206 }
207 void invalidateIndex() { tree()->indexValid = false; }
208
209 // --- Node lifecycle events ---
210 using SceneEventFn = std::function<void(SceneHost *host, const std::string &action,
211 const std::string &nodeId,
212 const std::string &parentId)>;
213 void setEventHandler(SceneEventFn fn) { eventHandler_ = std::move(fn); }
215 void fireEvent(const std::string &action, const std::string &nodeId,
216 const std::string &parentId = {});
217
218 void setVisible(bool v) { meta()->visible = v; }
219 void setLayer(int layer) { meta()->layer = layer; }
220
225 bool setParent(int childIndex, int parentIndex);
226 bool addChild(int parentIndex, int childIndex);
227 bool removeChild(int parentIndex, int childIndex);
228
236 SceneMutationStatus appendNode(SceneNode node, const std::string &parentId = {});
243 SceneMutationStatus removeLeaf(const std::string &nodeId);
245 SceneMutationStatus renameNode(const std::string &nodeId, const std::string &name);
247 SceneMutationStatus setLocalTransform(const std::string &nodeId, float x, float y, float z,
248 float yaw, float pitch, float roll, float sx, float sy, float sz);
249
251 void forEachDepthFirst(int nodeIndex, void (*fn)(SceneHost *, int, void *), void *user);
252
253 using NodeVisitFn = std::function<void(SceneHost *host, int index, SceneNode &node)>;
254 using NodePredFn = std::function<bool(SceneHost *host, int index, const SceneNode &node)>;
255
257 void walkDepthFirst(NodeVisitFn fn);
258 void walkDepthFirstFrom(int nodeIndex, NodeVisitFn fn);
260 void walkBreadthFirst(NodeVisitFn fn);
261 void walkBreadthFirstFrom(int nodeIndex, NodeVisitFn fn);
263 void walkChildren(int parentIndex, NodeVisitFn fn);
265 void walkAncestors(int nodeIndex, NodeVisitFn fn);
266
268 [[nodiscard]] SceneNode *findIf(NodePredFn pred);
269 [[nodiscard]] SceneNode *findIfFrom(int nodeIndex, NodePredFn pred);
270 std::vector<SceneNode *> filter(NodePredFn pred);
271 std::vector<SceneNode *> filterFrom(int nodeIndex, NodePredFn pred);
272 std::vector<int> filterIndices(NodePredFn pred);
273 std::vector<int> filterIndicesFrom(int nodeIndex, NodePredFn pred);
274
275 std::vector<SceneNode *> findAllByName(const std::string &name);
276 std::vector<SceneNode *> findAllByKey(const std::string &key);
277 std::vector<SceneNode *> findAllVisible(bool visible = true);
278 std::vector<SceneNode *> findAllBySpace(const std::string &space);
279 std::vector<SceneNode *> findAllLinked();
280
281 std::vector<std::string> collectIds();
282 std::vector<std::string> collectIdsFrom(int nodeIndex);
283 std::vector<std::string> collectIdsWhere(NodePredFn pred);
284 std::vector<std::string> collectIdsByName(const std::string &name);
285 std::vector<std::string> collectIdsVisible(bool visible = true);
286
295 bool link(const std::string &nodeId, int kind, void *target, int syncMode = 0);
296
297 bool linkRenderable2D(const std::string &nodeId, graphics::Renderable2D *r);
298 bool linkRenderable3D(const std::string &nodeId, graphics::Renderable3D *r);
299 bool linkPhysics2D(const std::string &nodeId, physics::Body *b, int syncMode = 0);
300 bool linkPhysics3D(const std::string &nodeId, physics::Body3D *b, int syncMode = 0);
301 bool linkCamera3D(const std::string &nodeId, graphics::Camera3D *c);
302 bool linkAudio3D(const std::string &nodeId, audio::Source *s);
303
305 bool unlink(const std::string &nodeId, int kind);
307 bool unlink(const std::string &nodeId);
308
309 [[nodiscard]] SceneLink *findLink(SceneNode *node, int kind);
318 [[nodiscard]] const SceneLink *findLink(const SceneNode *node, int kind) const;
319 int linkCount(const std::string &nodeId);
320
322 bool setParentById(const std::string &childId, const std::string &parentId);
323 bool removeChildById(const std::string &parentId, const std::string &childId);
324
325 bool addTag(SceneNode *node, const std::string &tag);
326 bool removeTag(SceneNode *node, const std::string &tag);
327 bool hasTag(const SceneNode *node, const std::string &tag) const;
328 std::vector<SceneNode *> findAllByTag(const std::string &tag);
329
330private:
331 SceneEventFn eventHandler_;
332};
333
334} // namespace eve::scene
LogicalId target
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
int root
Definition AnimSmr.cpp:119
const std::string & s
std::string nodeId
std::uint64_t parentId
#define EVENGINE_API_PLATFORM
Definition Export.h:107
std::uint32_t key
double r
float v
std::int32_t c
Strong identity values shared by persistence, content and logical-name APIs.
TokenKind kind
std::string name
MeleePoint3 b
Definition MeleeHit.cpp:41
const std::string * tag
graphics::Canvas * previous
TileLayer * layer
bool dirty
std::string action
Definition PlayHost.cpp:117
std::string path
Definition PlayHost.cpp:110
std::function< eve::Result< void >()> release
Definition Procgen.cpp:86
Move-only, checked operation results for the common layer.
const RoadNode * node
std::string filter
bool visible
UIHostHandle host
uint32_t index
Move-only operation result carrying either a value or Status.
Definition Result.h:155
A playable audio source (static buffer or streaming decoder). Not thread-safe for playback control ex...
Definition Source.h:30
Id128 public API.
Definition Identity.h:112
EVENGINE_API_BACKENDS public API.
Default renderable entity for declarative 2D sprites / solid quads.
EVENGINE_API_BACKENDS public API.
3D rigid body (Box3D) in meter-space coordinates (+Y up by convention). Owned by a World3D; create pr...
Definition Body3D.h:24
2D rigid body (Box2D) in pixel-space coordinates. Owned by a World; create shapes with newRectangleFi...
Definition Body.h:21
ECS mount point for one scene graph (full scene or nested subtree root). Isomorphic to eve::ui::UIHos...
Definition SceneHost.h:89
void setOwnerId(uint32_t ownerId)
Definition SceneHost.h:139
std::function< void(SceneHost *host, const std::string &action, const std::string &nodeId, const std::string &parentId)> SceneEventFn
Definition SceneHost.h:212
void setLayer(int layer)
Definition SceneHost.h:219
void setEventHandler(SceneEventFn fn)
Definition SceneHost.h:213
uint32_t getOwnerId()
Definition SceneHost.h:140
void setVisible(bool v)
Definition SceneHost.h:218
std::function< bool(SceneHost *host, int index, const SceneNode &node)> NodePredFn
Definition SceneHost.h:254
void markSubtreeDirtyById(const std::string &nodeId)
Definition SceneHost.h:204
std::function< void(SceneHost *host, int index, SceneNode &node)> NodeVisitFn
Definition SceneHost.h:253
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
SceneMutationStatus
Outcome of an atomic scene-tree mutation.
Definition SceneHost.h:41
SettlementPipeline::Stage fn
Declarative scene-node description (build once / on dirty → flatten into SceneHost::Tree)....
Definition NodeDesc.h:19
std::vector< SceneNode > nodes
Definition SceneHost.h:111
std::unordered_map< std::string, int > idIndex
Definition SceneHost.h:110
Retained scene node (arena). Conceptual GameObject; isomorphic to eve::ui::UINode.
Definition SceneHost.h:46
uint32_t objectId
Lazy companion SceneObject ECS runtime id; 0 = none. Never a persisted identity.
Definition SceneHost.h:75
eve::SceneObjectId persistentId
Persisted object identity; nil means the legacy locator is still in use.
Definition SceneHost.h:49
float bminX
Local-space AABB (for picking / culling / spatial index).
Definition SceneHost.h:58
std::vector< std::string > tags
Gameplay groups (Godot-style): multiple string tags per node.
Definition SceneHost.h:55
bool subtreeDirty
Subtree needs world recompute (API edits mark node + ancestors).
Definition SceneHost.h:68
std::vector< SceneLink > links
Generic links (renderable / physics / camera / audio). Target owned elsewhere.
Definition SceneHost.h:72
std::string space
Definition SceneHost.h:52