@vpmedia/phaser 1.0.1 → 1.0.3

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.
Files changed (112) hide show
  1. package/README.md +20 -3
  2. package/dist/phaser.cjs +1 -1
  3. package/dist/phaser.cjs.LICENSE.txt +1 -1
  4. package/dist/phaser.cjs.map +1 -1
  5. package/dist/phaser.js +1 -1
  6. package/dist/phaser.js.LICENSE.txt +1 -1
  7. package/dist/phaser.js.map +1 -1
  8. package/package.json +23 -17
  9. package/src/index.js +142 -0
  10. package/src/phaser/core/animation.js +355 -0
  11. package/src/phaser/core/animation_manager.js +238 -0
  12. package/src/phaser/core/animation_parser.js +133 -0
  13. package/src/phaser/core/array_set.js +107 -0
  14. package/src/phaser/core/cache.js +558 -0
  15. package/src/phaser/core/const.js +106 -0
  16. package/src/phaser/core/device.js +67 -0
  17. package/src/phaser/core/device_util.js +388 -0
  18. package/src/phaser/core/dom.js +207 -0
  19. package/src/phaser/core/event_manager.js +243 -0
  20. package/src/phaser/core/factory.js +74 -0
  21. package/src/phaser/core/frame.js +75 -0
  22. package/src/phaser/core/frame_data.js +84 -0
  23. package/src/phaser/core/frame_util.js +33 -0
  24. package/src/phaser/core/game.js +412 -0
  25. package/src/phaser/core/input.js +401 -0
  26. package/src/phaser/core/input_button.js +102 -0
  27. package/src/phaser/core/input_handler.js +687 -0
  28. package/src/phaser/core/input_mouse.js +289 -0
  29. package/src/phaser/core/input_mspointer.js +197 -0
  30. package/src/phaser/core/input_pointer.js +427 -0
  31. package/src/phaser/core/input_touch.js +157 -0
  32. package/src/phaser/core/loader.js +1057 -0
  33. package/src/phaser/core/loader_parser.js +109 -0
  34. package/src/phaser/core/raf.js +46 -0
  35. package/src/phaser/core/raf_fb.js +75 -0
  36. package/src/phaser/core/raf_to.js +34 -0
  37. package/src/phaser/core/scale_manager.js +806 -0
  38. package/src/phaser/core/scene.js +65 -0
  39. package/src/phaser/core/scene_manager.js +309 -0
  40. package/src/phaser/core/signal.js +175 -0
  41. package/src/phaser/core/signal_binding.js +69 -0
  42. package/src/phaser/core/sound.js +538 -0
  43. package/src/phaser/core/sound_manager.js +364 -0
  44. package/src/phaser/core/stage.js +108 -0
  45. package/src/phaser/core/time.js +203 -0
  46. package/src/phaser/core/timer.js +276 -0
  47. package/src/phaser/core/timer_event.js +21 -0
  48. package/src/phaser/core/tween.js +329 -0
  49. package/src/phaser/core/tween_data.js +258 -0
  50. package/src/phaser/core/tween_easing.js +341 -0
  51. package/src/phaser/core/tween_manager.js +185 -0
  52. package/src/phaser/core/world.js +18 -0
  53. package/src/phaser/display/bitmap_text.js +322 -0
  54. package/src/phaser/display/button.js +194 -0
  55. package/src/phaser/display/canvas/buffer.js +36 -0
  56. package/src/phaser/display/canvas/graphics.js +227 -0
  57. package/src/phaser/display/canvas/masker.js +39 -0
  58. package/src/phaser/display/canvas/pool.js +126 -0
  59. package/src/phaser/display/canvas/renderer.js +123 -0
  60. package/src/phaser/display/canvas/tinter.js +144 -0
  61. package/src/phaser/display/canvas/util.js +159 -0
  62. package/src/phaser/display/display_object.js +597 -0
  63. package/src/phaser/display/graphics.js +723 -0
  64. package/src/phaser/display/graphics_data.js +27 -0
  65. package/src/phaser/display/graphics_data_util.js +15 -0
  66. package/src/phaser/display/group.js +227 -0
  67. package/src/phaser/display/image.js +288 -0
  68. package/src/phaser/display/sprite_batch.js +15 -0
  69. package/src/phaser/display/sprite_util.js +250 -0
  70. package/src/phaser/display/text.js +1089 -0
  71. package/src/phaser/display/webgl/abstract_filter.js +25 -0
  72. package/src/phaser/display/webgl/base_texture.js +68 -0
  73. package/src/phaser/display/webgl/blend_manager.js +35 -0
  74. package/src/phaser/display/webgl/earcut.js +662 -0
  75. package/src/phaser/display/webgl/earcut_node.js +28 -0
  76. package/src/phaser/display/webgl/fast_sprite_batch.js +242 -0
  77. package/src/phaser/display/webgl/filter_manager.js +46 -0
  78. package/src/phaser/display/webgl/filter_texture.js +61 -0
  79. package/src/phaser/display/webgl/graphics.js +624 -0
  80. package/src/phaser/display/webgl/graphics_data.js +42 -0
  81. package/src/phaser/display/webgl/mask_manager.js +36 -0
  82. package/src/phaser/display/webgl/render_texture.js +81 -0
  83. package/src/phaser/display/webgl/renderer.js +234 -0
  84. package/src/phaser/display/webgl/shader/complex.js +74 -0
  85. package/src/phaser/display/webgl/shader/fast.js +97 -0
  86. package/src/phaser/display/webgl/shader/normal.js +225 -0
  87. package/src/phaser/display/webgl/shader/primitive.js +72 -0
  88. package/src/phaser/display/webgl/shader/strip.js +77 -0
  89. package/src/phaser/display/webgl/shader_manager.js +89 -0
  90. package/src/phaser/display/webgl/sprite_batch.js +320 -0
  91. package/src/phaser/display/webgl/stencil_manager.js +170 -0
  92. package/src/phaser/display/webgl/texture.js +117 -0
  93. package/src/phaser/display/webgl/texture_util.js +34 -0
  94. package/src/phaser/display/webgl/util.js +78 -0
  95. package/src/phaser/geom/circle.js +186 -0
  96. package/src/phaser/geom/ellipse.js +65 -0
  97. package/src/phaser/geom/line.js +190 -0
  98. package/src/phaser/geom/matrix.js +147 -0
  99. package/src/phaser/geom/point.js +164 -0
  100. package/src/phaser/geom/polygon.js +140 -0
  101. package/src/phaser/geom/rectangle.js +306 -0
  102. package/src/phaser/geom/rounded_rectangle.js +36 -0
  103. package/src/phaser/geom/util/circle.js +122 -0
  104. package/src/phaser/geom/util/ellipse.js +34 -0
  105. package/src/phaser/geom/util/line.js +135 -0
  106. package/src/phaser/geom/util/matrix.js +53 -0
  107. package/src/phaser/geom/util/point.js +296 -0
  108. package/src/phaser/geom/util/polygon.js +28 -0
  109. package/src/phaser/geom/util/rectangle.js +229 -0
  110. package/src/phaser/geom/util/rounded_rectangle.js +32 -0
  111. package/src/phaser/util/math.js +297 -0
  112. package/src/phaser/util/string.js +32 -0
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import Tween from './tween';
7
+ import {
8
+ LinearNone,
9
+ QuadraticIn, QuadraticOut, QuadraticInOut,
10
+ CubicIn, CubicOut, CubicInOut,
11
+ QuarticIn, QuarticOut, QuarticInOut,
12
+ QuinticIn, QuinticOut, QuinticInOut,
13
+ SinusoidalIn, SinusoidalOut, SinusoidalInOut,
14
+ ExponentialIn, ExponentialOut, ExponentialInOut,
15
+ CircularIn, CircularOut, CircularInOut,
16
+ ElasticIn, ElasticOut, ElasticInOut,
17
+ BackIn, BackOut, BackInOut,
18
+ BounceIn, BounceOut, BounceInOut,
19
+ } from './tween_easing';
20
+ import { GROUP } from './const';
21
+
22
+ export default class {
23
+
24
+ constructor(game) {
25
+ this.game = game;
26
+ this.frameBased = false;
27
+ this._tweens = [];
28
+ this._add = [];
29
+ this.easeMap = {
30
+ Linear: LinearNone,
31
+ Quad: QuadraticOut,
32
+ Cubic: CubicOut,
33
+ Quart: QuarticOut,
34
+ Quint: QuinticOut,
35
+ Sine: SinusoidalOut,
36
+ Expo: ExponentialOut,
37
+ Circ: CircularOut,
38
+ Elastic: ElasticOut,
39
+ Back: BackOut,
40
+ Bounce: BounceOut,
41
+ 'Quad.easeIn': QuadraticIn,
42
+ 'Cubic.easeIn': CubicIn,
43
+ 'Quart.easeIn': QuarticIn,
44
+ 'Quint.easeIn': QuinticIn,
45
+ 'Sine.easeIn': SinusoidalIn,
46
+ 'Expo.easeIn': ExponentialIn,
47
+ 'Circ.easeIn': CircularIn,
48
+ 'Elastic.easeIn': ElasticIn,
49
+ 'Back.easeIn': BackIn,
50
+ 'Bounce.easeIn': BounceIn,
51
+ 'Quad.easeOut': QuadraticOut,
52
+ 'Cubic.easeOut': CubicOut,
53
+ 'Quart.easeOut': QuarticOut,
54
+ 'Quint.easeOut': QuinticOut,
55
+ 'Sine.easeOut': SinusoidalOut,
56
+ 'Expo.easeOut': ExponentialOut,
57
+ 'Circ.easeOut': CircularOut,
58
+ 'Elastic.easeOut': ElasticOut,
59
+ 'Back.easeOut': BackOut,
60
+ 'Bounce.easeOut': BounceOut,
61
+ 'Quad.easeInOut': QuadraticInOut,
62
+ 'Cubic.easeInOut': CubicInOut,
63
+ 'Quart.easeInOut': QuarticInOut,
64
+ 'Quint.easeInOut': QuinticInOut,
65
+ 'Sine.easeInOut': SinusoidalInOut,
66
+ 'Expo.easeInOut': ExponentialInOut,
67
+ 'Circ.easeInOut': CircularInOut,
68
+ 'Elastic.easeInOut': ElasticInOut,
69
+ 'Back.easeInOut': BackInOut,
70
+ 'Bounce.easeInOut': BounceInOut,
71
+ };
72
+
73
+ this.game.onPause.add(this._pauseAll, this);
74
+ this.game.onResume.add(this._resumeAll, this);
75
+ }
76
+
77
+ getAll() {
78
+ return this._tweens;
79
+ }
80
+
81
+ removeAll() {
82
+ for (let i = 0; i < this._tweens.length; i += 1) {
83
+ this._tweens[i].pendingDelete = true;
84
+ }
85
+ this._add = [];
86
+ }
87
+
88
+ removeFrom(obj, children) {
89
+ let i;
90
+ let len;
91
+ if (Array.isArray(obj)) {
92
+ for (i = 0, len = obj.length; i < len; i += 1) {
93
+ this.removeFrom(obj[i]);
94
+ }
95
+ } else if (obj.type === GROUP && children) {
96
+ for (i = 0, len = obj.children.length; i < len; i += 1) {
97
+ this.removeFrom(obj.children[i]);
98
+ }
99
+ } else {
100
+ for (i = 0, len = this._tweens.length; i < len; i += 1) {
101
+ if (obj === this._tweens[i].target) {
102
+ this.remove(this._tweens[i]);
103
+ }
104
+ }
105
+ for (i = 0, len = this._add.length; i < len; i += 1) {
106
+ if (obj === this._add[i].target) {
107
+ this.remove(this._add[i]);
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ add(tween) {
114
+ tween._manager = this;
115
+ this._add.push(tween);
116
+ }
117
+
118
+ create(object) {
119
+ return new Tween(object, this.game, this);
120
+ }
121
+
122
+ remove(tween) {
123
+ let i = this._tweens.indexOf(tween);
124
+ if (i !== -1) {
125
+ this._tweens[i].pendingDelete = true;
126
+ } else {
127
+ i = this._add.indexOf(tween);
128
+ if (i !== -1) {
129
+ this._add[i].pendingDelete = true;
130
+ }
131
+ }
132
+ }
133
+
134
+ update() {
135
+ const addTweens = this._add.length;
136
+ let numTweens = this._tweens.length;
137
+ if (numTweens === 0 && addTweens === 0) {
138
+ return false;
139
+ }
140
+ let i = 0;
141
+ while (i < numTweens) {
142
+ if (this._tweens[i].update(this.game.time.time)) {
143
+ i += 1;
144
+ } else {
145
+ this._tweens.splice(i, 1);
146
+ numTweens -= 1;
147
+ }
148
+ }
149
+ // If there are any new tweens to be added, do so now - otherwise they can be spliced out of the array before ever running
150
+ if (addTweens > 0) {
151
+ this._tweens = this._tweens.concat(this._add);
152
+ this._add.length = 0;
153
+ }
154
+ return true;
155
+ }
156
+
157
+ isTweening(object) {
158
+ return this._tweens.some(tween => tween.target === object);
159
+ }
160
+
161
+ _pauseAll() {
162
+ for (let i = this._tweens.length - 1; i >= 0; i -= 1) {
163
+ this._tweens[i]._pause();
164
+ }
165
+ }
166
+
167
+ _resumeAll() {
168
+ for (let i = this._tweens.length - 1; i >= 0; i -= 1) {
169
+ this._tweens[i]._resume();
170
+ }
171
+ }
172
+
173
+ pauseAll() {
174
+ for (let i = this._tweens.length - 1; i >= 0; i -= 1) {
175
+ this._tweens[i].pause();
176
+ }
177
+ }
178
+
179
+ resumeAll() {
180
+ for (let i = this._tweens.length - 1; i >= 0; i -= 1) {
181
+ this._tweens[i].resume(true);
182
+ }
183
+ }
184
+
185
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import Group from '../display/group';
7
+
8
+ export default class extends Group {
9
+
10
+ constructor(game) {
11
+ super(game, null, '__world', false);
12
+ }
13
+
14
+ boot() {
15
+ this.game.stage.addChild(this);
16
+ }
17
+
18
+ }
@@ -0,0 +1,322 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import DisplayObject from './display_object';
7
+ import Image from './image';
8
+ import Point from '../geom/point';
9
+ import { BITMAP_TEXT } from '../core/const';
10
+
11
+ export default class extends DisplayObject {
12
+
13
+ constructor(game, x = 0, y = 0, font = '', text = '', size = 32, align = 'left') {
14
+ super();
15
+ this.game = game;
16
+ this.type = BITMAP_TEXT;
17
+ this.position.set(x, y);
18
+ this.textWidth = 0;
19
+ this.textHeight = 0;
20
+ this._prevAnchor = new Point();
21
+ this._glyphs = [];
22
+ this._maxWidth = 0;
23
+ this._text = text.toString() || '';
24
+ this._data = game.cache.getBitmapFont(font);
25
+ this._font = font;
26
+ this._fontSize = size;
27
+ this._align = align;
28
+ this._tint = 0xFFFFFF;
29
+ this.updateText();
30
+ this.dirty = false;
31
+ }
32
+
33
+ destroy() {
34
+ this._prevAnchor = null;
35
+ this._glyphs = null;
36
+ this._text = null;
37
+ this._data = null;
38
+ super.destroy();
39
+ }
40
+
41
+ preUpdate() {
42
+ if (this.pendingDestroy) {
43
+ this.destroy();
44
+ return;
45
+ }
46
+
47
+ if (!this.exists || !this.parent.exists) {
48
+ this.renderOrderID = -1;
49
+ return;
50
+ }
51
+ if (this.visible) {
52
+ this.game.stage.currentRenderOrderID += 1;
53
+ this.renderOrderID = this.game.stage.currentRenderOrderID;
54
+ }
55
+ for (let i = 0; i < this.children.length; i += 1) {
56
+ this.children[i].preUpdate();
57
+ }
58
+ }
59
+
60
+ setText(text) {
61
+ this.text = text;
62
+ }
63
+
64
+ scanLine(data, scale, text) {
65
+ let x = 0;
66
+ let w = 0;
67
+ let lastSpace = -1;
68
+ let wrappedWidth = 0;
69
+ let prevCharCode = null;
70
+ const maxWidth = (this._maxWidth > 0) ? this._maxWidth : null;
71
+ const chars = [];
72
+ // Let's scan the text and work out if any of the lines are > maxWidth
73
+ let end = true;
74
+ for (let i = 0; i < text.length; i += 1) {
75
+ end = i === text.length - 1;
76
+ if (/(?:\r\n|\r|\n)/.test(text.charAt(i))) {
77
+ return {
78
+ width: w,
79
+ text: text.substr(0, i),
80
+ end,
81
+ chars,
82
+ };
83
+ }
84
+ let charCode = text.charCodeAt(i);
85
+ let charData = data.chars[charCode];
86
+ let c = 0;
87
+ // If the character data isn't found in the data array
88
+ // then we replace it with a blank space
89
+ if (charData === undefined) {
90
+ charCode = 32;
91
+ charData = data.chars[charCode];
92
+ }
93
+ // Adjust for kerning from previous character to this one
94
+ const kerning = (prevCharCode && charData.kerning[prevCharCode]) ? charData.kerning[prevCharCode] : 0;
95
+ // Record the last space in the string and the current width
96
+ if (/(\s)/.test(text.charAt(i))) {
97
+ lastSpace = i;
98
+ wrappedWidth = w;
99
+ }
100
+ // What will the line width be if we add this character to it?
101
+ c = (kerning + charData.texture.width + charData.xOffset) * scale;
102
+ // Do we need to line-wrap?
103
+ if (maxWidth && ((w + c) >= maxWidth) && lastSpace > -1) {
104
+ // The last space was at "lastSpace" which was "i - lastSpace" characters ago
105
+ return {
106
+ width: wrappedWidth || w,
107
+ text: text.substr(0, i - (i - lastSpace)),
108
+ end,
109
+ chars,
110
+ };
111
+ }
112
+ w += (charData.xAdvance + kerning) * scale;
113
+ chars.push(x + (charData.xOffset + kerning) * scale);
114
+ x += (charData.xAdvance + kerning) * scale;
115
+ prevCharCode = charCode;
116
+ }
117
+ return {
118
+ width: w,
119
+ text,
120
+ end,
121
+ chars,
122
+ };
123
+ }
124
+
125
+ cleanText(text, replace = '') {
126
+ const data = this._data.font;
127
+ if (!data) {
128
+ return '';
129
+ }
130
+ const re = /\r\n|\n\r|\n|\r/g;
131
+ const lines = text.replace(re, '\n').split('\n');
132
+ for (let i = 0; i < lines.length; i += 1) {
133
+ let output = '';
134
+ const line = lines[i];
135
+ for (let c = 0; c < line.length; c += 1) {
136
+ if (data.chars[line.charCodeAt(c)]) {
137
+ output = output.concat(line[c]);
138
+ } else {
139
+ output = output.concat(replace);
140
+ }
141
+ }
142
+ lines[i] = output;
143
+ }
144
+ return lines.join('\n');
145
+ }
146
+
147
+ updateText() {
148
+ const data = this._data.font;
149
+ if (!data) {
150
+ return;
151
+ }
152
+ let text = this.text;
153
+ const scale = this._fontSize / data.size;
154
+ const lines = [];
155
+ let y = 0;
156
+ this.textWidth = 0;
157
+ let line = { end: text.length === 0 };
158
+ do {
159
+ line = this.scanLine(data, scale, text);
160
+ line.y = y;
161
+ lines.push(line);
162
+ if (line.width > this.textWidth) {
163
+ this.textWidth = line.width;
164
+ }
165
+ y += (data.lineHeight * scale);
166
+ text = text.substr(line.text.length + 1);
167
+ } while (line.end === false);
168
+ this.textHeight = y;
169
+ let t = 0;
170
+ let align = 0;
171
+ const ax = this.textWidth * this.anchor.x;
172
+ const ay = this.textHeight * this.anchor.y;
173
+ for (let i = 0; i < lines.length; i += 1) {
174
+ const currentLine = lines[i];
175
+ if (this._align === 'right') {
176
+ align = this.textWidth - currentLine.width;
177
+ } else if (this._align === 'center') {
178
+ align = (this.textWidth - currentLine.width) / 2;
179
+ }
180
+ for (let c = 0; c < currentLine.text.length; c += 1) {
181
+ let charCode = currentLine.text.charCodeAt(c);
182
+ let charData = data.chars[charCode];
183
+ if (charData === undefined) {
184
+ charCode = 32;
185
+ charData = data.chars[charCode];
186
+ }
187
+ let g = this._glyphs[t];
188
+ if (g) {
189
+ // Sprite already exists in the glyphs pool, so we'll reuse it for this letter
190
+ g.texture = charData.texture;
191
+ } else {
192
+ // We need a new sprite as the pool is empty or exhausted
193
+ g = new Image(this.game, 0, 0, charData.texture);
194
+ g.name = currentLine.text[c];
195
+ this._glyphs.push(g);
196
+ }
197
+ g.position.x = (currentLine.chars[c] + align) - ax;
198
+ g.position.y = (currentLine.y + (charData.yOffset * scale)) - ay;
199
+ g.scale.set(scale);
200
+ g.tint = this.tint;
201
+ g.texture.requiresReTint = true;
202
+ if (!g.parent) {
203
+ this.addChild(g);
204
+ }
205
+ t += 1;
206
+ }
207
+ }
208
+ // Remove unnecessary children
209
+ // This moves them from the display list (children array) but retains them in the _glyphs pool
210
+ for (let i = t; i < this._glyphs.length; i += 1) {
211
+ this.removeChild(this._glyphs[i]);
212
+ }
213
+ }
214
+
215
+ purgeGlyphs() {
216
+ const len = this._glyphs.length;
217
+ const kept = [];
218
+ for (let i = 0; i < this._glyphs.length; i += 1) {
219
+ if (this._glyphs[i].parent !== this) {
220
+ this._glyphs[i].destroy();
221
+ } else {
222
+ kept.push(this._glyphs[i]);
223
+ }
224
+ }
225
+ this._glyphs = [];
226
+ this._glyphs = kept;
227
+ this.updateText();
228
+ return len - kept.length;
229
+ }
230
+
231
+ updateTransform() {
232
+ if (this.dirty || !this.anchor.equals(this._prevAnchor)) {
233
+ this.updateText();
234
+ this.dirty = false;
235
+ this._prevAnchor.copyFrom(this.anchor);
236
+ }
237
+ super.updateTransform();
238
+ }
239
+
240
+ get align() {
241
+ return this._align;
242
+ }
243
+
244
+ set align(value) {
245
+ if (value !== this._align && (value === 'left' || value === 'center' || value === 'right')) {
246
+ this._align = value;
247
+ this.updateText();
248
+ }
249
+ }
250
+
251
+ get tint() {
252
+ return this._tint;
253
+ }
254
+
255
+ set tint(value) {
256
+ if (value !== this._tint) {
257
+ this._tint = value;
258
+ this.updateText();
259
+ }
260
+ }
261
+
262
+ get font() {
263
+ return this._font;
264
+ }
265
+
266
+ set font(value) {
267
+ const trimmedValue = value.trim();
268
+ if (trimmedValue !== this._font) {
269
+ this._font = trimmedValue;
270
+ this._data = this.game.cache.getBitmapFont(this._font);
271
+ this.updateText();
272
+ }
273
+ }
274
+
275
+ get fontSize() {
276
+ return this._fontSize;
277
+ }
278
+
279
+ set fontSize(value) {
280
+ value = parseInt(value, 10);
281
+ if (value !== this._fontSize && value > 0) {
282
+ this._fontSize = value;
283
+ this.updateText();
284
+ }
285
+ }
286
+
287
+ get text() {
288
+ return this._text;
289
+ }
290
+
291
+ set text(value) {
292
+ const typedValue = value.toString();
293
+ if (typedValue !== this._text) {
294
+ this._text = typedValue || '';
295
+ this.updateText();
296
+ }
297
+ }
298
+
299
+ get maxWidth() {
300
+ return this._maxWidth;
301
+ }
302
+
303
+ set maxWidth(value) {
304
+ if (value !== this._maxWidth) {
305
+ this._maxWidth = value;
306
+ this.updateText();
307
+ }
308
+ }
309
+
310
+ get smoothed() {
311
+ return !this._data.base.scaleMode;
312
+ }
313
+
314
+ set smoothed(value) {
315
+ if (value) {
316
+ this._data.base.scaleMode = 0;
317
+ } else {
318
+ this._data.base.scaleMode = 1;
319
+ }
320
+ }
321
+
322
+ }
@@ -0,0 +1,194 @@
1
+ /**
2
+ * @author Andras Csizmadia <andras@vpmedia.hu>
3
+ * @author Richard Davey <rich@photonstorm.com>
4
+ * @copyright Copyright (c) 2018-present Richard Davey, Photon Storm Ltd., Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)
5
+ */
6
+ import Image from './image';
7
+ import Signal from '../core/signal';
8
+ import InputHandler from '../core/input_handler';
9
+ import { BUTTON, POINTER_CONTACT } from '../core/const';
10
+
11
+ const STATE_OVER = 'Over';
12
+ const STATE_OUT = 'Out';
13
+ const STATE_DOWN = 'Down';
14
+ const STATE_UP = 'Up';
15
+ const STATE_DISABLED = 'Disabled';
16
+
17
+ export default class extends Image {
18
+
19
+ constructor(game, x = 0, y = 0, key = null, callback = null, callbackContext = null, overFrame = null, outFrame = null, downFrame = null, upFrame = null) {
20
+ super(game, x, y, key, outFrame);
21
+ this.type = BUTTON;
22
+ this._onOverFrame = null;
23
+ this._onOutFrame = null;
24
+ this._onDownFrame = null;
25
+ this._onUpFrame = null;
26
+ this._onDisabledFrame = null;
27
+ this.onInputOver = new Signal();
28
+ this.onInputOut = new Signal();
29
+ this.onInputDown = new Signal();
30
+ this.onInputUp = new Signal();
31
+ this.onOverMouseOnly = true;
32
+ this.justReleasedPreventsOver = POINTER_CONTACT;
33
+ this.freezeFrames = false;
34
+ this.forceOut = false;
35
+ this.input = new InputHandler(this);
36
+ this.input.start(0, true);
37
+ this.input.useHandCursor = true;
38
+ this.setFrames(overFrame, outFrame, downFrame, upFrame);
39
+ if (callback !== null) {
40
+ this.onInputUp.add(callback, callbackContext);
41
+ }
42
+ // Redirect the input events to here so we can handle animation updates, etc
43
+ this.events.onInputOver.add(this.onInputOverHandler, this);
44
+ this.events.onInputOut.add(this.onInputOutHandler, this);
45
+ this.events.onInputDown.add(this.onInputDownHandler, this);
46
+ this.events.onInputUp.add(this.onInputUpHandler, this);
47
+ }
48
+
49
+ destroy() {
50
+ this._onOverFrame = null;
51
+ this._onOutFrame = null;
52
+ this._onDownFrame = null;
53
+ this._onUpFrame = null;
54
+ this._onDisabledFrame = null;
55
+ if (this.onInputOver) {
56
+ this.onInputOver.dispose();
57
+ this.onInputOut.dispose();
58
+ this.onInputDown.dispose();
59
+ this.onInputUp.dispose();
60
+ }
61
+ this.onInputOver = null;
62
+ this.onInputOut = null;
63
+ this.onInputDown = null;
64
+ this.onInputUp = null;
65
+ if (this.input) {
66
+ this.input.destroy();
67
+ }
68
+ this.input = null;
69
+ super.destroy();
70
+ }
71
+
72
+ setEnabled(isEnabled, isImmediate) {
73
+ this.input.enabled = isEnabled;
74
+ if (isImmediate) {
75
+ this.changeStateFrame(isEnabled ? STATE_UP : STATE_DISABLED);
76
+ } else {
77
+ setTimeout(() => {
78
+ this.changeStateFrame(isEnabled ? STATE_UP : STATE_DISABLED);
79
+ }, 1);
80
+ }
81
+ }
82
+
83
+ clearFrames() {
84
+ this.setFrames(null, null, null, null);
85
+ }
86
+
87
+ removedFromWorld() {
88
+ this.inputEnabled = false;
89
+ }
90
+
91
+ setStateFrame(state, frame, switchImmediately = false) {
92
+ const frameKey = '_on' + state + 'Frame';
93
+ if (frame) {
94
+ this[frameKey] = frame;
95
+ if (switchImmediately) {
96
+ this.changeStateFrame(state);
97
+ }
98
+ } else {
99
+ this[frameKey] = null;
100
+ }
101
+ }
102
+
103
+ changeStateFrame(newState) {
104
+ if (this.freezeFrames) {
105
+ return false;
106
+ }
107
+ const state = this.input.enabled || !this._onDisabledFrame ? newState : STATE_DISABLED;
108
+ const frameKey = '_on' + state + 'Frame';
109
+ const frame = this[frameKey];
110
+ if (typeof frame === 'string') {
111
+ this.frameName = frame;
112
+ return true;
113
+ } else if (typeof frame === 'number') {
114
+ this.frame = frame;
115
+ return true;
116
+ }
117
+ return false;
118
+ }
119
+
120
+ setFrames(overFrame, outFrame, downFrame, upFrame) {
121
+ this.setStateFrame(STATE_OVER, overFrame, this.input.pointerOver());
122
+ this.setStateFrame(STATE_OUT, outFrame, !this.input.pointerOver());
123
+ this.setStateFrame(STATE_DOWN, downFrame, this.input.pointerDown());
124
+ this.setStateFrame(STATE_UP, upFrame, this.input.pointerUp());
125
+ }
126
+
127
+ onInputOverHandler(sprite, pointer) {
128
+ if (pointer.justReleased() && (this.justReleasedPreventsOver & pointer.pointerMode) === pointer.pointerMode) {
129
+ // If the Pointer was only just released then we don't fire an over event
130
+ return;
131
+ }
132
+ this.changeStateFrame(STATE_OVER);
133
+ if (this.onOverMouseOnly && !pointer.isMouse) {
134
+ return;
135
+ }
136
+ if (this.onInputOver) {
137
+ this.onInputOver.dispatch(this, pointer);
138
+ }
139
+ }
140
+
141
+ onInputOutHandler(sprite, pointer) {
142
+ this.changeStateFrame(STATE_OUT);
143
+ if (this.onInputOut) {
144
+ this.onInputOut.dispatch(this, pointer);
145
+ }
146
+ }
147
+
148
+ onInputDownHandler(sprite, pointer) {
149
+ this.changeStateFrame(STATE_DOWN);
150
+ if (this.onInputDown) {
151
+ this.onInputDown.dispatch(this, pointer);
152
+ }
153
+ }
154
+
155
+ onInputUpHandler(sprite, pointer, isOver) {
156
+ if (this.onInputUp) {
157
+ this.onInputUp.dispatch(this, pointer, isOver);
158
+ }
159
+ if (this.freezeFrames) {
160
+ return;
161
+ }
162
+ if (this.forceOut === true || (this.forceOut & pointer.pointerMode) === pointer.pointerMode) {
163
+ this.changeStateFrame(STATE_OUT);
164
+ } else {
165
+ const changedUp = this.changeStateFrame(STATE_UP);
166
+ if (!changedUp) {
167
+ // No Up frame to show..
168
+ if (isOver) {
169
+ this.changeStateFrame(STATE_OVER);
170
+ } else {
171
+ this.changeStateFrame(STATE_OUT);
172
+ }
173
+ }
174
+ }
175
+ }
176
+
177
+ get inputEnabled() {
178
+ return this.input && this.input.enabled;
179
+ }
180
+
181
+ set inputEnabled(value) {
182
+ if (value) {
183
+ if (this.input === null) {
184
+ this.input = new InputHandler(this);
185
+ this.input.start();
186
+ } else if (this.input && !this.input.enabled) {
187
+ this.input.start();
188
+ }
189
+ } else if (this.input && this.input.enabled) {
190
+ this.input.stop();
191
+ }
192
+ }
193
+
194
+ }