XuaWindow.cs 6.9 KB

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