|
| 1 | +/** |
| 2 | +* @author Richard Davey <rich@photonstorm.com> |
| 3 | +* @copyright 2014 Photon Storm Ltd. |
| 4 | +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
| 5 | +*/ |
| 6 | + |
| 7 | +/** |
| 8 | +* A responsive grid manager. |
| 9 | +* |
| 10 | +* @class Phaser.FlexGrid |
| 11 | +* @constructor |
| 12 | +* @param {Phaser.ScaleManager} manager - The ScaleManager. |
| 13 | +*/ |
| 14 | +Phaser.FlexGrid = function (manager, width, height) { |
| 15 | + |
| 16 | + /** |
| 17 | + * @property {Phaser.Game} game - A reference to the currently running Game. |
| 18 | + */ |
| 19 | + this.game = manager.game; |
| 20 | + |
| 21 | + /** |
| 22 | + * @property {Phaser.ScaleManager} scale - A reference to the ScaleManager. |
| 23 | + */ |
| 24 | + this.manager = manager; |
| 25 | + |
| 26 | + // The perfect dimensions on which everything else is based |
| 27 | + this.width = width; |
| 28 | + this.height = height; |
| 29 | + |
| 30 | + this.bounds = new Phaser.Rectangle(0, 0, width, height); |
| 31 | + |
| 32 | + /** |
| 33 | + * @property {Phaser.Point} position - |
| 34 | + * @readonly |
| 35 | + */ |
| 36 | + this.position = new Phaser.Point(0, 0); |
| 37 | + |
| 38 | + /** |
| 39 | + * @property {Phaser.Point} scaleFactor - The scale factor based on the game dimensions vs. the scaled dimensions. |
| 40 | + * @readonly |
| 41 | + */ |
| 42 | + this.scaleFactor = new Phaser.Point(1, 1); |
| 43 | + |
| 44 | + /** |
| 45 | + * @property {Phaser.Point} scaleFactorInversed - The inversed scale factor. The displayed dimensions divided by the game dimensions. |
| 46 | + * @readonly |
| 47 | + */ |
| 48 | + this.scaleFactorInversed = new Phaser.Point(1, 1); |
| 49 | + |
| 50 | + this.ratioH = width / height; |
| 51 | + this.ratioV = height / width; |
| 52 | + |
| 53 | + this.multiplier = 0; |
| 54 | + |
| 55 | + this.fitHorizontally = false; |
| 56 | + this.fitVertically = false; |
| 57 | + |
| 58 | + this.layers = []; |
| 59 | + |
| 60 | +}; |
| 61 | + |
| 62 | +Phaser.FlexGrid.prototype = { |
| 63 | + |
| 64 | + setSize: function (width, height) { |
| 65 | + |
| 66 | + // These are locked and don't change until setSize is called again |
| 67 | + this.width = width; |
| 68 | + this.height = height; |
| 69 | + |
| 70 | + this.ratioH = width / height; |
| 71 | + this.ratioV = height / width; |
| 72 | + |
| 73 | + this.refresh(); |
| 74 | + |
| 75 | + }, |
| 76 | + |
| 77 | + createLayer: function () { |
| 78 | + |
| 79 | + var layer = this.game.add.group(); |
| 80 | + |
| 81 | + // Override the position and scale so they point to our objects instead, this will keep them matched |
| 82 | + layer.position = this.position; |
| 83 | + layer.scale = this.scaleFactor; |
| 84 | + |
| 85 | + this.layers.push(layer); |
| 86 | + |
| 87 | + return layer; |
| 88 | + |
| 89 | + }, |
| 90 | + |
| 91 | + onResize: function (width, height) { |
| 92 | + |
| 93 | + this.refresh(); |
| 94 | + |
| 95 | + }, |
| 96 | + |
| 97 | + refresh: function () { |
| 98 | + |
| 99 | + // Now let's scale it |
| 100 | + |
| 101 | + this.multiplier = Math.min((this.manager.height / this.height), (this.manager.width / this.width)); |
| 102 | + |
| 103 | + this.bounds.width = Math.round(this.width * this.multiplier); |
| 104 | + this.bounds.height = Math.round(this.height * this.multiplier); |
| 105 | + |
| 106 | + // Max checks? |
| 107 | + |
| 108 | + this.scaleFactor.set(this.bounds.width / this.width, this.bounds.height / this.height); |
| 109 | + this.scaleFactorInversed.set(this.width / this.bounds.width, this.height / this.bounds.height); |
| 110 | + |
| 111 | + this.bounds.centerOn(this.manager.bounds.centerX, this.manager.bounds.centerY); |
| 112 | + |
| 113 | + this.position.set(this.bounds.x, this.bounds.y); |
| 114 | + |
| 115 | + }, |
| 116 | + |
| 117 | + debug: function () { |
| 118 | + |
| 119 | + this.game.debug.text("h: " + this.ratioH + " v: " + this.ratioV, 32, 32); |
| 120 | + this.game.debug.text(this.scaleFactor, 32, 64); |
| 121 | + // this.game.debug.text(this.scaleFactorInversed, 32, 64+32); |
| 122 | + // this.game.debug.geom(this.bounds); |
| 123 | + |
| 124 | + }, |
| 125 | + |
| 126 | +}; |
| 127 | + |
| 128 | +Phaser.FlexGrid.prototype.constructor = Phaser.FlexGrid; |
0 commit comments