/* * GridSizer * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; /** * GridSizer is a layout manager that organizes its components in a regular * grid. * * Parameters rows and cols specify how many rows and * columns the grid will have, respectively. All cells in the grid have the * same size. However, each cell can have an individual border, by setting the * border parameter when adding an object to the sizer. * The anchor parameter can be specified using the following constants: * LEFT, RIGHT, TOP, BOTTOM, TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, * BOTTOM_RIGHT, CENTER. * *

Example

* * A GridSizer which with 3 rows and 5 columns, and occupying all space of a * canvas can be created as: * * var area = new createjs.Rectangle(0, 0, canvas.width, canvas.height); * var hbox = new creatine.GridSizer(3, 5, area); * * @class GridSizer * @constructor * @param {Number} rows The number of rows in the grid. * @param {Number} cols The number of columns in the grid. * @param {createjs.Rectangle} area A rectangle containing the usable area of * the sizer. **/ var GridSizer = function(rows, cols, area) { this.initialize(rows, cols, area); } var p = GridSizer.prototype; /** * The number of rows in the grid. * * @property rows * @type {Number} **/ p.rows = null; /** * The number of columns in the grid. * * @property cols * @type {Number} **/ p.cols = null; /** * The list within all items in this sizer together with their proportions, * border, and anchor. * * @property children * @type {Array} * @private **/ p.children = null; /** * A rectangle representing the area of which the sizer can use. * * @property area * @type {createjs.Rectangle} **/ p.area = null; /** * Initialization method. * * @method initialize * @param {Number} rows The number of rows in the grid. * @param {Number} cols The number of columns in the grid. * @param {createjs.Rectangle} area A rectangle containing the usable area * of the sizer. * @protected **/ p.initialize = function(rows, cols, area) { this.rows = rows; this.cols = cols; this.children = []; this.area = area; } /** * Adds a new object to the sizer. * * @method add * @param {Object} object The DisplayObject to be organized by this sizer. * @param {Number} border The spacing around this item. Default to 0. * @param {Constant} anchor The anchor of the object. Default to * creatine.TOP_LEFT **/ p.add = function(object, border, anchor) { anchor = anchor || creatine.TOP_LEFT; border = border || 0; this.children.push([object, anchor, border]); } /** * Resize this sizer and its children. * * For the root sizer, the parameter area does not to be * specified, but it is automatically set in nested sizers. * * @method layout * @param {createjs.Rectangle} area A rectangle containing the usable area * of the sizer. **/ p.layout = function(area) { if (area) this.area = area; var base_w = this.area.width/this.cols; var base_h = this.area.height/this.rows; for (var i=0; icreatine.TOP_LEFT * @private **/ p._resize_child = function(item, area, anchor) { if (item.layout) item.layout(area); if (item.getBounds) var bounds = item.getBounds(); if (bounds) { var w = bounds.width; var h = bounds.height; } else { var w = 0; var h = 0; } // Set y switch (anchor) { case creatine.TOP: case creatine.TOP_LEFT: case creatine.TOP_RIGHT: item.y = area.y; break; case creatine.LEFT: case creatine.CENTER: case creatine.RIGHT: item.y = (area.y+area.height/2)-h/2; break; case creatine.BOTTOM: case creatine.BOTTOM_LEFT: case creatine.BOTTOM_RIGHT: item.y = area.y+area.height-h; break; } // Set x switch (anchor) { case creatine.LEFT: case creatine.TOP_LEFT: case creatine.BOTTOM_LEFT: item.x = area.x; break; case creatine.TOP: case creatine.CENTER: case creatine.BOTTOM: item.x = (area.x+area.width/2)-w/2; break; case creatine.RIGHT: case creatine.TOP_RIGHT: case creatine.BOTTOM_RIGHT: item.x = area.x+area.width-w; break; } } creatine.GridSizer = GridSizer; }());