소스 검색

copy to clipboard feature

gravydevsupreme 7 년 전
부모
커밋
cb231ff93c

+ 63 - 0
src/XUnity.AutoTranslator.Plugin.Core/AutoTranslationPlugin.cs

@@ -53,6 +53,13 @@ namespace XUnity.AutoTranslator.Plugin.Core
       private HashSet<string> _newUntranslated = new HashSet<string>();
       private HashSet<string> _translatedTexts = new HashSet<string>();
 
+      /// <summary>
+      /// Keeps track of things to copy to clipboard.
+      /// </summary>
+      private List<string> _textsToCopyToClipboardOrdered = new List<string>();
+      private HashSet<string> _textsToCopyToClipboard = new HashSet<string>();
+      private float _clipboardUpdated = Time.realtimeSinceStartup;
+
       /// <summary>
       /// The number of http translation errors that has occurred up until now.
       /// </summary>
@@ -238,6 +245,21 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
+      private void QueueNewUntranslatedForClipboard( string key )
+      {
+         if( Settings.CopyToClipboard )
+         {
+            key = key.ChangeToSingleLineForDialogue();
+            if( !_textsToCopyToClipboard.Contains( key ) )
+            {
+               _textsToCopyToClipboard.Add( key );
+               _textsToCopyToClipboardOrdered.Add( key );
+
+               _clipboardUpdated = Time.realtimeSinceStartup;
+            }
+         }
+      }
+
       private void QueueNewUntranslatedForDisk( string key )
       {
          if( Settings.IgnoreWhitespaceInDialogue )
@@ -424,6 +446,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
             string translation;
             if( TryGetTranslation( text, out translation ) )
             {
+               QueueNewUntranslatedForClipboard( text );
+
                if( !string.IsNullOrEmpty( translation ) )
                {
                   SetTranslatedText( ui, translation, info );
@@ -464,6 +488,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
 
                               if( !string.IsNullOrEmpty( stabilizedText ) && IsTranslatable( stabilizedText ) )
                               {
+                                 QueueNewUntranslatedForClipboard( text );
+
                                  info?.Reset( stabilizedText );
 
                                  // once the text has stabilized, attempt to look it up
@@ -502,6 +528,8 @@ namespace XUnity.AutoTranslator.Plugin.Core
                   {
                      _startedOperationsForNonStabilizableComponents.Add( text );
 
+                     QueueNewUntranslatedForClipboard( text );
+
                      // Lets try not to spam a service that might not be there...
                      if( AutoTranslateClient.IsConfigured && _consecutiveErrors < Settings.MaxErrors )
                      {
@@ -563,6 +591,7 @@ namespace XUnity.AutoTranslator.Plugin.Core
       {
          try
          {
+            CopyToClipboard();
             KickoffTranslations();
             FinishTranslations();
 
@@ -771,6 +800,40 @@ namespace XUnity.AutoTranslator.Plugin.Core
          }
       }
 
+      private void CopyToClipboard()
+      {
+         if( Settings.CopyToClipboard 
+            && _textsToCopyToClipboardOrdered.Count > 0 
+            && Time.realtimeSinceStartup - _clipboardUpdated > Settings.ClipboardDebounceTime )
+         {
+            try
+            {
+               var builder = new StringBuilder();
+               foreach( var text in _textsToCopyToClipboardOrdered )
+               {
+                  if( text.Length + builder.Length > Settings.MaxClipboardCopyCharacters ) break;
+
+                  builder.AppendLine( text );
+               }
+
+               TextEditor editor = (TextEditor)GUIUtility.GetStateObject( typeof( TextEditor ), GUIUtility.keyboardControl );
+               editor.text = builder.ToString();
+               editor.SelectAll();
+               editor.Copy();
+
+            }
+            catch( Exception e )
+            {
+               Console.WriteLine( "[XUnity.AutoTranslator][ERROR]: An error while copying text to clipboard. " + Environment.NewLine + e );
+            }
+            finally
+            {
+               _textsToCopyToClipboard.Clear();
+               _textsToCopyToClipboardOrdered.Clear();
+            }
+         }
+      }
+
       private void PrintObjects()
       {
 

+ 5 - 0
src/XUnity.AutoTranslator.Plugin.Core/Configuration/Settings.cs

@@ -13,6 +13,7 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static readonly int MaxErrors = 5;
       public static readonly int MaxConcurrentTranslations = 5;
       public static readonly TimeSpan WebClientLifetime = TimeSpan.FromSeconds( 20 );
+      public static readonly float ClipboardDebounceTime = 0.5f;
       
       // can be changed
       public static string ServiceEndpoint;
@@ -35,6 +36,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
       public static string BaiduAppId;
       public static string BaiduAppSecret;
       public static int ForceSplitTextAfterCharacters;
+      public static bool CopyToClipboard;
+      public static int MaxClipboardCopyCharacters;
 
       public static void Configure()
       {
@@ -76,6 +79,8 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
          IgnoreWhitespaceInDialogue = Config.Current.Preferences[ "Behaviour" ][ "IgnoreWhitespaceInDialogue" ].GetOrDefault( true );
          MinDialogueChars = Config.Current.Preferences[ "Behaviour" ][ "MinDialogueChars" ].GetOrDefault( 20 );
          ForceSplitTextAfterCharacters = Config.Current.Preferences[ "Behaviour" ][ "ForceSplitTextAfterCharacters" ].GetOrDefault( 0 );
+         CopyToClipboard = Config.Current.Preferences[ "Behaviour" ][ "CopyToClipboard" ].GetOrDefault( false );
+         MaxClipboardCopyCharacters = Config.Current.Preferences[ "Behaviour" ][ "MaxClipboardCopyCharacters" ].GetOrDefault( 450 );
 
          BaiduAppId = Config.Current.Preferences[ "Baidu" ][ "BaiduAppId" ].GetOrDefault( "" );
          BaiduAppSecret = Config.Current.Preferences[ "Baidu" ][ "BaiduAppSecret" ].GetOrDefault( "" );

+ 75 - 0
src/XUnity.AutoTranslator.Plugin.Core/Translatiton/TranslationContext.cs

@@ -0,0 +1,75 @@
+//using System;
+//using System.Collections.Generic;
+//using System.IO;
+//using System.Linq;
+//using System.Text;
+
+//namespace XUnity.AutoTranslator.Plugin.Core.Translatiton
+//{
+//   public class TranslationContext
+//   {
+//      private Dictionary<string, string> _staticTranslations;
+//      private Dictionary<string, string> _dynamicTranslations;
+//      private Dictionary<string, HashSet<string>> _variables;
+
+//      public TranslationContext()
+//      {
+//         _staticTranslations = new Dictionary<string, string>();
+//         _dynamicTranslations = new Dictionary<string, string>();
+//         _variables = new Dictionary<string, HashSet<string>>();
+//      }
+
+//      public void AddVariable( string name, string value )
+//      {
+//         if( !_variables.TryGetValue( name, out var values ) )
+//         {
+//            values = new HashSet<string>();
+//         }
+
+//         values.Add( value );
+//      }
+//   }
+
+//   public class Translation
+//   {
+//      public string Key { get; set; }
+
+//      public string Expression { get; set; }
+//   }
+
+//   public enum DirectiveType
+//   {
+//      SetVariable,
+//      UnsetVariable
+//   }
+
+//   public class TranslationDirective
+//   {
+//      public TranslationDirective( string directive )
+//      {
+
+//      }
+//   }
+
+//   public class RangeValue
+//   {
+
+//   }
+//   public class TranslationReader : IDisposable
+//   {
+//      private readonly TextReader _reader;
+
+//      private string _activeLine;
+//      private List<TranslationDirective> _activeDirectives;
+
+//      public TranslationReader( TextReader reader )
+//      {
+//         _reader = reader;
+//      }
+
+//      public Translation Read()
+//      {
+
+//      }
+//   }
+//}