From: Robin Krens <robin@robinkrens.nl>
Date: Tue, 24 Nov 2020 19:52:08 +0000 (+0100)
Subject: basic functionality: falling snow
X-Git-Url: https://robinkrens.nl/gitweb/?a=commitdiff_plain;h=46686fe6792f7fb278a51c79ebcfb3a84d809cf5;p=qt-letitsnow

basic functionality: falling snow
---

diff --git a/contents/ui/main.qml b/contents/ui/main.qml
index 4b6503b..f19618d 100644
--- a/contents/ui/main.qml
+++ b/contents/ui/main.qml
@@ -30,24 +30,75 @@ import QtQuick.Controls 1.2
 
 Item {
 	id: root;
+	
+	property var snow: [];
+	readonly property int snowCount: 100;
+	readonly property int screenWidth: Qt.application.screens[0].width;
+	readonly property int screenHeight: Qt.application.screens[0].height;
+	
 	Button {
+
+		id: letitsnow;
+
         	anchors.centerIn: parent;
 	        text: "Let it snow!";
 
 		onClicked: {
-			var snow = [];
-			for (var i = 0; i < 10; i++) {
+			
+			//var screenWidth = Qt.application.screens[0].width;
+			//var screenHeight = Qt.application.screens[0].height;
+			
+			for (var i = 0; i < snowCount; i++) {
+				
+				var component = Qt.createComponent("snowWindow.qml");
+				
+				snow[i] = component.createObject(root);
 				
-				snow[i] = Qt.createComponent("snowWindow.qml");
-				var snowWindow = snow[i].createObject(root);
-				snowWindow.x = Math.random() * 500;
-				snowWindow.y = Math.random() * 500;
-				//snowWindow.opacity = 0.9;
-				//snowWindow.flags = Qt.WA_TransparentForMouseEvents | Qt.X11BypassWindowManagerHint;
-
-				//snowWindow.flags =  Qt.WA_TransparentForMouseEvents| Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint;
-				snowWindow.showMaximized();
+				snow[i].x = Math.random() * screenWidth;
+				snow[i].y = Math.random() * screenHeight;
+				
+				snow[i].showMaximized();
 			}
-        }
-    }
+			
+			snowFalling.running = true;
+        	}
+	}
+
+	Timer {
+
+		id: snowFalling
+
+		function test() {
+		}
+
+        	interval: 50; running: false; repeat: true
+		
+		onTriggered: {
+			
+			for (var i = 0; i < snowCount; i++) {
+				if(snow[i].dir == 0) {
+					snow[i].x += 2;
+				} else {
+					snow[i].x -= 2;
+				}
+				
+				if (snow[i].disposition++ % snow[i].swirl == 0) {
+					snow[i].dir = !snow[i].dir;
+				}
+
+				if(snow[i].x > screenWidth) {
+					snow[i].x = 0;
+				} else if (snow[i].x < 0) {
+					snow[i].x = 1440;
+				}
+
+				snow[i].y += 2;
+
+				if (snow[i].y > screenHeight) {
+					snow[i].y = 0;
+				}
+			}
+		}
+    	}
+
 }
diff --git a/contents/ui/snowWindow.qml b/contents/ui/snowWindow.qml
index fa51e65..56960fb 100644
--- a/contents/ui/snowWindow.qml
+++ b/contents/ui/snowWindow.qml
@@ -25,26 +25,20 @@ import QtQuick.Window 2.2
 ApplicationWindow {
 	
 	id: root;
-	
-	width: 30; height: 30;
-	x: 500; y: 500;
-	//color: "transparent";
-	opacity: 0.5;
-	title: qsTr("Snowy")
-
-	flags: Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.WA_TransparentForMouseEvents | Qt.WA_TranslucentBackground | Qt.X11BypassWindowManagerHint;
-
 
-	//flags: Qt.WA_TranslucentBackground | Qt.X11BypassWindowManagerHint | Qt.WA_TransparentForMouseEvents;
+	property int dir: (Math.random() * 100) % 2; /* could go either left or right */
+	property int swirl: (Math.random() * 1000) % 55 + 2; /* random falling 'swirl' */
+	property int disposition: 0;
 
-	//flags: Qt.WA_TranslucentBackground | Qt.WA_TransparentForMouseEvents| Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint;
+	width: 3; height: 3;
+	//x: 500; y: 500;
+	opacity: 1;
+	//title: qsTr("Snowy")
 
-	//flags: Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.WA_TransparentForMouseEvents | Qt.WA_TranslucentBackground;
-	//color: "transparent";
+	flags: Qt.WindowStaysOnTopHint 
+		| Qt.FramelessWindowHint
+		| Qt.WA_TransparentForMouseEvents
+		| Qt.WA_TranslucentBackground 
+		| Qt.X11BypassWindowManagerHint;
 
-    //		Text {
-    //	    	anchors.centerIn: parent
-    //	    	color: "red";
-    //	    	text: qsTr("Hello World.")
-    //		}
 }