* Create component from loaded package * 从已加载的包创建组件 * * Note: Disposes existing component before creating new one to avoid visual overlap * 注意:创建新组件前会先销毁已有组件,避免视觉叠加
(componentName?: string)
| 293 | * 注意:创建新组件前会先销毁已有组件,避免视觉叠加 |
| 294 | */ |
| 295 | public createComponent(componentName?: string): GObject | null { |
| 296 | const name = componentName || this.componentName; |
| 297 | if (!this._package) { |
| 298 | return null; |
| 299 | } |
| 300 | if (!name) { |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | // Dispose existing component before creating new one |
| 305 | // 创建新组件前先销毁已有组件 |
| 306 | if (this._component) { |
| 307 | if (this._root) { |
| 308 | this._root.removeChild(this._component); |
| 309 | } |
| 310 | this._component.dispose(); |
| 311 | this._component = null; |
| 312 | } |
| 313 | |
| 314 | try { |
| 315 | this._component = this._package.createObject(name); |
| 316 | |
| 317 | if (this._component && this._root) { |
| 318 | this.syncProperties(); |
| 319 | this._root.addChild(this._component); |
| 320 | } |
| 321 | |
| 322 | this._version++; |
| 323 | this._onStateChange?.('updated'); |
| 324 | return this._component; |
| 325 | } catch (e) { |
| 326 | // Log full error with stack trace for debugging |
| 327 | console.error(`[FGUIComponent] Error creating component "${name}":`, e); |
| 328 | this._error = e instanceof Error ? e.message : String(e); |
| 329 | return null; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Get child by name from the component |
no test coverage detected