-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop/remove-internal-access' into next
- Loading branch information
Showing
17 changed files
with
316 additions
and
156 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
Packages/ga.fuquna.rosettaui.uitoolkit/Runtime/RosettaUIRootUIToolkit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
Packages/ga.fuquna.rosettaui.uitoolkit/Runtime/VisualElement/FoldoutCustom.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...es/ga.fuquna.rosettaui.uitoolkit/Runtime/VisualElement/Utilities/DropDownMenuGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace RosettaUI.UIToolkit | ||
{ | ||
public static class DropDownMenuGenerator | ||
{ | ||
public static object Generate(IEnumerable<MenuItem> menuItems, Rect position, VisualElement targetElement = null, bool anchored = false) | ||
{ | ||
if (menuItems == null) return null; | ||
|
||
#if UNITY_2023_1_OR_NEWER | ||
// refs: DropdownUtility, EditorDelegateRegistration, GenericOSMenu | ||
var menu = new GenericDropdownMenu(); | ||
#else | ||
// refs: BasePopupField | ||
// https://github.com/Unity-Technologies/UnityCsReference/blob/c84064be69f20dcf21ebe4a7bbc176d48e2f289c/ModuleOverrides/com.unity.ui/Core/Controls/BasePopupField.cs#L206 | ||
var isPlayer = targetElement?.panel.contextType == ContextType.Player; | ||
|
||
GenericDropdownMenuWrapper menu; | ||
if (isPlayer) | ||
{ | ||
var genericMenu = new GenericDropdownMenu(); | ||
menu = new GenericDropdownMenuWrapper(genericMenu); | ||
} | ||
// GenericDropdownMenu だとエディターでエラーになる場合があるのでDropdownUtility経由でOSのメニューを使用する | ||
// RosettaUIEditorWindowExample > MiscExample > UI.Popup() でエラーになる | ||
// at Unity2022.3 | ||
else | ||
{ | ||
// DropdownUtility.CreateDropdown() は internal なのでリフレクションで取得 | ||
#if false | ||
menu = DropdownUtility.CreateDropdown(); | ||
#else | ||
_createDropdownFunc ??= (Func<object>)typeof(VisualElement).Assembly | ||
.GetType("UnityEngine.UIElements.DropdownUtility")? | ||
.GetMethod("CreateDropdown", BindingFlags.NonPublic | BindingFlags.Static)? | ||
.CreateDelegate(typeof(Func<object>)); | ||
|
||
if (_createDropdownFunc == null) | ||
{ | ||
return null; | ||
} | ||
|
||
menu = new GenericDropdownMenuWrapper(_createDropdownFunc()); | ||
#endif | ||
} | ||
|
||
#endif | ||
|
||
foreach (var item in menuItems) | ||
{ | ||
if ( item == MenuItem.Separator) | ||
{ | ||
menu.AddSeparator(""); | ||
continue; | ||
} | ||
|
||
if (item.isEnable) | ||
{ | ||
menu.AddItem(item.name, item.isChecked, item.action); | ||
} | ||
else | ||
{ | ||
menu.AddDisabledItem(item.name, item.isChecked); | ||
} | ||
} | ||
|
||
menu.DropDown(position, targetElement, anchored); | ||
return menu; | ||
} | ||
|
||
#if !UNITY_2023_1_OR_NEWER | ||
private static Func<object> _createDropdownFunc; | ||
|
||
/// <summary> | ||
/// UnityEngine.UIElements.IGenericMenu が internal なので代替インターフェース | ||
/// </summary> | ||
private class GenericDropdownMenuWrapper | ||
{ | ||
private class MethodInfoSet | ||
{ | ||
private static readonly Type[] AddItemTypes = {typeof(string), typeof(bool), typeof(Action)}; | ||
private static readonly Type[] AddDisabledItemTypes = {typeof(string), typeof(bool)}; | ||
private static readonly Type[] AddSeparatorTypes = {typeof(string)}; | ||
private static readonly Type[] DropDownTypes = {typeof(Rect), typeof(VisualElement), typeof(bool)}; | ||
|
||
public readonly MethodInfo addItem; | ||
public readonly MethodInfo addDisabledItem; | ||
public readonly MethodInfo addSeparator; | ||
public readonly MethodInfo dropDown; | ||
|
||
public MethodInfoSet(Type type) | ||
{ | ||
addItem = type.GetMethod("AddItem", AddItemTypes); | ||
addDisabledItem = type.GetMethod("AddDisabledItem", AddDisabledItemTypes); | ||
addSeparator = type.GetMethod("AddSeparator", AddSeparatorTypes); | ||
dropDown = type.GetMethod("DropDown", DropDownTypes); | ||
} | ||
} | ||
|
||
|
||
private static readonly Dictionary<Type, MethodInfoSet> TypeToMethodInfoSet = new(); | ||
|
||
private static MethodInfoSet GetMethodInfoSet(Type type) | ||
{ | ||
if (!TypeToMethodInfoSet.TryGetValue(type, out var methodInfoSet)) | ||
{ | ||
methodInfoSet = new MethodInfoSet(type); | ||
TypeToMethodInfoSet[type] = methodInfoSet; | ||
} | ||
|
||
return methodInfoSet; | ||
} | ||
|
||
|
||
private readonly Action<string, bool, Action> _addItemAction; | ||
private readonly Action<string, bool> _addDisabledItemAction; | ||
private readonly Action<string> _addSeparatorAction; | ||
private readonly Action<Rect, VisualElement, bool> _dropDownAction; | ||
|
||
public GenericDropdownMenuWrapper(object baseObject) | ||
{ | ||
var methodInfoSet = GetMethodInfoSet(baseObject.GetType()); | ||
_addItemAction = (Action<string, bool, Action>)Delegate.CreateDelegate(typeof(Action<string, bool, Action>), baseObject, methodInfoSet.addItem); | ||
_addDisabledItemAction = (Action<string, bool>)Delegate.CreateDelegate(typeof(Action<string, bool>), baseObject, methodInfoSet.addDisabledItem); | ||
_addSeparatorAction = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), baseObject, methodInfoSet.addSeparator); | ||
_dropDownAction = (Action<Rect, VisualElement, bool>)Delegate.CreateDelegate(typeof(Action<Rect, VisualElement, bool>), baseObject, methodInfoSet.dropDown); | ||
} | ||
|
||
public void AddItem(string name, bool isChecked, Action action) => _addItemAction(name, isChecked, action); | ||
public void AddDisabledItem(string name, bool isChecked) => _addDisabledItemAction(name, isChecked); | ||
public void AddSeparator(string path) => _addSeparatorAction(path); | ||
public void DropDown(Rect position, VisualElement targetElement, bool anchored) => _dropDownAction(position, targetElement, anchored); | ||
} | ||
#endif | ||
} | ||
} |
File renamed without changes.
66 changes: 66 additions & 0 deletions
66
....rosettaui.uitoolkit/Runtime/VisualElement/Utilities/GenericDropdownMenuIgnoreAnchored.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace RosettaUI.UIToolkit | ||
{ | ||
/// <summary> | ||
/// Dropdown()の第3引数を無視するGenericDropdownMenu | ||
/// BasePopupFieldがDropdownMenuでanchor == trueで呼び出しているが、強制的falseにする | ||
/// | ||
/// IGenericMenuがinternalなので次のようなクラスを動的に生成する | ||
/// | ||
/// public class GenericDropdownMenuIgnoreAnchored : GenericDropdownMenu, IGenericMenu | ||
/// { | ||
/// void IGenericMenu.DropDown(Rect position, VisualElement targetElement, bool anchored) | ||
/// => base.DropDown(position, targetElement, false); | ||
/// } | ||
/// </summary> | ||
public static class GenericDropdownMenuIgnoreAnchoredBuilder | ||
{ | ||
private static readonly Type GenericDropdownMenuIgnoreAnchoredType; | ||
|
||
public static GenericDropdownMenu CreateGenericDropdownMenuIgnoreAnchored() | ||
{ | ||
return (GenericDropdownMenu)Activator.CreateInstance(GenericDropdownMenuIgnoreAnchoredType); | ||
} | ||
|
||
static GenericDropdownMenuIgnoreAnchoredBuilder() | ||
{ | ||
const string dropdownMethodName = "DropDown"; | ||
|
||
var iGenericMenuType = typeof(GenericDropdownMenu).Assembly.GetType("UnityEngine.UIElements.IGenericMenu"); | ||
var moduleBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("RosettaUI.UIToolkit.DynamicAssembly"), AssemblyBuilderAccess.Run).DefineDynamicModule("RosettaUI.UIToolkit.DynamicModule"); | ||
var typeBuilder = moduleBuilder.DefineType("RosettaUI.UIToolkit.GenericDropdownMenuIgnoreAnchored", TypeAttributes.Public | TypeAttributes.Class, typeof(GenericDropdownMenu), new[]{iGenericMenuType}); | ||
|
||
// インターフェイスの実装 | ||
typeBuilder.AddInterfaceImplementation(iGenericMenuType); | ||
|
||
var dropDownMethodArgumentTypes = new[] { typeof(Rect), typeof(VisualElement), typeof(bool) }; | ||
var methodBuilder = typeBuilder.DefineMethod( | ||
dropdownMethodName, | ||
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig | MethodAttributes.NewSlot, | ||
null, | ||
dropDownMethodArgumentTypes | ||
); | ||
|
||
// IL生成 (メソッドの実装) | ||
var ilGenerator = methodBuilder.GetILGenerator(); | ||
|
||
// 静的メソッドの呼び出しを行うILコードを生成 | ||
ilGenerator.Emit(OpCodes.Ldarg_0); // this | ||
ilGenerator.Emit(OpCodes.Ldarg_1); // Rect position | ||
ilGenerator.Emit(OpCodes.Ldarg_2); // VisualElement targetElement | ||
ilGenerator.Emit(OpCodes.Ldc_I4_0); // bool anchored (false) | ||
ilGenerator.Emit(OpCodes.Call, typeof(GenericDropdownMenu).GetMethod(dropdownMethodName, dropDownMethodArgumentTypes)); | ||
ilGenerator.Emit(OpCodes.Ret); | ||
|
||
// メソッドをインターフェイスのメソッドにマップする | ||
typeBuilder.DefineMethodOverride(methodBuilder, iGenericMenuType.GetMethod(dropdownMethodName)); | ||
|
||
GenericDropdownMenuIgnoreAnchoredType = typeBuilder.CreateType(); | ||
} | ||
} | ||
} |
File renamed without changes.
1 change: 0 additions & 1 deletion
1
Packages/ga.fuquna.rosettaui.uitoolkit/Runtime/VisualElement/Utilities/PopupMenuUtility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
8 changes: 0 additions & 8 deletions
8
Packages/ga.fuquna.rosettaui.uitoolkit/UnityInternalAccess.meta
This file was deleted.
Oops, something went wrong.
62 changes: 0 additions & 62 deletions
62
Packages/ga.fuquna.rosettaui.uitoolkit/UnityInternalAccess/DropDownMenuGenerator.cs
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
...es/ga.fuquna.rosettaui.uitoolkit/UnityInternalAccess/GenericDropdownMenuIgnoreAnchored.cs
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
...es/ga.fuquna.rosettaui.uitoolkit/UnityInternalAccess/RosettaUI.UnityInternalAccess.asmdef
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
....fuquna.rosettaui.uitoolkit/UnityInternalAccess/RosettaUI.UnityInternalAccess.asmdef.meta
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.