| 1 |
/* |
| 2 |
* This file is part of unity-2d |
| 3 |
* |
| 4 |
* Copyright 2010-2011 Canonical Ltd. |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; version 3. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
import QtQuick 2.0 |
| 20 |
import Lomiri.Components 0.1 |
| 21 |
import "../common" |
| 22 |
import "../common/units.js" as Units |
| 23 |
import "../common/utils.js" as Utils |
| 24 |
|
| 25 |
ListView { |
| 26 |
id: flow |
| 27 |
|
| 28 |
property int previousCurrentIndex: -1 |
| 29 |
property int firstFullSceneIndex: 0 |
| 30 |
property bool stackItems: false |
| 31 |
property alias animateContentX: contentXAnimation.enabled |
| 32 |
property real stackedItemsX: 0 |
| 33 |
|
| 34 |
property real margin: Units.tvPx(135) |
| 35 |
property real itemWidth: Units.tvPx(245) |
| 36 |
signal itemClicked |
| 37 |
signal seekRequested |
| 38 |
|
| 39 |
cacheBuffer: width / 2 |
| 40 |
orientation: ListView.Horizontal |
| 41 |
spacing: Units.tvPx(36) |
| 42 |
contentX: firstFullSceneIndex * (itemWidth + spacing) |
| 43 |
Behavior on contentX { id: contentXAnimation; enabled: true; SmoothedAnimation {duration: 300}} |
| 44 |
|
| 45 |
function previous() { |
| 46 |
if (currentIndex > 0) currentIndex -= 1 |
| 47 |
seekRequested() |
| 48 |
} |
| 49 |
|
| 50 |
function next() { |
| 51 |
if (currentIndex < count + 1) currentIndex += 1 |
| 52 |
seekRequested() |
| 53 |
} |
| 54 |
|
| 55 |
// TODO See if we can make it work with highlightRangeMode & friends |
| 56 |
onCurrentIndexChanged: |
| 57 |
{ |
| 58 |
if (!stackItems) { |
| 59 |
if (previousCurrentIndex != -1) { |
| 60 |
if (previousCurrentIndex < currentIndex) { |
| 61 |
// Going to the right |
| 62 |
var fullScenesThatFitOnScreen = Math.floor(width / (itemWidth + spacing)) |
| 63 |
if (currentIndex - firstFullSceneIndex >= fullScenesThatFitOnScreen - 1) { |
| 64 |
// We went to the one before the end |
| 65 |
if (currentIndex < count - 1) { |
| 66 |
// But not that much too the end |
| 67 |
firstFullSceneIndex++ |
| 68 |
} else { |
| 69 |
firstFullSceneIndex = count - fullScenesThatFitOnScreen |
| 70 |
} |
| 71 |
} |
| 72 |
} else if (previousCurrentIndex > currentIndex) { |
| 73 |
// Going to the left |
| 74 |
if (currentIndex <= firstFullSceneIndex) { |
| 75 |
if (currentIndex != 0) { |
| 76 |
// But not that much too the beginning |
| 77 |
firstFullSceneIndex-- |
| 78 |
} else { |
| 79 |
firstFullSceneIndex = 0 |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
previousCurrentIndex = currentIndex |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
delegate: Item { |
| 89 |
id: item |
| 90 |
|
| 91 |
property variant imageSourceUrl: imageSource |
| 92 |
property alias imageHeight: image.height |
| 93 |
property alias imageX: looseItem.x |
| 94 |
|
| 95 |
property variant segments: [-flow.itemWidth, 0.0, flow.width - flow.itemWidth - margin * 2, flow.width - margin * 2] |
| 96 |
property real absoluteX: item.x - flow.contentX |
| 97 |
|
| 98 |
width: delegateButton.width |
| 99 |
height: delegateButton.height |
| 100 |
|
| 101 |
Keys.forwardTo: [delegateButton] |
| 102 |
|
| 103 |
Item { |
| 104 |
id: looseItem |
| 105 |
parent: flow |
| 106 |
x: margin + item.x - flow.contentX |
| 107 |
y: item.y - flow.contentY |
| 108 |
width: item.width |
| 109 |
height: item.height |
| 110 |
z: flow.z + flow.count - Math.abs(index - flow.currentIndex) |
| 111 |
property real unstackingFactor: 1 |
| 112 |
|
| 113 |
states: [ |
| 114 |
State { |
| 115 |
name: "stacked" |
| 116 |
when: stackItems |
| 117 |
PropertyChanges { |
| 118 |
target: looseItem |
| 119 |
x: stackedItemsX |
| 120 |
unstackingFactor: 0 |
| 121 |
visible: index == currentIndex |
| 122 |
} |
| 123 |
}, |
| 124 |
State { |
| 125 |
name: "unstacked" |
| 126 |
when: !stackItems |
| 127 |
PropertyChanges { |
| 128 |
target: looseItem |
| 129 |
visible: true |
| 130 |
} |
| 131 |
} |
| 132 |
] |
| 133 |
|
| 134 |
transitions: [ |
| 135 |
Transition { |
| 136 |
from: "stacked" |
| 137 |
to: "unstacked" |
| 138 |
reversible: true |
| 139 |
PropertyAnimation { |
| 140 |
properties: "x, unstackingFactor" |
| 141 |
easing.type: Easing.OutQuad |
| 142 |
} |
| 143 |
} |
| 144 |
] |
| 145 |
|
| 146 |
AbstractButton { |
| 147 |
id: delegateButton |
| 148 |
|
| 149 |
width: flow.itemWidth |
| 150 |
height: flow.height |
| 151 |
|
| 152 |
property real angle: Utils.segmentsLinearInterpolation(segments, [63.0, 0.0, 0.0, -63.0], absoluteX) |
| 153 |
property real origin: Utils.segmentsLinearInterpolation(segments, [width, width, 0.0, 0.0], absoluteX) |
| 154 |
transform: Rotation { origin.x: delegateButton.origin; origin.y: height/2; axis { x: 0; y: 1; z: 0 } angle: looseItem.unstackingFactor * delegateButton.angle} |
| 155 |
property real scaleHelper: 1 - 0.2 * looseItem.unstackingFactor // 0.8 + (1 - 0.8) * (1 - looseItem.unstackingFactor) |
| 156 |
scale: Utils.segmentsLinearInterpolation(segments, [scaleHelper, 1.0, 1.0, scaleHelper], absoluteX) |
| 157 |
|
| 158 |
onClicked: { |
| 159 |
flow.currentIndex = index |
| 160 |
flow.itemClicked() |
| 161 |
} |
| 162 |
|
| 163 |
Item { |
| 164 |
width: image.width |
| 165 |
height: image.height |
| 166 |
anchors.centerIn: image |
| 167 |
|
| 168 |
Image { |
| 169 |
source: "artwork/scene_selection_glow.png" |
| 170 |
anchors.fill: parent |
| 171 |
anchors.margins: Units.tvPx(-27) |
| 172 |
opacity: item.activeFocus && looseItem.state == "unstacked" ? 1.0 : 0.0 |
| 173 |
smooth: true |
| 174 |
Behavior on opacity {NumberAnimation {duration: 200; easing.type: Easing.OutQuad}} |
| 175 |
} |
| 176 |
|
| 177 |
Rectangle { |
| 178 |
id: outline |
| 179 |
anchors.centerIn: parent |
| 180 |
width: parent.width + Units.tvPx(3) * 2 |
| 181 |
height: parent.height + Units.tvPx(3) * 2 |
| 182 |
color: "black" |
| 183 |
smooth: true |
| 184 |
radius: Units.tvPx(2) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
Image { |
| 190 |
id: image |
| 191 |
|
| 192 |
anchors.centerIn: parent |
| 193 |
fillMode: Image.PreserveAspectFit |
| 194 |
clip: true |
| 195 |
width: Units.tvPx(245) |
| 196 |
height: Units.tvPx(130) |
| 197 |
|
| 198 |
source: imageSourceUrl |
| 199 |
smooth: true |
| 200 |
asynchronous: true |
| 201 |
|
| 202 |
opacity: 0 |
| 203 |
|
| 204 |
Behavior on opacity { NumberAnimation { duration: 200 } } |
| 205 |
|
| 206 |
onSourceChanged: opacity = 0 |
| 207 |
onStatusChanged: if (status == Image.Ready) opacity = 1 |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
} |