Skip to content

Commit

Permalink
Add ButtonAttachComponent, rename some stuff, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytra committed Jul 1, 2024
1 parent d16571c commit 4192136
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using FrooxEngine;
using FrooxEngine.Undo;

namespace Obsidian;

[Category(new string[] { "Obsidian/Common UI/Button Interactions" })]
public class ButtonAttachComponent : Component, IButtonPressReceiver, IComponent, IComponentBase, IDestroyable, IWorker, IWorldElement, IUpdatable, IChangeable, IAudioUpdatable, IInitializable, ILinkable
{
public readonly SyncRef<Slot> TargetSlot;

public readonly SyncType ComponentType;

public readonly Sync<bool> Undoable;

protected override void OnAttach()
{
base.OnAwake();
Undoable.Value = true;
}

public void Pressed(IButton button, ButtonEventData eventData)
{
Slot target = TargetSlot.Target;
Type componentType = ComponentType.Value;
if (target != null && componentType != null && componentType.ContainsGenericParameters == false)
{
var comp = target.AttachComponent(componentType);
if (Undoable)
{
comp.CreateSpawnUndoPoint();
}
}
}

public void Pressing(IButton button, ButtonEventData eventData)
{
}

public void Released(IButton button, ButtonEventData eventData)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

namespace Obsidian;

// This feed has two functions.

// If TargetSlot has a reference, it returns the components on that slot, optionally also returning components on children slots (IncludeChildrenSlots bool)
// It also returns the sync members (fields, lists etc) as DataFeedEntity<ISyncMember>

// If TargetSlot is null, it returns the components from the component library, which includes the categories (DataFeedCategoryItem)
// When enumerating the component library, the Component reference on the ComponentDataItemInterface will be null and there will be no members

[Category(new string[] { "Obsidian/Radiant UI/Data Feeds/Feeds" })]
public class ComponentsDataFeed : Component, IDataFeedComponent, IDataFeed, IWorldElement
{
Expand All @@ -27,7 +35,7 @@ private static bool SearchStringValid(string str)
return !string.IsNullOrWhiteSpace(str) && str.Length >= 3;
}

private void AddComponent(Component c)
private void OnSlotComponentAdded(Component c)
{
// If local elements are written to synced fields it can cause exceptions and crashes
if (c.IsLocalElement) return;
Expand All @@ -45,7 +53,7 @@ private void AddComponent(Component c)
}
}

private void RemoveComponent(Component c)
private void OnSlotComponentRemoved(Component c)
{
foreach (KeyValuePair<SearchPhraseFeedUpdateHandler, ComponentsDataFeedData> updateHandler in _updateHandlers)
{
Expand Down Expand Up @@ -102,14 +110,14 @@ private void ProcessUpdate(SearchPhraseFeedUpdateHandler handler, ComponentData

private void Subscribe(Slot s)
{
s.ComponentAdded += AddComponent;
s.ComponentRemoved += RemoveComponent;
s.ComponentAdded += OnSlotComponentAdded;
s.ComponentRemoved += OnSlotComponentRemoved;
}

private void Unsubscribe(Slot s)
{
s.ComponentAdded -= AddComponent;
s.ComponentRemoved -= RemoveComponent;
s.ComponentAdded -= OnSlotComponentAdded;
s.ComponentRemoved -= OnSlotComponentRemoved;
}

protected override void OnAwake()
Expand Down Expand Up @@ -205,10 +213,16 @@ private IEnumerable<Type> EnumerateAllTypes(CategoryNode<Type> categoryNode)
}
}

private string GetCategoryKey(CategoryNode<Type> categoryNode)
{
return categoryNode.Name;
}

private DataFeedCategory GenerateCategory(string key, IReadOnlyList<string> path)
{
DataFeedCategory dataFeedCategory = new DataFeedCategory();
dataFeedCategory.InitBase(key, path, null, ("Category." + key).AsLocaleKey(), OfficialAssets.Graphics.Icons.Gizmo.TransformLocal);
// random icon
dataFeedCategory.InitBase(key, path, null, key, OfficialAssets.Graphics.Icons.Gizmo.TransformLocal);
return dataFeedCategory;
}

Expand All @@ -218,10 +232,6 @@ public async IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path
{
yield break;
}
//if (TargetSlot.Target == null && (path == null || path.Count == 0) && !SearchStringValid(searchPhrase))
//{
// yield break;
//}
if (groupKeys != null && groupKeys.Count > 0)
{
yield break;
Expand Down Expand Up @@ -251,7 +261,7 @@ public async IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path
}
foreach (var subCat2 in catNode.Subcategories)
{
yield return GenerateCategory(subCat2.Name, path);
yield return GenerateCategory(GetCategoryKey(subCat2), path);
}
if (SearchStringValid(searchPhrase))
{
Expand All @@ -276,7 +286,7 @@ public async IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path
}
foreach (var subCat in lib.Subcategories)
{
yield return GenerateCategory(subCat.Name, path);
yield return GenerateCategory(GetCategoryKey(subCat), path);
}
if (SearchStringValid(searchPhrase))
{
Expand Down

0 comments on commit 4192136

Please sign in to comment.