Explorar el Código

add speed indication to downloader

Malf hace 2 años
padre
commit
1c13012886
Se han modificado 5 ficheros con 31 adiciones y 6 borrados
  1. 10 1
      Downloader_UI.gd
  2. 8 1
      Main.tscn
  3. 3 0
      TopoLoader.gd
  4. 6 2
      chunk.gd
  5. 4 2
      downloader.gd

+ 10 - 1
Downloader_UI.gd

@@ -5,6 +5,8 @@ extends Panel
 # var a = 2
 # var b = "text"
 
+var last_downloaded=0
+var last_update=OS.get_ticks_msec()
 
 # Called when the node enters the scene tree for the first time.
 func _ready():
@@ -16,8 +18,15 @@ func _ready():
 #	pass
 
 func _on_status_updated(download):
+	var now = OS.get_ticks_msec()
+	var timeElapsed=1#(now-last_update)/1000
+	last_update = now
+	var diff = (download._downloaded_size - last_downloaded) / 1024
+	var speed = diff / timeElapsed
+	last_downloaded = download._downloaded_size
 	rect_size = $VBoxContainer.rect_size
-	$VBoxContainer/name.text = download._file_name
+	$VBoxContainer/name.text = download._current_url
 	$VBoxContainer/size.text = "size: %s"%download._file_size
 	$VBoxContainer/downloaded.text = "downloaded: %s"%download._downloaded_size
+	$VBoxContainer/speed.text = "%skb/s"%speed
 	$VBoxContainer/ProgressBar.value = download._downloaded_percent

+ 8 - 1
Main.tscn

@@ -140,10 +140,17 @@ margin_bottom = 76.0
 text = "downloaded:"
 align = 1
 
-[node name="ProgressBar" type="ProgressBar" parent="UI/Downloader/VBoxContainer"]
+[node name="speed" type="Label" parent="UI/Downloader/VBoxContainer"]
 margin_top = 80.0
 margin_right = 264.0
 margin_bottom = 94.0
+text = "speed:"
+align = 1
+
+[node name="ProgressBar" type="ProgressBar" parent="UI/Downloader/VBoxContainer"]
+margin_top = 98.0
+margin_right = 264.0
+margin_bottom = 112.0
 
 [node name="reference" type="MeshInstance" parent="."]
 visible = false

+ 3 - 0
TopoLoader.gd

@@ -71,6 +71,9 @@ func cacheArea(latMinIN, latMaxIN, lonMinIN, lonMaxIN):
 	if not _validate(latMinIN,lonMinIN) and not _validate(latMaxIN,lonMaxIN) :
 		print("out of scope!")
 		return
+	
+	if not file.is_open():
+		file.open(getFilepath(), File.READ)
 		
 	var lat = latMinIN
 	while lat <= latMaxIN:

+ 6 - 2
chunk.gd

@@ -15,6 +15,8 @@ var scaleFact:int = 1
 var origin:Vector3
 var center:Vector3
 
+var initialized = false
+
 func setParams(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN, radiusIN:float, scaleFactIN):
 	latMin = latMinIN
 	latMax = latMaxIN
@@ -26,6 +28,10 @@ func setParams(latMinIN, latMaxIN, lonMinIN, lonMaxIN, resolutionIN, radiusIN:fl
 
 # Called when the node enters the scene tree for the first time.
 func _ready():
+	initialize()
+	#.translate(.get_node("Mesh").origin)
+
+func initialize():
 	print("chunk %s,%s initializing" %[latMin, lonMin])
 	var initialized = .get_node("Mesh").initialize(latMin, latMax, lonMin, lonMax, resolution, radius, scaleFact)
 	if not initialized:
@@ -33,12 +39,10 @@ func _ready():
 		while not download._file_name == .get_node("Mesh").tl.getFilepath():
 			print("awaiting file download for %s" %.get_node("Mesh").tl.getFilepath())
 			download = yield(Messenger, "fileDownloaded")
-			print(download)
 	.get_node("Mesh").genMesh()
 	.get_node("Origin").transform.origin = .get_node("Mesh").origin
 	.get_node("Center").transform.origin = .get_node("Mesh").center
 	print("chunk %s,%s ready" %[latMin, lonMin])
-	#.translate(.get_node("Mesh").origin)
 
 # Called every frame. 'delta' is the elapsed time since the previous frame.
 #func _process(delta):

+ 4 - 2
downloader.gd

@@ -36,6 +36,7 @@ class Download:
 func _init() -> void:
 	set_process(false)
 	connect("request_completed", self, "_on_request_completed")
+	connect("stats_updated", Messenger, "_on_statusUpdate")
 
 func get_stats() -> Download:
 	return currentDownload
@@ -74,9 +75,8 @@ func _process(delta):
 func _download():
 	ensureFolder(currentDownload._kind)
 	download_file = currentDownload._file_name
-	.connect("stats_updated", Messenger, "_on_statusUpdate")
 	_send_head_request()
-	print("downloading %s" %currentDownload._file_name)
+	print("downloading %s" %currentDownload._current_url)
 	downloading = true
 
 func ensureFolder(kind):
@@ -139,6 +139,8 @@ func _on_request_completed(p_result,
 			_send_get_request()
 			
 		elif _last_method == HTTPClient.METHOD_GET:
+			print("file download complete %s"%currentDownload._current_url)
+			_file.close()
 			emit_signal("file_downloaded", currentDownload)
 			Messenger._on_file_downloaded(currentDownload)
 			downloads[currentDownload._kind].erase(currentDownload._file_name)