Skip to content

Commit

Permalink
Add ability to mirror menu items across providers
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
Varneon committed Apr 22, 2024
1 parent 035b673 commit ab07c72
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ private static void PostProcessMenuProvider(MenuProvider menuProvider, Immutable
foreach (MenuItemInfo menuItem in menuItems)
{
TryRegisterMenuItem(menuProvider, menuItem);

if (menuItem.MirrorMenu && !string.IsNullOrWhiteSpace(menuItem.MirrorPath))
{
menuProvider.TryRegisterMirror(menuItem.Path, menuItem.MirrorMenu, menuItem.MirrorPath);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ internal class Item : IComparable<Item>
[SerializeField]
internal MenuEventCallbackReceiver CallbackReceiver;

[SerializeField]
internal MenuProvider MirrorMenu;

[SerializeField]
internal string MirrorPath;

[SerializeField]
internal MenuItemType ItemType;

Expand Down Expand Up @@ -132,14 +138,23 @@ private static GUIContent GetItemHeaderContent(MenuItemType type, string name)

public MenuItemInfo GetInfo()
{
MenuItemInfo info;

switch (ItemType)
{
case MenuItemType.Button: return new MenuButtonItemInfo(Path, CallbackReceiver, Tooltip, Priority, Enabled, PlatformFlags);
case MenuItemType.Toggle: return new MenuToggleItemInfo(Path, CallbackReceiver, DefaultBoolean, OffLabel, OnLabel, Tooltip, Priority, Enabled, PlatformFlags);
case MenuItemType.Option: return new MenuOptionItemInfo(Path, CallbackReceiver, Options, DefaultOption, Tooltip, Priority, Enabled, PlatformFlags);
case MenuItemType.Slider: return new MenuSliderItemInfo(Path, CallbackReceiver, DefaultFloat, MinValue, MaxValue, Steps, Unit, Tooltip, Priority, Enabled, PlatformFlags);
case MenuItemType.Button: info = new MenuButtonItemInfo(Path, CallbackReceiver, Tooltip, Priority, Enabled, PlatformFlags); break;
case MenuItemType.Toggle: info = new MenuToggleItemInfo(Path, CallbackReceiver, DefaultBoolean, OffLabel, OnLabel, Tooltip, Priority, Enabled, PlatformFlags); break;
case MenuItemType.Option: info = new MenuOptionItemInfo(Path, CallbackReceiver, Options, DefaultOption, Tooltip, Priority, Enabled, PlatformFlags); break;
case MenuItemType.Slider: info = new MenuSliderItemInfo(Path, CallbackReceiver, DefaultFloat, MinValue, MaxValue, Steps, Unit, Tooltip, Priority, Enabled, PlatformFlags); break;
default: throw new NotImplementedException();
}

if(MirrorMenu && !string.IsNullOrWhiteSpace(MirrorPath))
{
info.RegisterMirror(MirrorMenu, MirrorPath);
}

return info;
}

public int CompareTo(Item other)
Expand Down Expand Up @@ -180,6 +195,16 @@ internal bool DrawInspectorPanel(out bool removed)

CallbackReceiver = (MenuEventCallbackReceiver)UnityEditor.EditorGUILayout.ObjectField("Callback Receiver", CallbackReceiver, typeof(MenuEventCallbackReceiver), true);

if (CallbackReceiver)
{
MirrorMenu = (MenuProvider)UnityEditor.EditorGUILayout.ObjectField("Mirror Menu", MirrorMenu, typeof(MenuProvider), true);

if (MirrorMenu)
{
MirrorPath = UnityEditor.EditorGUILayout.TextField("Mirror Path", MirrorPath);
}
}

Priority = UnityEditor.EditorGUILayout.DelayedIntField("Priority", Priority);

Enabled = UnityEditor.EditorGUILayout.Toggle("Enabled", Enabled);
Expand Down
12 changes: 12 additions & 0 deletions Packages/com.varneon.vudon.menus/Runtime/Scripts/MenuItemInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using Varneon.VUdon.Common.VRCEnums;
using Varneon.VUdon.Menus.Enums;

Expand All @@ -10,6 +11,10 @@ public abstract class MenuItemInfo : IComparable<MenuItemInfo>

public MenuEventCallbackReceiver CallbackReceiver;

public MenuProvider MirrorMenu;

public string MirrorPath;

public string Tooltip;

public int Priority = 0;
Expand All @@ -28,5 +33,12 @@ public int CompareTo(MenuItemInfo other)

return delta == 0 ? -1 : delta;
}

public void RegisterMirror(MenuProvider menu, string path)
{
MirrorMenu = menu;

MirrorPath = path;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ protected const string
public virtual bool TryRegisterSlider(string path, MenuEventCallbackReceiver callbackReceiver, float defaultValue, float minValue = DEFAULT_MIN_FLOAT, float maxValue = DEFAULT_MAX_FLOAT, int steps = DEFAULT_STEPS, string unit = DEFAULT_UNIT, string tooltip = DEFAULT_TOOLTIP) => TryRegisterSlider(path, callbackReceiver, defaultValue, minValue, maxValue, steps, unit, tooltip, true);
public virtual bool TryRegisterSlider(string path, MenuEventCallbackReceiver callbackReceiver, float defaultValue, float minValue = DEFAULT_MIN_FLOAT, float maxValue = DEFAULT_MAX_FLOAT, int steps = DEFAULT_STEPS, string unit = DEFAULT_UNIT, string tooltip = DEFAULT_TOOLTIP, bool enabled = true) { return false; }

public virtual bool TryRegisterMirror(string path, MenuProvider mirrorMenu, string mirrorPath) { return false; }

public virtual bool TrySetItemEnabled(string path, bool enabled, MenuEventCallbackReceiver callbackReceiver = null) { return false; }

public virtual bool TryRemoveItem(string path, MenuEventCallbackReceiver callbackReceiver = null) { return false; }
Expand Down

0 comments on commit ab07c72

Please sign in to comment.