载入中...
搜索中...
未找到
UIComponentScript.cpp
浏览该文件的文档.
2
3#include <cstring>
4#include <simplesquirrel/simplesquirrel.hpp>
5
6namespace eve::ui::detail {
7namespace {
9const char *kUIComponentScript = R"SQ(
10if (!("_uiComponents" in eve)) eve._uiComponents <- []
11if (!("_flushingUIComponents" in eve)) eve._flushingUIComponents <- false
12if (!("_nextUIComponentOwner" in eve)) eve._nextUIComponentOwner <- 0
13
14eve._scheduleUIComponent <- function(component) {
15 if (component == null || component._scheduled) return
16 component._scheduled = true
17 eve._uiComponents.append(component)
18}
19
20eve._flushUIComponents <- function() {
21 if (eve._flushingUIComponents) return
22 eve._flushingUIComponents = true
23 local pending = eve._uiComponents
24 eve._uiComponents = []
25 for (local i = 0; i < pending.len(); i += 1) {
26 local component = pending[i]
27 component._scheduled = false
28 try {
29 if (component._mounted && component._parent == null && component.dirty)
30 component.updateIfDirty()
31 } catch (error) {
32 for (local j = i + 1; j < pending.len(); j += 1) eve._uiComponents.append(pending[j])
33 eve._flushingUIComponents = false
34 throw error
35 }
36 }
37 eve._flushingUIComponents = false
38}
39
40::eve_ui_flush_components <- function() {
41 eve._flushUIComponents()
42}
43
44eve.UIComponent <- class {
45 hostName = ""
46 dirty = true
47 forceFull = false
48 props = null
49 state = null
50 _ui = null
51 _parent = null
52 _mounted = false
53 _scheduled = false
54 _children = null
55 _nextChildren = null
56 _keyedChildren = null
57 _eventBindings = null
58 _eventOwner = 0
59
60 constructor(uiInstance = null, initialProps = null) {
61 _ui = uiInstance
62 hostName = ""
63 dirty = true
64 forceFull = false
65 props = initialProps != null ? initialProps : {}
66 state = {}
67 _parent = null
68 _mounted = false
69 _scheduled = false
70 _children = []
71 _nextChildren = []
72 _keyedChildren = {}
73 _eventBindings = []
74 eve._nextUIComponentOwner += 1
75 _eventOwner = eve._nextUIComponentOwner
76 }
77
78 function setUI(uiInstance) { _ui = uiInstance; return this }
79
80 function _applyProps(nextProps, replace, notify) {
81 if (nextProps == null) return this
82 if (replace) props = {}
83 foreach (key, value in nextProps) props.rawset(key, value)
84 if (notify) markDirty()
85 return this
86 }
87
88 function setProps(nextProps, replace = false) {
89 return _applyProps(nextProps, replace, true)
90 }
91
92 function mountAs(name) {
93 hostName = name
94 dirty = true
95 forceFull = true
96 updateIfDirty()
97 return this
98 }
99
100 function setState(patch = null) {
101 if (patch != null) {
102 foreach (key, value in patch) state.rawset(key, value)
103 }
104 markDirty()
105 return this
106 }
107 function markDirty() {
108 dirty = true
109 if (_parent != null && _parent != this) _parent.markDirty()
110 else if (_mounted) eve._scheduleUIComponent(this)
111 return this
112 }
113
114 // Compose a persistent child instance inside this component's active build pass.
115 function renderChild(component, nextProps = null, replaceProps = false) {
116 if (component == null) return null
117 component._parent = this
118 component.setUI(this.ui())
119 component._applyProps(nextProps, replaceProps, false)
120 _nextChildren.append(component)
121 local firstMount = !component._mounted
122 component._nextChildren = []
123 component.build()
124 component._finishChildPass()
125 component.dirty = false
126 component._mounted = true
127 if (firstMount) component.onMount()
128 else component.onUpdated()
129 return component
130 }
131
132 // Reuse a child component by a stable key. factory is called only for a new key.
133 function renderKeyed(key, factory, nextProps = null, replaceProps = false) {
134 if (key == null) throw "UIComponent renderKeyed requires a key"
135 local component = null
136 if (key in _keyedChildren) component = _keyedChildren[key]
137 else {
138 component = factory()
139 if (component == null) throw "UIComponent renderKeyed factory returned null"
140 _keyedChildren.rawset(key, component)
141 }
142 return renderChild(component, nextProps, replaceProps)
143 }
144
145 function _bindEvent(kind, id, fn) {
146 if (id == null || id == "" || fn == null) return this
147 foreach (binding in _eventBindings) {
148 if (binding.kind == kind && binding.id == id) {
149 binding.fn = fn
150 if (_mounted) _syncEventHandlers()
151 return this
152 }
153 }
154 _eventBindings.append({ kind = kind, id = id, fn = fn })
155 if (_mounted) _syncEventHandlers()
156 return this
157 }
158
159 function bindClick(id, fn) { return _bindEvent("click", id, fn) }
160 function bindChange(id, fn) { return _bindEvent("change", id, fn) }
161
162 function clearEventBindings() {
163 _eventBindings = []
164 this.ui().componentClearHandlers(_eventOwner)
165 return this
166 }
167
168 function _rootComponent() {
169 local root = this
170 while (root._parent != null && root._parent != root) root = root._parent
171 return root
172 }
173
174 function _syncEventHandlers() {
175 local u = this.ui()
176 u.componentClearHandlers(_eventOwner)
177 local root = _rootComponent()
178 if (root.hostName == "" || !u.select(root.hostName)) return
179 foreach (binding in _eventBindings) {
180 if (binding.kind == "click") u.componentOnClick(_eventOwner, binding.id, binding.fn)
181 else u.componentOnChange(_eventOwner, binding.id, binding.fn)
182 }
183 }
184
185 function _syncEventTree() {
186 _syncEventHandlers()
187 foreach (child in _children) child._syncEventTree()
188 }
189
190 function _containsChild(items, child) {
191 foreach (candidate in items) {
192 if (candidate == child) return true
193 }
194 return false
195 }
196
197 function _finishChildPass() {
198 foreach (child in _children) {
199 if (!_containsChild(_nextChildren, child)) child._unmountTree()
200 }
201 local staleKeys = []
202 foreach (key, child in _keyedChildren) {
203 if (!_containsChild(_nextChildren, child)) staleKeys.append(key)
204 }
205 foreach (key in staleKeys) delete _keyedChildren[key]
206 _children = _nextChildren
207 _nextChildren = []
208 }
209
210 function _unmountTree() {
211 if (!_mounted) return false
212 foreach (child in _children) child._unmountTree()
213 _children = []
214 _nextChildren = []
215 _mounted = false
216 _scheduled = false
217 dirty = false
218 this.ui().componentClearHandlers(_eventOwner)
219 onUnmount()
220 _parent = null
221 return true
222 }
223
224 function unmount() {
225 if (!_unmountTree()) return false
226 local u = this.ui()
227 if (hostName != "" && u.select(hostName)) u.setHostVisible(false)
228 return true
229 }
230
231 // Override in subclass: call this.ui().beginWindow / text / button / end ...
232 function build() {}
233 function onMount() {}
234 function onUpdated() {}
235 function onUnmount() {}
236
237 function ui() {
238 if (_ui != null) return _ui
239 try {
240 if (::ui != null) return ::ui
241 } catch (e) {}
242 _ui = ::eve.UI()
243 return _ui
244 }
245
246 function updateIfDirty() {
247 if (!dirty) return false
248 local firstMount = !_mounted
249 local u = this.ui()
250 _scheduled = false
251 dirty = false
252 _nextChildren = []
253 u.beginBuild()
254 try {
255 this.build()
256 _finishChildPass()
257 local name = hostName
258 if (name == null || name == "") name = "default"
259 hostName = name
260 if (forceFull) {
261 if (!u.mountBuildAs(name)) throw "UIComponent mount failed: " + name
262 forceFull = false
263 } else {
264 if (!u.remountBuildAs(name)) throw "UIComponent remount failed: " + name
265 }
266 _mounted = true
267 u.setHostVisible(true)
268 if (firstMount) onMount()
269 else onUpdated()
270 _syncEventTree()
271 } catch (error) { markDirty(); throw error }
272 return true
273 }
274
275 function rebuild(force = false) {
276 dirty = true
277 forceFull = force
278 return updateIfDirty()
279 }
280}
281// Note: eve.Component is reserved for script ECS (see exposeECS). Use eve.UIComponent.
282)SQ";
283
284} // namespace
285
286void injectUIComponentClass(ssq::Table &eveTable) {
287 // A failed run used to be discarded, so a broken UIComponent.nut left eve.UIComponent
288 // missing with no signal (the same shape the Scene injections had).
289 (void)script::compileAndRunChunk(eveTable.getHandle(), "UIComponent.nut", kUIComponentScript);
290}
291
292} // namespace eve::ui::detail
The single Squirrel projection for common Result, Status and Value.
SQRESULT compileAndRunChunk(HSQUIRRELVM vm, const char *name, const char *source)
Compile and run one embedded script chunk against the root table.
void injectUIComponentClass(ssq::Table &eveTable)