From 2f328933b4fb7fdb8b133bc66671be24992c7a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Ib=C3=A1=C3=B1ez=20S=C3=A1nchez?= Date: Thu, 21 Dec 2023 19:47:17 +0100 Subject: [PATCH] Display of images in demo project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jacob Ibáñez Sánchez --- .../scenes/achievements/AchievementDisplay.gd | 21 +++++++++ .../achievements/AchievementDisplay.tscn | 6 +++ .../scenes/leaderboards/LeaderboardDisplay.gd | 31 ++++++++----- .../leaderboards/LeaderboardDisplay.tscn | 36 ++++++++------- plugin/demo/scenes/players/PlayerDisplay.gd | 25 ++++++++++- plugin/demo/scenes/players/PlayerDisplay.tscn | 45 +++---------------- .../autoloads/godot_play_game_services.gd | 18 ++++++++ 7 files changed, 116 insertions(+), 66 deletions(-) diff --git a/plugin/demo/scenes/achievements/AchievementDisplay.gd b/plugin/demo/scenes/achievements/AchievementDisplay.gd index 6cb3b03..7ddec97 100644 --- a/plugin/demo/scenes/achievements/AchievementDisplay.gd +++ b/plugin/demo/scenes/achievements/AchievementDisplay.gd @@ -1,5 +1,7 @@ extends Control +@onready var icon_rect: TextureRect = %IconRect + @onready var id_label: Label = %IdLabel @onready var name_label: Label = %NameLabel @onready var description_label: Label = %DescriptionLabel @@ -96,8 +98,27 @@ func _connect_signals() -> void: _waiting = false _set_up_display() ) + GodotPlayGameServices.image_stored.connect(func(file_path: String): + if file_path == achievement.revealed_image_uri\ + or file_path == achievement.unlocked_image_uri: + _set_up_icon() + ) func _set_up_waiting() -> void: _waiting = true unlock_button.disabled = true unlock_button.text = "Wait..." + +func _set_up_icon() -> void: + var property: String + match achievement.state: + AchievementsClient.State.STATE_REVEALED: + property = achievement.revealed_image_uri + AchievementsClient.State.STATE_UNLOCKED: + property = achievement.unlocked_image_uri + + if property and not property.is_empty(): + GodotPlayGameServices.display_image_in_texture_rect( + icon_rect, + property + ) diff --git a/plugin/demo/scenes/achievements/AchievementDisplay.tscn b/plugin/demo/scenes/achievements/AchievementDisplay.tscn index 8655874..a668f86 100644 --- a/plugin/demo/scenes/achievements/AchievementDisplay.tscn +++ b/plugin/demo/scenes/achievements/AchievementDisplay.tscn @@ -24,7 +24,13 @@ theme_override_constants/margin_bottom = 25 layout_mode = 2 theme_override_constants/separation = 25 +[node name="IconRect" type="TextureRect" parent="MarginContainer/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +stretch_mode = 3 + [node name="Id" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +visible = false layout_mode = 2 [node name="Label" type="Label" parent="MarginContainer/VBoxContainer/Id"] diff --git a/plugin/demo/scenes/leaderboards/LeaderboardDisplay.gd b/plugin/demo/scenes/leaderboards/LeaderboardDisplay.gd index 10d9dff..1afa207 100644 --- a/plugin/demo/scenes/leaderboards/LeaderboardDisplay.gd +++ b/plugin/demo/scenes/leaderboards/LeaderboardDisplay.gd @@ -1,17 +1,19 @@ extends Control -@onready var leaderboard_id_label: Label = %LeaderboardId -@onready var leaderboard_name_label: Label = %LeaderboardName +@onready var icon_rect: TextureRect = %IconRect -@onready var player_rank_label: Label = %PlayerRank -@onready var player_score_label: Label = %PlayerScore +@onready var id_label: Label = %IdLabel +@onready var name_label: Label = %NameLabel -@onready var new_score_line_edit: LineEdit = %NewScore -@onready var submit_score_button: Button = %SubmitScore +@onready var player_rank_label: Label = %PlayerRankLabel +@onready var player_score_label: Label = %PlayerScoreLabel -@onready var time_span_option: OptionButton = %TimeSpan -@onready var collection_option: OptionButton = %Collection -@onready var show_variant_button: Button = %ShowVariant +@onready var new_score_line_edit: LineEdit = %NewScoreLineEdit +@onready var submit_score_button: Button = %SubmitScoreButton + +@onready var time_span_option: OptionButton = %TimeSpanOption +@onready var collection_option: OptionButton = %CollectionOption +@onready var show_variant_button: Button = %ShowVariantButton var leaderboard: LeaderboardsClient.Leaderboard @@ -24,8 +26,15 @@ var _selected_collection: LeaderboardsClient.Collection func _ready() -> void: if leaderboard: - leaderboard_id_label.text = leaderboard.leaderboard_id - leaderboard_name_label.text = leaderboard.display_name + GodotPlayGameServices.image_stored.connect(func(file_path: String): + if file_path == leaderboard.icon_image_uri: + GodotPlayGameServices.display_image_in_texture_rect( + icon_rect, + file_path + ) + ) + id_label.text = leaderboard.leaderboard_id + name_label.text = leaderboard.display_name _set_up_display_score() _set_up_submit_score() _set_up_variants() diff --git a/plugin/demo/scenes/leaderboards/LeaderboardDisplay.tscn b/plugin/demo/scenes/leaderboards/LeaderboardDisplay.tscn index eb76c2a..6b5a58e 100644 --- a/plugin/demo/scenes/leaderboards/LeaderboardDisplay.tscn +++ b/plugin/demo/scenes/leaderboards/LeaderboardDisplay.tscn @@ -24,27 +24,33 @@ theme_override_constants/margin_bottom = 25 layout_mode = 2 theme_override_constants/separation = 25 -[node name="LeaderboardId" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +[node name="IconRect" type="TextureRect" parent="MarginContainer/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +stretch_mode = 3 + +[node name="Id" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +visible = false layout_mode = 2 -[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/LeaderboardId"] +[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/Id"] layout_mode = 2 -text = "Leaderboard ID:" +text = "ID:" -[node name="LeaderboardId" type="Label" parent="MarginContainer/VBoxContainer/LeaderboardId"] +[node name="IdLabel" type="Label" parent="MarginContainer/VBoxContainer/Id"] unique_name_in_owner = true layout_mode = 2 size_flags_horizontal = 3 text_overrun_behavior = 3 -[node name="LeaderboardName" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +[node name="Name" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] layout_mode = 2 -[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/LeaderboardName"] +[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/Name"] layout_mode = 2 -text = "Leaderboard Name:" +text = "Name:" -[node name="LeaderboardName" type="Label" parent="MarginContainer/VBoxContainer/LeaderboardName"] +[node name="NameLabel" type="Label" parent="MarginContainer/VBoxContainer/Name"] unique_name_in_owner = true layout_mode = 2 size_flags_horizontal = 3 @@ -57,7 +63,7 @@ layout_mode = 2 layout_mode = 2 text = "Player Rank:" -[node name="PlayerRank" type="Label" parent="MarginContainer/VBoxContainer/PlayerRank"] +[node name="PlayerRankLabel" type="Label" parent="MarginContainer/VBoxContainer/PlayerRank"] unique_name_in_owner = true layout_mode = 2 @@ -68,7 +74,7 @@ layout_mode = 2 layout_mode = 2 text = "Player Score:" -[node name="PlayerScore" type="Label" parent="MarginContainer/VBoxContainer/PlayerScore"] +[node name="PlayerScoreLabel" type="Label" parent="MarginContainer/VBoxContainer/PlayerScore"] unique_name_in_owner = true layout_mode = 2 @@ -87,14 +93,14 @@ layout_mode = 2 text = "New score:" horizontal_alignment = 1 -[node name="NewScore" type="LineEdit" parent="MarginContainer/VBoxContainer/SubmitScore/NewScore"] +[node name="NewScoreLineEdit" type="LineEdit" parent="MarginContainer/VBoxContainer/SubmitScore/NewScore"] unique_name_in_owner = true layout_mode = 2 alignment = 1 virtual_keyboard_type = 2 clear_button_enabled = true -[node name="SubmitScore" type="Button" parent="MarginContainer/VBoxContainer/SubmitScore"] +[node name="SubmitScoreButton" type="Button" parent="MarginContainer/VBoxContainer/SubmitScore"] unique_name_in_owner = true layout_mode = 2 theme = ExtResource("1_oo2te") @@ -116,7 +122,7 @@ layout_mode = 2 layout_mode = 2 text = "Time Span" -[node name="TimeSpan" type="OptionButton" parent="MarginContainer/VBoxContainer/Variants/TimeSpan"] +[node name="TimeSpanOption" type="OptionButton" parent="MarginContainer/VBoxContainer/Variants/TimeSpan"] unique_name_in_owner = true layout_mode = 2 @@ -127,11 +133,11 @@ layout_mode = 2 layout_mode = 2 text = "Collection" -[node name="Collection" type="OptionButton" parent="MarginContainer/VBoxContainer/Variants/Collection"] +[node name="CollectionOption" type="OptionButton" parent="MarginContainer/VBoxContainer/Variants/Collection"] unique_name_in_owner = true layout_mode = 2 -[node name="ShowVariant" type="Button" parent="MarginContainer/VBoxContainer/Variants"] +[node name="ShowVariantButton" type="Button" parent="MarginContainer/VBoxContainer/Variants"] unique_name_in_owner = true layout_mode = 2 theme = ExtResource("1_oo2te") diff --git a/plugin/demo/scenes/players/PlayerDisplay.gd b/plugin/demo/scenes/players/PlayerDisplay.gd index ffd387f..fd57e7a 100644 --- a/plugin/demo/scenes/players/PlayerDisplay.gd +++ b/plugin/demo/scenes/players/PlayerDisplay.gd @@ -1,5 +1,7 @@ extends Control +@onready var avatar_rect: TextureRect = %AvatarRect + @onready var id_label: Label = %IdLabel @onready var name_label: Label = %NameLabel @onready var title_label: Label = %TitleLabel @@ -13,15 +15,36 @@ var player: PlayersClient.Player func _ready() -> void: if player: - _set_up_display() + _set_up_display.call_deferred() compare_button.pressed.connect(func(): PlayersClient.compare_profile(player.player_id) ) func _set_up_display() -> void: + GodotPlayGameServices.image_stored.connect(func(file_path: String): + if file_path == player.hi_res_image_uri: + GodotPlayGameServices.display_image_in_texture_rect( + avatar_rect, + file_path + ) + ) id_label.text = player.player_id name_label.text = player.display_name title_label.text = player.title status_label.text = PlayersClient.PlayerFriendStatus.find_key(player.friend_status) level_label.text = str(player.level_info.current_level.level_number) xp_label.text = str(player.level_info.current_xp_total) + +func _load_and_retry(image_uri: String) -> Image: + var image = Image.new() + var retries := 3 + var error := ERR_FILE_NOT_FOUND + while retries > 0 or error == ERR_FILE_NOT_FOUND: + if retries != 3: + await get_tree().create_timer(1.0).timeout + error = image.load_png_from_buffer(Image.load_from_file(image_uri).get_data()) + retries -= 1 + if retries == 0: + print("Error loading file!!") + image = null + return image diff --git a/plugin/demo/scenes/players/PlayerDisplay.tscn b/plugin/demo/scenes/players/PlayerDisplay.tscn index 9bb2316..0965054 100644 --- a/plugin/demo/scenes/players/PlayerDisplay.tscn +++ b/plugin/demo/scenes/players/PlayerDisplay.tscn @@ -24,7 +24,13 @@ theme_override_constants/margin_bottom = 25 layout_mode = 2 theme_override_constants/separation = 25 +[node name="AvatarRect" type="TextureRect" parent="MarginContainer/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +stretch_mode = 3 + [node name="Id" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +visible = false layout_mode = 2 [node name="Label" type="Label" parent="MarginContainer/VBoxContainer/Id"] @@ -107,42 +113,3 @@ layout_mode = 2 theme = ExtResource("1_uv4o5") text = "Compare" text_overrun_behavior = 3 - -[node name="HSeparator2" type="HSeparator" parent="MarginContainer/VBoxContainer"] -visible = false -layout_mode = 2 - -[node name="Variants" type="VBoxContainer" parent="MarginContainer/VBoxContainer"] -visible = false -layout_mode = 2 -theme_override_constants/separation = 25 - -[node name="TimeSpan" type="VBoxContainer" parent="MarginContainer/VBoxContainer/Variants"] -layout_mode = 2 - -[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/Variants/TimeSpan"] -layout_mode = 2 -text = "Time Span" - -[node name="TimeSpan" type="OptionButton" parent="MarginContainer/VBoxContainer/Variants/TimeSpan"] -unique_name_in_owner = true -layout_mode = 2 - -[node name="Collection" type="VBoxContainer" parent="MarginContainer/VBoxContainer/Variants"] -layout_mode = 2 - -[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/Variants/Collection"] -layout_mode = 2 -text = "Collection" - -[node name="Collection" type="OptionButton" parent="MarginContainer/VBoxContainer/Variants/Collection"] -unique_name_in_owner = true -layout_mode = 2 - -[node name="ShowVariant" type="Button" parent="MarginContainer/VBoxContainer/Variants"] -unique_name_in_owner = true -layout_mode = 2 -theme = ExtResource("1_uv4o5") -disabled = true -text = "Show Leaderboard Variant" -text_overrun_behavior = 3 diff --git a/plugin/export_scripts_template/autoloads/godot_play_game_services.gd b/plugin/export_scripts_template/autoloads/godot_play_game_services.gd index 36ba09b..8a354eb 100644 --- a/plugin/export_scripts_template/autoloads/godot_play_game_services.gd +++ b/plugin/export_scripts_template/autoloads/godot_play_game_services.gd @@ -8,6 +8,11 @@ extends Node ## This Autoload also calls the [code]initialize()[/code] method of the plugin, ## checking if the user is authenticated. +## Signal emitted after an image is downloaded and saved to the device.[br] +## [br] +## [param file_path]: The path to the stored file. +signal image_stored(file_path: String) + ## Main entry point to the android plugin. With this object, you can call the ## kotlin methods directly. var android_plugin: Object @@ -26,3 +31,16 @@ func _ready() -> void: android_plugin.initialize() else: printerr("No plugin found!") + + if android_plugin: + android_plugin.imageStored.connect(func(file_path: String): + image_stored.emit(file_path) + ) + +## Displays the given image in the given texture rectangle.[br] +## [br] +## [param texture_rect]: The texture rectangle control to display the image.[br] +## [param file_path]: The file path of the image, for example user://image.png. +func display_image_in_texture_rect(texture_rect: TextureRect, file_path: String) -> void: + var image := Image.load_from_file(file_path) + texture_rect.texture = ImageTexture.create_from_image(image)