TextTranslationInfo.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Harmony;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  9. using XUnity.AutoTranslator.Plugin.Core.Extensions;
  10. using XUnity.AutoTranslator.Plugin.Core.Fonts;
  11. namespace XUnity.AutoTranslator.Plugin.Core
  12. {
  13. public class TextTranslationInfo
  14. {
  15. private static readonly string MultiLinePropertyName = "multiLine";
  16. private static readonly string OverflowMethodPropertyName = "overflowMethod";
  17. private Action<object> _unresize;
  18. private Action<object> _unfont;
  19. private bool _hasCheckedTypeWriter;
  20. private MonoBehaviour _typewriter;
  21. private object _alteredSpacing;
  22. public TextTranslationInfo()
  23. {
  24. }
  25. public string OriginalText { get; set; }
  26. public string TranslatedText { get; set; }
  27. public bool IsTranslated { get; set; }
  28. public bool IsCurrentlySettingText { get; set; }
  29. public void ResetScrollIn( object graphic )
  30. {
  31. if( !_hasCheckedTypeWriter )
  32. {
  33. _hasCheckedTypeWriter = true;
  34. if( Constants.ClrTypes.Typewriter != null )
  35. {
  36. var ui = graphic as Component;
  37. if( ui != null )
  38. {
  39. _typewriter = (MonoBehaviour)ui.GetComponent( Constants.ClrTypes.Typewriter );
  40. }
  41. }
  42. }
  43. if( _typewriter != null )
  44. {
  45. AccessTools.Method( Constants.ClrTypes.Typewriter, "OnEnable" )?.Invoke( _typewriter, null );
  46. }
  47. }
  48. public void ChangeFont( object graphic )
  49. {
  50. if( graphic == null ) return;
  51. if( graphic is Text )
  52. {
  53. var ui = graphic as Text;
  54. var previousFont = ui.font;
  55. var newFont = FontCache.GetOrCreate( previousFont?.fontSize ?? ui.fontSize );
  56. if( !ReferenceEquals( newFont, previousFont ) )
  57. {
  58. ui.font = newFont;
  59. _unfont = obj =>
  60. {
  61. ( (Text)obj ).font = previousFont;
  62. };
  63. }
  64. }
  65. }
  66. public void UnchangeFont( object graphic )
  67. {
  68. if( graphic == null ) return;
  69. _unfont?.Invoke( graphic );
  70. _unfont = null;
  71. }
  72. public void ResizeUI( object graphic )
  73. {
  74. // do not resize if there is no object of ir it is already resized
  75. if( graphic == null ) return;
  76. if( graphic is Text )
  77. {
  78. var ui = (Text)graphic;
  79. // text is likely to be longer than there is space for, simply expand out anyway then
  80. var componentWidth = ( (RectTransform)ui.transform ).rect.width;
  81. var quarterScreenSize = Screen.width / 4;
  82. var isComponentWide = componentWidth > quarterScreenSize;
  83. // width < quarterScreenSize is used to determine the likelihood of a text using multiple lines
  84. // the idea is, if the UI element is larger than the width of half the screen, there is a larger
  85. // likelihood that it will go into multiple lines too.
  86. var originalHorizontalOverflow = ui.horizontalOverflow;
  87. var originalVerticalOverflow = ui.verticalOverflow;
  88. var originalLineSpacing = ui.lineSpacing;
  89. if( isComponentWide && !ui.resizeTextForBestFit )
  90. {
  91. ui.horizontalOverflow = HorizontalWrapMode.Wrap;
  92. ui.verticalOverflow = VerticalWrapMode.Overflow;
  93. if( Settings.ResizeUILineSpacingScale.HasValue && !Equals( _alteredSpacing, originalLineSpacing ) )
  94. {
  95. var alteredSpacing = originalLineSpacing * Settings.ResizeUILineSpacingScale.Value;
  96. _alteredSpacing = alteredSpacing;
  97. ui.lineSpacing = alteredSpacing;
  98. }
  99. if( _unresize == null )
  100. {
  101. _unresize = g =>
  102. {
  103. var gui = (Text)g;
  104. gui.horizontalOverflow = originalHorizontalOverflow;
  105. gui.verticalOverflow = originalVerticalOverflow;
  106. if( Settings.ResizeUILineSpacingScale.HasValue )
  107. {
  108. gui.lineSpacing = originalLineSpacing;
  109. }
  110. };
  111. }
  112. }
  113. }
  114. else
  115. {
  116. var type = graphic.GetType();
  117. // special handling for NGUI to better handle textbox sizing
  118. if( type == Constants.ClrTypes.UILabel )
  119. {
  120. var originalMultiLine = type.GetProperty( MultiLinePropertyName )?.GetGetMethod()?.Invoke( graphic, null );
  121. var originalOverflowMethod = type.GetProperty( OverflowMethodPropertyName )?.GetGetMethod()?.Invoke( graphic, null );
  122. //var originalSpacingY = graphic.GetSpacingY();
  123. type.GetProperty( MultiLinePropertyName )?.GetSetMethod()?.Invoke( graphic, new object[] { true } );
  124. type.GetProperty( OverflowMethodPropertyName )?.GetSetMethod()?.Invoke( graphic, new object[] { 0 } );
  125. //if( Settings.ResizeUILineSpacingScale.HasValue && !Equals( _alteredSpacing, originalSpacingY ) )
  126. //{
  127. // var alteredSpacing = originalSpacingY.Multiply( Settings.ResizeUILineSpacingScale.Value );
  128. // _alteredSpacing = alteredSpacing;
  129. // graphic.SetSpacingY( alteredSpacing );
  130. //}
  131. if( _unresize == null )
  132. {
  133. _unresize = g =>
  134. {
  135. var gtype = g.GetType();
  136. gtype.GetProperty( MultiLinePropertyName )?.GetSetMethod()?.Invoke( g, new object[] { originalMultiLine } );
  137. gtype.GetProperty( OverflowMethodPropertyName )?.GetSetMethod()?.Invoke( g, new object[] { originalOverflowMethod } );
  138. //if( Settings.ResizeUILineSpacingScale.HasValue )
  139. //{
  140. // g.SetSpacingY( originalSpacingY );
  141. //}
  142. };
  143. }
  144. }
  145. }
  146. }
  147. public void UnresizeUI( object graphic )
  148. {
  149. if( graphic == null ) return;
  150. _unresize?.Invoke( graphic );
  151. _unresize = null;
  152. }
  153. public TextTranslationInfo Reset( string newText )
  154. {
  155. IsTranslated = false;
  156. TranslatedText = null;
  157. OriginalText = newText;
  158. return this;
  159. }
  160. public void SetTranslatedText( string translatedText )
  161. {
  162. IsTranslated = true;
  163. TranslatedText = translatedText;
  164. }
  165. }
  166. }