-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathsprite_clone.go
More file actions
135 lines (122 loc) · 3.5 KB
/
sprite_clone.go
File metadata and controls
135 lines (122 loc) · 3.5 KB
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
/*
* Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spx
import (
"context"
"reflect"
coreproject "github.com/goplus/spx/v2/internal/core/project"
"github.com/goplus/spx/v2/internal/engine"
spxlog "github.com/goplus/spx/v2/internal/log"
)
func (p *SpriteImpl) Clone__0() {
p.CloneWith(nil)
}
func (p *SpriteImpl) Clone__1(data any) {
p.CloneWith(data)
}
func (p *SpriteImpl) CloneWith(__xgo_optional_data any) {
doClone(p.sprite, __xgo_optional_data, false, nil)
}
// TODO(xsw): use classfile clone mechanism instead of reflection.
func doClone(sprite Sprite, data any, isAsync bool, onCloned func(sprite *SpriteImpl)) {
if sprite == nil {
spxlog.Panicf("doClone: sprite is nil")
}
src := spriteOf(sprite)
if isDebugInstrEnabled() {
spxlog.Debug("Clone: %s", src.name)
}
in := reflect.ValueOf(sprite).Elem()
v := reflect.New(in.Type())
out, outPtr := v.Elem(), v.Interface().(Sprite)
dest := cloneSprite(out, outPtr, in, nil)
src.g.addClonedShape(src, dest)
if onCloned != nil {
onCloned(dest)
}
dispatchCloneLifecycle(dest, data, isAsync)
}
func cloneSprite(out reflect.Value, outPtr Sprite, in reflect.Value, v coreproject.StageShape) *SpriteImpl {
dest := spriteOf(outPtr)
func() {
out.Set(in)
for i, n := 0, out.NumField(); i < n; i++ {
fld := out.Field(i).Addr()
if ini := fld.MethodByName("InitFrom"); ini.IsValid() {
ini.Call([]reflect.Value{in.Field(i).Addr()})
}
}
}()
dest.sprite = outPtr
dest.runtimeState.IsCostumeDirty = true
src := spriteOf(in.Addr().Interface().(Sprite))
dest.components.cloneFrom(&src.components, dest)
if v != nil {
applySpriteProps(dest, v)
} else {
dest.onAwake(func() {
dest.awake()
})
runMain(outPtr.Main)
}
dest.resetRuntimeProxy(true)
return dest
}
func dispatchCloneLifecycle(dest *SpriteImpl, data any, isAsync bool) {
dispatch := func() {
dest.doWhenAwake(dest)
if dest.spriteState.HasOnCloned {
dest.doWhenCloned(dest, data)
}
}
if isAsync {
engine.Go(dest.pthis, func(context.Context) {
dispatch()
})
return
}
dispatch()
}
func applySpriteProps(dest *SpriteImpl, v coreproject.StageShape) {
transform := dest.transform()
if x, ok := v["x"]; ok {
transform.x = x.(float64)
}
if y, ok := v["y"]; ok {
transform.y = y.(float64)
}
if heading, ok := v["heading"]; ok {
transform.direction = heading.(float64)
}
if style, ok := v["rotationStyle"]; ok {
transform.rotationStyle = toRotationStyle(style.(string))
}
if visible, ok := v["visible"]; ok {
dest.spriteState.IsVisible = visible.(bool)
}
if size, ok := v["size"]; ok {
dest.runtimeState.Scale = size.(float64)
}
if idx, ok := v["costumeIndex"]; ok {
dest.setCostumeIndex(int(idx.(float64)))
}
dest.spriteState.Cloned = false
}
func applySprite(out reflect.Value, sprite Sprite, v coreproject.StageShape) (*SpriteImpl, Sprite) {
in := reflect.ValueOf(sprite).Elem()
outPtr := out.Addr().Interface().(Sprite)
return cloneSprite(out, outPtr, in, v), outPtr
}