Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 1.53 KB | None | 0 0
  1.     private function makeLinesOptimized(message:String)
  2.     {
  3.         var currentLine:String = "";
  4.         var currentLineWidth:Int = 0;
  5.        
  6.         var currentWord:String = "";
  7.         var currentWordWidth:Int = 0;
  8.        
  9.         for (i in 0...message.length)
  10.         {
  11.             var character:String = message.charAt(i);
  12.             var charWidth:Int = Math.ceil(chatFont.getCharWidth(character.charCodeAt(0)));
  13.            
  14.             currentWord += character;
  15.             currentWordWidth += charWidth;
  16.            
  17.             // Word is over! New word starting!
  18.             if (character == " " || i == message.length - 1)
  19.             {
  20.                 // Will adding this word to the line exceed the length?
  21.                 if (currentLineWidth + currentWordWidth < MAX_LINE)
  22.                 {
  23.                     currentLine += currentWord;
  24.                     currentLineWidth += currentWordWidth;
  25.                    
  26.                     currentWordWidth = 0;
  27.                    
  28.                     if (widestLine < currentLineWidth)
  29.                     {
  30.                         widestLine = currentLineWidth;
  31.                     }
  32.                 }
  33.                
  34.                 else
  35.                 {
  36.                     if (widestLine < currentLineWidth)
  37.                     {
  38.                         widestLine = currentLineWidth;
  39.                     }
  40.                     lines.push(currentLine);
  41.                    
  42.                     currentLine = currentWord;
  43.                     currentLineWidth = currentWordWidth;
  44.                 }
  45.                
  46.                 currentWord = "";
  47.                 currentWordWidth = 0;
  48.                
  49.                 continue;
  50.             }
  51.            
  52.             // We're mid-word; this won't fit.
  53.             else if (currentWordWidth >= MAX_LINE)
  54.             {
  55.                 if (currentLine != "")
  56.                 {
  57.                     lines.push(currentLine);
  58.                 }
  59.                
  60.                 lines.push(currentWord);
  61.                
  62.                 currentWord = "";
  63.                 currentWordWidth = 0;
  64.                
  65.                 currentLine = "";
  66.                 currentLineWidth = 0;
  67.             }
  68.         }
  69.        
  70.         lines.push(currentLine);
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement