| 1 |
/* |
| 2 |
* Copyright (C) 2012 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License version 3 as |
| 6 |
* published by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Authors: |
| 17 |
* Jim Nelson <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
function findChild(parent, cond) { |
| 21 |
if (parent.children) { |
| 22 |
for (var ctr = 0; ctr < parent.children.length; ctr++) { |
| 23 |
var child = parent.children[ctr]; |
| 24 |
|
| 25 |
if (cond(child)) |
| 26 |
return child; |
| 27 |
|
| 28 |
// recurse |
| 29 |
var found = findChild(child, cond); |
| 30 |
if (found) |
| 31 |
return found; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// If the item has a contentItem, search its children, otherwise done. |
| 36 |
return (parent.contentItem) ? findChild(parent.contentItem, cond) : undefined; |
| 37 |
} |
| 38 |
|
| 39 |
function gatherChildren(parent, accum, cond) { |
| 40 |
if (parent.children) { |
| 41 |
for (var ctr = 0; ctr < parent.children.length; ctr++) { |
| 42 |
var child = parent.children[ctr]; |
| 43 |
|
| 44 |
if (cond(child)) |
| 45 |
accum[accum.length] = child; |
| 46 |
|
| 47 |
gatherChildren(child, accum, cond); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if (parent.contentItem) |
| 52 |
gatherChildren(parent.contentItem, accum, cond); |
| 53 |
} |
| 54 |
|
| 55 |
// Translates a rectangle's coordinates from one item's coordinate space to |
| 56 |
// another's. |
| 57 |
function translateRect(rect, fromItem, toItem) { |
| 58 |
var newRect = fromItem.mapToItem(toItem, rect.x, rect.y, rect.width, rect.height); |
| 59 |
newRect.width = rect.width; |
| 60 |
newRect.height = rect.height; |
| 61 |
|
| 62 |
return newRect; |
| 63 |
} |
| 64 |
|
| 65 |
// Gets the rect of an item relative to another item. |
| 66 |
function getRectRelativeTo(item, relativeTo) { |
| 67 |
return translateRect({"x": 0, "y": 0, "width": item.width, "height": item.height}, item, relativeTo); |
| 68 |
} |
| 69 |
|
| 70 |
// Useful only for vertically scrolling containers of multiple items |
| 71 |
function getVisibleItems(container, relativeTo, cond) { |
| 72 |
var v = []; |
| 73 |
gatherChildren(container.contentItem, v, function visibleCond(child) { |
| 74 |
if (!cond(child)) |
| 75 |
return false; |
| 76 |
|
| 77 |
var rect = getRectRelativeTo(child, relativeTo); |
| 78 |
|
| 79 |
return ((rect.y + rect.height) >= 0 && rect.y <= container.height); |
| 80 |
}); |
| 81 |
|
| 82 |
return v; |
| 83 |
} |
| 84 |
|
| 85 |
function doesPointIntersectRect(x, y, rect) { |
| 86 |
return x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y && y <= (rect.y + rect.height); |
| 87 |
} |
| 88 |
|
| 89 |
function isNumber(n) { |
| 90 |
return n === parseFloat(n); |
| 91 |
} |
| 92 |
|
| 93 |
function isEven(n) { |
| 94 |
return isNumber(n) && (n % 2 == 0); |
| 95 |
} |
| 96 |
|
| 97 |
function isOdd(n) { |
| 98 |
return isNumber(n) && (n % 2 == 1); |
| 99 |
} |
| 100 |
|
| 101 |
function interpolate(start, end, parameter) { |
| 102 |
return start + parameter * (end - start); |
| 103 |
} |