Unity 8
carousel.js
1 /*
2  * Copyright (C) 2013 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 as published by
6  * the Free Software Foundation; version 3.
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 
17 .pragma library
18 
28 function getContinuousIndex(x, tileWidth, gapToMiddlePhase, gapToEndPhase, kGapEnd, kMiddleIndex, kXBeginningEnd) {
29  if (x < gapToMiddlePhase) {
30  // beginning
31  return x * kXBeginningEnd
32  } else if (x > gapToEndPhase) {
33  // end
34  return x * kXBeginningEnd + kGapEnd
35  }
36 
37  // middle
38  return x / tileWidth + kMiddleIndex
39 }
40 
50 function getXFromContinuousIndex(index, viewWidth, contentWidth, tileWidth, gapToMiddlePhase, gapToEndPhase, drawBuffer) {
51  var middleX = (index + 0.5) * tileWidth - viewWidth / 2
52 
53  if (middleX < gapToMiddlePhase) {
54  // inverse of 'middleIndex - kGap' of getContinuousIndex()
55  return index /
56  ((1 / tileWidth) +
57  (viewWidth / (2 * tileWidth * gapToMiddlePhase)) -
58  1 / (2 * gapToMiddlePhase))
59  - drawBuffer
60  } else if (middleX > gapToEndPhase) {
61  // inverse of 'middleIndex + kGap' of getContinuousIndex()
62  return (index +
63  1 -
64  viewWidth / tileWidth +
65  (contentWidth * viewWidth - viewWidth * viewWidth) / (2 * tileWidth * gapToMiddlePhase) +
66  (viewWidth - contentWidth) / (2 * gapToMiddlePhase)) /
67  (1 / tileWidth +
68  viewWidth / (2 * tileWidth * gapToMiddlePhase) -
69  1 / (2 * gapToMiddlePhase))
70  - drawBuffer
71  }
72 
73  // inverse of 'middleIndex' of getContinuousIndex()
74  return middleX - drawBuffer
75 }
76 
83 function getViewTranslation(x, tileWidth, gapToMiddlePhase, gapToEndPhase, translationXViewFactor) {
84  if (x < gapToMiddlePhase) {
85  // beginning
86  return (gapToMiddlePhase - x) * translationXViewFactor
87  } else if (x > gapToEndPhase) {
88  // end
89  return (gapToEndPhase - x) * translationXViewFactor
90  }
91 
92  // middle
93  return 0
94 }
95 
102 function getItemScale(distance, continuousIndex, numberOfItems, scaleFactor) {
103  var distanceAbs = Math.abs(distance)
104  var distanceToBounds = Math.min(continuousIndex, numberOfItems - continuousIndex)
105  var k = Math.max(200 + 100 * (-distanceToBounds / (3 * scaleFactor)), 50)
106  return Math.max(0.01, 1 - Math.pow(distanceAbs, 2.5) / (k * scaleFactor))
107 }
108 
117 function getItemTranslation(index, selectedIndex, distance, scale, maxScale, maxTranslation) {
118  if (index === selectedIndex) return 0
119  var sign = distance > 0 ? 1 : -1
120  return sign * (maxScale - scale) * maxTranslation
121 }