Warcraft III: MapsModelsSkinsIconsSpellsToolsTutorials
WC3 JASS: Code SnippetsJASS and vJASS Spells and SystemsJass Tutorials
Chat @Discord

Author Topic: [System] WordWrap  (Read 11181 times)

  • Rookie - level 1
  • *
  • Posts: 41
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • Reputation: 13
    • View Profile
[System] WordWrap
« on: August 17, 2012, 01:02:38 AM »
http://en.wikipedia.org/wiki/Word_wrapping

This is not as featured/precise as text editor word wrapping, but it does the job pretty well.

The description is in the documentation.

Code: jass  [Select]
  1. library WordWrap /* v2.1.0.0
  2. *********************************************************************
  3. *
  4. *  Simulates word wrapping for multiboards and texttags.
  5. *  Word wrapping is where, if a text is too long, it will
  6. *  cut to the next line rather than cutting off the text.
  7. *
  8. *  How it works:
  9. *
  10. *      - Specify a source string and a size margin. The margin
  11. *        determines what the size of the line can be before it
  12. *        splits the string into fragments. The value is more or
  13. *        less arbritrary, so just experiment with values.
  14. *      - Explodes the string by spaces into words.
  15. *      - Checks the size of each word.
  16. *      - Once the size margin is reached, it will cut the string.
  17. *      - The first line is stored in the array under the index 0.
  18. *      - Rinse and repeat until it fragments the entire string.
  19. *      - You can retrieve the count to know the max index reached.
  20. *
  21. *********************************************************************
  22. *
  23. *   */uses/*
  24. *  
  25. *       */ StringSize /*  [url]http://wc3jass.com/5014/snippet-stringsize/msg38482/topicseen/#new[/url]
  26. *
  27. *    This library does not require Ascii, but StringSize requires
  28. *    the Ascii library:
  29. *
  30. *          Ascii          [url]http://www.hiveworkshop.com/forums/jass-functions-413/snippet-ascii-190746/[/url]
  31. *
  32. *********************************************************************
  33. *
  34. *  function WordWrapString takes string source, real sizeMargin, boolean preserveColors returns nothing
  35. *
  36. *      - Breaks the string into several fragments.
  37. *      - Size margin determines the size the string can reach
  38. *        before it fragments the string and breaks to the next
  39. *        line.
  40. *      - Preserve colors is a boolean to check if a color
  41. *        code is being broken during the word wrap. If you
  42. *        are not using color codes (ex: |cffffcc00text|r)
  43. *        then you should set this to false to save performance.
  44. *
  45. *  function GetWrappedStringFragment takes integer index returns string
  46. *
  47. *      - Retrieves the fragmented strings. The indices start at 0
  48. *        and will go up to the "count" index - 1. (see below) The last
  49. *        line will be:
  50. *
  51. *          GetWrappedStringFragment(GetWrappedStringCount() - 1)
  52. *
  53. *  function GetWrappedStringCount takes nothing returns integer
  54. *
  55. *      - Returns the total number of fragments. Indices start at 0,
  56. *        and the final line will be this number minus 1.
  57. *
  58. *********************************************************************
  59. *
  60. *  struct WordWrap
  61. *
  62. *      static method getLine takes integer index returns string
  63. *
  64. *      static method getCount takes nothing returns integer
  65. *
  66. *      static method create takes string source, real sizeMargin, boolean preserveColors returns thistype
  67. *
  68. *********************************************************************
  69. *
  70. *  Credits
  71. *
  72. *      - cleeezzz & tooltiperror for the idea.
  73. *
  74. *  Bugs
  75. *
  76. *      - If there is a single word that is longer than the
  77. *        "sizeMargin" input, then this system will fail.
  78. *        It requires extra checks. I may update this later
  79. *        to fix that problem, but for now this should be fine.
  80. *        PM or message me if you have any problems.
  81. *
  82. *********************************************************************/
  83.  
  84.     struct WordWrap extends array
  85.         private static string array line
  86.         private static integer count = 0
  87.  
  88.         static method getLine takes integer index returns string
  89.             return line[index]
  90.         endmethod
  91.        
  92.         static method getCount takes nothing returns integer
  93.             return count
  94.         endmethod
  95.        
  96.         private static method preserveColor takes nothing returns nothing
  97.             local integer i = 0
  98.             local integer k = 0
  99.             local integer l = 0
  100.             local string temp = ""
  101.             local string hex = ""
  102.             local boolean carryOn = false
  103.             loop
  104.                 exitwhen i == count
  105.                 set carryOn = false
  106.                 set hex = ""
  107.                 set k = 0
  108.                 set l = StringLength(line[i])
  109.                 loop
  110.                     exitwhen k == l
  111.                     set temp = SubString(line[i], k, k+1)
  112.                     if temp == "|" then
  113.                         set temp = SubString(line[i], k+1, k+2)
  114.                         if temp == "c" then
  115.                             set hex = SubString(line[i], k, k+10)
  116.                             set carryOn = true
  117.                         elseif temp == "r" then
  118.                             set carryOn = false
  119.                         endif
  120.                     endif
  121.                     set k = k + 1
  122.                 endloop
  123.                 set i = i + 1
  124.                 if carryOn then
  125.                     set line[i] = hex + line[i]
  126.                 endif
  127.             endloop
  128.         endmethod
  129.        
  130.         static method create takes string source, real size, boolean preserveColors returns thistype
  131.             local integer l = StringLength(source)
  132.             local integer i = 0
  133.             local integer k = 0
  134.             local string s
  135.            
  136.             local string word
  137.             local real result = 0
  138.             local real ssize = 0
  139.            
  140.             loop
  141.                 exitwhen count == 0
  142.                 set count = count - 1
  143.                 set line[count] = ""
  144.             endloop
  145.             loop
  146.                 exitwhen i == l
  147.                 set s = SubString(source, i, i+1)
  148.                 if s == " " or i == (l-1) then
  149.                     set word = SubString(source, k, i) + s
  150.                     set ssize = MeasureString(word)
  151.                     set result = result + ssize
  152.                     if result <= size then
  153.                         set line[count] = line[count] + word
  154.                     else
  155.                         set count = count + 1
  156.                         set line[count] = word
  157.                         set result = ssize
  158.                     endif
  159.                     set k = i + 1
  160.                 endif
  161.                 set i = i + 1
  162.             endloop
  163.             set count = count + 1
  164.             if preserveColors then
  165.                 call ForForce(bj_FORCE_PLAYER[0], function thistype.preserveColor)
  166.             endif
  167.             return 0
  168.         endmethod
  169.     endstruct
  170.  
  171.     function WordWrapString takes string source, real sizeMargin, boolean preserveColors returns nothing
  172.         call WordWrap.create(source, sizeMargin, preserveColors)
  173.     endfunction
  174.  
  175.     function GetWrappedStringFragment takes integer i returns string
  176.         return WordWrap.getLine(i)
  177.     endfunction
  178.  
  179.     function GetWrappedStringCount takes nothing returns integer
  180.         return WordWrap.getCount()
  181.     endfunction
  182.  
  183. endlibrary
  184.  

Here is more information and pictures. First, take a look at this sample code:
Code: jass  [Select]
  1. scope Testing initializer Init
  2.    
  3.     private function esc takes nothing returns nothing
  4.         local string text = "Hello, my name is Joe. I work in a button factory. One day, my boss said to me, \"Hiya Joe! Are you busy?\" "+/*
  5.         */" I said, \"No\"."    //our original text
  6.         local integer i = 0
  7.         call WordWrapString(text, 450, true) //We will break it into lines, 450 maximum pixels per line
  8.         loop
  9.             exitwhen i == GetWrappedStringCount()    //loop through each line up to the max line count.
  10.             call BJDebugMsg(GetWrappedStringFragment(i))  //This is how you read a line
  11.             set i = i + 1
  12.         endloop
  13.     endfunction
  14.    
  15.     private function Init takes nothing returns nothing
  16.         local trigger t = CreateTrigger()
  17.         call TriggerRegisterPlayerEvent(t, Player(0), EVENT_PLAYER_END_CINEMATIC)
  18.         call TriggerAddAction(t, function esc)
  19.     endfunction
  20. endscope

This is that sample above, with a width of 150:

This is that sample above, but with a width of 450:


For color preservation, it is basically an option to preserve color codes when skipping to the next line. If it is set to "false", then it will not preserve the colors, boosting performance, but ruining how it looks. If you have something like this:
|cff00FF00Hello World! It is a
beautiful day!|r

It should color the entire text. However, without color preservation, it will only color the top line. It should be fine to disable this if you are only color coding individual words, or if you aren't using color codes, but otherwise it should be enabled.

Here is an example with color preservation:


Without it, it would look like this:


Enjoy, all feedback is welcome.
« Last Edit: September 01, 2012, 09:06:51 PM by Purgeandfire »

  • Your Awesome Site Director
  • Recognized User
  • Starter - level 1
  • *
  • Posts: 382
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • Reputation: 677
    • View Profile
Re: [System] WordWrap
« Reply #1 on: August 17, 2012, 01:06:00 AM »
This is great! Really need a good system for making text on my SRPG, thanks for this one purge! Gonna try it when I got home.
Chronicles of Darkness
by: SonofJay

A BlizzMod Hosted Project

They can hate, let them hate, make them hate.

  • Rookie - level 1
  • *
  • Posts: 41
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • Reputation: 13
    • View Profile
Re: [System] WordWrap
« Reply #2 on: August 18, 2012, 01:15:22 AM »
Thanks.

I might end up making a multiboard/text tag system for this. It seems a bit much to have a system for everything, but it is really frustrating to deal with. If I made something to auto-extend multiboards or w/e or to create new text tags for new lines, then that thing would no longer be a problem. :)

If only there was a way to get resolution. I'm still looking to see if there is any hope of retrieving that.

  • Rookie - level 1
  • *
  • Posts: 48
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • Reputation: 8
  • cool != useful
    • View Profile
Re: [System] WordWrap
« Reply #3 on: August 18, 2012, 02:59:49 AM »
I think the best way is to ask the user to say which resolution he is using through a dialog box or whatever else.
And because we don't always know which one we are using you could chek for each resolution what is the lowest texttag size available, and then display all these sizes with the text equal to the resolution, then the user just have to choice the biggest one he can see, but meh maybe it's already too much for the average battle.net player :p
vJass is already a jass preprocessor, no need to (ab)use vJass features in order to make some inferior vJass preprocessor coded "by hand".

  • Rookie - level 1
  • *
  • Posts: 41
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • Reputation: 13
    • View Profile
Re: [System] WordWrap
« Reply #4 on: September 01, 2012, 09:07:57 PM »
Updated with optimizations and fixed a bug that came with my 2.0 update.

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 986
  • WC3 Skins: 6
  • WC3 Icons: 2
  • WC3 Spells: 5
  • WC3 Models: 25
  • WC3 Tutorials: 13
  • WC3 Tools: 10
  • WC3 Maps: 11
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
Re: [System] WordWrap
« Reply #5 on: September 02, 2012, 09:22:07 AM »
Definitely a very nice piece of code, and it does the job pretty well. Approved of course!!

We can give you full hosting for your projects, a complete page!!

A custom altered melee map where you can play Naga and Demons. Check it out!!
Use Dropbox...

 

New Hero Selection System for version 1.6!!!

Started by SonofJayBoard General Information

Replies: 3
Views: 5674
Last post July 01, 2013, 03:59:31 PM
by Chaosy
Advanced AI system (AKA AMAI 2)

Started by moyackBoard Jass Theory & Questions

Replies: 6
Views: 13097
Last post September 06, 2012, 09:54:35 PM
by moyack
[System] Simulate 3D Sound

Started by PurgeandfireBoard Codes & Snippets

Replies: 7
Views: 14304
Last post July 15, 2013, 12:36:35 AM
by Purgeandfire
Advanced Streak System v4.3.0.0

Started by Magtheridon96Board Warcraft III Spells and Systems

Replies: 2
Views: 7134
Last post June 25, 2013, 08:54:56 PM
by LembidiZ
What people want in a Custom XP System?

Started by Naoto ShiroganeBoard Triggers & Scripting

Replies: 3
Views: 4037
Last post April 02, 2015, 06:36:41 PM
by moyack
Blizzard Modding Information Center Starcraft II Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!