Added media player and character select and started some other stuff
This commit is contained in:
192
MainWindow.gd
192
MainWindow.gd
@@ -13,7 +13,39 @@ var list := $ScrollContainer/VBoxContainer
|
||||
var game_label := $GameLabel
|
||||
|
||||
@onready
|
||||
var player := $Player
|
||||
var character_grid_container := $ScrollContainer2
|
||||
|
||||
@onready
|
||||
var character_grid := $ScrollContainer2/GridContainer
|
||||
|
||||
@onready
|
||||
var play_button := $PanelContainer/HBoxContainer/PlayButton
|
||||
|
||||
@onready
|
||||
var pause_button := $PanelContainer/HBoxContainer/PauseButton
|
||||
|
||||
@onready
|
||||
var stop_button := $PanelContainer/HBoxContainer/StopButton
|
||||
|
||||
@onready
|
||||
var audio := $AudioStreamPlayer
|
||||
|
||||
@onready
|
||||
var progress := $PanelContainer/HBoxContainer/HSlider
|
||||
|
||||
@onready
|
||||
var label := $PanelContainer/HBoxContainer/Label
|
||||
|
||||
@onready
|
||||
var add_player := $Players/VBoxContainer/HBoxContainer/AddPlayer
|
||||
|
||||
@onready
|
||||
var players := $Players/VBoxContainer
|
||||
|
||||
var Player := preload("res://Player.tscn")
|
||||
|
||||
@onready
|
||||
var path = '/Users/sebastian/ResilioSync/Sorterat_test/Metal Gear Solid 4 - Guns of the Patriots/2-16 Metal Gear Saga.mp3'
|
||||
|
||||
func get_suggestion_list() -> void:
|
||||
var http_request = HTTPRequest.new()
|
||||
@@ -25,12 +57,48 @@ func get_suggestion_list() -> void:
|
||||
if error != OK:
|
||||
push_error("An error occurred in the HTTP request.")
|
||||
|
||||
func add_players():
|
||||
var new_player := Player.instantiate()
|
||||
players.add_child(new_player)
|
||||
new_player.connect("change_character_clicked", _on_player_change_character_clicked.bind(new_player))
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready():
|
||||
play_button.pressed.connect(fetch_first_song)
|
||||
pause_button.pressed.connect(pause)
|
||||
stop_button.pressed.connect(stop)
|
||||
progress.drag_started.connect(_on_drag_started)
|
||||
progress.drag_ended.connect(_on_drag_ended)
|
||||
|
||||
add_player.pressed.connect(add_players)
|
||||
|
||||
open_button.pressed.connect(open)
|
||||
player.fetched.connect(fetched)
|
||||
get_suggestion_list()
|
||||
#dir_contents("/Users/sebastian/ResilioSync/Sorterat_test")
|
||||
|
||||
var characters := DirAccess.open("res://characters/")
|
||||
if characters:
|
||||
characters.list_dir_begin()
|
||||
var file_name = characters.get_next()
|
||||
while file_name != "":
|
||||
if !file_name.ends_with(".import"):
|
||||
|
||||
var texture = load("res://characters/" + file_name)
|
||||
|
||||
var texture_btn := TextureButton.new()
|
||||
|
||||
character_grid.add_child(texture_btn)
|
||||
texture_btn.custom_minimum_size = Vector2(80, 40)
|
||||
texture_btn.ignore_texture_size = true
|
||||
texture_btn.stretch_mode = TextureButton.STRETCH_KEEP_ASPECT
|
||||
texture_btn.texture_normal = texture
|
||||
|
||||
texture_btn.pressed.connect(select_character.bind(file_name))
|
||||
|
||||
file_name = characters.get_next()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func fetched():
|
||||
var http_request = HTTPRequest.new()
|
||||
@@ -79,8 +147,9 @@ func open():
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta):
|
||||
|
||||
pass
|
||||
if audio.has_stream_playback() && !is_changing && !audio.stream_paused:
|
||||
progress.value = audio.get_playback_position()
|
||||
label.text = format_text(progress.value, stream.get_length())
|
||||
|
||||
func _on_file_dialog_dir_selected(path):
|
||||
print(path)
|
||||
@@ -109,9 +178,118 @@ func dir_contents(path: String) -> void:
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var is_changing: bool = false
|
||||
var playback_position: float
|
||||
var stream: AudioStream
|
||||
|
||||
func set_songs(new_songs) -> void:
|
||||
songs = new_songs
|
||||
|
||||
func make_request(address, func_name) -> void:
|
||||
var http_request = HTTPRequest.new()
|
||||
add_child(http_request)
|
||||
http_request.request_completed.connect(func_name)
|
||||
|
||||
# Perform a GET request. The URL below returns JSON as of writing.
|
||||
var error = http_request.request(address)
|
||||
if error != OK:
|
||||
push_error("An error occurred in the HTTP request.")
|
||||
|
||||
|
||||
func format_time(time: float) -> String:
|
||||
var mins: String = "%02d" % floor(time / 60)
|
||||
var sec: String = "%02d" % round(fmod(time, 60))
|
||||
return mins + ":" + sec
|
||||
|
||||
func format_text(part: float, total: float) -> String:
|
||||
return format_time(part) + " / " + format_time(total)
|
||||
|
||||
|
||||
func fetch_first_song() -> void:
|
||||
var http_request = HTTPRequest.new()
|
||||
add_child(http_request)
|
||||
#http.set_download_file("https://music.sanplex.tech/music/first")
|
||||
http_request.request_completed.connect(first_song_fetched)
|
||||
|
||||
# Perform a GET request. The URL below returns JSON as of writing.
|
||||
var error = http_request.request("https://music.sanplex.tech/music/rand")
|
||||
if error != OK:
|
||||
push_error("An error occurred in the HTTP request.")
|
||||
|
||||
func first_song_fetched(result, response_code, headers, body) -> void:
|
||||
if result != HTTPRequest.RESULT_SUCCESS:
|
||||
push_error("Song couldn't be downloaded. Try a different song.")
|
||||
var sound = AudioStreamMP3.new()
|
||||
sound.data = body
|
||||
make_request("https://music.sanplex.tech/music/addQue", add_que)
|
||||
play(sound)
|
||||
|
||||
func add_que(result, response_code, headers, body) -> void:
|
||||
print("response_code", response_code)
|
||||
fetched()
|
||||
|
||||
func play(song) -> void:
|
||||
if audio.stream_paused:
|
||||
audio.stream_paused = false
|
||||
audio.seek(playback_position)
|
||||
print("continue")
|
||||
elif song:
|
||||
print("play given song")
|
||||
audio.stream = song
|
||||
audio.play()
|
||||
stream = audio.stream
|
||||
else:
|
||||
if songs:
|
||||
#print(songs)
|
||||
path = songs[0]
|
||||
print(path)
|
||||
print(FileAccess.file_exists(path))
|
||||
audio.stream = load_mp3(path)
|
||||
print("play")
|
||||
audio.play()
|
||||
stream = audio.stream
|
||||
progress.max_value = round(stream.get_length())
|
||||
progress.tick_count = round(stream.get_length() / 60)
|
||||
|
||||
func pause() -> void:
|
||||
audio.stream_paused = true
|
||||
playback_position = audio.get_playback_position()
|
||||
|
||||
func stop() -> void:
|
||||
audio.stop()
|
||||
audio.stream_paused = false
|
||||
progress.value = 0
|
||||
|
||||
func _on_drag_started() -> void:
|
||||
is_changing = true
|
||||
|
||||
func _on_drag_ended(_changed) -> void:
|
||||
audio.seek(progress.value)
|
||||
playback_position = progress.value
|
||||
is_changing = false
|
||||
|
||||
func load_mp3(_path) -> AudioStream:
|
||||
var file = FileAccess.open(_path, FileAccess.READ)
|
||||
var sound = AudioStreamMP3.new()
|
||||
sound.data = file.get_buffer(file.get_length())
|
||||
return sound
|
||||
|
||||
var current_player
|
||||
func _on_player_change_character_clicked(new_player):
|
||||
print("_on_player_change_character_clicked")
|
||||
current_player = new_player
|
||||
character_grid_container.visible = true
|
||||
|
||||
func select_character(file_name: String):
|
||||
print("select_character")
|
||||
character_grid_container.visible = false
|
||||
current_player._on_control_character_selected_clicked(file_name)
|
||||
|
||||
Reference in New Issue
Block a user