Skip to content

Commit ef84a26

Browse files
committed
fix: input hotkey adjustments
1 parent 765b2bc commit ef84a26

File tree

3 files changed

+199
-24
lines changed

3 files changed

+199
-24
lines changed

src/XUnity.AutoTranslator.Plugin.Core/UI/TranslationAggregatorOptionsWindow.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,48 +66,51 @@ private void CreateWindowUI( int id )
6666
{
6767
AutoTranslationPlugin.Current.DisableAutoTranslator();
6868

69+
float posy = GUIUtil.WindowTitleClearance + GUIUtil.ComponentSpacing;
70+
6971
if( GUI.Button( GUIUtil.R( WindowWidth - 22, 2, 20, 16 ), "X" ) )
7072
{
7173
IsShown = false;
7274
}
7375

74-
GUILayout.Label( "Available Translators" );
76+
GUI.Label( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, WindowWidth - GUIUtil.ComponentSpacing, GUIUtil.LabelHeight ), "Available Translators" );
77+
posy += GUIUtil.LabelHeight + GUIUtil.HalfComponentSpacing;
7578

76-
// GROUP
77-
_scrollPosition = GUILayout.BeginScrollView( _scrollPosition, GUI.skin.box );
79+
GUI.Box( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, WindowWidth - GUIUtil.ComponentSpacing, _toggles.Count * GUIUtil.RowHeight + GUIUtil.ComponentSpacing ), "" );
80+
posy += GUIUtil.HalfComponentSpacing;
7881

7982
foreach( var vm in _toggles )
8083
{
8184
var previousEnabled = GUI.enabled;
8285

8386
GUI.enabled = vm.Enabled;
8487
var previousValue = vm.IsToggled();
85-
var newValue = GUILayout.Toggle( previousValue, vm.Text );
88+
var newValue = GUI.Toggle( GUIUtil.R( GUIUtil.ComponentSpacing, posy, WindowWidth - GUIUtil.ComponentSpacing * 2, GUIUtil.RowHeight ), previousValue, vm.Text );
8689
if( previousValue != newValue )
8790
{
8891
vm.OnToggled();
8992
}
9093

9194
GUI.enabled = previousEnabled;
95+
posy += GUIUtil.RowHeight;
9296
}
9397

94-
GUILayout.EndScrollView();
98+
posy += GUIUtil.HalfComponentSpacing + GUIUtil.ComponentSpacing;
99+
100+
GUI.Label( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, 60, GUIUtil.LabelHeight ), "Height" );
101+
_viewModel.Height = Mathf.Round( GUI.HorizontalSlider( GUIUtil.R( GUIUtil.HalfComponentSpacing + 60, posy + 5, WindowWidth - GUIUtil.ComponentSpacing * 2 - 60, GUIUtil.LabelHeight ), _viewModel.Height, 50, 300 ) );
102+
posy += GUIUtil.LabelHeight + GUIUtil.ComponentSpacing;
95103

96-
GUILayout.BeginHorizontal();
97-
GUILayout.Label( "Height" );
98-
_viewModel.Height = Mathf.Round( GUILayout.HorizontalSlider( _viewModel.Height, 50, 300, GUILayout.MaxWidth( 250 ) ) );
99-
GUILayout.EndHorizontal();
104+
GUI.Label( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, 60, GUIUtil.LabelHeight ), "Width" );
105+
_viewModel.Width = Mathf.Round( GUI.HorizontalSlider( GUIUtil.R( GUIUtil.HalfComponentSpacing + 60, posy + 5, WindowWidth - GUIUtil.ComponentSpacing * 2 - 60, GUIUtil.LabelHeight ), _viewModel.Width, 200, 1000 ) );
106+
posy += GUIUtil.LabelHeight + GUIUtil.ComponentSpacing;
100107

101-
GUILayout.BeginHorizontal();
102-
GUILayout.Label( "Width" );
103-
_viewModel.Width = Mathf.Round( GUILayout.HorizontalSlider( _viewModel.Width, 200, 1000, GUILayout.MaxWidth( 250 ) ) );
104-
GUILayout.EndHorizontal();
108+
_windowRect.height = posy + GUIUtil.ComponentSpacing;
105109

106110
GUI.DragWindow();
107111
}
108112
finally
109113
{
110-
111114
AutoTranslationPlugin.Current.EnableAutoTranslator();
112115
}
113116
}

src/XUnity.AutoTranslator.Plugin.Core/UI/TranslationAggregatorWindow.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ private void CreateWindowUI( int id )
199199
}
200200
}
201201

202+
private bool? _isScrollViewSupported;
203+
202204
private void DrawTextArea( float posy, ScrollPositioned positioned, string title, IEnumerable<string> texts )
203205
{
204206
GUI.Label( GUIUtil.R( GUIUtil.HalfComponentSpacing + 5, posy + 5, _viewModel.Width - GUIUtil.ComponentSpacing, GUIUtil.LabelHeight ), title );
@@ -207,15 +209,39 @@ private void DrawTextArea( float posy, ScrollPositioned positioned, string title
207209

208210
float boxWidth = _viewModel.Width - GUIUtil.ComponentSpacing;
209211
float boxHeight = _viewModel.Height - GUIUtil.LabelHeight;
210-
GUILayout.BeginArea( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, boxWidth, boxHeight ) );
211-
positioned.ScrollPosition = GUILayout.BeginScrollView( positioned.ScrollPosition, GUI.skin.box );
212+
GUILayout.BeginArea( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, boxWidth, boxHeight ), GUI.skin.box );
212213

213-
foreach( var text in texts )
214+
if( _isScrollViewSupported == false )
215+
{
216+
foreach( var text in texts )
217+
{
218+
GUILayout.Label( text, GUIUtil.LabelTranslation, ArrayHelper.Null<GUILayoutOption>() );
219+
}
220+
}
221+
else
214222
{
215-
GUILayout.Label( text, GUIUtil.LabelTranslation, ArrayHelper.Null<GUILayoutOption>() );
223+
try
224+
{
225+
positioned.ScrollPosition = GUILayout.BeginScrollView( positioned.ScrollPosition, ArrayHelper.Null<GUILayoutOption>() );
226+
_isScrollViewSupported = true;
227+
228+
foreach( var text in texts )
229+
{
230+
GUILayout.Label( text, GUIUtil.LabelTranslation, ArrayHelper.Null<GUILayoutOption>() );
231+
}
232+
233+
GUILayout.EndScrollView();
234+
}
235+
catch( Exception )
236+
{
237+
_isScrollViewSupported = false;
238+
foreach( var text in texts )
239+
{
240+
GUILayout.Label( text, GUIUtil.LabelTranslation, ArrayHelper.Null<GUILayoutOption>() );
241+
}
242+
}
216243
}
217244

218-
GUILayout.EndScrollView();
219245
GUILayout.EndArea();
220246
}
221247
}

src/XUnity.Common/Utilities/UnityInput.cs

Lines changed: 151 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ public static IInputSystem Current
2929
{
3030
try
3131
{
32-
try
33-
{
34-
_current = new LegacyInputSystem();
35-
XuaLogger.AutoTranslator.Debug( "[UnityInput] Using LegacyInputSystem" );
36-
}
32+
try
33+
{
34+
#if IL2CPP
35+
_current = new Il2CppLegacyInputSystem();
36+
XuaLogger.AutoTranslator.Debug( "[UnityInput] Using Il2CppLegacyInputSystem" );
37+
#else
38+
_current = new LegacyInputSystem();
39+
XuaLogger.AutoTranslator.Debug( "[UnityInput] Using LegacyInputSystem" );
40+
#endif
41+
}
3742
catch
3843
{
3944
var newInputSystem = new NewInputSystem();
@@ -54,7 +59,11 @@ public static IInputSystem Current
5459
/// <summary>
5560
/// True if the Input class is not disabled.
5661
/// </summary>
62+
#if IL2CPP
63+
public bool LegacyInputSystemAvailable => Current is Il2CppLegacyInputSystem;
64+
#else
5765
public bool LegacyInputSystemAvailable => Current is LegacyInputSystem;
66+
#endif
5867
}
5968

6069
/// <summary>
@@ -894,4 +903,141 @@ internal class LegacyInputSystem : IInputSystem
894903

895904
public IEnumerable<KeyCode> SupportedKeyCodes { get; } = (KeyCode[])Enum.GetValues(typeof(KeyCode));
896905
}
906+
907+
#if IL2CPP
908+
internal class Il2CppLegacyInputSystem : IInputSystem
909+
{
910+
private Type GetInputType()
911+
{
912+
var type = XUnity.Common.Constants.UnityTypes.Input?.ClrType;
913+
if (type == null) throw new InvalidOperationException("Failed to find the UnityEngine.Input type");
914+
return type;
915+
}
916+
917+
[MethodImpl(MethodImplOptions.NoInlining)]
918+
public Il2CppLegacyInputSystem() => GetKeyDown(KeyCode.A);
919+
920+
private System.Reflection.MethodInfo _getKeyString;
921+
public bool GetKey(string name)
922+
{
923+
if (_getKeyString == null) _getKeyString = GetInputType().GetMethod("GetKey", new[] { typeof(string) });
924+
return _getKeyString != null && (bool)_getKeyString.Invoke(null, new object[] { name });
925+
}
926+
927+
private System.Reflection.MethodInfo _getKey;
928+
public bool GetKey(KeyCode key)
929+
{
930+
if (_getKey == null) _getKey = GetInputType().GetMethod("GetKey", new[] { typeof(KeyCode) });
931+
return _getKey != null && (bool)_getKey.Invoke(null, new object[] { key });
932+
}
933+
934+
private System.Reflection.MethodInfo _getKeyDownString;
935+
public bool GetKeyDown(string name)
936+
{
937+
if (_getKeyDownString == null) _getKeyDownString = GetInputType().GetMethod("GetKeyDown", new[] { typeof(string) });
938+
return _getKeyDownString != null && (bool)_getKeyDownString.Invoke(null, new object[] { name });
939+
}
940+
941+
private System.Reflection.MethodInfo _getKeyDown;
942+
public bool GetKeyDown(KeyCode key)
943+
{
944+
if (_getKeyDown == null) _getKeyDown = GetInputType().GetMethod("GetKeyDown", new[] { typeof(KeyCode) });
945+
return _getKeyDown != null && (bool)_getKeyDown.Invoke(null, new object[] { key });
946+
}
947+
948+
private System.Reflection.MethodInfo _getKeyUpString;
949+
public bool GetKeyUp(string name)
950+
{
951+
if (_getKeyUpString == null) _getKeyUpString = GetInputType().GetMethod("GetKeyUp", new[] { typeof(string) });
952+
return _getKeyUpString != null && (bool)_getKeyUpString.Invoke(null, new object[] { name });
953+
}
954+
955+
private System.Reflection.MethodInfo _getKeyUp;
956+
public bool GetKeyUp(KeyCode key)
957+
{
958+
if (_getKeyUp == null) _getKeyUp = GetInputType().GetMethod("GetKeyUp", new[] { typeof(KeyCode) });
959+
return _getKeyUp != null && (bool)_getKeyUp.Invoke(null, new object[] { key });
960+
}
961+
962+
private System.Reflection.MethodInfo _getMouseButton;
963+
public bool GetMouseButton(int button)
964+
{
965+
if (_getMouseButton == null) _getMouseButton = GetInputType().GetMethod("GetMouseButton", new[] { typeof(int) });
966+
return _getMouseButton != null && (bool)_getMouseButton.Invoke(null, new object[] { button });
967+
}
968+
969+
private System.Reflection.MethodInfo _getMouseButtonDown;
970+
public bool GetMouseButtonDown(int button)
971+
{
972+
if (_getMouseButtonDown == null) _getMouseButtonDown = GetInputType().GetMethod("GetMouseButtonDown", new[] { typeof(int) });
973+
return _getMouseButtonDown != null && (bool)_getMouseButtonDown.Invoke(null, new object[] { button });
974+
}
975+
976+
private System.Reflection.MethodInfo _getMouseButtonUp;
977+
public bool GetMouseButtonUp(int button)
978+
{
979+
if (_getMouseButtonUp == null) _getMouseButtonUp = GetInputType().GetMethod("GetMouseButtonUp", new[] { typeof(int) });
980+
return _getMouseButtonUp != null && (bool)_getMouseButtonUp.Invoke(null, new object[] { button });
981+
}
982+
983+
private System.Reflection.MethodInfo _resetInputAxes;
984+
public void ResetInputAxes()
985+
{
986+
if (_resetInputAxes == null) _resetInputAxes = GetInputType().GetMethod("ResetInputAxes", Type.EmptyTypes);
987+
_resetInputAxes?.Invoke(null, null);
988+
}
989+
990+
private System.Reflection.PropertyInfo _mousePosition;
991+
public Vector3 mousePosition
992+
{
993+
get
994+
{
995+
if (_mousePosition == null) _mousePosition = GetInputType().GetProperty("mousePosition");
996+
return _mousePosition != null ? (Vector3)_mousePosition.GetValue(null, null) : Vector3.zero;
997+
}
998+
}
999+
1000+
private System.Reflection.PropertyInfo _mouseScrollDelta;
1001+
public Vector2 mouseScrollDelta
1002+
{
1003+
get
1004+
{
1005+
if (_mouseScrollDelta == null) _mouseScrollDelta = GetInputType().GetProperty("mouseScrollDelta");
1006+
return _mouseScrollDelta != null ? (Vector2)_mouseScrollDelta.GetValue(null, null) : Vector2.zero;
1007+
}
1008+
}
1009+
1010+
private System.Reflection.PropertyInfo _mousePresent;
1011+
public bool mousePresent
1012+
{
1013+
get
1014+
{
1015+
if (_mousePresent == null) _mousePresent = GetInputType().GetProperty("mousePresent");
1016+
return _mousePresent != null && (bool)_mousePresent.GetValue(null, null);
1017+
}
1018+
}
1019+
1020+
private System.Reflection.PropertyInfo _anyKey;
1021+
public bool anyKey
1022+
{
1023+
get
1024+
{
1025+
if (_anyKey == null) _anyKey = GetInputType().GetProperty("anyKey");
1026+
return _anyKey != null && (bool)_anyKey.GetValue(null, null);
1027+
}
1028+
}
1029+
1030+
private System.Reflection.PropertyInfo _anyKeyDown;
1031+
public bool anyKeyDown
1032+
{
1033+
get
1034+
{
1035+
if (_anyKeyDown == null) _anyKeyDown = GetInputType().GetProperty("anyKeyDown");
1036+
return _anyKeyDown != null && (bool)_anyKeyDown.GetValue(null, null);
1037+
}
1038+
}
1039+
1040+
public IEnumerable<KeyCode> SupportedKeyCodes { get; } = (KeyCode[])Enum.GetValues(typeof(KeyCode));
1041+
}
1042+
#endif
8971043
}

0 commit comments

Comments
 (0)