melonjs 10.0.2 → 10.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -13
- package/dist/melonjs.js +1213 -828
- package/dist/melonjs.min.js +3 -3
- package/dist/melonjs.module.d.ts +581 -454
- package/dist/melonjs.module.js +1182 -823
- package/package.json +6 -6
- package/src/camera/camera2d.js +3 -3
- package/src/game.js +1 -1
- package/src/index.js +4 -0
- package/src/input/pointer.js +22 -21
- package/src/input/pointerevent.js +10 -12
- package/src/level/tiled/TMXLayer.js +1 -1
- package/src/loader/loader.js +2 -2
- package/src/loader/loadingscreen.js +40 -72
- package/src/math/color.js +22 -4
- package/src/physics/body.js +1 -7
- package/src/physics/bounds.js +2 -2
- package/src/physics/detector.js +0 -1
- package/src/physics/world.js +2 -1
- package/src/renderable/container.js +1 -0
- package/src/renderable/imagelayer.js +2 -0
- package/src/renderable/nineslicesprite.js +211 -0
- package/src/renderable/renderable.js +3 -3
- package/src/renderable/sprite.js +2 -2
- package/src/state/stage.js +2 -2
- package/src/state/state.js +2 -2
- package/src/system/device.js +15 -1
- package/src/text/text.js +82 -21
- package/src/video/renderer.js +5 -3
- package/src/video/texture.js +17 -7
- package/src/video/texture_cache.js +11 -0
- package/src/video/video.js +3 -3
- package/src/video/webgl/buffer/vertex.js +130 -0
- package/src/video/webgl/shaders/quad.vert +1 -1
- package/src/video/webgl/webgl_compositor.js +104 -167
- package/src/video/webgl/webgl_renderer.js +25 -20
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import Sprite from "./sprite.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @classdesc
|
|
5
|
+
* A NineSliceSprite is similar to a Sprite, but it uses 9-slice scaling to strech its inner area to fit the size of the Renderable,
|
|
6
|
+
* by proportionally scaling a sprite by splitting it in a grid of nine parts (with only parts 1, 3, 7, 9 not being scaled). <br>
|
|
7
|
+
* <img src="images/9-slice-scaling.png"/><br>
|
|
8
|
+
* @see https://en.wikipedia.org/wiki/9-slice_scaling
|
|
9
|
+
* @class NineSliceSprite
|
|
10
|
+
* @extends me.Sprite
|
|
11
|
+
* @memberOf me
|
|
12
|
+
* @constructor
|
|
13
|
+
* @param {Number} x the x coordinates of the sprite object
|
|
14
|
+
* @param {Number} y the y coordinates of the sprite object
|
|
15
|
+
* @param {Object} settings Configuration parameters for the Sprite object
|
|
16
|
+
* @param {Number} settings.width the width of the Renderable over which the sprite needs to be stretched
|
|
17
|
+
* @param {Number} settings.height the height of the Renderable over which the sprite needs to be stretched
|
|
18
|
+
* @param {me.Renderer.Texture|HTMLImageElement|HTMLCanvasElement|String} settings.image reference to a texture, spritesheet image or to a texture atlas
|
|
19
|
+
* @param {String} [settings.name=""] name of this object
|
|
20
|
+
* @param {String} [settings.region] region name of a specific region to use when using a texture atlas, see {@link me.Renderer.Texture}
|
|
21
|
+
* @param {Number} [settings.framewidth] Width of a single frame within the spritesheet
|
|
22
|
+
* @param {Number} [settings.frameheight] Height of a single frame within the spritesheet
|
|
23
|
+
* @param {String|me.Color} [settings.tint] a tint to be applied to this sprite
|
|
24
|
+
* @param {Number} [settings.flipX] flip the sprite on the horizontal axis
|
|
25
|
+
* @param {Number} [settings.flipY] flip the sprite on the vertical axis
|
|
26
|
+
* @param {me.Vector2d} [settings.anchorPoint={x:0.5, y:0.5}] Anchor point to draw the frame at (defaults to the center of the frame).
|
|
27
|
+
* @example
|
|
28
|
+
* this.panelSprite = new me.NineSliceSprite(0, 0, {
|
|
29
|
+
* image : game.texture,
|
|
30
|
+
* region : "grey_panel",
|
|
31
|
+
* width : this.width,
|
|
32
|
+
* height : this.height
|
|
33
|
+
* });
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
class NineSliceSprite extends Sprite {
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @ignore
|
|
40
|
+
*/
|
|
41
|
+
constructor(x, y, settings) {
|
|
42
|
+
// call the super constructor
|
|
43
|
+
super(x, y, settings);
|
|
44
|
+
|
|
45
|
+
// ensure mandatory properties are defined
|
|
46
|
+
if ((typeof settings.width !== "number") || (typeof settings.height !== "number")) {
|
|
47
|
+
throw new Error("height and width properties are mandatory");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// override the renderable sprite with the given one
|
|
51
|
+
// resize based on the active frame
|
|
52
|
+
this.width = settings.width;
|
|
53
|
+
this.height = settings.height;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @ignore
|
|
58
|
+
*/
|
|
59
|
+
draw(renderer) {
|
|
60
|
+
// the frame to draw
|
|
61
|
+
var frame = this.current;
|
|
62
|
+
|
|
63
|
+
// cache the current position and size
|
|
64
|
+
var dx = this.pos.x,
|
|
65
|
+
dy = this.pos.y;
|
|
66
|
+
|
|
67
|
+
var w = frame.width,
|
|
68
|
+
h = frame.height;
|
|
69
|
+
|
|
70
|
+
// frame offset in the texture/atlas
|
|
71
|
+
var frame_offset = frame.offset;
|
|
72
|
+
var g_offset = this.offset;
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
// remove image's TexturePacker/ShoeBox rotation
|
|
76
|
+
if (frame.angle !== 0) {
|
|
77
|
+
renderer.translate(-dx, -dy);
|
|
78
|
+
renderer.rotate(frame.angle);
|
|
79
|
+
dx -= h;
|
|
80
|
+
w = frame.height;
|
|
81
|
+
h = frame.width;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
var sx = g_offset.x + frame_offset.x,
|
|
85
|
+
sy = g_offset.y + frame_offset.y;
|
|
86
|
+
|
|
87
|
+
// should this be configurable ?
|
|
88
|
+
var corner_width = frame.width / 4,
|
|
89
|
+
corner_height = frame.height / 4;
|
|
90
|
+
|
|
91
|
+
// OPTIMIZE ME !
|
|
92
|
+
|
|
93
|
+
// DRAW CORNERS
|
|
94
|
+
|
|
95
|
+
// Top Left
|
|
96
|
+
renderer.drawImage(
|
|
97
|
+
this.image,
|
|
98
|
+
sx, // sx
|
|
99
|
+
sy, // sy
|
|
100
|
+
corner_width, corner_height, // sw,sh
|
|
101
|
+
dx, dy, // dx,dy
|
|
102
|
+
corner_width, corner_height // dw,dh
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// Top Right
|
|
106
|
+
renderer.drawImage(
|
|
107
|
+
this.image,
|
|
108
|
+
sx + w - corner_width, // sx
|
|
109
|
+
sy, // sy
|
|
110
|
+
corner_width, corner_height, // sw,sh
|
|
111
|
+
dx + this.width - corner_width, // dx
|
|
112
|
+
dy, // dy
|
|
113
|
+
corner_width, corner_height // dw,dh
|
|
114
|
+
);
|
|
115
|
+
// Bottom Left
|
|
116
|
+
renderer.drawImage(
|
|
117
|
+
this.image,
|
|
118
|
+
sx, // sx
|
|
119
|
+
sy + h - corner_height, // sy
|
|
120
|
+
corner_width, corner_height, // sw,sh
|
|
121
|
+
dx, // dx
|
|
122
|
+
dy + this.height - corner_height, // dy
|
|
123
|
+
corner_width, corner_height // dw,dh
|
|
124
|
+
);
|
|
125
|
+
// Bottom Right
|
|
126
|
+
renderer.drawImage(
|
|
127
|
+
this.image,
|
|
128
|
+
sx + w - corner_width, // sx
|
|
129
|
+
sy + h - corner_height, // sy
|
|
130
|
+
corner_width, corner_height, // sw,sh
|
|
131
|
+
dx + this.width - corner_width, //dx
|
|
132
|
+
dy + this.height - corner_height, // dy
|
|
133
|
+
corner_width, corner_height // dw,dh
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
// DRAW SIDES and CENTER
|
|
138
|
+
var image_center_width = w - (corner_width << 1);
|
|
139
|
+
var image_center_height = h - (corner_height << 1);
|
|
140
|
+
|
|
141
|
+
var target_center_width = this.width - (corner_width << 1);
|
|
142
|
+
var target_center_height = this.height - (corner_height << 1);
|
|
143
|
+
|
|
144
|
+
//Top center
|
|
145
|
+
renderer.drawImage(
|
|
146
|
+
this.image,
|
|
147
|
+
sx + corner_width, // sx
|
|
148
|
+
sy, // sy
|
|
149
|
+
image_center_width, // sw
|
|
150
|
+
corner_height, // sh
|
|
151
|
+
dx + corner_width, // dx
|
|
152
|
+
dy, // dy
|
|
153
|
+
target_center_width, // dw
|
|
154
|
+
corner_height // dh
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
//Bottom center
|
|
158
|
+
renderer.drawImage(
|
|
159
|
+
this.image,
|
|
160
|
+
sx + corner_width, // sx
|
|
161
|
+
sy + h - corner_height, // sy
|
|
162
|
+
image_center_width, // sw
|
|
163
|
+
corner_height, // sh
|
|
164
|
+
dx + corner_width, // dx
|
|
165
|
+
dy + this.height - corner_height, // dx
|
|
166
|
+
target_center_width, // dw
|
|
167
|
+
corner_height // dh
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
// Middle Left
|
|
171
|
+
renderer.drawImage(
|
|
172
|
+
this.image,
|
|
173
|
+
sx, // sx
|
|
174
|
+
sy + corner_height, // sy
|
|
175
|
+
corner_width, // sw
|
|
176
|
+
image_center_height, // sh
|
|
177
|
+
dx, // dx
|
|
178
|
+
dy + corner_height, // dy
|
|
179
|
+
corner_width, // dw
|
|
180
|
+
target_center_height // dh
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
// Middle Right
|
|
184
|
+
renderer.drawImage(
|
|
185
|
+
this.image,
|
|
186
|
+
sx + w - corner_width, // sx
|
|
187
|
+
sy + corner_height, // sy
|
|
188
|
+
corner_width, // sw
|
|
189
|
+
image_center_height, // sh
|
|
190
|
+
dx + this.width - corner_width, // dx
|
|
191
|
+
dy + corner_height, // dy
|
|
192
|
+
corner_width, // dw
|
|
193
|
+
target_center_height // dh
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
// Middle Center
|
|
197
|
+
renderer.drawImage(
|
|
198
|
+
this.image,
|
|
199
|
+
sx + corner_width, // sx
|
|
200
|
+
sy + corner_height, // sy
|
|
201
|
+
image_center_width, // sw
|
|
202
|
+
image_center_height, // sh
|
|
203
|
+
dx + corner_width, // dx
|
|
204
|
+
dy + corner_height, // dy
|
|
205
|
+
target_center_width, // dw
|
|
206
|
+
target_center_height // dh
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
export default NineSliceSprite;
|
|
@@ -49,7 +49,6 @@ class Renderable extends Rect {
|
|
|
49
49
|
* @public
|
|
50
50
|
* @type {me.Body}
|
|
51
51
|
* @see me.Body
|
|
52
|
-
* @see me.collision#check
|
|
53
52
|
* @name body
|
|
54
53
|
* @memberOf me.Renderable#
|
|
55
54
|
* @example
|
|
@@ -422,6 +421,7 @@ class Renderable extends Rect {
|
|
|
422
421
|
if (isNaN(this.alpha)) {
|
|
423
422
|
this.alpha = 1.0;
|
|
424
423
|
}
|
|
424
|
+
this.isDirty = true;
|
|
425
425
|
}
|
|
426
426
|
}
|
|
427
427
|
|
|
@@ -739,8 +739,8 @@ class Renderable extends Rect {
|
|
|
739
739
|
// offset by the anchor point
|
|
740
740
|
renderer.translate(-ax, -ay);
|
|
741
741
|
|
|
742
|
-
// apply the
|
|
743
|
-
renderer.setTint(this.tint);
|
|
742
|
+
// apply the current tint and opacity
|
|
743
|
+
renderer.setTint(this.tint, this.getOpacity());
|
|
744
744
|
}
|
|
745
745
|
|
|
746
746
|
/**
|
package/src/renderable/sprite.js
CHANGED
|
@@ -21,7 +21,7 @@ import Renderable from "./renderable.js";
|
|
|
21
21
|
* @param {String} [settings.region] region name of a specific region to use when using a texture atlas, see {@link me.Renderer.Texture}
|
|
22
22
|
* @param {Number} [settings.framewidth] Width of a single frame within the spritesheet
|
|
23
23
|
* @param {Number} [settings.frameheight] Height of a single frame within the spritesheet
|
|
24
|
-
* @param {String|Color} [settings.tint] a tint to be applied to this sprite
|
|
24
|
+
* @param {String|me.Color} [settings.tint] a tint to be applied to this sprite
|
|
25
25
|
* @param {Number} [settings.flipX] flip the sprite on the horizontal axis
|
|
26
26
|
* @param {Number} [settings.flipY] flip the sprite on the vertical axis
|
|
27
27
|
* @param {me.Vector2d} [settings.anchorPoint={x:0.5, y:0.5}] Anchor point to draw the frame at (defaults to the center of the frame).
|
|
@@ -182,7 +182,7 @@ class Sprite extends Renderable {
|
|
|
182
182
|
// set the default rotation angle is defined in the settings
|
|
183
183
|
// * WARNING: rotating sprites decreases performance with Canvas Renderer
|
|
184
184
|
if (typeof (settings.rotation) !== "undefined") {
|
|
185
|
-
this.
|
|
185
|
+
this.rotate(settings.rotation);
|
|
186
186
|
}
|
|
187
187
|
|
|
188
188
|
// update anchorPoint
|
package/src/state/stage.js
CHANGED
|
@@ -20,7 +20,7 @@ var default_settings = {
|
|
|
20
20
|
* @memberOf me
|
|
21
21
|
* @constructor
|
|
22
22
|
* @param {Object} [options] The stage` parameters
|
|
23
|
-
* @param {
|
|
23
|
+
* @param {me.Camera2d[]} [options.cameras=[new me.Camera2d()]] a list of cameras (experimental)
|
|
24
24
|
* @param {Function} [options.onResetEvent] called by the state manager when reseting the object
|
|
25
25
|
* @param {Function} [options.onDestroyEvent] called by the state manager before switching to another state
|
|
26
26
|
* @see me.state
|
|
@@ -57,7 +57,7 @@ class Stage {
|
|
|
57
57
|
* @ignore
|
|
58
58
|
*/
|
|
59
59
|
reset() {
|
|
60
|
-
|
|
60
|
+
|
|
61
61
|
// add all defined cameras
|
|
62
62
|
this.settings.cameras.forEach((camera) => {
|
|
63
63
|
this.cameras.set(camera.name, camera);
|
package/src/state/state.js
CHANGED
|
@@ -4,7 +4,7 @@ import * as event from "./../system/event.js";
|
|
|
4
4
|
import timer from "./../system/timer.js";
|
|
5
5
|
import * as game from "./../game.js";
|
|
6
6
|
import Stage from "./../state/stage.js";
|
|
7
|
-
import
|
|
7
|
+
import DefaultLoadingScreen from "./../loader/loadingscreen.js";
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
// current state
|
|
@@ -137,7 +137,7 @@ function _switchState(state) {
|
|
|
137
137
|
// initialize me.state on system boot
|
|
138
138
|
event.on(event.BOOT, () => {
|
|
139
139
|
// set the built-in loading stage
|
|
140
|
-
state.set(state.LOADING,
|
|
140
|
+
state.set(state.LOADING, new DefaultLoadingScreen());
|
|
141
141
|
// set and enable the default stage
|
|
142
142
|
state.set(state.DEFAULT, new Stage());
|
|
143
143
|
// enable by default as soon as the display is initialized
|
package/src/system/device.js
CHANGED
|
@@ -130,6 +130,10 @@ function _checkCapabilities() {
|
|
|
130
130
|
device.hasDeviceOrientation = !!window.DeviceOrientationEvent;
|
|
131
131
|
device.hasAccelerometer = !!window.DeviceMotionEvent;
|
|
132
132
|
|
|
133
|
+
// support the ScreenOrientation API
|
|
134
|
+
device.ScreenOrientation = (typeof screen !== "undefined") &&
|
|
135
|
+
(typeof screen.orientation !== "undefined");
|
|
136
|
+
|
|
133
137
|
// fullscreen api detection & polyfill when possible
|
|
134
138
|
device.hasFullscreenSupport = prefixed("fullscreenEnabled", document) ||
|
|
135
139
|
document.mozFullScreenEnabled;
|
|
@@ -273,6 +277,16 @@ let device = {
|
|
|
273
277
|
*/
|
|
274
278
|
hasDeviceOrientation : false,
|
|
275
279
|
|
|
280
|
+
/**
|
|
281
|
+
* Supports the ScreenOrientation API
|
|
282
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/onchange
|
|
283
|
+
* @type Boolean
|
|
284
|
+
* @readonly
|
|
285
|
+
* @name ScreenOrientation
|
|
286
|
+
* @memberOf me.device
|
|
287
|
+
*/
|
|
288
|
+
ScreenOrientation : false,
|
|
289
|
+
|
|
276
290
|
/**
|
|
277
291
|
* Browser full screen support
|
|
278
292
|
* @type Boolean
|
|
@@ -705,7 +719,7 @@ let device = {
|
|
|
705
719
|
var screen = window.screen;
|
|
706
720
|
|
|
707
721
|
// first try using "standard" values
|
|
708
|
-
if (
|
|
722
|
+
if (this.ScreenOrientation === true) {
|
|
709
723
|
var orientation = prefixed("orientation", screen);
|
|
710
724
|
if (typeof orientation !== "undefined" && typeof orientation.type === "string") {
|
|
711
725
|
// Screen Orientation API specification
|
package/src/text/text.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import Color from "./../math/color.js";
|
|
2
2
|
import Renderer from "./../video/renderer.js";
|
|
3
|
-
import
|
|
3
|
+
import WebGLRenderer from "./../video/webgl/webgl_renderer.js";
|
|
4
|
+
import { renderer, createCanvas } from "./../video/video.js";
|
|
4
5
|
import * as stringUtil from "./../utils/string.js";
|
|
5
6
|
import pool from "./../system/pooling.js";
|
|
6
7
|
import Renderable from "./../renderable/renderable.js";
|
|
8
|
+
import { nextPowerOfTwo } from "./../math/math.js";
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
/*
|
|
@@ -22,7 +24,7 @@ var toPX = [12, 24, 0.75, 1];
|
|
|
22
24
|
* apply the current font style to the given context
|
|
23
25
|
* @ignore
|
|
24
26
|
*/
|
|
25
|
-
var setContextStyle = function(context, font, stroke) {
|
|
27
|
+
var setContextStyle = function(context, font, stroke = false) {
|
|
26
28
|
context.font = font.font;
|
|
27
29
|
context.fillStyle = font.fillStyle.toRGBA();
|
|
28
30
|
if (stroke === true) {
|
|
@@ -52,7 +54,8 @@ var setContextStyle = function(context, font, stroke) {
|
|
|
52
54
|
* @param {String} [settings.textBaseline="top"] the text baseline
|
|
53
55
|
* @param {Number} [settings.lineHeight=1.0] line spacing height
|
|
54
56
|
* @param {me.Vector2d} [settings.anchorPoint={x:0.0, y:0.0}] anchor point to draw the text at
|
|
55
|
-
* @param {
|
|
57
|
+
* @param {Boolean} [settings.offScreenCanvas=false] whether to draw the font to an individual "cache" texture first
|
|
58
|
+
* @param {(String|String[])} [settings.text=""] a string, or an array of strings
|
|
56
59
|
* @example
|
|
57
60
|
* var font = new me.Text(0, 0, {font: "Arial", size: 8, fillStyle: this.color});
|
|
58
61
|
*/
|
|
@@ -143,6 +146,17 @@ class Text extends Renderable {
|
|
|
143
146
|
*/
|
|
144
147
|
this.lineHeight = settings.lineHeight || 1.0;
|
|
145
148
|
|
|
149
|
+
/**
|
|
150
|
+
* whether to draw the font to a indidividual offscreen canvas texture first <br>
|
|
151
|
+
* Note: this will improve performances when using WebGL, but will impact
|
|
152
|
+
* memory consumption as every text element will have its own canvas texture
|
|
153
|
+
* @public
|
|
154
|
+
* @type Boolean
|
|
155
|
+
* @default false
|
|
156
|
+
* @name me.Text#offScreenCanvas
|
|
157
|
+
*/
|
|
158
|
+
this.offScreenCanvas = false;
|
|
159
|
+
|
|
146
160
|
/**
|
|
147
161
|
* the text to be displayed
|
|
148
162
|
* @private
|
|
@@ -185,8 +199,31 @@ class Text extends Renderable {
|
|
|
185
199
|
this.italic();
|
|
186
200
|
}
|
|
187
201
|
|
|
202
|
+
if (settings.offScreenCanvas === true) {
|
|
203
|
+
this.offScreenCanvas = true;
|
|
204
|
+
this.canvas = createCanvas(2, 2, true);
|
|
205
|
+
this.context = this.canvas.getContext("2d");
|
|
206
|
+
}
|
|
207
|
+
|
|
188
208
|
// set the text
|
|
189
209
|
this.setText(settings.text);
|
|
210
|
+
|
|
211
|
+
// force update bounds on object creation
|
|
212
|
+
this.update(0);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @ignore */
|
|
216
|
+
onDeactivateEvent() {
|
|
217
|
+
// free the canvas and potential corresponding texture when deactivated
|
|
218
|
+
if (this.offScreenCanvas === true) {
|
|
219
|
+
if (renderer instanceof WebGLRenderer) {
|
|
220
|
+
renderer.currentCompositor.unbindTexture2D(renderer.cache.get(this.canvas));
|
|
221
|
+
renderer.cache.remove(this.canvas);
|
|
222
|
+
}
|
|
223
|
+
this.canvas.width = this.canvas.height = 0;
|
|
224
|
+
this.context = undefined;
|
|
225
|
+
this.canvas = undefined;
|
|
226
|
+
}
|
|
190
227
|
}
|
|
191
228
|
|
|
192
229
|
/**
|
|
@@ -267,11 +304,7 @@ class Text extends Renderable {
|
|
|
267
304
|
* @param {Number|String|String[]} value a string, or an array of strings
|
|
268
305
|
* @return this object for chaining
|
|
269
306
|
*/
|
|
270
|
-
setText(value) {
|
|
271
|
-
if (typeof value === "undefined") {
|
|
272
|
-
value = "";
|
|
273
|
-
}
|
|
274
|
-
|
|
307
|
+
setText(value = "") {
|
|
275
308
|
if (this._text.toString() !== value.toString()) {
|
|
276
309
|
if (!Array.isArray(value)) {
|
|
277
310
|
this._text = ("" + value).split("\n");
|
|
@@ -289,28 +322,25 @@ class Text extends Renderable {
|
|
|
289
322
|
* @name measureText
|
|
290
323
|
* @memberOf me.Text.prototype
|
|
291
324
|
* @function
|
|
292
|
-
* @param {me.CanvasRenderer|me.WebGLRenderer} [renderer] reference
|
|
325
|
+
* @param {me.CanvasRenderer|me.WebGLRenderer} [renderer] reference to the active renderer
|
|
293
326
|
* @param {String} [text] the text to be measured
|
|
294
327
|
* @param {me.Rect|me.Bounds} [ret] a object in which to store the text metrics
|
|
295
328
|
* @returns {TextMetrics} a TextMetrics object with two properties: `width` and `height`, defining the output dimensions
|
|
296
329
|
*/
|
|
297
330
|
measureText(_renderer, text, ret) {
|
|
298
331
|
var context;
|
|
332
|
+
var textMetrics = ret || this.getBounds();
|
|
333
|
+
var lineHeight = this.fontSize * this.lineHeight;
|
|
334
|
+
var strings = typeof text !== "undefined" ? ("" + (text)).split("\n") : this._text;
|
|
299
335
|
|
|
300
|
-
if (
|
|
301
|
-
context =
|
|
336
|
+
if (this.offScreenCanvas === true) {
|
|
337
|
+
context = this.context;
|
|
302
338
|
} else if (_renderer instanceof Renderer) {
|
|
303
339
|
context = _renderer.getFontContext();
|
|
304
340
|
} else {
|
|
305
|
-
|
|
306
|
-
context = _renderer;
|
|
341
|
+
context = renderer.getFontContext();
|
|
307
342
|
}
|
|
308
343
|
|
|
309
|
-
var textMetrics = ret || this.getBounds();
|
|
310
|
-
var lineHeight = this.fontSize * this.lineHeight;
|
|
311
|
-
|
|
312
|
-
var strings = typeof text !== "undefined" ? ("" + (text)).split("\n") : this._text;
|
|
313
|
-
|
|
314
344
|
// save the previous context
|
|
315
345
|
context.save();
|
|
316
346
|
|
|
@@ -346,7 +376,33 @@ class Text extends Renderable {
|
|
|
346
376
|
*/
|
|
347
377
|
update(/* dt */) {
|
|
348
378
|
if (this.isDirty === true) {
|
|
349
|
-
this.measureText();
|
|
379
|
+
var bounds = this.measureText(renderer);
|
|
380
|
+
if (this.offScreenCanvas === true) {
|
|
381
|
+
var width = Math.round(bounds.width),
|
|
382
|
+
height = Math.round(bounds.height);
|
|
383
|
+
|
|
384
|
+
if (renderer instanceof WebGLRenderer) {
|
|
385
|
+
// invalidate the previous corresponding texture so that it can reuploaded once changed
|
|
386
|
+
renderer.currentCompositor.unbindTexture2D(renderer.cache.get(this.canvas));
|
|
387
|
+
|
|
388
|
+
if (renderer.WebGLVersion === 1) {
|
|
389
|
+
// round size to next Pow2
|
|
390
|
+
width = nextPowerOfTwo(bounds.width);
|
|
391
|
+
height = nextPowerOfTwo(bounds.height);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// resize the cache canvas if necessary
|
|
396
|
+
if (this.canvas.width < width || this.canvas.height < height) {
|
|
397
|
+
this.canvas.width = width;
|
|
398
|
+
this.canvas.height = height;
|
|
399
|
+
// resizing the canvas will automatically clear its content
|
|
400
|
+
} else {
|
|
401
|
+
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
402
|
+
}
|
|
403
|
+
this._drawFont(this.context, this._text, this.pos.x - bounds.x, this.pos.y - bounds.y, false);
|
|
404
|
+
|
|
405
|
+
}
|
|
350
406
|
}
|
|
351
407
|
return this.isDirty;
|
|
352
408
|
}
|
|
@@ -396,7 +452,12 @@ class Text extends Renderable {
|
|
|
396
452
|
}
|
|
397
453
|
|
|
398
454
|
// draw the text
|
|
399
|
-
|
|
455
|
+
if (this.offScreenCanvas === true) {
|
|
456
|
+
renderer.drawImage(this.canvas, this.getBounds().x, this.getBounds().y);
|
|
457
|
+
} else {
|
|
458
|
+
renderer.drawFont(this._drawFont(renderer.getFontContext(), this._text, x, y, stroke));
|
|
459
|
+
}
|
|
460
|
+
|
|
400
461
|
|
|
401
462
|
// for backward compatibilty
|
|
402
463
|
if (typeof this.ancestor === "undefined") {
|
|
@@ -428,7 +489,7 @@ class Text extends Renderable {
|
|
|
428
489
|
/**
|
|
429
490
|
* @ignore
|
|
430
491
|
*/
|
|
431
|
-
_drawFont(context, text, x, y, stroke) {
|
|
492
|
+
_drawFont(context, text, x, y, stroke = false) {
|
|
432
493
|
setContextStyle(context, this, stroke);
|
|
433
494
|
|
|
434
495
|
var lineHeight = this.fontSize * this.lineHeight;
|
package/src/video/renderer.js
CHANGED
|
@@ -180,7 +180,7 @@ class Renderer {
|
|
|
180
180
|
* @function
|
|
181
181
|
* @param {HTMLCanvasElement} canvas
|
|
182
182
|
* @param {Boolean} [transparent=true] use false to disable transparency
|
|
183
|
-
* @return {
|
|
183
|
+
* @return {CanvasRenderingContext2D}
|
|
184
184
|
*/
|
|
185
185
|
getContext2d(c, transparent) {
|
|
186
186
|
if (typeof c === "undefined" || c === null) {
|
|
@@ -422,11 +422,13 @@ class Renderer {
|
|
|
422
422
|
* @name setTint
|
|
423
423
|
* @memberOf me.Renderer.prototype
|
|
424
424
|
* @function
|
|
425
|
-
* @param {me.Color}
|
|
425
|
+
* @param {me.Color} tint the tint color
|
|
426
|
+
* @param {Number} [alpha] an alpha value to be applied to the tint
|
|
426
427
|
*/
|
|
427
|
-
setTint(tint) {
|
|
428
|
+
setTint(tint, alpha = tint.alpha) {
|
|
428
429
|
// global tint color
|
|
429
430
|
this.currentTint.copy(tint);
|
|
431
|
+
this.currentTint.alpha *= alpha;
|
|
430
432
|
}
|
|
431
433
|
|
|
432
434
|
/**
|
package/src/video/texture.js
CHANGED
|
@@ -282,10 +282,10 @@ export class Texture {
|
|
|
282
282
|
var sh = atlas[frame].height;
|
|
283
283
|
|
|
284
284
|
atlas[frame].uvs = new Float32Array([
|
|
285
|
-
s.x / w, //
|
|
286
|
-
s.y / h, //
|
|
287
|
-
(s.x + sw) / w, //
|
|
288
|
-
(s.y + sh) / h //
|
|
285
|
+
s.x / w, // u0 (left)
|
|
286
|
+
s.y / h, // v0 (top)
|
|
287
|
+
(s.x + sw) / w, // u1 (right)
|
|
288
|
+
(s.y + sh) / h // v1 (bottom)
|
|
289
289
|
]);
|
|
290
290
|
// Cache source coordinates
|
|
291
291
|
// TODO: Remove this when the Batcher only accepts a region name
|
|
@@ -421,7 +421,8 @@ export class Texture {
|
|
|
421
421
|
* @function
|
|
422
422
|
* @param {String} name name of the sprite
|
|
423
423
|
* @param {Object} [settings] Additional settings passed to the {@link me.Sprite} contructor
|
|
424
|
-
* @
|
|
424
|
+
* @param {Boolean} [nineSlice=false] if true returns a 9-slice sprite
|
|
425
|
+
* @return {me.Sprite|me.NineSliceSprite}
|
|
425
426
|
* @example
|
|
426
427
|
* // create a new texture object under the `game` namespace
|
|
427
428
|
* game.texture = new me.video.renderer.Texture(
|
|
@@ -434,11 +435,20 @@ export class Texture {
|
|
|
434
435
|
* var sprite = game.texture.createSpriteFromName("coin.png");
|
|
435
436
|
* // set the renderable position to bottom center
|
|
436
437
|
* sprite.anchorPoint.set(0.5, 1.0);
|
|
438
|
+
* ...
|
|
439
|
+
* ...
|
|
440
|
+
* // create a 9-slice sprite
|
|
441
|
+
* var dialogPanel = game.texture.createSpriteFromName(
|
|
442
|
+
* "rpg_dialo.png",
|
|
443
|
+
* // width & height are mandatory for 9-slice sprites
|
|
444
|
+
* { width: this.width, height: this.height },
|
|
445
|
+
* true
|
|
446
|
+
* );
|
|
437
447
|
*/
|
|
438
|
-
createSpriteFromName(name, settings) {
|
|
448
|
+
createSpriteFromName(name, settings, nineSlice = false) {
|
|
439
449
|
// instantiate a new sprite object
|
|
440
450
|
return pool.pull(
|
|
441
|
-
"me.Sprite",
|
|
451
|
+
nineSlice === true ? "me.NineSliceSprite" : "me.Sprite",
|
|
442
452
|
0, 0,
|
|
443
453
|
Object.assign({
|
|
444
454
|
image: this,
|
|
@@ -57,6 +57,17 @@ class TextureCache {
|
|
|
57
57
|
return this.cache.get(image);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* @ignore
|
|
62
|
+
*/
|
|
63
|
+
remove(image) {
|
|
64
|
+
if (!this.cache.has(image)) {
|
|
65
|
+
if (this.cache.remove(image) === true) {
|
|
66
|
+
this.length--;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
60
71
|
/**
|
|
61
72
|
* @ignore
|
|
62
73
|
*/
|
package/src/video/video.js
CHANGED
|
@@ -302,9 +302,9 @@ export function init(game_width, game_height, options) {
|
|
|
302
302
|
},
|
|
303
303
|
false
|
|
304
304
|
);
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
window.screen.
|
|
305
|
+
|
|
306
|
+
if (device.ScreenOrientation === true) {
|
|
307
|
+
window.screen.orientation.onchange = function (e) {
|
|
308
308
|
event.emit(event.WINDOW_ONORIENTATION_CHANGE, e);
|
|
309
309
|
};
|
|
310
310
|
}
|