OnlineLevelContainer.gd 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. class_name OnlineLevelContainer
  2. extends Button
  3. var level_name := ""
  4. var level_author := ""
  5. var level_thumbnail = null
  6. var level_id := ""
  7. var thumbnail_url := ""
  8. var difficulty := "Easy"
  9. var featured = false
  10. signal level_selected(container: OnlineLevelContainer)
  11. const DIFFICULTY_TO_STAR_TRANSLATION := {
  12. "Easy": 0,
  13. "Medium": 2,
  14. "Hard": 3,
  15. "Extreme": 4
  16. }
  17. static var cached_thumbnails := {}
  18. func _ready() -> void:
  19. set_process(false)
  20. setup_visuals()
  21. func _process(_delta: float) -> void:
  22. if Input.is_action_just_pressed("ui_accept") and visible:
  23. level_selected.emit(self)
  24. func setup_visuals() -> void:
  25. %LevelName.text = Global.sanitize_string(level_name)
  26. %LevelAuthor.text = level_author
  27. if featured:
  28. self_modulate = Color.YELLOW
  29. var idx := 0
  30. print(difficulty)
  31. var difficulty_int = DIFFICULTY_TO_STAR_TRANSLATION[difficulty]
  32. for i in %DifficultyStars.get_children():
  33. i.region_rect.position.x = 24 if idx > difficulty_int else [0, 0, 8, 8, 16][difficulty_int]
  34. idx += 1
  35. get_thumbnail()
  36. func get_thumbnail() -> void:
  37. if cached_thumbnails.has(level_id):
  38. %LevelIcon.texture = cached_thumbnails[level_id]
  39. $MarginContainer/HBoxContainer/HSplitContainer/LeftHalf/LevelIcon/Error.hide()
  40. return
  41. if thumbnail_url == "":
  42. $MarginContainer/HBoxContainer/HSplitContainer/LeftHalf/LevelIcon/Label.hide()
  43. $MarginContainer/HBoxContainer/HSplitContainer/LeftHalf/LevelIcon/Error.show()
  44. return
  45. $ThumbnailDownloader.request(thumbnail_url, [], HTTPClient.METHOD_GET)
  46. func on_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray) -> void:
  47. var image = Image.new()
  48. if thumbnail_url.contains(".webp"):
  49. image.load_webp_from_buffer(body)
  50. elif thumbnail_url.contains(".jpeg") or thumbnail_url.contains(".jpg"):
  51. image.load_jpg_from_buffer(body)
  52. else:
  53. image.load_png_from_buffer(body)
  54. %LevelIcon.texture = ImageTexture.create_from_image(image)
  55. cached_thumbnails[level_id] = %LevelIcon.texture