basic functionality: falling snow
[qt-letitsnow] / contents / ui / main.qml
1 /*
2  *   Copyright 2020 Robin Krens <robin@robinkrens.nl>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU Library General Public License as
6  *   published by the Free Software Foundation; either version 2 or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details
13  *
14  *   You should have received a copy of the GNU Library General Public
15  *   License along with this program; if not, write to the
16  *   Free Software Foundation, Inc.,
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 import QtQuick 2.6
21 import QtQuick.Layouts 1.0
22 import QtQuick.Window 2.2
23
24 //import org.kde.plasma.plasmoid 2.0 as Plasmoid
25 //import org.kde.plasma.core 2.0 as PlasmaCore
26 //import org.kde.plasma.components 2.0 as PlasmaComponents
27
28 import QtQuick 2.3
29 import QtQuick.Controls 1.2
30
31 Item {
32         id: root;
33         
34         property var snow: [];
35         readonly property int snowCount: 100;
36         readonly property int screenWidth: Qt.application.screens[0].width;
37         readonly property int screenHeight: Qt.application.screens[0].height;
38         
39         Button {
40
41                 id: letitsnow;
42
43                 anchors.centerIn: parent;
44                 text: "Let it snow!";
45
46                 onClicked: {
47                         
48                         //var screenWidth = Qt.application.screens[0].width;
49                         //var screenHeight = Qt.application.screens[0].height;
50                         
51                         for (var i = 0; i < snowCount; i++) {
52                                 
53                                 var component = Qt.createComponent("snowWindow.qml");
54                                 
55                                 snow[i] = component.createObject(root);
56                                 
57                                 snow[i].x = Math.random() * screenWidth;
58                                 snow[i].y = Math.random() * screenHeight;
59                                 
60                                 snow[i].showMaximized();
61                         }
62                         
63                         snowFalling.running = true;
64                 }
65         }
66
67         Timer {
68
69                 id: snowFalling
70
71                 function test() {
72                 }
73
74                 interval: 50; running: false; repeat: true
75                 
76                 onTriggered: {
77                         
78                         for (var i = 0; i < snowCount; i++) {
79                                 if(snow[i].dir == 0) {
80                                         snow[i].x += 2;
81                                 } else {
82                                         snow[i].x -= 2;
83                                 }
84                                 
85                                 if (snow[i].disposition++ % snow[i].swirl == 0) {
86                                         snow[i].dir = !snow[i].dir;
87                                 }
88
89                                 if(snow[i].x > screenWidth) {
90                                         snow[i].x = 0;
91                                 } else if (snow[i].x < 0) {
92                                         snow[i].x = 1440;
93                                 }
94
95                                 snow[i].y += 2;
96
97                                 if (snow[i].y > screenHeight) {
98                                         snow[i].y = 0;
99                                 }
100                         }
101                 }
102         }
103
104 }