Repository navigation
Expand file tree
/
Copy pathcostume.go
More file actions
178 lines (160 loc) · 5.22 KB
/
Copy pathcostume.go
File metadata and controls
178 lines (160 loc) · 5.22 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
/*
* 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/spbase/mathf"
assetutil "github.com/goplus/spx/v3/internal/assets"
coreproject "github.com/goplus/spx/v3/internal/core/project"
"github.com/goplus/spx/v3/internal/engine"
)
// costumeSetImage represents metadata for a costume set image.
type costumeSetImage struct {
path string
rc coreproject.CostumeSetRect
width float64
height float64
nx int // number of frames in the image
}
// costume represents a single costume (image frame) for a sprite or backdrop.
type costume struct {
name SpriteCostumeName
width, height int
center mathf.Vec2 // center point
imageSize mathf.Vec2 // actual image dimensions
pivot mathf.Vec2
faceRight float64
bitmapResolution int
path string
setIndex int // costume index in set (-1 if not part of a set)
posX, posY int // position in atlas (left top corner)
atlasUVRect mathf.Vec4 // UV coordinates for atlas texture
}
// newCostumeWithSize creates a costume with specified dimensions (no image file).
func newCostumeWithSize(width, height int) *costume {
frame := assetutil.NewSizedFrame(width, height)
return &costume{
setIndex: -1,
width: frame.Width,
height: frame.Height,
bitmapResolution: frame.BitmapResolution,
posX: frame.PosX,
posY: frame.PosY,
imageSize: frame.ImageSize,
center: frame.Center,
atlasUVRect: frame.AtlasUVRect,
}
}
// newCostumeWith creates a costume from a costume set image.
func newCostumeWith(name string, img *costumeSetImage, faceRight float64, frameIndex, bitmapResolution int) *costume {
frame := assetutil.NewAtlasFrame(
img.width,
img.height,
img.path,
img.rc.X,
img.rc.Y,
img.rc.W,
img.rc.H,
img.nx,
frameIndex,
bitmapResolution,
getImageSizeCached,
)
return &costume{
path: img.path,
name: name,
setIndex: frameIndex,
faceRight: faceRight,
bitmapResolution: frame.BitmapResolution,
imageSize: frame.ImageSize,
width: frame.Width,
height: frame.Height,
posX: frame.PosX,
posY: frame.PosY,
atlasUVRect: frame.AtlasUVRect,
center: frame.Center,
}
}
// newCostume creates a costume from a costume configuration.
func newCostume(config *coreproject.CostumeConfig) *costume {
fullPath := config.Path
frame := assetutil.NewStandaloneFrame(
config.ImageWidth,
config.ImageHeight,
fullPath,
config.BitmapResolution,
getImageSizeCached,
)
return &costume{
name: config.Name,
setIndex: -1,
center: mathf.Vec2{X: config.X, Y: config.Y},
faceRight: config.FaceRight,
bitmapResolution: frame.BitmapResolution,
path: fullPath,
imageSize: frame.ImageSize,
width: frame.Width,
height: frame.Height,
posX: frame.PosX,
posY: frame.PosY,
atlasUVRect: frame.AtlasUVRect,
}
}
func newBackdropCostume(config *coreproject.BackdropConfig) *costume {
costume := newCostume(&config.CostumeConfig)
costume.pivot = config.Pivot
return costume
}
func costumeAssetPath(path string) string {
return engine.ToAssetPath(path)
}
func (c *costume) getAssetPath() string {
return costumeAssetPath(c.path)
}
// getImageSizeCached retrieves image size from cache or loads it.
func getImageSizeCached(imagePath string) mathf.Vec2 {
cache := imageSizeCacheRef()
if v, ok := cache.Load(imagePath); ok {
return v.(mathf.Vec2)
}
size := getCostumeAssetSize(imagePath)
cache.Store(imagePath, size)
return size
}
// getCostumeAssetSize loads the actual image size from the asset.
func getCostumeAssetSize(imagePath string) mathf.Vec2 {
assetPath := costumeAssetPath(imagePath)
if game, ok := engine.GetGame().(*Game); ok && game != nil {
return game.engine().ResMgr.GetImageSize(assetPath)
}
return engine.Managers().ResMgr.GetImageSize(assetPath)
}
// getSize returns the size of the costume accounting for bitmap resolution.
func (c *costume) getSize() (int, int) {
return c.width / c.bitmapResolution, c.height / c.bitmapResolution
}
// renderAnchorInSPX converts the costume center from top-left, Y-down asset
// coordinates into a local SPX anchor relative to the geometric image center.
func (c *costume) renderAnchorInSPX() mathf.Vec2 {
resolution := float64(c.bitmapResolution)
return mathf.NewVec2(
(c.center.X-float64(c.width)/2)/resolution,
(float64(c.height)/2-c.center.Y)/resolution,
)
}
// isAtlas returns true if this costume is part of an atlas/set.
func (c *costume) isAtlas() bool {
return c.setIndex >= 0
}