Advertisement
Guest User

Untitled

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