GUIUtil.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace XUnity.AutoTranslator.Plugin.Core.UI
  7. {
  8. internal static class GUIUtil
  9. {
  10. public const float WindowTitleClearance = 10;
  11. public const float ComponentSpacing = 10;
  12. public const float HalfComponentSpacing = ComponentSpacing / 2;
  13. public const float LabelWidth = 60;
  14. public const float LabelHeight = 21;
  15. public const float RowHeight = 21;
  16. public static readonly RectOffset Empty = new RectOffset( 0, 0, 0, 0 );
  17. public static readonly GUIStyle LabelTranslation = new GUIStyle( GUI.skin.label )
  18. {
  19. richText = false,
  20. margin = new RectOffset( GUI.skin.label.margin.left, GUI.skin.label.margin.right, 0, 0 ),
  21. padding = new RectOffset( GUI.skin.label.padding.left, GUI.skin.label.padding.right, 2, 3 )
  22. };
  23. public static readonly GUIStyle LabelCenter = new GUIStyle( GUI.skin.label )
  24. {
  25. alignment = TextAnchor.UpperCenter,
  26. richText = true
  27. };
  28. public static readonly GUIStyle LabelRight = new GUIStyle( GUI.skin.label )
  29. {
  30. alignment = TextAnchor.UpperRight
  31. };
  32. public static readonly GUIStyle LabelRich = new GUIStyle( GUI.skin.label )
  33. {
  34. richText = true
  35. };
  36. public static readonly GUIStyle NoMarginButtonStyle = new GUIStyle( GUI.skin.button ) { margin = Empty };
  37. public static readonly GUIStyle NoMarginButtonPressedStyle = new GUIStyle( GUI.skin.button )
  38. {
  39. margin = Empty,
  40. onNormal = GUI.skin.button.onActive,
  41. onFocused = GUI.skin.button.onActive,
  42. onHover = GUI.skin.button.onActive,
  43. normal = GUI.skin.button.onActive,
  44. focused = GUI.skin.button.onActive,
  45. hover = GUI.skin.button.onActive,
  46. };
  47. public static readonly GUIStyle NoSpacingBoxStyle = new GUIStyle( GUI.skin.box )
  48. {
  49. margin = Empty,
  50. padding = Empty
  51. };
  52. public static GUIStyle WindowBackgroundStyle = new GUIStyle
  53. {
  54. normal = new GUIStyleState
  55. {
  56. background = CreateBackgroundTexture()
  57. }
  58. };
  59. public static Rect R( float x, float y, float width, float height ) => new Rect( x, y, width, height );
  60. private static Texture2D CreateBackgroundTexture()
  61. {
  62. var windowBackground = new Texture2D( 1, 1, TextureFormat.ARGB32, false );
  63. windowBackground.SetPixel( 0, 0, new Color( 0.6f, 0.6f, 0.6f, 1 ) );
  64. windowBackground.Apply();
  65. GameObject.DontDestroyOnLoad( windowBackground );
  66. return windowBackground;
  67. }
  68. public static GUIStyle GetWindowBackgroundStyle()
  69. {
  70. if( !WindowBackgroundStyle.normal.background )
  71. {
  72. WindowBackgroundStyle = new GUIStyle
  73. {
  74. normal = new GUIStyleState
  75. {
  76. background = CreateBackgroundTexture()
  77. }
  78. };
  79. }
  80. return WindowBackgroundStyle;
  81. }
  82. public static bool IsAnyMouseButtonOrScrollWheelDownSafe
  83. {
  84. get
  85. {
  86. return Features.SupportsMouseScrollDelta
  87. ? IsAnyMouseButtonOrScrollWheelDown
  88. : IsAnyMouseButtonOrScrollWheelDownLegacy;
  89. }
  90. }
  91. public static bool IsAnyMouseButtonOrScrollWheelDownLegacy
  92. {
  93. get
  94. {
  95. return Input.GetMouseButtonDown( 0 )
  96. || Input.GetMouseButtonDown( 1 )
  97. || Input.GetMouseButtonDown( 2 );
  98. }
  99. }
  100. public static bool IsAnyMouseButtonOrScrollWheelDown
  101. {
  102. get
  103. {
  104. return Input.mouseScrollDelta.y != 0f
  105. || Input.GetMouseButtonDown( 0 )
  106. || Input.GetMouseButtonDown( 1 )
  107. || Input.GetMouseButtonDown( 2 );
  108. }
  109. }
  110. public static bool IsAnyMouseButtonOrScrollWheelSafe
  111. {
  112. get
  113. {
  114. return Features.SupportsMouseScrollDelta
  115. ? IsAnyMouseButtonOrScrollWheel
  116. : IsAnyMouseButtonOrScrollWheelLegacy;
  117. }
  118. }
  119. public static bool IsAnyMouseButtonOrScrollWheelLegacy
  120. {
  121. get
  122. {
  123. return Input.GetMouseButton( 0 )
  124. || Input.GetMouseButton( 1 )
  125. || Input.GetMouseButton( 2 );
  126. }
  127. }
  128. public static bool IsAnyMouseButtonOrScrollWheel
  129. {
  130. get
  131. {
  132. return Input.mouseScrollDelta.y != 0f
  133. || Input.GetMouseButton( 0 )
  134. || Input.GetMouseButton( 1 )
  135. || Input.GetMouseButton( 2 );
  136. }
  137. }
  138. }
  139. }