载入中...
搜索中...
未找到
SceneComponentScript.cpp
浏览该文件的文档.
2
3#include <cstring>
4#include <simplesquirrel/simplesquirrel.hpp>
5
6namespace eve::scene::detail {
7namespace {
9const char *kSceneComponentScript = R"SQ(
10if (!("_sceneComponents" in eve)) eve._sceneComponents <- []
11if (!("_flushingSceneComponents" in eve)) eve._flushingSceneComponents <- false
12eve._scheduleSceneComponent <- function(component) {
13 if (component._scheduled) return
14 component._scheduled = true
15 eve._sceneComponents.append(component)
16}
17::eve_scene_flush_components <- function() {
18 if (eve._flushingSceneComponents) return
19 eve._flushingSceneComponents = true
20 local pending = eve._sceneComponents
21 eve._sceneComponents = []
22 for (local i = 0; i < pending.len(); i += 1) {
23 local component = pending[i]
24 component._scheduled = false
25 try {
26 if (component._mounted && component.dirty) component.updateIfDirty()
27 } catch (error) {
28 for (local j = i + 1; j < pending.len(); j += 1) eve._sceneComponents.append(pending[j])
29 eve._flushingSceneComponents = false
30 throw error
31 }
32 }
33 eve._flushingSceneComponents = false
34}
35eve.SceneComponent <- class {
36 hostName = ""
37 dirty = true
38 forceFull = false
39 _scene = null
40 _mounted = false
41 _scheduled = false
42
43 constructor(sceneInstance = null) {
44 _scene = sceneInstance
45 hostName = ""
46 dirty = true
47 forceFull = false
48 _mounted = false
49 }
50
51 function setScene(sceneInstance) { _scene = sceneInstance }
52
53 function mountAs(name) {
54 hostName = name
55 dirty = true
56 forceFull = true
57 updateIfDirty()
58 }
59
60 function setState() { markDirty() }
61 function markDirty() {
62 dirty = true
63 if (_mounted) eve._scheduleSceneComponent(this)
64 }
65 function onMount() {}
66
67 // Override: call this.scene().beginNode / addNode / end ...
68 function build() {}
69
70 function scene() {
71 if (_scene != null) return _scene
72 try {
73 if (::scene != null) return ::scene
74 } catch (e) {}
75 _scene = ::eve.Scene()
76 return _scene
77 }
78
79 function updateIfDirty() {
80 if (!dirty) return false
81 local s = this.scene()
82 dirty = false
83 _scheduled = false
84 s.beginBuild()
85 try {
86 this.build()
87 local name = hostName
88 if (name == null || name == "") name = "default"
89 if (forceFull) {
90 s.mountBuildAs(name)
91 forceFull = false
92 } else {
93 s.remountBuildAs(name)
94 }
95 if (!_mounted) {
96 _mounted = true
97 onMount()
98 }
99 if (dirty) eve._scheduleSceneComponent(this)
100 } catch (error) { markDirty(); throw error }
101 return true
102 }
103
104 function rebuild(force = false) {
105 dirty = true
106 forceFull = force
107 return updateIfDirty()
108 }
109}
110)SQ";
111
112} // namespace
113
114void injectSceneComponentClass(ssq::Table &eveTable) {
115 // A failed run used to be discarded, so a broken SceneComponent.nut left
116 // eve.SceneComponent missing with no signal from the engine.
117 (void)script::compileAndRunChunk(eveTable.getHandle(), "SceneComponent.nut", kSceneComponentScript);
118}
119
120} // namespace eve::scene::detail
The single Squirrel projection for common Result, Status and Value.
void injectSceneComponentClass(ssq::Table &eveTable)
SQRESULT compileAndRunChunk(HSQUIRRELVM vm, const char *name, const char *source)
Compile and run one embedded script chunk against the root table.