Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Draws a piece of text in the map using separate character images.
- * asOrigin - Center point as origin of text. Preferrably an area.
- * asText - The string to draw.
- * alMaxCharsPerLine - The max amount of characters per line before it breaks a new line.
- * abWrap - Whether it should wrap by word instead of character when it breaks a new line.
- */
- void DrawText(string &in asOrigin, string &in asText, int alMaxCharsPerLine) {
- int chars = 0; //amount of chars in each word
- int pause = 0; //used for 2 things, first to split string by space, second to split line by word.
- int charCount = 0; //counts total amounts of individual characters in the string.
- float charSize = 0.12f; //specifies the distance between each character.
- float lineSize = 0.15f; //specifies the distance between each line.
- int xpos = 0; //x position for each char
- int ypos = 0; //y position for each char
- int spaceLeft; //calculates char space left per line
- CreateEntityAtArea("text_helper", "text_dir_helper.ent", asOrigin, true); //create helper entity
- PlaceEntityAtEntity("text_helper", asOrigin, "", true); //Rotate it
- Print("INFO: Drawing string: '"+asText+"'\n");
- for(int i = 0; i <= asText.length(); i++) { //loops through each char
- string letter = StringSub(asText, i, 1); //current char in the loop
- if(letter == " " || i == asText.length()) { //if a space (or end of string)
- string word = StringSub(asText, pause, i-pause); //then get text since last pause (space or start)
- AddLocalVarStringArray("Word", word); //adds each individual word to an array.
- pause = i+1; //set pause to index of current char + 1 (the next space)
- }
- }
- string[] words = GetLocalVarStringArray("Word"); //get the array split up by each word.
- pause = 0; //reset the pause var so we can reuse it
- for(int i = 0; i < words.length(); i++) { //loop through each word
- string word = words[i]; //current word
- chars = word.length(); //amount of characters in the current word
- pause += chars; //adding length of word to pause
- spaceLeft = alMaxCharsPerLine - chars - pause; //calc the space left on the line.
- AddDebugMessage("chars="+chars+" pause="+pause+" spaceLeft="+spaceLeft, false);
- if(chars > spaceLeft) { //if the current word is longer than the limit (word wrap)
- pause = 0; //reset this line's char counter
- xpos = 0; //reset the horizontal position on the line
- ypos += 1; //push down a line
- }
- for(int j = 0; j <= chars; j++) { //loop through each character of current word
- string currentChar = StringSub(word, j, 1);
- //safety check
- if(currentChar == "") continue;
- //handle special characters
- if(currentChar == "!") currentChar = "exclamation";
- if(currentChar == '"') currentChar = "dquote";
- if(currentChar == "?") currentChar = "question";
- if(currentChar == "/") currentChar = "fslash";
- if(currentChar == ".") currentChar = "period";
- if(currentChar == ":") currentChar = "colon";
- if(currentChar == "&") {
- //new line
- xpos = 0;
- ypos++;
- continue;
- }
- string charID = charCount + "_" + currentChar; //create unique ID for internal name.
- string charFile = "text_" + currentChar + ".ent";
- //draw the character
- CreateEntityAtArea(charID, charFile, asOrigin, true);
- PlaceEntityAtEntity(charID, "text_helper", "Body_1", true);
- SetEntityPos(charID,
- GetEntityPosX(charID) - (xpos * charSize),
- GetEntityPosY(charID) - (ypos * lineSize),
- GetEntityPosZ(charID)
- );
- if(!GetEntityExists(charID)) Print("ERROR: Couldn't create entity from file: '"+charFile+"'");
- charCount++;
- xpos++; //jump to next character slot.
- }
- pause++;
- xpos++; //jump to next character slot (in this case creating an empty space)
- }
- }
Add Comment
Please, Sign In to add comment