Mudbill

Text print script

Oct 30th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. /**
  2. * Draws a piece of text in the map using separate character images.
  3. * asOrigin - Center point as origin of text. Preferrably an area.
  4. * asText - The string to draw.
  5. * alMaxCharsPerLine - The max amount of characters per line before it breaks a new line.
  6. * abWrap - Whether it should wrap by word instead of character when it breaks a new line.
  7. */
  8. void DrawText(string &in asOrigin, string &in asText, int alMaxCharsPerLine) {
  9.     int chars = 0; //amount of chars in each word
  10.     int pause = 0; //used for 2 things, first to split string by space, second to split line by word.
  11.     int charCount = 0; //counts total amounts of individual characters in the string.
  12.     float charSize = 0.12f; //specifies the distance between each character.
  13.     float lineSize = 0.15f; //specifies the distance between each line.
  14.     int xpos = 0; //x position for each char
  15.     int ypos = 0; //y position for each char
  16.     int spaceLeft; //calculates char space left per line
  17.  
  18.     CreateEntityAtArea("text_helper", "text_dir_helper.ent", asOrigin, true); //create helper entity
  19.     PlaceEntityAtEntity("text_helper", asOrigin, "", true); //Rotate it
  20.  
  21.     Print("INFO: Drawing string: '"+asText+"'\n");
  22.    
  23.     for(int i = 0; i <= asText.length(); i++) { //loops through each char
  24.         string letter = StringSub(asText, i, 1); //current char in the loop
  25.         if(letter == " " || i == asText.length()) { //if a space (or end of string)
  26.             string word = StringSub(asText, pause, i-pause); //then get text since last pause (space or start)
  27.             AddLocalVarStringArray("Word", word); //adds each individual word to an array.
  28.             pause = i+1; //set pause to index of current char + 1 (the next space)
  29.         }
  30.     }
  31.    
  32.     string[] words = GetLocalVarStringArray("Word"); //get the array split up by each word.
  33.    
  34.     pause = 0; //reset the pause var so we can reuse it
  35.    
  36.     for(int i = 0; i < words.length(); i++) { //loop through each word
  37.         string word = words[i]; //current word
  38.         chars = word.length(); //amount of characters in the current word
  39.         pause += chars; //adding length of word to pause
  40.         spaceLeft = alMaxCharsPerLine - chars - pause; //calc the space left on the line.
  41.         AddDebugMessage("chars="+chars+" pause="+pause+" spaceLeft="+spaceLeft, false);
  42.  
  43.         if(chars > spaceLeft) { //if the current word is longer than the limit (word wrap)
  44.             pause = 0; //reset this line's char counter
  45.             xpos = 0; //reset the horizontal position on the line
  46.             ypos += 1; //push down a line
  47.         }
  48.        
  49.         for(int j = 0; j <= chars; j++) { //loop through each character of current word
  50.             string currentChar = StringSub(word, j, 1);
  51.            
  52.             //safety check
  53.             if(currentChar == "") continue;
  54.            
  55.             //handle special characters
  56.             if(currentChar == "!") currentChar = "exclamation";
  57.             if(currentChar == '"') currentChar = "dquote";
  58.             if(currentChar == "?") currentChar = "question";
  59.             if(currentChar == "/") currentChar = "fslash";
  60.             if(currentChar == ".") currentChar = "period";
  61.             if(currentChar == ":") currentChar = "colon";
  62.             if(currentChar == "&") {
  63.                 //new line
  64.                 xpos = 0;
  65.                 ypos++;
  66.                 continue;
  67.             }
  68.             string charID = charCount + "_" + currentChar; //create unique ID for internal name.
  69.             string charFile = "text_" + currentChar + ".ent";
  70.  
  71.             //draw the character
  72.             CreateEntityAtArea(charID, charFile, asOrigin, true);
  73.             PlaceEntityAtEntity(charID, "text_helper", "Body_1", true);
  74.             SetEntityPos(charID,
  75.                 GetEntityPosX(charID) - (xpos * charSize),
  76.                 GetEntityPosY(charID) - (ypos * lineSize),
  77.                 GetEntityPosZ(charID)
  78.             );
  79.            
  80.             if(!GetEntityExists(charID)) Print("ERROR: Couldn't create entity from file: '"+charFile+"'");
  81.            
  82.             charCount++;
  83.             xpos++; //jump to next character slot.
  84.         }
  85.         pause++;
  86.         xpos++; //jump to next character slot (in this case creating an empty space)
  87.     }
  88. }
Add Comment
Please, Sign In to add comment