Skip to content

Commit

Permalink
feat!(selection): selection now indexes based on ID (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-Morgan authored Nov 20, 2023
1 parent bb43ba5 commit d158bbd
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 147 deletions.
18 changes: 7 additions & 11 deletions Assets/Tests/PlayMode/ConvertToNativeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
Expand All @@ -9,7 +7,6 @@
using Speckle.Core.Api;
using Speckle.Core.Models;
using UnityEngine;
using UnityEngine.TestTools;

namespace Speckle.ConnectorUnity.Tests
{
Expand All @@ -25,7 +22,7 @@ private static Base Receive(string stream)
{
return Task.Run(async () => await Helpers.Receive(stream)).Result;
}

[Test, TestCaseSource(nameof(TestCases))]
public void ToNative_Passes(string stream)
{
Expand All @@ -37,14 +34,13 @@ public void ToNative_Passes(string stream)
Assert.That(results, HasSomeComponent<SpeckleProperties>());
}

private static Constraint HasSomeComponent<T>() where T : Component
private static Constraint HasSomeComponent<T>()
where T : Component
{
return Has.Some.Matches<ConversionResult>(
x =>
{
return x.WasSuccessful(out var success, out _)
&& success.GetComponent<T>();
});
return Has.Some.Matches<ConversionResult>(x =>
{
return x.WasSuccessful(out var success, out _) && success.GetComponent<T>();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Speckle.ConnectorUnity.Components.Editor
[CustomEditor(typeof(SpeckleReceiver))]
public class SpeckleReceiverEditor : UnityEditor.Editor
{
private static bool _generateAssets = false;
private static bool _generateAssets;
private bool _foldOutStatus = true;
private Texture2D? _previewImage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ private bool WriteObject(Base speckleObject, Object nativeObject)
return _readCache.TrySaveObject(speckleObject, nativeObject);
}

public override void BeginWrite()
{
base.BeginWrite();
//AssetDatabase.StartAssetEditing();
}

public override void FinishWrite()
{
if (!isWriting)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using Speckle.Core.Api;
using Speckle.Core.Credentials;
using UnityEditor;
Expand Down Expand Up @@ -49,8 +50,8 @@ public StreamSelectionDrawer()
("Description", s => s.description),
("Is Public", s => s.isPublic.ToString()),
("Role", s => s.role),
("Created at", s => s.createdAt.ToString()),
("Updated at", s => s.updatedAt.ToString()),
("Created at", s => s.createdAt.ToString(CultureInfo.InvariantCulture)),
("Updated at", s => s.updatedAt.ToString(CultureInfo.InvariantCulture)),
};
}
}
Expand All @@ -70,7 +71,11 @@ public BranchSelectionDrawer()
$"<{nameof(BranchSelection.CommitsLimit)}>k__BackingField",
};

details = new (string, Func<Branch, string>)[] { ("Description", s => s.description), };
details = new (string, Func<Branch, string>)[]
{
("Model Id", s => s.id),
("Description", s => s.description),
};
}
}

Expand All @@ -85,7 +90,7 @@ public CommitSelectionDrawer()
{
("Commit Id", s => s.id),
("Author Name", s => s.authorName),
("Created At", s => s.createdAt.ToString()),
("Created At", s => s.createdAt.ToString(CultureInfo.InvariantCulture)),
("Source Application", s => s.sourceApplication),
("Reference Object Id", s => s.referencedObject),
};
Expand All @@ -108,9 +113,9 @@ public abstract class OptionSelectionDrawer<TOption> : PropertyDrawer

protected (string, Func<TOption, string>)[] details = { };

private string[] GetFormattedOptions(TOption[] options)
private string[] GetFormattedOptions(IReadOnlyList<TOption> options)
{
int optionsCount = options.Length;
int optionsCount = options.Count;
string[] choices = new string[optionsCount];
for (int i = 0; i < optionsCount; i++)
{
Expand Down Expand Up @@ -182,10 +187,10 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
var provider = ScriptableObject.CreateInstance<StringListSearchProvider>();
provider.Title = typeof(TOption).Name;
provider.listItems = GetFormattedOptions(t.Options);
;

provider.onSetIndexCallback = o =>
{
t.SelectedIndex = o;
t.Selected = t.Options[o];
};
SearchWindow.Open(new SearchWindowContext(windowPos), provider);
}
Expand Down Expand Up @@ -260,7 +265,7 @@ public sealed class StringListSearchProvider : ScriptableObject, ISearchWindowPr
public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
{
List<SearchTreeEntry> searchList =
new(listItems.Length + 1) { new SearchTreeGroupEntry(new GUIContent(Title), 0) };
new(listItems.Length + 1) { new SearchTreeGroupEntry(new GUIContent(Title)) };

for (int i = 0; i < listItems.Length; i++)
{
Expand All @@ -275,9 +280,9 @@ public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
return searchList;
}

public bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context)
public bool OnSelectEntry(SearchTreeEntry searchTreeEntry, SearchWindowContext context)
{
onSetIndexCallback?.Invoke((int)SearchTreeEntry.userData);
onSetIndexCallback?.Invoke((int)searchTreeEntry.userData);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ public class SpecklePropertiesEditor : UnityEditor.Editor
{
private static readonly string[] SpeckleTypeOptionStrings;
private static readonly Type[] SpeckleTypeOptions;

private static HashSet<string> ArrayFoldoutState = new();
private static bool instancePropFoldoutState = true;
private static bool dynamicPropFoldoutState = true;
private static bool isEditMode = false;
private static bool isEditMode;

static SpecklePropertiesEditor()
{
var options = typeof(Mesh).Assembly
.GetTypes()
.Where(x => x.IsSubclassOf(typeof(Base)) && !x.IsAbstract).ToList();
.Where(x => x.IsSubclassOf(typeof(Base)) && !x.IsAbstract)
.ToList();

var strings = options
.Where(x => x.FullName != null)
.Select(x => x.FullName!.Replace('.', '/'));
var manualTypes = new [] { typeof(Base), typeof(Collection)};
var manualStrings = new []{ nameof(Base), nameof(Collection)};

var manualTypes = new[] { typeof(Base), typeof(Collection) };
var manualStrings = new[] { nameof(Base), nameof(Collection) };

//Manually Add `Base`
SpeckleTypeOptions = options.Concat(manualTypes).ToArray();
SpeckleTypeOptionStrings = strings.Concat(manualStrings).ToArray();
Expand All @@ -45,64 +46,82 @@ static SpecklePropertiesEditor()
}

private static GUILayoutOption[] propLayoutOptions = { GUILayout.ExpandWidth(true) };

public override void OnInspectorGUI()
{
SpeckleProperties properties = (SpeckleProperties)target;

//Edit Mode
isEditMode = EditorGUILayout.ToggleLeft("Enable Inspector Edit Mode (experimental)", isEditMode);
isEditMode = EditorGUILayout.ToggleLeft(
"Enable Inspector Edit Mode (experimental)",
isEditMode
);

if (isEditMode)
{
GUILayout.Label(
"Modifying properties through the inspector is experimental and can lead to invalid objects, proceed at your own risk!",
EditorStyles.helpBox);
EditorStyles.helpBox
);
GUILayout.Space(10);
}
GUI.enabled = isEditMode;

// SpeckleType
GUILayout.Label("Speckle Type: ", EditorStyles.boldLabel );
GUILayout.Label("Speckle Type: ", EditorStyles.boldLabel);

var oldIndex = Array.IndexOf(SpeckleTypeOptions, properties.SpeckleType);
var speckleTypeSelectedIndex = EditorGUILayout.Popup(oldIndex, SpeckleTypeOptionStrings);

if(oldIndex != speckleTypeSelectedIndex && speckleTypeSelectedIndex >= 0)
var speckleTypeSelectedIndex = EditorGUILayout.Popup(
oldIndex,
SpeckleTypeOptionStrings
);

if (oldIndex != speckleTypeSelectedIndex && speckleTypeSelectedIndex >= 0)
{
properties.SpeckleType = SpeckleTypeOptions[speckleTypeSelectedIndex];
}

// Instance Properties
var InstancePropertyNames = DynamicBase.GetInstanceMembersNames(properties.SpeckleType);
instancePropFoldoutState = EditorGUILayout.Foldout(instancePropFoldoutState, "Instance Properties: ", EditorStyles.foldoutHeader);
var instancePropertyNames =
(IReadOnlyCollection<string>)
DynamicBase.GetInstanceMembersNames(properties.SpeckleType);
instancePropFoldoutState = EditorGUILayout.Foldout(
instancePropFoldoutState,
"Instance Properties: ",
EditorStyles.foldoutHeader
);
if (instancePropFoldoutState)
{
foreach (var propName in InstancePropertyNames)
foreach (var propName in instancePropertyNames)
{
if (!properties.Data.TryGetValue(propName, out object? existingValue)) continue;

if (!properties.Data.TryGetValue(propName, out object? existingValue))
continue;

var newValue = CreateField(existingValue, propName, propLayoutOptions);
if(newValue != existingValue)
if (newValue != existingValue)
properties.Data[propName] = newValue;

}
}

GUILayout.Space(10);
dynamicPropFoldoutState = EditorGUILayout.Foldout(dynamicPropFoldoutState, "Dynamic Properties:", EditorStyles.foldoutHeader);
dynamicPropFoldoutState = EditorGUILayout.Foldout(
dynamicPropFoldoutState,
"Dynamic Properties:",
EditorStyles.foldoutHeader
);
if (dynamicPropFoldoutState)
{
var ignoreSet = InstancePropertyNames.ToImmutableHashSet();
var ignoreSet = instancePropertyNames.ToImmutableHashSet();
foreach (var kvp in properties.Data)
{
if (ignoreSet.Contains(kvp.Key)) continue;

if (ignoreSet.Contains(kvp.Key))
continue;

var existingValue = kvp.Value;
var newValue = CreateField(existingValue, kvp.Key, propLayoutOptions);
if(newValue != existingValue)
if (newValue != existingValue)
properties.Data[kvp.Key] = newValue;

GUILayout.Space(10);
}
}
Expand All @@ -118,13 +137,18 @@ public override void OnInspectorGUI()
_ => CreateFieldPrimitive(v, propName, options),
};

if (ret != null) return ret;

EditorGUILayout.TextField(propName, v == null? "NULL" : v.ToString());
if (ret != null)
return ret;

EditorGUILayout.TextField(propName, v == null ? "NULL" : v.ToString());
return v;
}

private static object? CreateFieldPrimitive(object? v, string propName, params GUILayoutOption[] options)

private static object? CreateFieldPrimitive(
object? v,
string propName,
params GUILayoutOption[] options
)
{
return v switch
{
Expand All @@ -135,33 +159,47 @@ public override void OnInspectorGUI()
string s => EditorGUILayout.TextField(propName, s, options),
bool b => EditorGUILayout.Toggle(propName, b, options),
Enum e => EditorGUILayout.EnumPopup(propName, e, options),
Point p => PointToVector3(EditorGUILayout.Vector3Field(propName, new Vector3((float)p.x, (float)p.z, (float)p.z), options), p),
Point p
=> PointToVector3(
EditorGUILayout.Vector3Field(
propName,
new Vector3((float)p.x, (float)p.z, (float)p.z),
options
),
p
),
_ => null,
};
}

private static Point PointToVector3(Vector3 vector, Point p)
{
p.x = vector.x;
p.y = vector.y;
p.z = vector.z;
return p;
}

private IList ArrayField(string propName, IList list, params GUILayoutOption[] options)
{
bool isExpanded = EditorGUILayout.Foldout(ArrayFoldoutState.Contains(propName), propName);
bool isExpanded = EditorGUILayout.Foldout(
ArrayFoldoutState.Contains(propName),
propName
);
if (isExpanded)
{
ArrayFoldoutState.Add(propName);
for (int i = 0; i < list.Count; i++)
{
object? item = list[i];
var r = CreateFieldPrimitive(item, i.ToString(), options);

if (r == null)
{
EditorGUILayout.TextField(i.ToString(), item == null? "NULL" : item.ToString());
EditorGUILayout.TextField(
i.ToString(),
item == null ? "NULL" : item.ToString()
);
continue;
}
//Update list item
Expand All @@ -175,6 +213,5 @@ private IList ArrayField(string propName, IList list, params GUILayoutOption[] o

return list;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ protected void Initialise(bool forceRefresh = false)
Branch.Initialise();
Commit.Initialise();
Commit.OnSelectionChange = () => OnCommitSelectionChange?.Invoke(Commit.Selected);
if (Account.Options is not { Length: > 0 } || forceRefresh)
if (Account.Options is not { Count: > 0 } || forceRefresh)
Account.RefreshOptions();
}

Expand All @@ -540,7 +540,7 @@ public void OnAfterDeserialize()

#region Deprecated members

[Obsolete("use " + nameof(ReceiveAndConvertRoutine), true)]
[Obsolete("use " + nameof(ReceiveAndConvert_Routine), true)]
public IEnumerator ReceiveAndConvertRoutine(
SpeckleReceiver speckleReceiver,
string rootObjectName,
Expand Down
Loading

0 comments on commit d158bbd

Please sign in to comment.