XuaWindow.cs 6.3 KB

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