From 22b27ffb97e99fb9856fc00094ad9ff87c6d56b1 Mon Sep 17 00:00:00 2001 From: jvalkeal Date: Fri, 30 Dec 2011 18:23:14 +0000 Subject: [PATCH] Adding bin packing algorithm to organise windows. --- .../hyperic/layout/RectanglePacker.js | 208 ++++++++++++++ .../hyperic/tests/test_rectanglepacker1.html | 271 ++++++++++++++++++ .../hyperic/tests/test_rectanglepacker2.html | 271 ++++++++++++++++++ 3 files changed, 750 insertions(+) create mode 100644 dojo-release-1.5.0-src/hyperic/layout/RectanglePacker.js create mode 100644 dojo-release-1.5.0-src/hyperic/tests/test_rectanglepacker1.html create mode 100644 dojo-release-1.5.0-src/hyperic/tests/test_rectanglepacker2.html diff --git a/dojo-release-1.5.0-src/hyperic/layout/RectanglePacker.js b/dojo-release-1.5.0-src/hyperic/layout/RectanglePacker.js new file mode 100644 index 0000000..c695b68 --- /dev/null +++ b/dojo-release-1.5.0-src/hyperic/layout/RectanglePacker.js @@ -0,0 +1,208 @@ +/** + * NOTE: This copyright does *not* cover user programs that use HQ + * program services by normal system calls through the application + * program interfaces provided as part of the Hyperic Plug-in Development + * Kit or the Hyperic Client Development Kit - this is merely considered + * normal use of the program, and does *not* fall under the heading of + * "derived work". + * + * Copyright (C) [2011], VMware, Inc. + * This file is part of HQ. + * + * HQ is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + */ +dojo.provide("hyperic.layout.RectanglePacker"); + +dojo.declare("hyperic.layout.RectanglePacker", + null, { + // summary: + // Class to implement bin packing algorithm to organise + // and scale window positions automatically. + // + // credits: + // + // + + constructor: function(width, height){ + this.root = {}; + this.reset( width, height ); + }, + + reset: function(width, height){ + this.root.x = 0; + this.root.y = 0; + this.root.w = width; + this.root.h = height; + delete this.root.left; + delete this.root.right; + + this.reservedWidth = 0; + this.reservedHeight = 0; + }, + + rawpack: function(/*Array*/blocks, sortfunc, xField, yField, wField, hField){ + // summary: + // Tries to pack given blocks to fit into existing dimensions. + // + // description: + // Packs given blocks to reserved canvas size. This method doesn't + // unsure successful packing. Fitted and non-fitted objects + // are returned separately in different arrays. + + var _x = xField || 'x'; + var _y = yField || 'y'; + var _w = wField || 'w'; + var _h = hField || 'h'; + + var clone = dojo.clone(blocks); + //this.reset(); + var fit = []; + var nofit = []; + for (var i=0; i node.w || h > node.h) + return null; + + if (w == node.w && h == node.h) { + node.used = true; + return {x: node.x, y: node.y}; + } + + node.left = this._cloneNode(node); + node.right = this._cloneNode(node); + + if (node.w - w > node.h - h) { + node.left.w = w; + node.right.x = node.x + w; + node.right.w = node.w - w; + } else { + node.left.h = h; + node.right.y = node.y + h; + node.right.h = node.h - h; + } + + return this._recursive(node.left, w, h); + } + + }, + + _cloneNode: function(node) { + return { + x: node.x, + y: node.y, + w: node.w, + h: node.h}; + } + + +}); \ No newline at end of file diff --git a/dojo-release-1.5.0-src/hyperic/tests/test_rectanglepacker1.html b/dojo-release-1.5.0-src/hyperic/tests/test_rectanglepacker1.html new file mode 100644 index 0000000..0c70c09 --- /dev/null +++ b/dojo-release-1.5.0-src/hyperic/tests/test_rectanglepacker1.html @@ -0,0 +1,271 @@ + + + Rectangle Packing Test 1 - rawpack + + + + + + + + + + + +

Rectangle Packing Test 1 - rawpack

+
+
+ Refresh + Calculated size: 000x000 + | % Filled: 00% + | % Blocks Fitted: 00% +
+ +
+
+ +
+ + +
+ + + + + + + + + + + + + + + + + + +
+ + + + +

Nofit blocks

+
    +
+ +
+ + + diff --git a/dojo-release-1.5.0-src/hyperic/tests/test_rectanglepacker2.html b/dojo-release-1.5.0-src/hyperic/tests/test_rectanglepacker2.html new file mode 100644 index 0000000..6cd3fe2 --- /dev/null +++ b/dojo-release-1.5.0-src/hyperic/tests/test_rectanglepacker2.html @@ -0,0 +1,271 @@ + + + Rectangle Packing Test 2 - scalepack + + + + + + + + + + + +

Rectangle Packing Test 2 - scalepack

+
+
+ Refresh + Calculated size: 000x000 + | % Filled: 00% + | % Blocks Fitted: 00% +
+ +
+
+ +
+ + +
+ + + + + + + + + + + + + + + + + + +
+ + + + +

Nofit blocks

+
    +
+ +
+ + +