Skip to content

Commit

Permalink
Added logic to load scenario images.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumrunner committed Apr 3, 2024
1 parent 21d723e commit 6efc350
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 17 deletions.
16 changes: 10 additions & 6 deletions unity/Assets/Scripts/ImgAsyncLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ 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<string, UIElement>();
texture_list = new Dictionary<string, Texture2D>();
default_quest_picture = Resources.Load("sprites/scenario_list/default_quest_picture") as Texture2D;
}

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()
Expand Down Expand Up @@ -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
{
Expand All @@ -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()]);
}
}
}
Expand Down
75 changes: 69 additions & 6 deletions unity/Assets/Scripts/UI/Screens/ContentSelectDownloadScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<string, RemoteContentPack> contentPack, IEnumerable<PackTypeData> localContentPackList)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -222,10 +239,31 @@ private UIElement RenderContentPackNameAndDescription(float offset, KeyValuePair
}
private UIElement RenderImage(float offset, KeyValuePair<string, RemoteContentPack> 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;
}
Expand Down Expand Up @@ -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);
}
}
}
9 changes: 9 additions & 0 deletions unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using UnityEngine;

namespace Assets.Scripts.UI.Screens
{
internal interface IContentImageDrawer
{
void DrawPicture(Texture2D texture, UIElement ui_picture_shadow);
}
}
11 changes: 11 additions & 0 deletions unity/Assets/Scripts/UI/Screens/IContentImageDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions unity/Assets/Scripts/UI/Screens/QuestSelectionScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> questList = null;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down

0 comments on commit 6efc350

Please sign in to comment.