-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
harbour-parkingchaos.qml
175 lines (150 loc) · 5.71 KB
/
harbour-parkingchaos.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* This file is part of harbour-parkingchaos.
* SPDX-FileCopyrightText: 2020-2022 Mirian Margiani
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
import Nemo.Configuration 1.0
import "pages"
import Opal.About 1.0 as A
ApplicationWindow
{
id: app
initialPage: Component { BoardPage { } }
cover: Qt.resolvedUrl("cover/CoverPage.qml")
allowedOrientations: defaultAllowedOrientations
bottomMargin: successBanner.visibleSize
readonly property int oHORIZONTAL: 1
readonly property int oVERTICAL: 0
signal levelRequested(var index)
signal levelWon()
property ListModel levelsModel: ListModel {}
property QtObject current: QtObject {
property ListModel map: ListModel {}
property int index: 0
property int level: 0
property int moves: 0
property int minimumMoves: 0
}
onLevelWon: successBanner.show();
onLevelRequested: {
successBanner.hide();
var level = levelsModel.get(index);
if (!level) {
console.log("error: failed to activate level '%1'".arg(index));
return;
} else {
config.lastLevelIndex = index;
config.lastLevelMap = "";
config.lastLevelMoves = 0;
}
// copy the map so we can modify it, but reset it
// by copying the original data again
current.map.clear();
for (var i = 0; i < level.tiles.count; i++) {
current.map.append(level.tiles.get(i));
}
current.index = index;
current.level = index+1;
current.moves = 0;
current.minimumMoves = level.minimumMoves;
}
DockedPanel {
id: successBanner
modal: true
open: false
height: bannerColumn.height + 2*Theme.paddingLarge
width: parent.width
dock: Dock.Bottom
Column {
id: bannerColumn
width: parent.width
Item { width: parent.width; height: Theme.paddingLarge }
Label {
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width - 2*Theme.horizontalPageMargin
text: qsTr("Won!");
font.pixelSize: Theme.fontSizeExtraLarge
color: Theme.highlightColor
horizontalAlignment: Text.AlignHCenter
}
Label {
anchors.horizontalCenter: parent.horizontalCenter
width: parent.width - 2*Theme.horizontalPageMargin
wrapMode: Text.Wrap
text: qsTr("You completed this level in %L1 move(s)!", "", current.moves).arg(current.moves);
font.pixelSize: Theme.fontSizeLarge
color: Theme.secondaryHighlightColor
horizontalAlignment: Text.AlignHCenter
}
Item { width: parent.width; height: Theme.paddingLarge }
Row {
anchors.horizontalCenter: parent.horizontalCenter
spacing: Theme.paddingMedium
Button {
text: qsTr("Restart")
onClicked: levelRequested(current.index)
}
Button {
text: qsTr("Next level")
onClicked: levelRequested(current.index+1)
}
}
}
}
property ConfigurationGroup config: ConfigurationGroup {
path: "/apps/harbour-parkingchaos"
property int configMigrated: 0
property int lastLevelIndex: 0
property int lastLevelMoves: 0 // TODO implement saving current status
property string lastLevelMap: "" // TODO implement saving current status
function migrate() {
if (configMigrated === 0) {
var _legacyConfig0 = Qt.createQmlObject(
"import Nemo.Configuration 1.0; ConfigurationGroup { path: '/' }",
app, 'LegacyConfiguration0')
lastLevelIndex = _legacyConfig0.value('/lastLevelIndex', 0)
lastLevelMap = _legacyConfig0.value('/lastLevelMap', '')
lastLevelMoves = _legacyConfig0.value('/lastLevelMoves', 0)
configMigrated = 1
_legacyConfig0.setValue('/lastLevelIndex', undefined)
_legacyConfig0.setValue('/lastLevelMap', undefined)
_legacyConfig0.setValue('/lastLevelMoves', undefined)
_legacyConfig0.destroy()
}
}
}
A.ChangelogNews {
changelogList: Qt.resolvedUrl("Changelog.qml")
}
Component.onCompleted: {
if (config.configMigrated < 1) {
config.migrate()
}
var levelsDatabase = DATA_DIRECTORY + "/levels.json";
console.log("loading levels from:", levelsDatabase);
var xhr = new XMLHttpRequest;
xhr.open("GET", levelsDatabase);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
var response = JSON.parse(xhr.responseText);
console.log("success: levels database loaded (%1 levels)".arg(response.length));
levelsModel.clear();
for (var i in response) {
levelsModel.append(response[i]);
}
levelRequested(config.lastLevelIndex);
if (config.lastLevelMap !== "") {
var map = JSON.parse(config.lastLevelMap);
current.map.clear();
for (var j in map) {
current.map.append(map[j]);
}
current.moves = config.lastLevelMoves;
}
}
};
xhr.send();
}
}