Skip to content

Commit

Permalink
chore: reduce repeated code
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxAndreassenRPM committed Nov 30, 2023
1 parent e594f04 commit 853bb45
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 89 deletions.
37 changes: 7 additions & 30 deletions Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,18 @@ public async Task LoadTemplateData()
public async void LoadAndCreateButtons()
{
await LoadTemplateData();
CreateButtons();
await LoadIcons();
}

public void CreateButtons()
{
if (partnerAssets.Length == 0)
{
SDKLogger.LogWarning(TAG, "No templates found. You need to load fetch the template data first.");
return;
}
foreach (var partnerAsset in partnerAssets)
{
var button = CreateButton(partnerAsset.Id);
var partnerAssetData = partnerAsset;
button.AddListener(() => AssetSelected(partnerAssetData));
}
}

public async Task LoadIcons()
{
foreach (var partnerAsset in partnerAssets)

CreateButtons(partnerAssets,
async (button, asset) =>
{
var downloadHandler = new DownloadHandlerTexture();
var webRequestDispatcher = new WebRequestDispatcher();
var url = $"{partnerAsset.ImageUrl}?w=64";
var url = $"{asset.ImageUrl}?w=64";
var response = await webRequestDispatcher.SendRequest<ResponseTexture>(url, HttpMethod.GET, downloadHandler: downloadHandler);
response.ThrowIfError();
OnIconLoaded(partnerAsset, response.Texture);
}
}

private void OnIconLoaded(PartnerAsset partnerAsset, Texture texture)
{
UpdateButtonIcon(partnerAsset.Id, texture);

UpdateButtonIcon(asset.Id, response.Texture);
});
}
}
}
24 changes: 5 additions & 19 deletions Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Threading;
using System.Threading.Tasks;
using ReadyPlayerMe.Core;

using UnityEngine;

namespace ReadyPlayerMe.AvatarCreator
Expand All @@ -21,24 +20,11 @@ public async Task LoadColorPalette(AvatarProperties avatarProperties)
public async void LoadAndCreateButtons(AvatarProperties avatarProperties)
{
await LoadColorPalette(avatarProperties);
CreateButtons();
}

public void CreateButtons()
{
if (colorAssets.Length == 0)
{
SDKLogger.LogWarning(TAG, "No templates found. You need to load fetch the template data first.");
return;
}
for (int i = 0; i < colorAssets.Length; i++)

CreateButtons(colorAssets, (button, asset) =>
{
var colorAsset = colorAssets[i];
var button = CreateButton(colorAsset.Id);
button.SetColor(colorAsset.HexColor);
button.AddListener(() => AssetSelected(colorAsset));
Debug.Log($"Create button {colorAsset.Id}");
}
button.SetColor(asset.HexColor);
});
}
}
}
21 changes: 21 additions & 0 deletions Runtime/AvatarCreator/UI/Elements/SelectionElement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using ReadyPlayerMe.Core;
using UnityEngine;
using UnityEngine.Events;

Expand All @@ -12,6 +14,8 @@ public interface IAssetData

public abstract class SelectionElement : MonoBehaviour
{
private const string TAG = nameof(SelectionElement);

[Header("UI Elements")]
[Space(5)]
[SerializeField] private ButtonElement buttonElementPrefab;
Expand All @@ -29,6 +33,23 @@ public ButtonElement CreateButton(string id)
return button;
}

public void CreateButtons<T>(T[] assets, Action<ButtonElement, T> onButtonCreated = default) where T : IAssetData
{
if (assets.Length == 0)
{
SDKLogger.LogWarning(TAG, "No assets provided.");
return;
}

for (int i = 0; i < assets.Length; i++)
{
var button = CreateButton(assets[i].Id);
button.AddListener(() => AssetSelected(assets[i]));
onButtonCreated?.Invoke(button, assets[i]);
Debug.Log($"Create button {assets[i].Id}");
}
}

public void ClearButtons()
{
foreach (var button in buttonMap)
Expand Down
53 changes: 13 additions & 40 deletions Runtime/AvatarCreator/UI/Elements/TemplateSelectionElement.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using ReadyPlayerMe.Core;
using UnityEngine.Networking;

namespace ReadyPlayerMe.AvatarCreator
{
Expand All @@ -12,7 +13,7 @@ public class TemplateSelectionElement : SelectionElement
{
private const string TAG = nameof(TemplateSelectionElement);

private List<AvatarTemplateData> avatarTemplateDataList;
private List<AvatarTemplateData> avatarTemplates;
private AvatarTemplateFetcher avatarTemplateFetcher;

private void Awake()
Expand All @@ -26,55 +27,27 @@ private void Awake()
public async void LoadAndCreateButtons()
{
await LoadTemplateData();
CreateButtons();
await LoadTemplateRenders();
CreateButtons(avatarTemplates.ToArray(), async (button, asset) =>
{
var downloadHandler = new DownloadHandlerTexture();
var webRequestDispatcher = new WebRequestDispatcher();
var url = $"{asset.ImageUrl}?w=64";
var response = await webRequestDispatcher.SendRequest<ResponseTexture>(url, HttpMethod.GET, downloadHandler: downloadHandler);
button.SetIcon(response.Texture);
});
}

/// <summary>
/// Loads avatar template data without fetching the icon renders.
/// </summary>
public async Task LoadTemplateData()
{
avatarTemplateDataList = await avatarTemplateFetcher.GetTemplates();
if (avatarTemplateDataList == null || avatarTemplateDataList.Count == 0)
avatarTemplates = await avatarTemplateFetcher.GetTemplates();

if (avatarTemplates == null || avatarTemplates.Count == 0)
{
SDKLogger.LogWarning(TAG, "No templates found");
}
}

/// <summary>
/// Loads avatar template data and icon renders. This will wait for all the icons to be downloaded.
/// </summary>
public async Task LoadTemplateRenders()
{
avatarTemplateDataList = await avatarTemplateFetcher.GetTemplatesWithRenders(OnIconLoaded);
}

private void OnIconLoaded(AvatarTemplateData avatarTemplate)
{
UpdateButtonIcon(avatarTemplate.Id, avatarTemplate.Texture);
}

/// <summary>
/// Creates buttons from the loaded template data and sets the icon and button event.
/// </summary>
public void CreateButtons()
{
if (avatarTemplateDataList.Count == 0)
{
SDKLogger.LogWarning(TAG, "No templates found. You need to load fetch the template data first.");
return;
}
for (var i = 0; i < avatarTemplateDataList.Count; i++)
{
var button = CreateButton(avatarTemplateDataList[i].Id);
var templateData = avatarTemplateDataList[i];
if (templateData.Texture != null)
{
button.SetIcon(templateData.Texture);
}
button.AddListener(() => AssetSelected(templateData));
}
}
}
}

0 comments on commit 853bb45

Please sign in to comment.