载入中...
搜索中...
未找到
SceneCapabilities.cpp
浏览该文件的文档.
1#include "common/Capability.h"
2#include "common/SceneQuery.h"
3#include "scene/Scene.h"
4#include "scene/SceneHost.h"
6
7#include <cstdint>
8#include <string>
9#include <vector>
10
11namespace eve::scene {
12namespace {
13
14SceneHost *hostByName(const std::string &name) {
15 if (name.empty()) return nullptr;
16 if (ecs::current()->getManager<SceneHost>() == nullptr) return nullptr;
17 auto view = ecs::View<SceneHost, SceneHost::Meta>();
18 for (auto it = view.begin(); it != view.end(); ++it) {
19 auto [meta] = *it;
20 if (meta->entity && meta->name == name) return meta->entity;
21 }
22 return nullptr;
23}
24
25SceneHost *hostAt(int index) {
26 if (index < 0 || ecs::current()->getManager<SceneHost>() == nullptr) return nullptr;
27 auto view = ecs::View<SceneHost, SceneHost::Meta>();
28 int i = 0;
29 for (auto it = view.begin(); it != view.end(); ++it, ++i) {
30 auto [meta] = *it;
31 if (i == index) return meta->entity;
32 }
33 return nullptr;
34}
35
36SceneNodeInfo toInfo(SceneHost *host, const SceneNode &n) {
37 SceneNodeInfo info;
38 info.id = n.id;
39 info.name = n.name;
40 info.path = host->getPathById(n.id);
41 info.visible = n.visible;
42 info.x = n.x;
43 info.y = n.y;
44 info.z = n.z;
45 info.yaw = n.yaw;
46 info.pitch = n.pitch;
47 info.roll = n.roll;
48 info.sx = n.sx;
49 info.sy = n.sy;
50 info.sz = n.sz;
51 if (auto *p = host->getParentById(n.id)) info.parent = p->id;
52 for (int i = 0; i < host->getChildCountById(n.id); ++i) {
53 if (auto *c = host->getChildAtById(n.id, i)) info.children.push_back(c->id);
54 }
55 return info;
56}
57
58class SceneQueryImpl final : public eve::ISceneQuery {
59public:
60 SceneHost *host() const {
61 auto *s = eve::ModuleManager::getInstance<Scene>("Scene");
62 return s ? s->current() : nullptr;
63 }
64
65 std::string activeHost() const override {
66 auto *h = host();
67 return h ? h->getName() : std::string();
68 }
69
70 int hostCount() const override {
71 if (ecs::current()->getManager<SceneHost>() == nullptr) return 0;
72 auto view = ecs::View<SceneHost, SceneHost::Meta>();
73 int n = 0;
74 for (auto it = view.begin(); it != view.end(); ++it) ++n;
75 return n;
76 }
77
78 std::string hostNameAt(int index) const override {
79 auto *h = hostAt(index);
80 return h ? h->getName() : std::string();
81 }
82
83 int nodeCount() const override {
84 auto *h = host();
85 return h ? h->getNodeCount() : 0;
86 }
87
88 std::string rootId() const override {
89 auto *h = host();
90 auto *r = h ? h->getRoot() : nullptr;
91 return r ? r->id : std::string();
92 }
93
94 std::vector<SceneNodeInfo> nodes(int limit) const override {
95 return nodesOf(host() ? host()->getName() : std::string(), limit);
96 }
97
98 std::vector<SceneNodeInfo> nodesOf(const std::string &hostName, int limit) const override {
99 std::vector<SceneNodeInfo> out;
100 SceneHost *h = hostName.empty() ? host() : hostByName(hostName);
101 if (!h) return out;
102 h->walkDepthFirst([&](SceneHost *, int, SceneNode &n) {
103 if (static_cast<int>(out.size()) >= limit) return;
104 out.push_back(toInfo(h, n));
105 });
106 return out;
107 }
108
109 bool getNode(const std::string &id, SceneNodeInfo *out) const override {
110 auto *h = host();
111 return h ? getNodeIn(h->getName(), id, out) : false;
112 }
113
114 bool getNodeIn(const std::string &hostName, const std::string &id,
115 SceneNodeInfo *out) const override {
116 SceneHost *h = hostName.empty() ? host() : hostByName(hostName);
117 SceneNode *n = h ? h->findById(id) : nullptr;
118 if (!n || !out) return false;
119 *out = toInfo(h, *n);
120 return true;
121 }
122
123 bool setNodeTransform(const std::string &id, float x, float y, float z) override {
124 auto *h = host();
125 SceneNode *n = h ? h->findById(id) : nullptr;
126 if (!n) return false;
127 n->x = x;
128 n->y = y;
129 n->z = z;
130 h->markTransformDirty();
131 return true;
132 }
133
134 bool setNodeVisible(const std::string &id, bool visible) override {
135 auto *h = host();
136 SceneNode *n = h ? h->findById(id) : nullptr;
137 if (!n) return false;
138 n->visible = visible;
139 return true;
140 }
141
142 void syncTransforms() override { TransformSystem::updateAll(); }
143};
144
145} // namespace
146
148 static SceneQueryImpl impl;
150}
151
152} // namespace eve::scene
void * impl
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
glm::vec3 n
Definition Grass.cpp:64
int h
uint32_t c
glm::vec4 p[6]
glm::mat4 view
const char * name
Definition RockMesh.cpp:21
uint32_t s
Definition Weather.cpp:28
Scene graph query/mutation surface (provided by the scene module).
Definition SceneQuery.h:30
I * query()
Definition Capability.h:77
void registerSceneCapabilities()