DropdownGUI.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace XUnity.AutoTranslator.Plugin.Core.UI
  4. {
  5. internal class DropdownGUI<TDropdownOptionViewModel, TSelection>
  6. where TDropdownOptionViewModel : DropdownOptionViewModel<TSelection>
  7. where TSelection : class
  8. {
  9. private const float MaxHeight = GUIUtil.RowHeight * 5;
  10. private GUIContent _noSelection;
  11. private GUIContent _unselect;
  12. private DropdownViewModel<TDropdownOptionViewModel, TSelection> _viewModel;
  13. private float _x;
  14. private float _y;
  15. private float _width;
  16. private bool _isShown;
  17. private Vector2 _scrollPosition;
  18. public DropdownGUI( float x, float y, float width, DropdownViewModel<TDropdownOptionViewModel, TSelection> viewModel )
  19. {
  20. _x = x;
  21. _y = y;
  22. _width = width;
  23. _noSelection = new GUIContent( "----", "<b>SELECT TRANSLATOR</b>\nNo translator is currently selected, which means no new translations will be performed. Please select one from the dropdown." );
  24. _unselect = new GUIContent( "----", "<b>UNSELECT TRANSLATOR</b>\nThis will unselect the current translator, which means no new translations will be performed." );
  25. _viewModel = viewModel;
  26. }
  27. public void OnGUI()
  28. {
  29. bool clicked = GUI.Button( GUIUtil.R( _x, _y, _width, GUIUtil.RowHeight ), _viewModel.CurrentSelection?.Text ?? _noSelection, _isShown ? GUIUtil.NoMarginButtonPressedStyle : GUI.skin.button );
  30. if( clicked )
  31. {
  32. _isShown = !_isShown;
  33. }
  34. if( _isShown )
  35. {
  36. ShowDropdown( _x, _y + GUIUtil.RowHeight, _width, GUI.skin.button );
  37. }
  38. if( !clicked && Event.current.isMouse )
  39. {
  40. _isShown = false;
  41. }
  42. }
  43. private void ShowDropdown( float x, float y, float width, GUIStyle buttonStyle )
  44. {
  45. var rect = GUIUtil.R( x, y, width, _viewModel.Options.Count * GUIUtil.RowHeight > MaxHeight ? MaxHeight : _viewModel.Options.Count * GUIUtil.RowHeight );
  46. GUILayout.BeginArea( rect, GUIUtil.NoSpacingBoxStyle );
  47. _scrollPosition = GUILayout.BeginScrollView( _scrollPosition );
  48. var style = _viewModel.CurrentSelection == null ? GUIUtil.NoMarginButtonPressedStyle : GUIUtil.NoMarginButtonStyle;
  49. if( GUILayout.Button( _unselect, style ) )
  50. {
  51. _viewModel.Select( null );
  52. _isShown = false;
  53. }
  54. foreach( var option in _viewModel.Options )
  55. {
  56. style = option.IsSelected() ? GUIUtil.NoMarginButtonPressedStyle : GUIUtil.NoMarginButtonStyle;
  57. GUI.enabled = option?.IsEnabled() ?? true;
  58. if( GUILayout.Button( option.Text, style ) )
  59. {
  60. _viewModel.Select( option );
  61. _isShown = false;
  62. }
  63. GUI.enabled = true;
  64. }
  65. GUILayout.EndScrollView();
  66. GUILayout.EndArea();
  67. }
  68. }
  69. }