Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsValidGenericTypeDriver, StringToTypeDriver, TypeFeedItem and TypeItemInterface, rework ComponentsDataFeed to use those for the component library #40

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Elements.Core;
using FrooxEngine;
using System;

namespace Obsidian;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void OnSlotComponentRemoved(Component c)
{
var result = updateHandler.Value.RemoveComponent(c);
result.data.ClearSubmitted();
updateHandler.Key.handler(ToItem(result.data), DataFeedItemChange.Removed);
updateHandler.Key.handler(GenerateComponent(result.data), DataFeedItemChange.Removed);
}
}

Expand Down Expand Up @@ -99,12 +99,12 @@ private void ProcessUpdate(SearchPhraseFeedUpdateHandler handler, ComponentData
if (data.Submitted)
{
data.ClearSubmitted();
handler.handler(ToItem(data), DataFeedItemChange.Removed);
handler.handler(GenerateComponent(data), DataFeedItemChange.Removed);
}
return;
}
data.MarkSubmitted();
DataFeedItem item = ToItem(data);
DataFeedItem item = GenerateComponent(data);
handler.handler(item, DataFeedItemChange.Added);
}

Expand All @@ -120,10 +120,18 @@ private void Unsubscribe(Slot s)
s.ComponentRemoved -= OnSlotComponentRemoved;
}

protected override void OnAwake()
protected override void OnStart()
{
base.OnAwake();
base.OnStart();
_lastSlot = TargetSlot.Target;
if (_lastSlot != null)
{
Subscribe(_lastSlot);
if (IncludeChildrenSlots)
{
_lastSlot.ForeachChild(childSlot => Subscribe(childSlot));
}
}
}

protected override void OnChanges()
Expand Down Expand Up @@ -226,6 +234,14 @@ private DataFeedCategory GenerateCategory(string key, IReadOnlyList<string> path
return dataFeedCategory;
}

private TypeFeedItem GenerateType(Type type, string key, IReadOnlyList<string> path)
{
TypeFeedItem typeFeedItem = new TypeFeedItem(type);
// random icon
typeFeedItem.InitBase(key, path, null, type.GetNiceName(), OfficialAssets.Graphics.Icons.Gizmo.TransformLocal);
return typeFeedItem;
}

public async IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path, IReadOnlyList<string> groupKeys, string searchPhrase, object viewData)
{
if (TargetSlot.Target != null && (path != null && path.Count > 0))
Expand Down Expand Up @@ -324,15 +340,22 @@ public async IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path
if (componentData.MatchesSearchParameters(optionalTerms, requiredTerms, excludedTerms))
{
componentData.MarkSubmitted();
yield return ToItem(componentData);
if (TargetSlot.Target != null)
{
yield return GenerateComponent(componentData);
}
else
{
yield return GenerateType(componentData.ComponentType, componentData.ComponentType.GetHashCode().ToString(), path);
}
}
}
Pool.Return(ref optionalTerms);
Pool.Return(ref requiredTerms);
Pool.Return(ref excludedTerms);
}

private DataFeedItem ToItem(ComponentData data)
private ComponentDataFeedItem GenerateComponent(ComponentData data)
{
return new ComponentDataFeedItem(data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Elements.Core;
using FrooxEngine;
using System;

namespace Obsidian;

public class TypeFeedItem : DataFeedItem
{
public Type Type;

public bool IsGenericType;

public Type GenericTypeDefinition;

public SlimList<TypeFeedItem> GenericTypes;

public int GenericTypesCount;

public TypeFeedItem(Type type)
{
InitBase(type.GetHashCode().ToString(), null, null, type.GetNiceName());
Type = type;
IsGenericType = type.IsGenericType;
GenericTypeDefinition = IsGenericType ? type.GetGenericTypeDefinition() : null;
int count = 0;
if (type.IsGenericTypeDefinition)
{
foreach (Type genericType in WorkerInitializer.GetCommonGenericTypes(type))
{
TypeFeedItem typeFeedItem = new TypeFeedItem(genericType);
typeFeedItem.InitBase(genericType.GetHashCode().ToString(), null, null, genericType.GetNiceName());
GenericTypes.Add(typeFeedItem);
count++;
}
}
GenericTypesCount = count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using FrooxEngine;

namespace Obsidian;

[Category(new string[] { "Obsidian/Radiant UI/Data Feeds/Interfaces" })]
public class TypeItemInterface : FeedItemInterface
{
public readonly SyncRef<IField<System.Type>> Type;

public readonly SyncRef<IField<bool>> IsGenericType;

public readonly SyncRef<IField<System.Type>> GenericTypeDefinition;

public readonly SyncRef<IField<int>> GenericTypesCount;

public readonly FeedSubTemplate<TypeFeedItem, TypeItemInterface> GenericTypes;

public override void Set(IDataFeedView view, DataFeedItem item)
{
base.Set(view, item);
if (item is TypeFeedItem typeFeedItem)
{
Type.TrySetTarget(typeFeedItem.Type);
IsGenericType.TrySetTarget(typeFeedItem.IsGenericType);
GenericTypeDefinition.TrySetTarget(typeFeedItem.GenericTypeDefinition);
GenericTypesCount.TrySetTarget(typeFeedItem.GenericTypesCount);
GenericTypes.Set(typeFeedItem.GenericTypes, view);
}
}
}
28 changes: 28 additions & 0 deletions ProjectObsidian/Components/Utility/IsValidGenericTypeDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Elements.Core;
using FrooxEngine;
using System;
using System.Linq;

namespace Obsidian;

[Category(new string[] { "Obsidian/Utility" })]
public class IsValidGenericTypeDriver : Component
{
public readonly SyncType Type;

public readonly FieldDrive<bool> Target;

protected override void OnChanges()
{
base.OnChanges();
if (!Target.IsLinkValid) return;
if (Type.Value == null || !Type.Value.IsGenericType)
{
Target.Target.Value = false;
}
else
{
Target.Target.Value = Type.Value.IsValidGenericType(validForInstantiation: true);
}
}
}
36 changes: 36 additions & 0 deletions ProjectObsidian/Components/Utility/StringToTypeDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Elements.Core;
using FrooxEngine;
using System;

namespace Obsidian;

[Category(new string[] { "Obsidian/Utility" })]
public class StringToTypeDriver : Component
{
public readonly Sync<string> Text;

public readonly FieldDrive<Type> Target;

protected override void OnChanges()
{
base.OnChanges();
if (!Target.IsLinkValid) return;
if (string.IsNullOrWhiteSpace(Text.Value))
{
Target.Target.Value = null;
}
else
{
try
{
var parsedType = WorkerManager.ParseNiceType(Text.Value);
Target.Target.Value = parsedType;
}
catch (Exception ex)
{
UniLog.Warning("Exception when parsing type from string:\n" + ex.ToString());
Target.Target.Value = null;
}
}
}
}