128 lines
4.3 KiB
GDScript
128 lines
4.3 KiB
GDScript
extends Control
|
|
|
|
@onready var artist_input: LineEdit = $Panel/VBoxContainer/HFlowContainer/Artist
|
|
@onready var album_input: LineEdit = $Panel/VBoxContainer/HFlowContainer/Album
|
|
@onready var search_button: Button = $Panel/VBoxContainer/HFlowContainer/Button
|
|
@onready var output: TextEdit = $Panel/VBoxContainer/HBoxContainer/StatusBar
|
|
@onready var http_request: HTTPRequest = $HTTPRequest
|
|
@onready var http_image_request: HTTPRequest = $ImageHttpRequest
|
|
@onready var collage_popup_panel: PopupPanel = $CollagePopupPanel
|
|
@onready var create_collage_button: Button = $Panel/VBoxContainer/HBoxContainer/CreateCollageButton
|
|
|
|
|
|
var _image_array: Array[ImageTexture] = []
|
|
|
|
func _ready() -> void:
|
|
http_request.request_completed.connect(_on_http_request_completed)
|
|
search_button.pressed.connect(_on_search)
|
|
|
|
http_image_request.request_completed.connect(_on_http_image_request_completed)
|
|
|
|
create_collage_button.pressed.connect(_on_create_collage)
|
|
|
|
|
|
func _on_create_collage():
|
|
var w = 1920
|
|
var h = 1080
|
|
var collage: ImageTexture = create_collage(w, h, false)
|
|
var img = collage.get_image()
|
|
var save_path = "user://saved_image.png"
|
|
var error: Error = img.save_png(save_path)
|
|
|
|
if error == OK:
|
|
print("Image saved successfully to: " + save_path)
|
|
else:
|
|
print("Error saving image: " + str(error))
|
|
#collage_popup_panel.get_node("TextureRect").texture = collage
|
|
#collage_popup_panel.size = Vector2(w, h)
|
|
#collage_popup_panel.show()
|
|
|
|
|
|
func _on_http_request_completed(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
|
|
if result == HTTPRequest.RESULT_SUCCESS:
|
|
var json_data = JSON.parse_string(body.get_string_from_utf8()) as Dictionary
|
|
if json_data:
|
|
var data_arr: Array = json_data["data"]
|
|
var album: Dictionary = data_arr[0]
|
|
var cover_url: String = album["cover_big"]
|
|
output.text = str(cover_url)
|
|
http_image_request.request(cover_url)
|
|
else:
|
|
output.text = "Failed to parse JSON"
|
|
else:
|
|
output.text = str("HTTP Request failed with result: ", result, " and code: ", response_code)
|
|
|
|
|
|
# https://api.deezer.com/search/album?q=Demon%20Hunter%20War
|
|
func _on_search() -> void:
|
|
var artist = artist_input.text.replace(" ", "%20")
|
|
var album = album_input.text.replace(" ", "%20")
|
|
if artist == "" or album == "": return
|
|
|
|
var url = str("https://api.deezer.com/search/album?q=",artist,"%20",album)
|
|
http_request.request(url)
|
|
|
|
|
|
func _on_http_image_request_completed(result: int, response_code: int, _headers: PackedStringArray, body: PackedByteArray) -> void:
|
|
if result == HTTPRequest.RESULT_SUCCESS:
|
|
var img = Image.new()
|
|
var err = img.load_jpg_from_buffer(body)
|
|
if err == OK:
|
|
var tex = ImageTexture.new()
|
|
tex = ImageTexture.create_from_image(img)
|
|
var new_tex_rect_node: TextureRect = TextureRect.new()
|
|
$Panel/VBoxContainer/ScrollContainer/ImageContainer.add_child(new_tex_rect_node)
|
|
new_tex_rect_node.texture = tex
|
|
_image_array.append(tex)
|
|
else:
|
|
output.text = str("Error loading image: ", err)
|
|
else:
|
|
output.text = str("HTTP Request failed with result: ", result, " and code: ", response_code)
|
|
|
|
|
|
func create_collage(w: int, h: int, fail_if_not_even: bool) -> ImageTexture:
|
|
var tile_w := 500
|
|
var tile_h := 500
|
|
|
|
# Compute how many tiles we need to cover the area
|
|
var cols:int = ceil(w / tile_w)
|
|
var rows:int = ceil(h / tile_h)
|
|
var total_needed:int = cols * rows
|
|
|
|
if fail_if_not_even and _image_array.size() < total_needed:
|
|
push_error("Not enough images to fill collage")
|
|
return null
|
|
|
|
var shuffled := _image_array.duplicate()
|
|
shuffled.shuffle()
|
|
while shuffled.size() < total_needed:
|
|
shuffled.append_array(shuffled)
|
|
|
|
# Actual cell size for each tile
|
|
var cell_w := w / cols
|
|
var cell_h := h / rows
|
|
|
|
# Create the collage image with black background
|
|
var collage := Image.create(w, h, false, Image.FORMAT_RGBA8)
|
|
collage.fill(Color.BLACK)
|
|
|
|
var idx := 0
|
|
for row in rows:
|
|
for col in cols:
|
|
if idx >= shuffled.size():
|
|
break
|
|
var tex: ImageTexture = shuffled[idx]
|
|
var img: Image = tex.get_image()
|
|
if img.get_format() != collage.get_format():
|
|
img.convert(collage.get_format())
|
|
|
|
# Resize to fit the cell exactly (aspect ratio is fine to distort slightly)
|
|
img.resize(cell_w, cell_h)
|
|
|
|
collage.blit_rect(img, Rect2i(Vector2i(0, 0), Vector2i(cell_w, cell_h)),
|
|
Vector2i(col * cell_w, row * cell_h))
|
|
|
|
idx += 1
|
|
|
|
return ImageTexture.create_from_image(collage)
|