~unity-team/+junk/dashboard-playground

« back to all changes in this revision

Viewing changes to Components/carousel.js

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
// get the element which is selected accordingly to x, the hero of the view
 
20
function getContinuousIndex(x, tileWidth, gapToMiddlePhase, gapToEndPhase, kGapEnd, kMiddleIndex, kXBeginningEnd) {
 
21
    if (x < gapToMiddlePhase) {
 
22
        // beginning
 
23
        return x * kXBeginningEnd
 
24
    } else if (x > gapToEndPhase) {
 
25
        // end
 
26
        return x * kXBeginningEnd + kGapEnd
 
27
    }
 
28
 
 
29
    // middle
 
30
    return x / tileWidth + kMiddleIndex
 
31
}
 
32
 
 
33
// obtain x position relative to an index, essentially an inverse of getContinuousIndex()
 
34
function getXFromContinuousIndex(index, viewWidth, contentWidth, tileWidth, gapToMiddlePhase, gapToEndPhase) {
 
35
    var middleX = (index + 0.5) * tileWidth - viewWidth / 2
 
36
 
 
37
    if (middleX < gapToMiddlePhase) {
 
38
        // inverse of 'middleIndex - kGap' of getContinuousIndex()
 
39
        return index /
 
40
               ((1 / tileWidth) +
 
41
                (viewWidth / (2 * tileWidth * gapToMiddlePhase)) -
 
42
                1 / (2 * gapToMiddlePhase))
 
43
    } else if (middleX > gapToEndPhase) {
 
44
        // inverse of 'middleIndex + kGap' of getContinuousIndex()
 
45
        return (index +
 
46
                1 -
 
47
                viewWidth / tileWidth +
 
48
                (contentWidth * viewWidth - viewWidth * viewWidth) / (2 * tileWidth * gapToMiddlePhase) +
 
49
                (viewWidth - contentWidth) / (2 * gapToMiddlePhase)) /
 
50
               (1 / tileWidth +
 
51
                viewWidth / (2 * tileWidth * gapToMiddlePhase) -
 
52
                1 / (2 * gapToMiddlePhase))
 
53
    }
 
54
 
 
55
    // inverse of 'middleIndex' of getContinuousIndex()
 
56
    return middleX
 
57
}
 
58
 
 
59
// get translation of the whole view, adds gaps on sides
 
60
function getViewTranslation(x, tileWidth, gapToMiddlePhase, gapToEndPhase, translationXViewFactor) {
 
61
    if (x < gapToMiddlePhase) {
 
62
        // beginning
 
63
        return (gapToMiddlePhase - x) * translationXViewFactor
 
64
    } else if (x > gapToEndPhase) {
 
65
        // end
 
66
        return (gapToEndPhase - x) * translationXViewFactor
 
67
    }
 
68
 
 
69
    // middle
 
70
    return 0
 
71
}
 
72
 
 
73
// item scale
 
74
function getItemScale(distance, continuousIndex, end, scaleFactor) {
 
75
    var distanceAbs = Math.abs(distance)
 
76
    var distanceToBounds = Math.min(continuousIndex, end - continuousIndex)
 
77
    var k = Math.max(200 + 100 * (-distanceToBounds / (3 * scaleFactor)), 50)
 
78
    return Math.max(0.01, 1 - Math.pow(distanceAbs, 2.5) / (k * scaleFactor))
 
79
}
 
80
 
 
81
// item translation
 
82
function getItemTranslation(distance, scale, maxScale, translationFactor) {
 
83
    var sign = distance > 0 ? 1 : -1
 
84
    return sign * (maxScale - scale) * translationFactor
 
85
}