melonjs 10.1.0 → 10.2.2

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 (92) hide show
  1. package/README.md +6 -12
  2. package/dist/melonjs.js +3119 -2857
  3. package/dist/melonjs.min.js +3 -3
  4. package/dist/melonjs.module.d.ts +2590 -2490
  5. package/dist/melonjs.module.js +2698 -2470
  6. package/package.json +10 -11
  7. package/src/audio/audio.js +43 -43
  8. package/src/camera/camera2d.js +55 -77
  9. package/src/entity/draggable.js +18 -17
  10. package/src/entity/droptarget.js +19 -18
  11. package/src/entity/entity.js +22 -26
  12. package/src/game.js +3 -3
  13. package/src/index.js +15 -11
  14. package/src/input/gamepad.js +13 -13
  15. package/src/input/input.js +1 -1
  16. package/src/input/keyboard.js +14 -16
  17. package/src/input/pointer.js +42 -35
  18. package/src/input/pointerevent.js +25 -33
  19. package/src/lang/deprecated.js +3 -3
  20. package/src/level/level.js +24 -16
  21. package/src/level/tiled/TMXGroup.js +6 -6
  22. package/src/level/tiled/TMXLayer.js +31 -31
  23. package/src/level/tiled/TMXObject.js +19 -19
  24. package/src/level/tiled/TMXTile.js +11 -12
  25. package/src/level/tiled/TMXTileMap.js +23 -21
  26. package/src/level/tiled/TMXTileset.js +13 -13
  27. package/src/level/tiled/TMXTilesetGroup.js +4 -4
  28. package/src/level/tiled/TMXUtils.js +13 -11
  29. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
  30. package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
  31. package/src/level/tiled/renderer/TMXRenderer.js +17 -17
  32. package/src/loader/loader.js +31 -27
  33. package/src/loader/loadingscreen.js +40 -72
  34. package/src/math/color.js +45 -64
  35. package/src/math/math.js +17 -17
  36. package/src/math/matrix2.js +46 -46
  37. package/src/math/matrix3.js +64 -64
  38. package/src/math/observable_vector2.js +45 -57
  39. package/src/math/observable_vector3.js +56 -70
  40. package/src/math/vector2.js +60 -59
  41. package/src/math/vector3.js +65 -64
  42. package/src/particles/emitter.js +53 -55
  43. package/src/particles/particle.js +1 -1
  44. package/src/physics/body.js +45 -51
  45. package/src/physics/bounds.js +36 -36
  46. package/src/physics/collision.js +15 -16
  47. package/src/physics/detector.js +10 -11
  48. package/src/physics/quadtree.js +19 -17
  49. package/src/physics/sat.js +17 -17
  50. package/src/physics/world.js +13 -10
  51. package/src/plugin/plugin.js +6 -6
  52. package/src/renderable/GUI.js +13 -18
  53. package/src/renderable/collectable.js +3 -3
  54. package/src/renderable/colorlayer.js +4 -4
  55. package/src/renderable/container.js +65 -46
  56. package/src/renderable/imagelayer.js +32 -31
  57. package/src/renderable/nineslicesprite.js +211 -0
  58. package/src/renderable/renderable.js +69 -67
  59. package/src/renderable/sprite.js +57 -43
  60. package/src/renderable/trigger.js +14 -15
  61. package/src/shapes/ellipse.js +27 -26
  62. package/src/shapes/line.js +8 -7
  63. package/src/shapes/poly.js +33 -31
  64. package/src/shapes/rectangle.js +50 -96
  65. package/src/state/stage.js +8 -8
  66. package/src/state/state.js +56 -56
  67. package/src/system/device.js +97 -84
  68. package/src/system/event.js +72 -72
  69. package/src/system/pooling.js +14 -14
  70. package/src/system/save.js +6 -3
  71. package/src/system/timer.js +20 -20
  72. package/src/text/bitmaptext.js +27 -33
  73. package/src/text/bitmaptextdata.js +9 -9
  74. package/src/text/text.js +118 -59
  75. package/src/tweens/easing.js +4 -4
  76. package/src/tweens/interpolation.js +4 -4
  77. package/src/tweens/tween.js +37 -27
  78. package/src/utils/agent.js +9 -8
  79. package/src/utils/array.js +4 -4
  80. package/src/utils/file.js +4 -4
  81. package/src/utils/function.js +5 -5
  82. package/src/utils/string.js +12 -12
  83. package/src/utils/utils.js +19 -19
  84. package/src/video/canvas/canvas_renderer.js +90 -90
  85. package/src/video/renderer.js +40 -39
  86. package/src/video/texture.js +85 -76
  87. package/src/video/texture_cache.js +11 -0
  88. package/src/video/video.js +30 -30
  89. package/src/video/webgl/buffer/vertex.js +9 -1
  90. package/src/video/webgl/glshader.js +20 -20
  91. package/src/video/webgl/webgl_compositor.js +47 -37
  92. package/src/video/webgl/webgl_renderer.js +104 -104
@@ -17,15 +17,16 @@ var deferredRemove = function (child, keepalive) {
17
17
  var globalFloatingCounter = 0;
18
18
 
19
19
  /**
20
+ * @classdesc
20
21
  * me.Container represents a collection of child objects
21
22
  * @class Container
22
23
  * @extends me.Renderable
23
24
  * @memberOf me
24
25
  * @constructor
25
- * @param {Number} [x=0] position of the container (accessible via the inherited pos.x property)
26
- * @param {Number} [y=0] position of the container (accessible via the inherited pos.y property)
27
- * @param {Number} [w=me.game.viewport.width] width of the container
28
- * @param {Number} [h=me.game.viewport.height] height of the container
26
+ * @param {number} [x=0] position of the container (accessible via the inherited pos.x property)
27
+ * @param {number} [y=0] position of the container (accessible via the inherited pos.y property)
28
+ * @param {number} [w=me.game.viewport.width] width of the container
29
+ * @param {number} [h=me.game.viewport.height] height of the container
29
30
  */
30
31
 
31
32
  class Container extends Renderable {
@@ -47,7 +48,7 @@ class Container extends Renderable {
47
48
  /**
48
49
  * whether the container is the root of the scene
49
50
  * @public
50
- * @type Boolean
51
+ * @type {boolean}
51
52
  * @default false
52
53
  * @name root
53
54
  * @memberOf me.Container
@@ -64,7 +65,7 @@ class Container extends Renderable {
64
65
  * The property of the child object that should be used to sort on <br>
65
66
  * value : "x", "y", "z"
66
67
  * @public
67
- * @type String
68
+ * @type {string}
68
69
  * @default me.game.sortOn
69
70
  * @name sortOn
70
71
  * @memberOf me.Container
@@ -74,7 +75,7 @@ class Container extends Renderable {
74
75
  /**
75
76
  * Specify if the children list should be automatically sorted when adding a new child
76
77
  * @public
77
- * @type Boolean
78
+ * @type {boolean}
78
79
  * @default true
79
80
  * @name autoSort
80
81
  * @memberOf me.Container
@@ -84,7 +85,7 @@ class Container extends Renderable {
84
85
  /**
85
86
  * Specify if the children z index should automatically be managed by the parent container
86
87
  * @public
87
- * @type Boolean
88
+ * @type {boolean}
88
89
  * @default true
89
90
  * @name autoDepth
90
91
  * @memberOf me.Container
@@ -94,7 +95,7 @@ class Container extends Renderable {
94
95
  /**
95
96
  * Specify if the container draw operation should clip his children to its own bounds
96
97
  * @public
97
- * @type Boolean
98
+ * @type {boolean}
98
99
  * @default false
99
100
  * @name clipping
100
101
  * @memberOf me.Container
@@ -106,7 +107,7 @@ class Container extends Renderable {
106
107
  * @name onChildChange
107
108
  * @memberOf me.Container#
108
109
  * @function
109
- * @param {Number} index added or removed child index
110
+ * @param {number} index added or removed child index
110
111
  */
111
112
  this.onChildChange = function (/* index */) {
112
113
  // to be extended
@@ -117,7 +118,7 @@ class Container extends Renderable {
117
118
  * all child bounds when updated (this is expensive and disabled by default,
118
119
  * only enable if necessary)
119
120
  * @public
120
- * @type Boolean
121
+ * @type {boolean}
121
122
  * @default false
122
123
  * @name enableChildBoundsUpdate
123
124
  * @memberOf me.Container
@@ -186,7 +187,7 @@ class Container extends Renderable {
186
187
  * @function
187
188
  * @param {me.Renderable} child
188
189
  * @param {number} [z] forces the z index of the child to the specified value
189
- * @return {me.Renderable} the added child
190
+ * @returns {me.Renderable} the added child
190
191
  */
191
192
  addChild(child, z) {
192
193
  if (child.ancestor instanceof Container) {
@@ -249,8 +250,8 @@ class Container extends Renderable {
249
250
  * @memberOf me.Container.prototype
250
251
  * @function
251
252
  * @param {me.Renderable} child
252
- * @param {Number} index
253
- * @return {me.Renderable} the added child
253
+ * @param {number} index
254
+ * @returns {me.Renderable} the added child
254
255
  */
255
256
  addChildAt(child, index) {
256
257
  if (index >= 0 && index < this.getChildren().length) {
@@ -308,7 +309,7 @@ class Container extends Renderable {
308
309
  * @memberOf me.Container.prototype
309
310
  * @function
310
311
  * @param {Function} callback fnction to execute on each element
311
- * @param {Object} [thisArg] value to use as this(i.e reference Object) when executing callback.
312
+ * @param {object} [thisArg] value to use as this(i.e reference Object) when executing callback.
312
313
  * @example
313
314
  * // iterate through all children of the root container
314
315
  * me.game.world.forEach((child) => {
@@ -370,7 +371,8 @@ class Container extends Renderable {
370
371
  * @name getChildAt
371
372
  * @memberOf me.Container.prototype
372
373
  * @function
373
- * @param {Number} index
374
+ * @param {number} index
375
+ * @returns {me.Renderable} the child at the specified index
374
376
  */
375
377
  getChildAt(index) {
376
378
  if (index >= 0 && index < this.getChildren().length) {
@@ -387,6 +389,7 @@ class Container extends Renderable {
387
389
  * @memberOf me.Container.prototype
388
390
  * @function
389
391
  * @param {me.Renderable} child
392
+ * @returns {number} index
390
393
  */
391
394
  getChildIndex(child) {
392
395
  return this.getChildren().indexOf(child);
@@ -398,6 +401,7 @@ class Container extends Renderable {
398
401
  * @memberOf me.Container
399
402
  * @function
400
403
  * @param {me.Renderable} child
404
+ * @returns {me.Renderable} child
401
405
  */
402
406
  getNextChild(child) {
403
407
  var index = this.getChildren().indexOf(child) - 1;
@@ -413,7 +417,7 @@ class Container extends Renderable {
413
417
  * @memberOf me.Container.prototype
414
418
  * @function
415
419
  * @param {me.Renderable} child
416
- * @return {Boolean}
420
+ * @returns {boolean}
417
421
  */
418
422
  hasChild(child) {
419
423
  return this === child.ancestor;
@@ -427,9 +431,9 @@ class Container extends Renderable {
427
431
  * @memberOf me.Container.prototype
428
432
  * @public
429
433
  * @function
430
- * @param {String} prop Property name
431
- * @param {String|RegExp|Number|Boolean} value Value of the property
432
- * @return {me.Renderable[]} Array of childs
434
+ * @param {string} prop Property name
435
+ * @param {string|RegExp|number|boolean} value Value of the property
436
+ * @returns {me.Renderable[]} Array of childs
433
437
  * @example
434
438
  * // get the first child object called "mainPlayer" in a specific container :
435
439
  * var ent = myContainer.getChildByProp("name", "mainPlayer");
@@ -448,6 +452,9 @@ class Container extends Renderable {
448
452
  getChildByProp(prop, value) {
449
453
  var objList = [];
450
454
 
455
+ /**
456
+ * @ignore
457
+ */
451
458
  function compare(obj, prop) {
452
459
  var v = obj[prop];
453
460
  if (value instanceof RegExp && typeof(v) === "string") {
@@ -476,18 +483,18 @@ class Container extends Renderable {
476
483
  * @memberOf me.Container.prototype
477
484
  * @public
478
485
  * @function
479
- * @param {Object} class type
480
- * @return {me.Renderable[]} Array of children
486
+ * @param {object} classType
487
+ * @returns {me.Renderable[]} Array of children
481
488
  */
482
- getChildByType(_class) {
489
+ getChildByType(classType) {
483
490
  var objList = [];
484
491
 
485
492
  this.forEach((child) => {
486
- if (child instanceof _class) {
493
+ if (child instanceof classType) {
487
494
  objList.push(child);
488
495
  }
489
496
  if (child instanceof Container) {
490
- objList = objList.concat(child.getChildByType(_class));
497
+ objList = objList.concat(child.getChildByType(classType));
491
498
  }
492
499
  });
493
500
 
@@ -503,8 +510,8 @@ class Container extends Renderable {
503
510
  * @memberOf me.Container.prototype
504
511
  * @public
505
512
  * @function
506
- * @param {String|RegExp|Number|Boolean} name child name
507
- * @return {me.Renderable[]} Array of children
513
+ * @param {string|RegExp|number|boolean} name child name
514
+ * @returns {me.Renderable[]} Array of children
508
515
  */
509
516
  getChildByName(name) {
510
517
  return this.getChildByProp("name", name);
@@ -518,23 +525,21 @@ class Container extends Renderable {
518
525
  * @memberOf me.Container.prototype
519
526
  * @public
520
527
  * @function
521
- * @param {String|RegExp|Number|Boolean} GUID child GUID
522
- * @return {me.Renderable} corresponding child or null
528
+ * @param {string|RegExp|number|boolean} guid child GUID
529
+ * @returns {me.Renderable} corresponding child or null
523
530
  */
524
531
  getChildByGUID(guid) {
525
532
  var obj = this.getChildByProp("GUID", guid);
526
533
  return (obj.length > 0) ? obj[0] : null;
527
534
  }
528
535
 
529
-
530
536
  /**
531
537
  * return all child in this container
532
-
533
538
  * @name getChildren
534
539
  * @memberOf me.Container.prototype
535
540
  * @public
536
541
  * @function
537
- * @return {me.Renderable[]} an array of renderable object
542
+ * @returns {me.Renderable[]} an array of renderable object
538
543
  */
539
544
  getChildren() {
540
545
  if (typeof this.children === "undefined") {
@@ -549,7 +554,7 @@ class Container extends Renderable {
549
554
  * @name updateBounds
550
555
  * @memberOf me.Renderable.prototype
551
556
  * @function
552
- * @return {me.Bounds} this shape bounding box Rectangle object
557
+ * @returns {me.Bounds} this shape bounding box Rectangle object
553
558
  */
554
559
  updateBounds(forceUpdateChildBounds = false) {
555
560
 
@@ -578,7 +583,7 @@ class Container extends Renderable {
578
583
  * @name isAttachedToRoot
579
584
  * @memberOf me.Container.prototype
580
585
  * @function
581
- * @returns Boolean
586
+ * @returns {boolean}
582
587
  */
583
588
  isAttachedToRoot() {
584
589
  if (this.root === true) {
@@ -597,7 +602,7 @@ class Container extends Renderable {
597
602
 
598
603
  /**
599
604
  * update the cointainer's bounding rect (private)
600
- * @private
605
+ * @ignore
601
606
  * @name updateBoundsPos
602
607
  * @memberOf me.Container.prototype
603
608
  * @function
@@ -638,7 +643,7 @@ class Container extends Renderable {
638
643
  * @public
639
644
  * @function
640
645
  * @param {me.Renderable} child
641
- * @param {Boolean} [keepalive=False] True to prevent calling child.destroy()
646
+ * @param {boolean} [keepalive=False] True to prevent calling child.destroy()
642
647
  */
643
648
  removeChild(child, keepalive) {
644
649
  if (this.hasChild(child)) {
@@ -657,7 +662,7 @@ class Container extends Renderable {
657
662
  * @memberOf me.Container.prototype
658
663
  * @function
659
664
  * @param {me.Renderable} child
660
- * @param {Boolean} [keepalive=False] True to prevent calling child.destroy()
665
+ * @param {boolean} [keepalive=False] True to prevent calling child.destroy()
661
666
  */
662
667
  removeChildNow(child, keepalive) {
663
668
  if (this.hasChild(child) && (this.getChildIndex(child) >= 0)) {
@@ -709,16 +714,16 @@ class Container extends Renderable {
709
714
  * @name setChildsProperty
710
715
  * @memberOf me.Container.prototype
711
716
  * @function
712
- * @param {String} property property name
713
- * @param {Object} value property value
714
- * @param {Boolean} [recursive=false] recursively apply the value to child containers if true
717
+ * @param {string} prop property name
718
+ * @param {object} value property value
719
+ * @param {boolean} [recursive=false] recursively apply the value to child containers if true
715
720
  */
716
- setChildsProperty(prop, val, recursive) {
721
+ setChildsProperty(prop, value, recursive) {
717
722
  this.forEach((child) => {
718
723
  if ((recursive === true) && (child instanceof Container)) {
719
- child.setChildsProperty(prop, val, recursive);
724
+ child.setChildsProperty(prop, value, recursive);
720
725
  }
721
- child[prop] = val;
726
+ child[prop] = value;
722
727
  });
723
728
  }
724
729
 
@@ -794,7 +799,7 @@ class Container extends Renderable {
794
799
  * @memberOf me.Container.prototype
795
800
  * @public
796
801
  * @function
797
- * @param {Boolean} [recursive=false] recursively sort all containers if true
802
+ * @param {boolean} [recursive=false] recursively sort all containers if true
798
803
  */
799
804
  sort(recursive) {
800
805
  // do nothing if there is already a pending sort
@@ -883,7 +888,14 @@ class Container extends Renderable {
883
888
  }
884
889
 
885
890
  /**
886
- * @ignore
891
+ * container update function. <br>
892
+ * automatically called by the game manager {@link me.game}
893
+ * @name update
894
+ * @memberOf me.Container.prototype
895
+ * @function
896
+ * @protected
897
+ * @param {number} dt time since the last update in milliseconds.
898
+ * @returns {boolean} true if the Container is dirty
887
899
  */
888
900
  update(dt) {
889
901
  var isFloating = false;
@@ -929,7 +941,14 @@ class Container extends Renderable {
929
941
  }
930
942
 
931
943
  /**
932
- * @ignore
944
+ * draw the container. <br>
945
+ * automatically called by the game manager {@link me.game}
946
+ * @name draw
947
+ * @memberOf me.Container.prototype
948
+ * @function
949
+ * @protected
950
+ * @param {me.CanvasRenderer|me.WebGLRenderer} renderer a renderer object
951
+ * @param {me.Rect|me.Bounds} [rect] the area or viewport to (re)draw
933
952
  */
934
953
  draw(renderer, rect) {
935
954
  var isFloating = false;
@@ -13,16 +13,16 @@ import * as stringUtil from "./../utils/string.js";
13
13
  * @extends me.Renderable
14
14
  * @memberOf me
15
15
  * @constructor
16
- * @param {Number} x x coordinate
17
- * @param {Number} y y coordinate
18
- * @param {Object} settings ImageLayer properties
19
- * @param {HTMLImageElement|HTMLCanvasElement|String} settings.image Image reference. See {@link me.loader.getImage}
20
- * @param {String} [settings.name="me.ImageLayer"] layer name
21
- * @param {Number} [settings.z=0] z-index position
22
- * @param {Number|me.Vector2d} [settings.ratio=1.0] Scrolling ratio to be applied. See {@link me.ImageLayer#ratio}
23
- * @param {String} [settings.repeat='repeat'] define if and how an Image Layer should be repeated (accepted values are 'repeat',
16
+ * @param {number} x x coordinate
17
+ * @param {number} y y coordinate
18
+ * @param {object} settings ImageLayer properties
19
+ * @param {HTMLImageElement|HTMLCanvasElement|string} settings.image Image reference. See {@link me.loader.getImage}
20
+ * @param {string} [settings.name="me.ImageLayer"] layer name
21
+ * @param {number} [settings.z=0] z-index position
22
+ * @param {number|me.Vector2d} [settings.ratio=1.0] Scrolling ratio to be applied. See {@link me.ImageLayer#ratio}
23
+ * @param {string} [settings.repeat='repeat'] define if and how an Image Layer should be repeated (accepted values are 'repeat',
24
24
  'repeat-x', 'repeat-y', 'no-repeat'). See {@link me.ImageLayer#repeat}
25
- * @param {Number|me.Vector2d} [settings.anchorPoint=0.0] Image origin. See {@link me.ImageLayer#anchorPoint}
25
+ * @param {number|me.Vector2d} [settings.anchorPoint=0.0] Image origin. See {@link me.ImageLayer#anchorPoint}
26
26
  * @example
27
27
  * // create a repetitive background pattern on the X axis using the citycloud image asset
28
28
  * me.game.world.addChild(new me.ImageLayer(0, 0, {
@@ -54,7 +54,7 @@ class ImageLayer extends Sprite {
54
54
  * - a number, to change the value for both axis <br>
55
55
  * - a json expression like `json:{"x":0.5,"y":0.5}` if you wish to specify a different value for both x and y
56
56
  * @public
57
- * @type me.Vector2d
57
+ * @type {me.Vector2d}
58
58
  * @default <1.0,1.0>
59
59
  * @name me.ImageLayer#ratio
60
60
  */
@@ -75,14 +75,14 @@ class ImageLayer extends Sprite {
75
75
  * By default, its upper-left corner is anchored to the viewport bounds upper left corner.<br>
76
76
  * The anchorPoint is a unit vector where each component falls in range [0.0,1.0].<br>
77
77
  * Some common examples:<br>
78
- * * &lt;0.0,0.0&gt; : (Default) Anchor image to the upper-left corner of viewport bounds
79
- * * &lt;0.5,0.5&gt; : Center the image within viewport bounds
80
- * * &lt;1.0,1.0&gt; : Anchor image to the lower-right corner of viewport bounds
78
+ * - &lt;0.0,0.0&gt; : (Default) Anchor image to the upper-left corner of viewport bounds
79
+ * - &lt;0.5,0.5&gt; : Center the image within viewport bounds
80
+ * - &lt;1.0,1.0&gt; : Anchor image to the lower-right corner of viewport bounds
81
81
  * To specify a value through Tiled, use one of the following format : <br>
82
82
  * - a number, to change the value for both axis <br>
83
83
  * - a json expression like `json:{"x":0.5,"y":0.5}` if you wish to specify a different value for both x and y
84
84
  * @public
85
- * @type me.Vector2d
85
+ * @type {me.Vector2d}
86
86
  * @default <0.0,0.0>
87
87
  * @name me.ImageLayer#anchorPoint
88
88
  */
@@ -107,26 +107,20 @@ class ImageLayer extends Sprite {
107
107
  * Define if and how an Image Layer should be repeated.<br>
108
108
  * By default, an Image Layer is repeated both vertically and horizontally.<br>
109
109
  * Acceptable values : <br>
110
- * * 'repeat' - The background image will be repeated both vertically and horizontally <br>
111
- * * 'repeat-x' - The background image will be repeated only horizontally.<br>
112
- * * 'repeat-y' - The background image will be repeated only vertically.<br>
113
- * * 'no-repeat' - The background-image will not be repeated.<br>
110
+ * - 'repeat' - The background image will be repeated both vertically and horizontally <br>
111
+ * - 'repeat-x' - The background image will be repeated only horizontally.<br>
112
+ * - 'repeat-y' - The background image will be repeated only vertically.<br>
113
+ * - 'no-repeat' - The background-image will not be repeated.<br>
114
114
  * @public
115
- * @type String
115
+ * @type {string}
116
116
  * @default 'repeat'
117
117
  * @name me.ImageLayer#repeat
118
118
  */
119
119
 
120
- /**
121
- * @ignore
122
- */
123
120
  get repeat() {
124
121
  return this._repeat;
125
122
  }
126
123
 
127
- /**
128
- * @ignore
129
- */
130
124
  set repeat(value) {
131
125
  this._repeat = value;
132
126
  switch (this._repeat) {
@@ -174,9 +168,9 @@ class ImageLayer extends Sprite {
174
168
  * @name resize
175
169
  * @memberOf me.ImageLayer.prototype
176
170
  * @function
177
- * @param {Number} w new width
178
- * @param {Number} h new height
179
- */
171
+ * @param {number} w new width
172
+ * @param {number} h new height
173
+ */
180
174
  resize(w, h) {
181
175
  super.resize(
182
176
  this.repeatX ? Infinity : w,
@@ -239,9 +233,11 @@ class ImageLayer extends Sprite {
239
233
  else {
240
234
  this.pos.y = y;
241
235
  }
236
+
237
+ this.isDirty = true;
242
238
  }
243
239
 
244
- /*
240
+ /**
245
241
  * override the default predraw function
246
242
  * as repeat and anchor are managed directly in the draw method
247
243
  * @ignore
@@ -257,8 +253,13 @@ class ImageLayer extends Sprite {
257
253
  }
258
254
 
259
255
  /**
260
- * draw the image layer
261
- * @ignore
256
+ * draw the ImageLayer. <br>
257
+ * automatically called by the game manager {@link me.game}
258
+ * @name draw
259
+ * @memberOf me.ImageLayer.prototype
260
+ * @function
261
+ * @protected
262
+ * @param {me.CanvasRenderer|me.WebGLRenderer} renderer a renderer object
262
263
  */
263
264
  draw(renderer) {
264
265
  var width = this.width,
@@ -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;