-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathshape_manager.go
More file actions
270 lines (237 loc) · 6.49 KB
/
shape_manager.go
File metadata and controls
270 lines (237 loc) · 6.49 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
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
/*
* 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 (
"github.com/goplus/spx/v2/internal/base/sliceutil"
"github.com/goplus/spx/v2/internal/engine"
spxlog "github.com/goplus/spx/v2/internal/log"
itime "github.com/goplus/spx/v2/internal/time"
)
// shapeManager manages the lifecycle of all runtime shapes.
// It is responsible for:
// - activation (delayed add)
// - destruction (delayed remove)
// - render layer grouping
// - minimizing per-frame allocations
type shapeManager struct {
items []Shape
tempItems []Shape
destroyItems []Shape
}
// init prepares internal buffers while preserving existing allocations when possible.
func (s *shapeManager) init() {
if s.items == nil {
s.items = make([]Shape, 0, 64)
} else {
s.items = s.items[:0]
}
if s.tempItems == nil {
s.tempItems = make([]Shape, 0, 50)
} else {
s.tempItems = s.tempItems[:0]
}
if s.destroyItems == nil {
s.destroyItems = make([]Shape, 0, 16)
} else {
s.destroyItems = s.destroyItems[:0]
}
}
// reset clears all internal state while keeping allocated memory.
// It is safe to call between scenes or rounds.
func (s *shapeManager) reset() {
engine.ClearAllSprites()
s.init()
}
// flushActivate updates all active non-sprite shapes for the current frame.
func (s *shapeManager) flushActivate(items []Shape) {
if len(items) == 0 {
return
}
delta := itime.DeltaTime()
for _, item := range items {
if _, ok := item.(*SpriteImpl); ok {
continue
}
switch v := item.(type) {
case *quoterBubble:
v.onUpdate(delta)
case *textBubble:
v.onUpdate(delta)
case *Monitor:
v.onUpdate(delta)
default:
if updater, ok := item.(interface{ onUpdate(float64) }); ok {
updater.onUpdate(delta)
}
}
}
}
func (s *shapeManager) collectProxyUpdates(items []Shape, buffer *engine.SpriteSyncBuffer) {
for _, item := range items {
if sprite, ok := item.(*SpriteImpl); ok {
sprite.collectProxyUpdate(buffer)
}
}
}
// flushDestroy performs cleanup for shapes that have been scheduled for destruction.
func (s *shapeManager) flushDestroy(buffer *engine.SpriteSyncBuffer) {
if len(s.destroyItems) == 0 {
return
}
for _, item := range s.destroyItems {
if sprite, ok := item.(*SpriteImpl); ok && sprite.runtimeState.SyncSprite != nil {
buffer.AddDelete(int64(sprite.runtimeState.SyncSprite.Id))
sprite.runtimeState.SyncSprite = nil
}
}
s.destroyItems = s.destroyItems[:0]
}
// add adds a shape immediately to the active list.
func (s *shapeManager) add(shape Shape) {
s.items = append(s.items, shape)
}
// remove schedules a shape for destruction at the end of the frame.
func (s *shapeManager) remove(shape Shape) {
s.destroyItems = append(s.destroyItems, shape)
}
// addShape delegates to add; kept for call-site consistency.
func (s *shapeManager) addShape(child Shape) {
s.add(child)
}
// addClonedShape inserts a cloned shape immediately after its source.
// This preserves rendering order and ensures clones appear behind their source.
func (s *shapeManager) addClonedShape(src, clone Shape) {
idx := s.findShapeIndex(src)
if idx < 0 {
spxlog.Debug("addClonedShape: clone a deleted sprite")
gco.Abort()
return
}
s.items = sliceutil.InsertAt(s.items, idx, clone)
s.updateRenderLayers()
}
// removeShape removes a shape from the active list and schedules it for destruction.
func (s *shapeManager) removeShape(child Shape) {
idx := s.findShapeIndex(child)
if idx < 0 {
return
}
s.items = sliceutil.DeleteAt(s.items, idx)
s.remove(child)
s.updateRenderLayers()
}
// activateShape moves a shape to the end of the active list.
func (s *shapeManager) activateShape(child Shape) {
items := s.items
for idx, item := range items {
if item == child {
if idx == len(items)-1 {
return
}
s.items = sliceutil.MoveToEnd(s.items, idx)
s.updateRenderLayers()
return
}
}
}
// goBackLayers moves a sprite forward or backward by n layers.
func (s *shapeManager) goBackLayers(spr *SpriteImpl, n int) {
if engine.HasLayerSortMethod() {
spxlog.Debug("Cannot manually set sprite layer when a layer sort mode is active.")
return
}
if n == 0 {
return
}
idx := s.findShapeIndex(spr)
if idx < 0 {
return
}
newIdx := s.calculateNewIndex(idx, n)
if newIdx == idx {
return
}
s.items = sliceutil.MoveToIndex(s.items, idx, newIdx)
s.updateRenderLayers()
}
// updateRenderLayers updates the layer index for all sprites.
func (s *shapeManager) updateRenderLayers() {
if engine.HasLayerSortMethod() {
return
}
layer := 0
for _, item := range s.items {
if sp, ok := item.(*SpriteImpl); ok {
layer++
sp.setLayer(layer)
}
}
}
// all returns all active shapes.
func (s *shapeManager) all() []Shape {
return s.items
}
// getTempShapes returns a copy of all active shapes in a temporary buffer.
func (s *shapeManager) getTempShapes() []Shape {
s.tempItems = sliceutil.CopyInto(s.tempItems, s.items, 50)
return s.tempItems
}
// count returns the number of active shapes.
func (s *shapeManager) count() int {
return len(s.items)
}
// findSprite finds a sprite by name (only non-cloned sprites).
func (s *shapeManager) findSprite(name SpriteName) *SpriteImpl {
for _, item := range s.items {
if sp, ok := item.(*SpriteImpl); ok {
if !sp.spriteState.Cloned && sp.name == name {
return sp
}
}
}
return nil
}
// findShapeIndex finds the index of a shape in the items slice.
func (s *shapeManager) findShapeIndex(target Shape) int {
for i, item := range s.items {
if item == target {
return i
}
}
return -1
}
// calculateNewIndex calculates the new index after moving n sprite layers.
func (s *shapeManager) calculateNewIndex(currentIdx, n int) int {
items := s.items
newIdx := currentIdx
if n > 0 {
for newIdx > 0 && n > 0 {
newIdx--
if _, ok := items[newIdx].(*SpriteImpl); ok {
n--
}
}
} else if n < 0 {
lastIdx := len(items) - 1
for newIdx < lastIdx && n < 0 {
newIdx++
if _, ok := items[newIdx].(*SpriteImpl); ok {
n++
}
}
}
return newIdx
}