Advertisement
Guest User

Untitled

a guest
Mar 29th, 2012
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. // This is the function using the split and sending it to the chat
  2. public void AddLine( string sender, string text, Color color )
  3. {      
  4.     if( logs.Count > chatLength )
  5.         logs.RemoveRange(0, chatLength/2 );
  6.    
  7.     List<string> split = SplitLine( text );
  8.    
  9.     if( split.Count > 0 )
  10.         foreach( string s in split )
  11.             logs.Add( new ChatEntry( s, style, color ) );
  12. }
  13.  
  14. // Split the line into a list, depending on the chat width
  15. private List<string> SplitLine( string inStr )
  16. {
  17.     lineHeight = Mathf.FloorToInt( chatRect.height /
  18.                 GUIManager.instance.skin.label.CalcHeight( new GUIContent("Cornichon"), 100) );
  19.     lineWidth = (int)chatRect.width-40;
  20.    
  21.     List<string> list = new List<string>();
  22.    
  23.     while( inStr.Length > 0 )
  24.     {
  25.         // La ligne rentre dans le chat, on sort de suite
  26.         if( style.CalcSize(new GUIContent(inStr)).x < lineWidth ){
  27.             list.Add( inStr );
  28.             break;
  29.         }
  30.        
  31.         int i = 0;
  32.         string result = GetSubStringOfSize( inStr, lineWidth, style, 10, 0, ref i );
  33.        
  34.         // Recherche du dernier espace pour ne pas couper un mot au milieux.
  35.         int LastIndexOf = result.LastIndexOf(' ');
  36.        
  37.         if( LastIndexOf > 0 && LastIndexOf < result.Length ){
  38.             list.Add( result.Substring( 0, LastIndexOf ) );
  39.             inStr = inStr.Substring( LastIndexOf, inStr.Length-LastIndexOf );
  40.         }
  41.         else{
  42.             list.Add(result);
  43.             inStr = inStr.Substring( i, inStr.Length-i );
  44.         }
  45.     }
  46.    
  47.     return list;
  48. }
  49.  
  50. // Recursive function to get a string of given size for a given style. If in the middle of word, return the end of the last.
  51. private string GetSubStringOfSize( string inStr, float size, GUIStyle style, int kernel, int start, ref int index )
  52. {
  53.     string subStr = "";
  54.     index = start;
  55.     float length = 0f;
  56.    
  57.     do
  58.     {          
  59.         index += kernel;
  60.         index = index > inStr.Length ? inStr.Length : index;
  61.        
  62.         subStr = inStr.Substring( 0, index );
  63.         length = style.CalcSize(new GUIContent(subStr)).x;
  64.        
  65.         if( index == inStr.Length || length >= size )
  66.         {              
  67.             if( kernel == 1 )
  68.                 return subStr;
  69.            
  70.             /*return subStr = GetSubStringOfSize( subStr, size,
  71.                 style, kernel / 2, index-kernel, ref index );*/
  72.            
  73.             int space = index-kernel;
  74.             string checkForSpaces = subStr.Substring( space, index-space );
  75.             int i = checkForSpaces.IndexOf(' ');
  76.             bool atLeastTwoSpaces = i > 0 ? checkForSpaces.IndexOf(' ', i) > 0 : false;
  77.            
  78.             if( atLeastTwoSpaces )
  79.                 return subStr = GetSubStringOfSize( subStr, size,
  80.                     style, kernel / 2, index-kernel, ref index );
  81.             else{
  82.                 index = index-kernel;
  83.                 return subStr;
  84.             }
  85.         }
  86.     }
  87.     while( length < size );
  88.    
  89.     return subStr;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement