-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.go
994 lines (828 loc) · 26.2 KB
/
scene.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
package engine
import (
"encoding/json"
"fmt"
"github.com/cadmean-ru/amphion/common/a"
"github.com/cadmean-ru/amphion/common/require"
"github.com/cadmean-ru/amphion/rendering"
"gopkg.in/yaml.v2"
"reflect"
)
const MaxSceneObjects = 1000
// SceneObject is an object in the scene. The scene itself is also a SceneObject.
type SceneObject struct {
id int
name string
children []*SceneObject
components []*ComponentContainer
messageListeners []*ComponentContainer
updatingComponents []*ComponentContainer
renderingComponents []*ComponentContainer
boundaryComponents []*ComponentContainer
layout *ComponentContainer
renderingNode *rendering.Node
Transform Transform
parent *SceneObject
enabled bool
initialized bool
started bool
inCurrentScene bool
willBeRemoved bool
}
//GetName returns the name of the scene object.
func (o *SceneObject) GetName() string {
return o.name
}
// GetId returns the id of the object in the scene.
// The id is not guaranteed to remain the same over time and serves for internal engine purposes.
// For identification use object's name.
func (o *SceneObject) GetId() int {
return o.id
}
// ToString returns the string representation of the scene object.
func (o *SceneObject) ToString() string {
return o.name
}
// DebugString return string for debugging.
func (o *SceneObject) DebugString() string {
return o.ToString()
}
//GetParent returns the parent object of this scene object. Returns nil if no parent object.
func (o *SceneObject) GetParent() *SceneObject {
return o.parent
}
func (o *SceneObject) appendChild(object *SceneObject) {
object.parent = o
object.Transform.SceneObject = object
object.Transform.parent = &o.Transform
object.setInCurrentScene(o.inCurrentScene)
o.children = append(o.children, object)
}
//AddChild adds a child object to this scene object.
func (o *SceneObject) AddChild(object *SceneObject) {
o.appendChild(object)
if o.inCurrentScene {
if !object.initialized {
object.Traverse(func(child *SceneObject) bool {
instance.updateRoutine.initSceneObject(child)
return true
}, true)
}
instance.rebuildMessageTree()
instance.RequestRendering()
}
}
//AddNewChild creates a new scene object with the given name and adds it as a child to the current object.
func (o *SceneObject) AddNewChild(name string) *SceneObject {
obj := NewSceneObject(name)
o.AddChild(obj)
return obj
}
//RemoveChild removes the specified child from this scene object.
func (o *SceneObject) RemoveChild(object *SceneObject) {
if !o.removeChildFromList(object) {
return
}
object.SetEnabled(false)
object.markForRemoval()
if o.inCurrentScene {
instance.rebuildMessageTree()
instance.RequestRendering()
}
}
func (o *SceneObject) removeChildFromList(object *SceneObject) bool {
index := -1
for i, c := range o.children {
if c == object {
index = i
}
}
if index != -1 {
o.children[index] = o.children[len(o.children)-1]
o.children = o.children[:len(o.children)-1]
return true
}
return false
}
//RemoveAllChildren removes all children from the receiver scene object.
func (o *SceneObject) RemoveAllChildren() {
for _, o1 := range o.children {
o.RemoveChild(o1)
}
}
//GetChildren returns the list of children of this scene object.
//Modifying the returned list wont modify the actual list of children of this scene object.
func (o *SceneObject) GetChildren() []*SceneObject {
c := make([]*SceneObject, len(o.children))
copy(c, o.children)
return c
}
//GetChildAt returns the child scene object at the given index.
//If there is no child with that index returns nil.
func (o *SceneObject) GetChildAt(index int) *SceneObject {
if index >= 0 && index < len(o.children) {
return o.children[index]
} else {
return nil
}
}
//GetChildrenCount returns the number of children of this object.
func (o *SceneObject) GetChildrenCount() int {
return len(o.children)
}
//GetChildByName finds scene object in the list of children of the current object.
//Returns nil if no object with the specified name was not found.
func (o *SceneObject) GetChildByName(name string) *SceneObject {
for _, c := range o.children {
if c != nil && c.name == name {
return c
}
}
panic(fmt.Sprintf("child scene object with name %s was not found (is it dirty?)", name))
}
//AddComponent adds a component to this scene object.
//Returns the given component.
func (o *SceneObject) AddComponent(component Component) Component {
container := NewComponentContainer(o, component)
o.components = append(o.components, container)
if _, ok := component.(UpdatingComponent); ok {
o.updatingComponents = append(o.updatingComponents, container)
}
if _, ok := component.(ViewComponent); ok {
o.renderingComponents = append(o.renderingComponents, container)
}
if _, ok := component.(MessageListenerComponent); ok {
o.messageListeners = append(o.messageListeners, container)
}
if _, ok := component.(BoundaryComponent); ok {
o.boundaryComponents = append(o.boundaryComponents, container)
}
if _, ok := component.(Layout); ok {
o.layout = container
}
if o.inCurrentScene {
instance.updateRoutine.initSceneObject(o)
instance.RequestRendering()
}
return component
}
// GetComponentByName searches for component with the specified name throughout the components attached to this object.
// The parameter n can be on of the following:
//
// - full component name, e.g. github.com/cadmean-ru/amphion/engine/builtin.TextView,
//
// - short component name, e.g. TextView,
//
// - regex string.
//
// If there are multiple components with the same name returns the first.
// Returns nil if no component with the name n was found or it has not been initialized or is disabled.
func (o *SceneObject) GetComponentByName(n string, includeDirty ...bool) Component {
dirty := getDirty(includeDirty...)
for _, c := range o.components {
if o.componentMatcher(c, n, dirty) {
return c.GetComponent()
}
}
panic(fmt.Sprintf("component with name %s was not found (is it dirty?)", n))
}
// GetComponentsByName searches for all components with the specified name throughout the components attached to this object.
// The parameter n can be on of the following:
//
// - full component name, e.g. github.com/cadmean-ru/amphion/engine/builtin.TextView,
//
// - short component name, e.g. TextView,
//
// - regex string.
//
// Returns empty slice if no components with the name n was found.
//Components that have not been initialized or are disabled will not be included.
func (o *SceneObject) GetComponentsByName(n string, includeDirty ...bool) []Component {
arr := make([]Component, 0, 1)
dirty := getDirty(includeDirty...)
for _, c := range o.components {
if o.componentMatcher(c, n, dirty) {
arr = append(arr, c.GetComponent())
}
}
return arr
}
func (o *SceneObject) componentMatcher(container *ComponentContainer, name string, dirty bool) bool {
return (dirty || !container.IsDirty()) && ComponentNameMatches(NameOfComponent(container.GetComponent()), name)
}
//GetComponents returns a slice of all components attached to the object.
//Modifying the returned list will not change the actual list of components of this scene object.
func (o *SceneObject) GetComponents(includeDirty ...bool) []Component {
dirty := getDirty(includeDirty...)
arr := make([]Component, 0, len(o.components))
for _, c := range o.components {
if !dirty && c.IsDirty() {
continue
}
arr = append(arr, c.component)
}
return arr
}
//RemoveComponent removes the given component from the scene object.
func (o *SceneObject) RemoveComponent(comp Component) {
var container *ComponentContainer
for _, c := range o.components {
if c.component == comp {
container = c
break
}
}
o.removeComponentFromAllLists(container)
}
//RemoveComponentByName removes a component with the given name from the scene object&
//If there are more than one component with the same name only the first encountered component will be removed.
func (o *SceneObject) RemoveComponentByName(name string) {
var container *ComponentContainer
for _, c := range o.components {
if o.componentMatcher(c, name, true) {
container = c
break
}
}
o.removeComponentFromAllLists(container)
}
func (o *SceneObject) removeComponentFromAllLists(container *ComponentContainer) {
if container == nil {
return
}
component := container.component
name := NameOfComponent(component)
o.components = o.removeComponentFromList(o.components, name)
if _, ok := component.(UpdatingComponent); ok {
o.updatingComponents = o.removeComponentFromList(o.updatingComponents, name)
}
if _, ok := component.(ViewComponent); ok {
o.renderingComponents = o.removeComponentFromList(o.renderingComponents, name)
}
if _, ok := component.(MessageListenerComponent); ok {
o.messageListeners = o.removeComponentFromList(o.messageListeners, name)
}
if _, ok := component.(BoundaryComponent); ok {
o.boundaryComponents = o.removeComponentFromList(o.boundaryComponents, name)
}
if _, ok := component.(Layout); ok {
o.layout = nil
}
if o.inCurrentScene {
instance.updateRoutine.stopComponent(container)
instance.RequestRendering()
}
}
func (o *SceneObject) removeComponentFromList(arr []*ComponentContainer, name string) []*ComponentContainer {
index := -1
for i, c := range o.components {
if o.componentMatcher(c, name, true) {
index = i
break
}
}
if index == -1 {
return arr
}
arr[index] = arr[len(arr)-1]
return arr[:len(arr)-1]
}
//RemoveAllComponents removes all components of the scene object.
func (o *SceneObject) RemoveAllComponents() {
for _, c := range o.components {
o.RemoveComponent(c.component)
}
}
func (o *SceneObject) markForRemoval() {
o.willBeRemoved = true
for _, c := range o.children {
c.markForRemoval()
}
}
//GetComponentContainers returns the list of containers, that makes possible to enable/disable specific components.
func (o *SceneObject) GetComponentContainers() []*ComponentContainer {
arr := make([]*ComponentContainer, 0, len(o.components))
arr = append(arr, o.components...)
return arr
}
//SetEnabled sets the enabled state of this object as specified.
func (o *SceneObject) SetEnabled(enabled bool) {
if o.willBeRemoved || o.enabled == enabled {
return
}
o.enabled = enabled
for _, c := range o.components {
c.SetEnabled(enabled)
}
if o.inCurrentScene {
if enabled {
instance.updateRoutine.startSceneObject(o)
} else {
instance.updateRoutine.stopSceneObject(o)
}
}
for _, so := range o.children {
so.SetEnabled(enabled)
}
}
//IsEnabled returns if this object is currently enabled or not.
func (o *SceneObject) IsEnabled() bool {
return o.enabled
}
// SetPosition sets the position of this object equal to the specified vector, requesting rendering.
func (o *SceneObject) SetPosition(v a.Vector3) {
o.Transform.Position = v
o.Redraw()
}
// SetPositionXyz sets the position of this object equal to a new vector with specified coordinates, requesting rendering.
func (o *SceneObject) SetPositionXyz(x, y, z float32) {
o.SetPosition(a.NewVector3(x, y, z))
}
//SetPositionXy sets the position of this object equal to a new vector with specified coordinates, requesting rendering.
func (o *SceneObject) SetPositionXy(x, y float32) {
o.SetPosition(a.NewVector3(x, y, o.Transform.Position.Z))
}
// SetSize Set the size of this object equal to the specified vector, requesting rendering.
func (o *SceneObject) SetSize(v a.Vector3) {
o.Transform.Size = v
o.Redraw()
}
// SetSizeXyz Set the size of this object equal to a new vector with specified coordinates, requesting rendering.
func (o *SceneObject) SetSizeXyz(x, y, z float32) {
o.SetSize(a.NewVector3(x, y, z))
}
// SetSizeXy Set the size of this object equal to a new vector with specified coordinates, requesting rendering.
func (o *SceneObject) SetSizeXy(x, y float32) {
o.SetSize(a.NewVector3(x, y, o.Transform.Size.Z))
}
// Redraw forces all views of this object to redraw and requests rendering.
func (o *SceneObject) Redraw() {
if o.IsDirty() || !o.inCurrentScene {
return
}
for _, view := range o.renderingComponents {
if view.IsDirty() {
continue
}
viewComp := view.component.(ViewComponent)
viewComp.Redraw()
}
instance.RequestRendering()
}
//func (o *SceneObject) Measure() a.IntVector3 {
// rect := common.NewRectBoundary(0, 0, 0, 0, 0, 0)
// for _, c := range o.children {
//
// }
//}
func (o *SceneObject) OnMessage(message Message) bool {
if !o.enabled {
return true
}
continuePropagation := true
for _, l := range o.messageListeners {
if !l.component.(MessageListenerComponent).OnMessage(message) {
continuePropagation = false
}
}
return continuePropagation
}
func (o *SceneObject) init(ctx InitContext) {
for _, c := range o.components {
if c.initialized {
continue
}
instance.currentComponent = c.component
c.component.OnInit(ctx)
c.initialized = true
if NameOfComponent(c.component) == "github.com/cadmean-ru/amphion/engine/builtin.ClipArea" {
LogDebug("init %+v", c)
}
}
o.initialized = true
instance.currentComponent = nil
}
func (o *SceneObject) start() {
for _, c := range o.components {
if c.IsDirty() || c.started {
continue
}
instance.currentComponent = c.component
c.component.OnStart()
c.started = true
}
o.started = true
instance.currentComponent = nil
}
func (o *SceneObject) update(ctx UpdateContext) {
for _, c := range o.updatingComponents {
if c.IsDirty() || !c.started {
continue
}
instance.currentComponent = c.component
c.component.(UpdatingComponent).OnUpdate(ctx)
}
if o.layout != nil && !o.layout.IsDirty() {
instance.currentComponent = o.layout.component
o.layout.component.(Layout).LayoutChildren()
}
instance.currentComponent = nil
}
func (o *SceneObject) stop() {
for _, c := range o.components {
if c.enabled || !c.started {
continue
}
instance.currentComponent = c.component
c.component.OnStop()
c.started = false
}
o.started = false
instance.currentComponent = nil
}
func (o *SceneObject) draw(ctx DrawingContext) {
for _, c := range o.renderingComponents {
if c.IsDirty() || !c.started {
continue
}
view := c.component.(ViewComponent)
instance.currentComponent = c.component
if !view.ShouldDraw() {
continue
}
view.OnDraw(ctx)
}
instance.currentComponent = nil
}
func (o *SceneObject) RenderTraverse(action func(node *rendering.Node), afterChildrenAction func(node *rendering.Node)) {
if o.IsDirty() {
return
}
action(o.renderingNode)
for _, c := range o.children {
c.RenderTraverse(action, afterChildrenAction)
}
afterChildrenAction(o.renderingNode)
}
func (o *SceneObject) GetRenderingNode() *rendering.Node {
return o.renderingNode
}
func (o *SceneObject) setInCurrentScene(b bool) {
o.inCurrentScene = b
for _, c := range o.children {
c.setInCurrentScene(b)
}
}
//HasViews checks if the scene object has any view components.
func (o *SceneObject) HasViews() bool {
return len(o.renderingComponents) > 0
}
//HasBoundary checks if the scene object has any boundary components.
func (o *SceneObject) HasBoundary() bool {
return len(o.boundaryComponents) > 0
}
func (o *SceneObject) IsPointInsideBoundaries(point a.Vector3) bool {
for _, b := range o.boundaryComponents {
if b.IsDirty() {
continue
}
if b.component.(BoundaryComponent).IsPointInside(point) {
return true
}
}
return false
}
func (o *SceneObject) IsPointInsideBoundaries2D(point a.Vector3) bool {
for _, b := range o.boundaryComponents {
if b.IsDirty() {
continue
}
if b.component.(BoundaryComponent).IsPointInside2D(point) {
return true
}
}
return false
}
//IsFocused checks if the scene object is currently focused.
func (o *SceneObject) IsFocused() bool {
return instance.sceneContext.focusedObject == o
}
//IsHovered checks if the cursor is over the scene object.
func (o *SceneObject) IsHovered() bool {
return instance.sceneContext.hoveredObject == o
}
func (o *SceneObject) IsVisibleInScene() bool {
sceneRect := instance.GetCurrentScene().Transform.GetGlobalRect()
rect := o.Transform.GetGlobalRect()
return rect.X.Max >= sceneRect.X.Min && rect.X.Min <= sceneRect.X.Max && rect.Y.Max >= sceneRect.Y.Min && rect.Y.Min <= sceneRect.Y.Max
}
// Traverse traverses the scene object tree (pre-order), calling the action function for each of the objects.
// If action returns false interrupts the process.
// By default the method skips dirty objects.
// To also include dirty objects pass true as the second argument.
func (o *SceneObject) Traverse(action func(object *SceneObject) bool, includeDirty ...bool) {
dirty := getDirty(includeDirty...)
if !dirty && o.IsDirty() {
return
}
if !action(o) {
return
}
for _, c := range o.children {
c.Traverse(action, dirty)
}
}
// ForEachObject traverses the scene object tree, calling the action function for each of the objects.
// The action is also called for the object on which the method was called.
// The method skips dirty objects.
func (o *SceneObject) ForEachObject(action func(object *SceneObject), includeDirty ...bool) {
dirty := getDirty(includeDirty...)
o.Traverse(func(object *SceneObject) bool {
action(object)
return true
}, dirty)
}
//ForEachChild cycles through all direct children of the scene object, calling the specified action for each of them.
//The method skips dirty objects.
func (o *SceneObject) ForEachChild(action func(object *SceneObject), includeDirty ...bool) {
dirty := getDirty(includeDirty...)
if !dirty && o.IsDirty() {
return
}
for _, c := range o.children {
if !dirty && c.IsDirty() {
continue
}
action(c)
}
}
//FindObjectByName searches for an object with the specified name through all the scene object tree.
//Returns the first suitable object.
//Returns nil if no object with the name was found.
//By default the search does not include dirty objects.
//To also include dirty objects pass true as the second argument.
func (o *SceneObject) FindObjectByName(name string, includeDirty ...bool) *SceneObject {
var found *SceneObject
dirty := getDirty(includeDirty...)
o.Traverse(func(object *SceneObject) bool {
if object.name == name {
found = object
return false
}
return true
}, dirty)
if found != nil {
return found
}
panic(fmt.Sprintf("scene object with name %s was not found (is it dirty?)", name))
}
//FindComponentByName searches for a component with the specified name through all the scene object tree.
//Returns the first suitable component.
//Returns nil if no component with the name was found.
//By default the search does not include dirty components.
//To also include dirty components pass true as the second argument.
func (o *SceneObject) FindComponentByName(name string, includeDirty ...bool) Component {
var found Component
dirty := getDirty(includeDirty...)
o.Traverse(func(object *SceneObject) bool {
for _, c := range object.components {
if o.componentMatcher(c, name, dirty) {
found = c.GetComponent()
return false
}
}
return true
}, dirty)
if found != nil {
return found
}
panic(fmt.Sprintf("component with name %s was not found (is it dirty?)", name))
}
// ForEachComponent iterates over each component attached to the object, calling the action function for each of them.
// The method skips dirty components.
func (o *SceneObject) ForEachComponent(action func(component Component)) {
for _, c := range o.components {
if c.IsDirty() {
continue
}
action(c.component)
}
}
//IsDirty checks if the scene object is dirty.
//An object is considered dirty if it has not been initialized or if it is disabled or
//it will be deleted in the next update.
//It is not safe to work with a dirty object.
func (o *SceneObject) IsDirty() bool {
return !o.initialized || !o.enabled || o.willBeRemoved
}
//Copy creates a new scene object with all components and children of the receiver.
//The new object is dirty. It is enabled if the receiver is enabled.
func (o *SceneObject) Copy(copyName string) *SceneObject {
var newObject *SceneObject
if instance == nil {
newObject = NewSceneObjectForTesting(copyName)
} else {
newObject = NewSceneObject(copyName)
}
for _, comp := range o.components {
compType := reflect.TypeOf(comp.component)
var compCopyValue reflect.Value
if compType.Kind() == reflect.Ptr {
compCopyValue = reflect.New(compType.Elem())
} else {
compCopyValue = reflect.Indirect(reflect.New(compType))
}
compCopy := compCopyValue.Interface().(Component)
if instance != nil {
cm := instance.GetComponentsManager()
state := cm.GetComponentState(comp.component)
cm.SetComponentState(compCopy, state)
}
newObject.AddComponent(compCopy)
}
newObject.Transform = o.Transform
newObject.enabled = o.enabled
for _, child := range o.children {
childCopy := child.Copy(child.name)
newObject.AddChild(childCopy)
}
return newObject
}
//RemoveFromScene removes the scene object from the current scene.
//After that the object is considered dirty.
func (o *SceneObject) RemoveFromScene() {
if o.parent == nil || !o.inCurrentScene {
return
}
o.GetParent().RemoveChild(o)
}
//SetParent changes the parent of the scene object to the specified object.
//Can be used to move a child from one object to another.
func (o *SceneObject) SetParent(newParent *SceneObject) {
if o.parent != nil {
o.parent.removeChildFromList(o)
}
newParent.AddChild(o)
o.Redraw()
}
func (o *SceneObject) ToMap() a.SiMap {
mChildren := make([]map[string]interface{}, len(o.children))
for i, c := range o.children {
mChildren[i] = c.ToMap()
}
mComponents := make([]map[string]interface{}, len(o.components))
for i, c := range o.components {
var state map[string]interface{}
if IsStatefulComponent(c.component) {
state = instance.GetComponentsManager().GetComponentState(c.component)
}
cMap := map[string]interface{} {
"name": NameOfComponent(c.GetComponent()),
"state": state,
}
mComponents[i] = cMap
}
return map[string]interface{}{
"name": o.name,
"id": o.id,
"children": mChildren,
"components": mComponents,
"transform": o.Transform.ToMap(),
}
}
func (o *SceneObject) DumpToMap() a.SiMap {
mChildren := make([]map[string]interface{}, len(o.children))
for i, c := range o.children {
mChildren[i] = c.DumpToMap()
}
mComponents := make([]map[string]interface{}, len(o.components))
for i, c := range o.components {
var state map[string]interface{}
if IsStatefulComponent(c.component) {
state = instance.GetComponentsManager().GetComponentState(c.component)
}
cMap := map[string]interface{} {
"name": NameOfComponent(c.GetComponent()),
"state": state,
"initialized": c.initialized,
"started": c.started,
"enabled": c.enabled,
}
mComponents[i] = cMap
}
return map[string]interface{}{
"name": o.name,
"id": o.id,
"initialized": o.initialized,
"started": o.started,
"enabled": o.enabled,
"willBeRemoved": o.willBeRemoved,
"children": mChildren,
"components": mComponents,
"transform": o.Transform.ToMap(),
"renderingTransform": o.Transform.ToRenderingTransform().ToMap(),
}
}
func (o *SceneObject) FromMap(siMap a.SiMap) {
o.name = siMap["name"].(string)
o.id = require.Int(siMap["id"])
o.Transform = NewTransformFromMap(a.RequireSiMap(siMap["transform"]))
// Decode components
iComponents := siMap["components"].([]interface{})
o.components = make([]*ComponentContainer, 0, len(iComponents))
o.renderingComponents = make([]*ComponentContainer, 0, 1)
o.updatingComponents = make([]*ComponentContainer, 0, 1)
o.boundaryComponents = make([]*ComponentContainer, 0, 1)
o.enabled = true
o.renderingNode = instance.renderer.MakeNode(o)
for _, c := range iComponents {
cMap := a.RequireSiMap(c)
cName := cMap["name"].(string)
cState := a.RequireSiMap(cMap["state"])
component := instance.GetComponentsManager().MakeComponent(cName)
if component == nil {
continue
}
if IsStatefulComponent(component) {
instance.GetComponentsManager().SetComponentState(component, cState)
}
o.AddComponent(component)
}
// Decode children
iChildren := siMap["children"].([]interface{})
o.children = make([]*SceneObject, 0, len(iChildren))
for _, c := range iChildren {
obj := &SceneObject{}
obj.FromMap(a.RequireSiMap(c))
o.appendChild(obj)
}
}
func (o *SceneObject) EncodeToYaml() ([]byte, error) {
return yaml.Marshal(o.ToMap())
}
func (o *SceneObject) DecodeFromYaml(data []byte) error {
oMap := make(a.SiMap)
err := yaml.Unmarshal(data, &oMap)
if err != nil {
return err
}
o.FromMap(oMap)
return nil
}
func (o *SceneObject) EncodeToJson() ([]byte, error) {
return json.Marshal(o.ToMap())
}
func (o *SceneObject) DumpToJson() ([]byte, error) {
return json.Marshal(o.DumpToMap())
}
// NewSceneObject creates a new instance of scene object.
// Can be used only with engine running.
func NewSceneObject(name string) *SceneObject {
obj := &SceneObject{
id: instance.idgen.NextId(),
name: name,
children: make([]*SceneObject, 0, 10),
components: make([]*ComponentContainer, 0, 10),
messageListeners: make([]*ComponentContainer, 0),
renderingComponents: make([]*ComponentContainer, 0, 1),
updatingComponents: make([]*ComponentContainer, 0, 1),
boundaryComponents: make([]*ComponentContainer, 0, 1),
enabled: true,
}
obj.renderingNode = instance.renderer.MakeNode(obj)
obj.Transform = NewTransform2D(obj)
return obj
}
//NewSceneObjectForTesting creates a new instance of scene object without engine running.
//Can be used for testing purposes.
//Do not use in actual Amphion apps!
func NewSceneObjectForTesting(name string, components ...Component) *SceneObject {
obj := &SceneObject{
id: 0,
name: name,
children: make([]*SceneObject, 0, 10),
components: make([]*ComponentContainer, 0, 10),
messageListeners: make([]*ComponentContainer, 0),
renderingComponents: make([]*ComponentContainer, 0, 1),
updatingComponents: make([]*ComponentContainer, 0, 1),
boundaryComponents: make([]*ComponentContainer, 0, 1),
enabled: true,
initialized: true,
}
obj.Transform = NewTransform2D(obj)
for _, c := range components {
obj.AddComponent(c)
}
ctx := newInitContext(obj)
for _, component := range obj.components {
component.GetComponent().OnInit(ctx)
component.initialized = true
}
return obj
}
func getDirty(dirties ...bool) bool {
if len(dirties) == 1 {
return dirties[0]
}
return false
}