-
Notifications
You must be signed in to change notification settings - Fork 965
/
Copy pathbitmaptextruntimeobject-pixi-renderer.ts
204 lines (180 loc) · 5.51 KB
/
bitmaptextruntimeobject-pixi-renderer.ts
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
namespace gdjs {
/**
* The PIXI.js renderer for the Bitmap Text runtime object.
*/
export class BitmapTextRuntimeObjectPixiRenderer {
_object: gdjs.BitmapTextRuntimeObject;
_pixiObject: PIXI.BitmapText;
/**
* @param runtimeObject The object to render
* @param instanceContainer The container in which the object is
*/
constructor(
runtimeObject: gdjs.BitmapTextRuntimeObject,
instanceContainer: gdjs.RuntimeInstanceContainer
) {
this._object = runtimeObject;
// Obtain the bitmap font to use in the object.
const bitmapFont = instanceContainer
.getGame()
.getBitmapFontManager()
.obtainBitmapFont(
runtimeObject._bitmapFontResourceName,
runtimeObject._textureAtlasResourceName
);
if (bitmapFont) {
this._pixiObject = new PIXI.BitmapText({
text: runtimeObject._text,
style: {
fontFamily: bitmapFont.fontFamily,
fontSize: bitmapFont.fontMetrics.fontSize,
},
});
} else {
const defaultBitmapFontName = instanceContainer
.getGame()
.getBitmapFontManager()
.getDefaultBitmapFont();
this._pixiObject = new PIXI.BitmapText({
text: runtimeObject._text,
style: {
fontFamily: defaultBitmapFontName,
fontSize: 20,
},
});
}
// Set the object on the scene
instanceContainer
.getLayer('')
.getRenderer()
.addRendererObject(this._pixiObject, runtimeObject.getZOrder());
// Set the anchor in the center, so that the object rotates around
// its center.
// @ts-ignore
this._pixiObject.anchor.x = 0.5;
// @ts-ignore
this._pixiObject.anchor.y = 0.5;
this.updateAlignment();
this.updateTextContent();
this.updateAngle();
this.updateOpacity();
this.updateScale();
this.updateWrappingWidth();
this.updateTint();
}
getRendererObject() {
return this._pixiObject;
}
onDestroy() {
// Mark the font from the object as not used anymore.
this._object
.getInstanceContainer()
.getGame()
.getBitmapFontManager()
.releaseBitmapFont(
Array.isArray(this._pixiObject.style.fontFamily)
? this._pixiObject.style.fontFamily[0]
: this._pixiObject.style.fontFamily
);
this._pixiObject.destroy();
}
getFontSize() {
return this._pixiObject.style.fontSize;
}
updateFont(): void {
// Get the new bitmap font to use
const bitmapFont = this._object
.getInstanceContainer()
.getGame()
.getBitmapFontManager()
.obtainBitmapFont(
this._object._bitmapFontResourceName,
this._object._textureAtlasResourceName
);
// Mark the old font as not used anymore
this._object
.getInstanceContainer()
.getGame()
.getBitmapFontManager()
.releaseBitmapFont(
Array.isArray(this._pixiObject.style.fontFamily)
? this._pixiObject.style.fontFamily[0]
: this._pixiObject.style.fontFamily
);
// Update the font used by the object:
if (bitmapFont) {
this._pixiObject.style.fontFamily = bitmapFont.fontFamily;
this._pixiObject.style.fontSize = bitmapFont.fontMetrics.fontSize;
this.updatePosition();
}
}
updateTint(): void {
this._pixiObject.tint = gdjs.rgbToHexNumber(
this._object._tint[0],
this._object._tint[1],
this._object._tint[2]
);
}
/**
* Get the tint of the bitmap object as a "R;G;B" string.
* @returns the tint of bitmap object in "R;G;B" format.
*/
getTint(): string {
return (
this._object._tint[0] +
';' +
this._object._tint[1] +
';' +
this._object._tint[2]
);
}
updateScale(): void {
this._pixiObject.scale.set(
Math.max(this._object._scaleX, 0),
Math.max(this._object._scaleY, 0)
);
this.updatePosition();
}
getScale() {
return Math.max(this._pixiObject.scale.x, this._pixiObject.scale.y);
}
updateWrappingWidth(): void {
if (this._object._wordWrap) {
this._pixiObject.style.wordWrap = true;
this._pixiObject.style.wordWrapWidth =
this._object._wrappingWidth / this._object._scaleX;
} else {
this._pixiObject.style.wordWrap = false;
this._pixiObject.style.wordWrapWidth = 0;
}
this.updatePosition();
}
updateTextContent(): void {
this._pixiObject.text = this._object._text;
this.updatePosition();
}
updateAlignment(): void {
// @ts-ignore - assume align is always a valid value.
this._pixiObject.align = this._object._align;
this.updatePosition();
}
updatePosition(): void {
this._pixiObject.position.x = this._object.x + this.getWidth() / 2;
this._pixiObject.position.y = this._object.y + this.getHeight() / 2;
}
updateAngle(): void {
this._pixiObject.rotation = gdjs.toRad(this._object.angle);
}
updateOpacity(): void {
this._pixiObject.alpha = this._object._opacity / 255;
}
getWidth(): float {
return this._pixiObject.width * this.getScale();
}
getHeight(): float {
return this._pixiObject.height * this.getScale();
}
}
export const BitmapTextRuntimeObjectRenderer =
BitmapTextRuntimeObjectPixiRenderer;
}