From 6efc3504c0a3b1a0b839d24e95b0b8e2b4b24a77 Mon Sep 17 00:00:00 2001 From: Quantumrunner <58113888+Quantumrunner@users.noreply.github.com> Date: Wed, 3 Apr 2024 21:09:03 +0200 Subject: [PATCH] Added logic to load scenario images. --- unity/Assets/Scripts/ImgAsyncLoader.cs | 16 ++-- .../UI/Screens/ContentSelectDownloadScreen.cs | 75 +++++++++++++++++-- .../Scripts/UI/Screens/IContentImageDrawer.cs | 9 +++ .../UI/Screens/IContentImageDrawer.cs.meta | 11 +++ .../UI/Screens/QuestSelectionScreen.cs | 10 +-- 5 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs create mode 100644 unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs.meta diff --git a/unity/Assets/Scripts/ImgAsyncLoader.cs b/unity/Assets/Scripts/ImgAsyncLoader.cs index 3b5f9d07..8572959b 100644 --- a/unity/Assets/Scripts/ImgAsyncLoader.cs +++ b/unity/Assets/Scripts/ImgAsyncLoader.cs @@ -19,11 +19,11 @@ internal class ImgAsyncLoader Texture2D default_quest_picture = null; // Father class - QuestSelectionScreen questSelectionScreen = null; + IContentImageDrawer contentImageDrawer = null; - public ImgAsyncLoader(QuestSelectionScreen qss) + public ImgAsyncLoader(IContentImageDrawer contentImageDrawer) { - questSelectionScreen = qss; + this.contentImageDrawer = contentImageDrawer; images_list = new Dictionary(); texture_list = new Dictionary(); default_quest_picture = Resources.Load("sprites/scenario_list/default_quest_picture") as Texture2D; @@ -31,7 +31,11 @@ public ImgAsyncLoader(QuestSelectionScreen qss) public void Add(string url, UIElement uie) { - images_list.Add(url, uie); + if(!images_list.ContainsKey(url)) + { + images_list.Add(url, uie); + } + } public void Clear() @@ -62,7 +66,7 @@ public void ImageDownloaded_callback(Texture2D texture, bool error, Uri uri) // Display default picture if (images_list.ContainsKey(uri.ToString())) // this can be empty if we display another screen while pictures are downloading - questSelectionScreen.DrawScenarioPicture(null, images_list[uri.ToString()]); + contentImageDrawer.DrawPicture(null, images_list[uri.ToString()]); } else { @@ -74,7 +78,7 @@ public void ImageDownloaded_callback(Texture2D texture, bool error, Uri uri) // Display pictures if (images_list.ContainsKey(uri.ToString())) // this can be empty if we display another screen while pictures are downloading - questSelectionScreen.DrawScenarioPicture(GetTexture(uri.ToString()), images_list[uri.ToString()]); + contentImageDrawer.DrawPicture(GetTexture(uri.ToString()), images_list[uri.ToString()]); } } } diff --git a/unity/Assets/Scripts/UI/Screens/ContentSelectDownloadScreen.cs b/unity/Assets/Scripts/UI/Screens/ContentSelectDownloadScreen.cs index 5308269e..ad454cee 100644 --- a/unity/Assets/Scripts/UI/Screens/ContentSelectDownloadScreen.cs +++ b/unity/Assets/Scripts/UI/Screens/ContentSelectDownloadScreen.cs @@ -2,13 +2,15 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Windows.Forms; using Assets.Scripts.Content; using UnityEngine; +using UnityEngine.Events; using ValkyrieTools; namespace Assets.Scripts.UI.Screens { - public class ContentSelectDownloadScreen : MonoBehaviour + public class ContentSelectDownloadScreen : MonoBehaviour, IContentImageDrawer { private const int LARGE_FONT_LIMIT = 32; @@ -25,6 +27,12 @@ public class ContentSelectDownloadScreen : MonoBehaviour private readonly StringKey DOWNLOAD_ONGOING = new StringKey("val", "DOWNLOAD_ONGOING"); private readonly StringKey OFFLINE_DUE_TO_ERROR = new StringKey("val", "OFFLINE_DUE_TO_ERROR"); + // Class to handle async images to display + ImgAsyncLoader images_list = null; + + // textures + Texture2D picture_shadow = null; + Texture2D picture_pin = null; private Texture2D button_download = null; private Texture2D button_update = null; private Texture2D button_play = null; @@ -37,6 +45,12 @@ public ContentSelectDownloadScreen() { CleanUpDialogs(); + // Initialize list of images for asynchronous loading + images_list = new ImgAsyncLoader(this); + + //preload textures + picture_shadow = Resources.Load("sprites/scenario_list/picture_shadow") as Texture2D; + picture_pin = Resources.Load("sprites/scenario_list/picture_pin") as Texture2D; button_download = Resources.Load("sprites/scenario_list/button_download") as Texture2D; button_update = Resources.Load("sprites/scenario_list/button_update") as Texture2D; button_play = Resources.Load("sprites/scenario_list/button_play") as Texture2D; @@ -86,9 +100,12 @@ private void DrawContentPackList() RenderActionButton(offset, contentPack, localContentPackList); RenderDeleteButton(offset, contentPack, localContentPackList); + offset += 7.1f; + + scrollArea.SetScrollSize(offset); } - //yield return null; + images_list.StartDownloadASync(); } private void RenderActionButton(float offset, KeyValuePair contentPack, IEnumerable localContentPackList) @@ -185,7 +202,7 @@ private UIElement RenderContentPackNameAndDescription(float offset, KeyValuePair // Content pack name UIElement ui = new UIElement(scrollArea.GetScrollTransform()); ui.SetBGColor(Color.clear); - ui.SetLocation(5.5f, offset + 0.3f, UIScaler.GetWidthUnits() - 8, 1.5f); + ui.SetLocation(5.5f, offset + 0.9f, UIScaler.GetWidthUnits() - 8, 1.5f); ui.SetTextPadding(0.5f); string name = remoteContentPack.Value.languages_name.FirstOrDefault().Value; @@ -222,10 +239,31 @@ private UIElement RenderContentPackNameAndDescription(float offset, KeyValuePair } private UIElement RenderImage(float offset, KeyValuePair contentPack) { - // Content pack name + // prepare/draw list of Images UIElement ui = new UIElement(scrollArea.GetScrollTransform()); - //ui.SetLocation(UIScaler.GetRight(-8.1f), offset + 1.4f, 1.8f, 1.8f); - //ui.SetImage(button_update); + ui.SetLocation(0.9f, offset + 0.8f, 5f, 5f); // this is the location for the shadow (to be displayed first) + ui.SetBGColor(Color.clear); + if (contentPack.Value.image.Length > 0) + { + //if (game.questsList.quest_list_mode != QuestsManager.QuestListMode.ONLINE) + //{ + // DrawPicture(ContentData.FileToTexture(Path.Combine(contentPack.Value., contentPack.Value.image)), ui); ; + //} + //else + if (images_list.IsImageAvailable(contentPack.Value.package_url + contentPack.Value.image)) + { + DrawPicture(images_list.GetTexture(contentPack.Value.package_url + contentPack.Value.image), ui); + } + else + { + images_list.Add(contentPack.Value.package_url + contentPack.Value.image, ui); + } + } + else + { + // Draw default Valkyrie picture + DrawPicture(images_list.GetTexture(null), ui); + } return ui; } @@ -345,5 +383,30 @@ public static void Quit() new ContentSelectScreen(); } + + public void DrawPicture(Texture2D texture, UIElement ui_picture_shadow) + { + float width_heigth = ui_picture_shadow.GetRectTransform().rect.width / UIScaler.GetPixelsPerUnit(); + UnityAction buttonCall = ui_picture_shadow.GetAction(); + + // draw picture shadow + ui_picture_shadow.SetImage(picture_shadow); + + // draw image + UIElement picture = new UIElement(ui_picture_shadow.GetTransform()); + picture.SetLocation(0.30f, 0.30f, width_heigth - 0.6f, width_heigth - 0.6f); + picture.SetBGColor(Color.clear); + picture.SetImage(texture); + picture.SetButton(buttonCall); + + // draw pin + const float pin_width = 1.4f; + const float pin_height = 1.6f; + UIElement pin = new UIElement(picture.GetTransform()); + pin.SetLocation((width_heigth / 2f) - (pin_width / 1.5f), (-pin_height / 2f), pin_width, pin_height); + pin.SetBGColor(Color.clear); + pin.SetImage(picture_pin); + pin.SetButton(buttonCall); + } } } \ No newline at end of file diff --git a/unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs b/unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs new file mode 100644 index 00000000..69e0ccdd --- /dev/null +++ b/unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs @@ -0,0 +1,9 @@ +using UnityEngine; + +namespace Assets.Scripts.UI.Screens +{ + internal interface IContentImageDrawer + { + void DrawPicture(Texture2D texture, UIElement ui_picture_shadow); + } +} \ No newline at end of file diff --git a/unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs.meta b/unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs.meta new file mode 100644 index 00000000..3f4123cc --- /dev/null +++ b/unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bc4dd907d26e631499608b40ff3f3b76 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/Assets/Scripts/UI/Screens/QuestSelectionScreen.cs b/unity/Assets/Scripts/UI/Screens/QuestSelectionScreen.cs index 11b40c13..f586a029 100644 --- a/unity/Assets/Scripts/UI/Screens/QuestSelectionScreen.cs +++ b/unity/Assets/Scripts/UI/Screens/QuestSelectionScreen.cs @@ -12,7 +12,7 @@ namespace Assets.Scripts.UI.Screens { // Class for quest selection window - public class QuestSelectionScreen: MonoBehaviour + public class QuestSelectionScreen: MonoBehaviour, IContentImageDrawer { // List of Quest.QuestData to display (either local or remote) List questList = null; @@ -407,7 +407,7 @@ private void DrawFlags() } } - public void DrawScenarioPicture(Texture2D texture, UIElement ui_picture_shadow) + public void DrawPicture(Texture2D texture, UIElement ui_picture_shadow) { float width_heigth = ui_picture_shadow.GetRectTransform().rect.width / UIScaler.GetPixelsPerUnit(); UnityAction buttonCall = ui_picture_shadow.GetAction(); @@ -938,11 +938,11 @@ public IEnumerator DrawQuestList() { if (game.questsList.quest_list_mode != QuestsManager.QuestListMode.ONLINE) { - DrawScenarioPicture(ContentData.FileToTexture(Path.Combine(q.path, q.image)), ui); ; + DrawPicture(ContentData.FileToTexture(Path.Combine(q.path, q.image)), ui); ; } else if (images_list.IsImageAvailable(q.package_url + q.image)) { - DrawScenarioPicture(images_list.GetTexture(q.package_url + q.image), ui); + DrawPicture(images_list.GetTexture(q.package_url + q.image), ui); } else { @@ -952,7 +952,7 @@ public IEnumerator DrawQuestList() else { // Draw default Valkyrie picture - DrawScenarioPicture(images_list.GetTexture(null), ui); + DrawPicture(images_list.GetTexture(null), ui); } // languages flags