XuaWindow.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using UnityEngine;
  5. using XUnity.AutoTranslator.Plugin.Core.Endpoints;
  6. using XUnity.AutoTranslator.Plugin.Core.Hooks.UGUI;
  7. namespace XUnity.AutoTranslator.Plugin.Core.UI
  8. {
  9. internal class XuaWindow
  10. {
  11. private const int WindowId = 5464332;
  12. private const float WindowHeight = 520;
  13. private const float WindowWidth = 320;
  14. private Rect _windowRect = new Rect( 20, 20, WindowWidth, WindowHeight );
  15. private DropdownGUI<TranslatorDropdownOptionViewModel, TranslationEndpointManager> _endpointDropdown;
  16. private XuaViewModel _viewModel;
  17. private bool _isMouseDownOnWindow = false;
  18. public bool IsShown
  19. {
  20. get => _viewModel.IsShown;
  21. set => _viewModel.IsShown = value;
  22. }
  23. public XuaWindow( XuaViewModel viewModel )
  24. {
  25. _viewModel = viewModel;
  26. }
  27. public void OnGUI()
  28. {
  29. GUI.Box( _windowRect, GUIContent.none, GUIUtil.GetWindowBackgroundStyle() );
  30. _windowRect = GUI.Window( WindowId, _windowRect, CreateWindowUI, "---- XUnity.AutoTranslator UI ----" );
  31. if( GUIUtil.IsAnyMouseButtonOrScrollWheelDown )
  32. {
  33. var point = new Vector2( Input.mousePosition.x, Screen.height - Input.mousePosition.y );
  34. _isMouseDownOnWindow = _windowRect.Contains( point );
  35. }
  36. if( !_isMouseDownOnWindow || !GUIUtil.IsAnyMouseButtonOrScrollWheel )
  37. return;
  38. // make sure window is focused if scroll wheel is used to indicate we consumed that event
  39. GUI.FocusWindow( WindowId );
  40. var point1 = new Vector2( Input.mousePosition.x, Screen.height - Input.mousePosition.y );
  41. if( !_windowRect.Contains( point1 ) )
  42. return;
  43. Input.ResetInputAxes();
  44. }
  45. private void CreateWindowUI( int id )
  46. {
  47. float posx = GUIUtil.ComponentSpacing;
  48. float posy = GUIUtil.WindowTitleClearance + GUIUtil.ComponentSpacing;
  49. const float col2 = WindowWidth - GUIUtil.LabelWidth - ( 3 * GUIUtil.ComponentSpacing );
  50. const float col1x = GUIUtil.ComponentSpacing;
  51. const float col2x = GUIUtil.LabelWidth + ( GUIUtil.ComponentSpacing * 2 );
  52. const float col12 = WindowWidth - ( 2 * GUIUtil.ComponentSpacing );
  53. if( GUI.Button( GUIUtil.R( WindowWidth - 22, 2, 20, 16 ), "X" ) )
  54. {
  55. IsShown = false;
  56. }
  57. // GROUP
  58. var toggles = _viewModel.Toggles;
  59. var groupHeight = ( GUIUtil.RowHeight * toggles.Count ) + ( GUIUtil.ComponentSpacing * ( toggles.Count ) ) - GUIUtil.HalfComponentSpacing;
  60. GUI.Box( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, WindowWidth - GUIUtil.ComponentSpacing, groupHeight ), "" );
  61. foreach( var vm in toggles )
  62. {
  63. var previousValue = vm.IsToggled();
  64. var newValue = GUI.Toggle( GUIUtil.R( col1x, posy + 3, col12, GUIUtil.RowHeight - 3 ), previousValue, vm.Text );
  65. if( previousValue != newValue )
  66. {
  67. vm.OnToggled();
  68. }
  69. posy += GUIUtil.RowHeight + GUIUtil.ComponentSpacing;
  70. }
  71. var commandButtons = _viewModel.CommandButtons;
  72. const int buttonsPerRow = 3;
  73. const float buttonWidth = ( col12 - ( GUIUtil.ComponentSpacing * ( buttonsPerRow - 1 ) ) ) / buttonsPerRow;
  74. var rows = commandButtons.Count / buttonsPerRow;
  75. if( commandButtons.Count % 3 != 0 ) rows++;
  76. // GROUP
  77. groupHeight = GUIUtil.LabelHeight + ( GUIUtil.RowHeight * rows ) + ( GUIUtil.ComponentSpacing * ( rows + 1 ) ) - GUIUtil.HalfComponentSpacing;
  78. GUI.Box( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, WindowWidth - GUIUtil.ComponentSpacing, groupHeight ), "" );
  79. GUI.Label( GUIUtil.R( col1x, posy, col12, GUIUtil.LabelHeight ), "---- Command Panel ----", GUIUtil.LabelCenter );
  80. posy += GUIUtil.RowHeight + GUIUtil.ComponentSpacing;
  81. for( int row = 0; row < rows; row++ )
  82. {
  83. for( int col = 0; col < buttonsPerRow; col++ )
  84. {
  85. int idx = ( row * buttonsPerRow ) + col;
  86. if( idx >= commandButtons.Count ) break;
  87. var vm = commandButtons[ idx ];
  88. GUI.enabled = vm.CanClick?.Invoke() != false;
  89. if( GUI.Button( GUIUtil.R( posx, posy, buttonWidth, GUIUtil.RowHeight ), vm.Text ) )
  90. {
  91. vm.OnClicked?.Invoke();
  92. }
  93. GUI.enabled = true;
  94. posx += GUIUtil.ComponentSpacing + buttonWidth;
  95. }
  96. posy += GUIUtil.RowHeight + GUIUtil.ComponentSpacing;
  97. }
  98. // GROUP
  99. groupHeight = GUIUtil.LabelHeight + ( GUIUtil.RowHeight * 1 ) + ( GUIUtil.ComponentSpacing * ( 2 ) ) - GUIUtil.HalfComponentSpacing;
  100. GUI.Box( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, WindowWidth - GUIUtil.ComponentSpacing, groupHeight ), "" );
  101. GUI.Label( GUIUtil.R( col1x, posy, col12, GUIUtil.LabelHeight ), "---- Select a Translator ----", GUIUtil.LabelCenter );
  102. posy += GUIUtil.RowHeight + GUIUtil.ComponentSpacing;
  103. GUI.Label( GUIUtil.R( col1x, posy, GUIUtil.LabelWidth, GUIUtil.LabelHeight ), "Translator: " );
  104. float endpointDropdownPosy = posy;
  105. posy += GUIUtil.RowHeight + GUIUtil.ComponentSpacing;
  106. // GROUP
  107. var labels = _viewModel.Labels;
  108. groupHeight = GUIUtil.LabelHeight + ( GUIUtil.RowHeight * labels.Count ) + ( GUIUtil.ComponentSpacing * ( labels.Count + 1 ) ) - GUIUtil.HalfComponentSpacing;
  109. GUI.Box( GUIUtil.R( GUIUtil.HalfComponentSpacing, posy, WindowWidth - GUIUtil.ComponentSpacing, groupHeight ), "" );
  110. GUI.Label( GUIUtil.R( col1x, posy, col12, GUIUtil.LabelHeight ), "---- Status ----", GUIUtil.LabelCenter );
  111. posy += GUIUtil.RowHeight + GUIUtil.ComponentSpacing;
  112. foreach( var label in labels )
  113. {
  114. GUI.Label( GUIUtil.R( col1x, posy, col12, GUIUtil.LabelHeight ), label.Title );
  115. GUI.Label( GUIUtil.R( col2x, posy, col2, GUIUtil.LabelHeight ), label.GetValue(), GUIUtil.LabelRight );
  116. posy += GUIUtil.RowHeight + GUIUtil.ComponentSpacing;
  117. }
  118. var endpointDropdown = _endpointDropdown ?? ( _endpointDropdown = new DropdownGUI<TranslatorDropdownOptionViewModel, TranslationEndpointManager>( col2x, endpointDropdownPosy, col2, _viewModel.EndpointOptions ) );
  119. endpointDropdown.OnGUI();
  120. GUI.Label( GUIUtil.R( col1x, posy, col12, GUIUtil.RowHeight * 5 ), GUI.tooltip, GUIUtil.LabelRich );
  121. GUI.DragWindow();
  122. }
  123. }
  124. }