Files
MusicPlayer/MainWindow.gd
Sebastian aad5f3d421 First commit
* Can open dir and play song
* Can fetch song from server and show game
* Can fetch list of games
2023-03-18 21:17:04 +01:00

118 lines
2.9 KiB
GDScript

extends Control
@onready
var open_button := $Open
@onready
var fileDialog := $FileDialog
@onready
var list := $ScrollContainer/VBoxContainer
@onready
var game_label := $GameLabel
@onready
var player := $Player
func get_suggestion_list() -> void:
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.request_completed.connect(self._http_request_completed)
# Perform a GET request. The URL below returns JSON as of writing.
var error = http_request.request("https://music.sanplex.tech/music/all")
if error != OK:
push_error("An error occurred in the HTTP request.")
# Called when the node enters the scene tree for the first time.
func _ready():
open_button.pressed.connect(open)
player.fetched.connect(fetched)
get_suggestion_list()
#dir_contents("/Users/sebastian/ResilioSync/Sorterat_test")
func fetched():
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.request_completed.connect(show_fetched)
# Perform a GET request. The URL below returns JSON as of writing.
var error = http_request.request("https://music.sanplex.tech/music/info")
if error != OK:
push_error("An error occurred in the HTTP request.")
func show_fetched(result, response_code, headers, body) -> void:
var json = JSON.new()
var error = json.parse(body.get_string_from_utf8())
if error == OK:
var data_received = json.get_data()
print("data_received: ", data_received)
game_label.text = data_received.Game
# Called when the HTTP request is completed.
func _http_request_completed(result, response_code, headers, body):
var json = JSON.new()
var error = json.parse(body.get_string_from_utf8())
if error == OK:
var data_received = json.get_data()
if typeof(data_received) == TYPE_ARRAY:
games.append_array(data_received)
for game in games:
var label := Label.new()
label.text = game
list.add_child(label)
#print(data_received) # Prints array
else:
print("Unexpected data")
# Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org).
#print(response)
func open():
fileDialog.popup()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _on_file_dialog_dir_selected(path):
print(path)
dir_contents(path)
var songs := []
var games := []
#var player = preload("res://Player.gd")
func dir_contents(path: String) -> void:
var dir = DirAccess.open(path)
if dir:
dir.list_dir_begin()
var file_name = dir.get_next()
songs.clear()
while file_name != "":
if dir.current_is_dir():
#print("Found directory: " + file_name)
games.append(file_name)
else:
#print("Found file: " + file_name)
if file_name.ends_with(".mp3"):
songs.append(path + "/" + file_name)
file_name = dir.get_next()
else:
print("An error occurred when trying to access the path.")
if songs:
player.set_songs(songs)
for game in games:
var label := Label.new()
label.text = game
list.add_child(label)