Skip to content

Commit

Permalink
com.rest.blockadelabs 1.0.2 (#9)
Browse files Browse the repository at this point in the history
- enable skybox style dropdown selector via property drawer
  • Loading branch information
StephenHodgson committed Jul 16, 2023
1 parent a4c0648 commit 9341027
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using BlockadeLabs.Skyboxes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Authentication;
using UnityEditor;
using UnityEngine;

namespace BlockadeLabs.Editor
{
[CustomPropertyDrawer(typeof(SkyboxStyle))]
public class SkyboxStylePropertyDrawer : PropertyDrawer
{
private static BlockadeLabsClient blockadeLabsClient;

private static BlockadeLabsClient BlockadeLabsClient => blockadeLabsClient ??= new BlockadeLabsClient();

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
try
{
if (!BlockadeLabsClient.HasValidAuthentication)
{
EditorGUI.LabelField(position, "Cannot fetch skybox styles");
return;
}
}
catch (AuthenticationException)
{
EditorGUI.HelpBox(position, "Check blockade labs api key", MessageType.Error);

return;
}
catch (Exception e)
{
Debug.LogError(e);

return;
}

var id = property.FindPropertyRelative("id");
var name = property.FindPropertyRelative("name");

if (options.Length < 1)
{
FetchStyles();

if (string.IsNullOrWhiteSpace(name.stringValue))
{
EditorGUI.HelpBox(position, "Fetching skybox styles...", MessageType.Info);
return;
}

EditorGUI.LabelField(position, label, new GUIContent(name.stringValue, id.intValue.ToString()));
return;
}

// dropdown
var index = -1;
dynamic currentVoiceOption = null;

if (id.intValue > 0)
{
currentVoiceOption = styles?.FirstOrDefault(style => style.Id.ToString() == id.intValue.ToString());
}

if (currentVoiceOption != null)
{
for (var i = 0; i < options.Length; i++)
{
if (options[i].tooltip.Contains(currentVoiceOption.Id.ToString()))
{
index = i;
break;
}
}
}

EditorGUI.BeginChangeCheck();
index = EditorGUI.Popup(position, label, index, options);

if (EditorGUI.EndChangeCheck())
{
currentVoiceOption = styles?.FirstOrDefault(style => options[index].text.Contains(style.Name));
id.intValue = currentVoiceOption!.Id;
name.stringValue = currentVoiceOption!.Name;
}
}

private static bool isFetchingStyles;

public static bool IsFetchingStyles => isFetchingStyles;

private static IReadOnlyList<SkyboxStyle> styles = new List<SkyboxStyle>();

public static IReadOnlyList<SkyboxStyle> Styles => styles;

private static GUIContent[] options = Array.Empty<GUIContent>();

public static IReadOnlyList<GUIContent> Options => options;

public static async void FetchStyles()
{
if (isFetchingStyles) { return; }
isFetchingStyles = true;

try
{
styles = await BlockadeLabsClient.SkyboxEndpoint.GetSkyboxStylesAsync();
options = styles.OrderBy(style => style.Id).Select(style => new GUIContent(style.Name, style.Id.ToString())).ToArray();
}
catch (Exception e)
{
Debug.LogError(e);
}
finally
{
isFetchingStyles = false;
}
}
}
}

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

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using System;
using UnityEngine;

namespace BlockadeLabs.Skyboxes
{
[Serializable]
public sealed class SkyboxStyle
{
[JsonConstructor]
Expand All @@ -13,22 +16,27 @@ public sealed class SkyboxStyle
[JsonProperty("max-char")] string maxChar,
[JsonProperty("negative-text-max-char")] int negativeTextMaxChar,
[JsonProperty("image")] string image,
[JsonProperty("sort_order")] int sortOrder
)
[JsonProperty("sort_order")] int sortOrder)
{
Id = id;
Name = name;
this.id = id;
this.name = name;
MaxChar = maxChar;
NegativeTextMaxChar = negativeTextMaxChar;
Image = image;
SortOrder = sortOrder;
}

[JsonProperty("id")]
public int Id { get; }
[SerializeField]
private string name;

[JsonProperty("name")]
public string Name { get; }
public string Name => name;

[SerializeField]
private int id;

[JsonProperty("id")]
public int Id => id;

[JsonProperty("max-char")]
public string MaxChar { get; }
Expand Down
2 changes: 1 addition & 1 deletion BlockadeLabs/Packages/com.rest.blockadelabs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "BlockadeLabs",
"description": "A Non-Official Blockade Labs Rest Client for Unity (UPM)",
"keywords": [],
"version": "1.0.1",
"version": "1.0.2",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.rest.blockadelabs#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.rest.blockadelabs/releases",
Expand Down

0 comments on commit 9341027

Please sign in to comment.