| 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 |
* Lucas Beeler <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Given 'input', constrain the value of 'input' to range between |
| 21 |
* 'lowConstraint' and 'highConstraint', inclusive. Return the constrained |
| 22 |
* value without modifying 'input'. |
| 23 |
*/ |
| 24 |
function clamp(input, lowConstraint, highConstraint) { |
| 25 |
if (input < lowConstraint) |
| 26 |
return lowConstraint; |
| 27 |
else if (input > highConstraint) |
| 28 |
return highConstraint; |
| 29 |
else |
| 30 |
return input; |
| 31 |
} |
| 32 |
|
| 33 |
function cloneRect(source) { |
| 34 |
var ret = { }; |
| 35 |
ret.x = source.x; |
| 36 |
ret.y = source.y; |
| 37 |
ret.width = source.width; |
| 38 |
ret.height = source.height; |
| 39 |
|
| 40 |
return ret; |
| 41 |
} |
| 42 |
|
| 43 |
function interpolateRect(start, end, factor) { |
| 44 |
var result = { }; |
| 45 |
|
| 46 |
result.x = start.x + factor * (end.x - start.x); |
| 47 |
result.y = start.y + factor * (end.y - start.y); |
| 48 |
result.width = start.width + factor * (end.width - start.width); |
| 49 |
result.height = start.height + factor * (end.height - start.height); |
| 50 |
|
| 51 |
return result; |
| 52 |
} |
| 53 |
|
| 54 |
/* Forces Geometry object 'item' to fit centered inside Geometry object |
| 55 |
* 'viewport', preserving the aspect of ratio of 'item' but potentially scaling |
| 56 |
* and translating it so that it snugly fits centered inside of 'viewport'. |
| 57 |
* Return the new scaled-up and translated Geometry for 'item'. |
| 58 |
*/ |
| 59 |
function fitRect(viewport, item) { |
| 60 |
if (item.width == 0 || item.height == 0) { |
| 61 |
return viewport; |
| 62 |
} |
| 63 |
|
| 64 |
var itemAspectRatio = item.width / item.height; |
| 65 |
var viewportAspectRatio = viewport.width / viewport.height; |
| 66 |
|
| 67 |
var result = { }; |
| 68 |
if (itemAspectRatio > viewportAspectRatio) { |
| 69 |
var scaleFactor = viewport.width / item.width; |
| 70 |
result.width = viewport.width; |
| 71 |
result.height = item.height * scaleFactor |
| 72 |
} else { |
| 73 |
scaleFactor = viewport.height / item.height; |
| 74 |
result.width = item.width * scaleFactor |
| 75 |
result.height = viewport.height; |
| 76 |
} |
| 77 |
|
| 78 |
result.width = clamp(result.width, 0, viewport.width); |
| 79 |
result.height = clamp(result.height, 0, viewport.height); |
| 80 |
result.x = viewport.x + (viewport.width - result.width) / 2; |
| 81 |
result.y = viewport.y + (viewport.height - result.height) / 2; |
| 82 |
result.scaleFactor = scaleFactor; |
| 83 |
|
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
function getRelativeRect(geom, relativeTo) { |
| 88 |
var result = { }; |
| 89 |
|
| 90 |
result.x = (geom.x - relativeTo.x) / relativeTo.width; |
| 91 |
result.y = (geom.y - relativeTo.y) / relativeTo.height; |
| 92 |
result.width = geom.width / relativeTo.width; |
| 93 |
result.height = geom.height / relativeTo.height; |
| 94 |
|
| 95 |
return result; |
| 96 |
} |
| 97 |
|
| 98 |
function sizeToRect(rect, qmlItem) { |
| 99 |
qmlItem.x = rect.x; |
| 100 |
qmlItem.y = rect.y; |
| 101 |
qmlItem.width = rect.width; |
| 102 |
qmlItem.height = rect.height; |
| 103 |
} |
| 104 |
|
| 105 |
function areEqual(geom1, geom2) { |
| 106 |
return (geom1.x === geom2.x && geom1.y === geom2.y && geom1.width === |
| 107 |
geom2.width && geom1.height === geom2.height); |
| 108 |
} |