Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Apidcloud on February 16, 2011, 10:58:43 am

Title: [resolved]slice text? <.<
Post by: Apidcloud on February 16, 2011, 10:58:43 am
Hey there XD
Im in trouble...(again)
How can I 'slit' text when it reaches the border of the text box? XD

I've seen this:

text = self.contents.slice_text("string", 400)
   text.each_index {|i|
       self.contents.draw_text(270, 36 + i*32, 460, 32, text[i])}

class Bitmap
 def slice_text(text, width)
   words = text.split(' ')
   return words if words.size == 1
   result, current_text = [], words.shift
   words.each_index {|i|
       if self.text_size("#{current_text} #{words[i]}").width > width
         result.push(current_text)
         current_text = words[i]
       else
         current_text = "#{current_text} #{words[i]}"
       end
       result.push(current_text) if i >= words.size - 1}
   return result
 end
end


Ok, I dont get 'all the code' yet..so i need your help to understand it XD

BB~



Title: Re: slice text? <.<
Post by: Blizzard on February 16, 2011, 01:58:33 pm
That piece of code does something slightly different. It splits the lines depending on words. If a word doesn't fit anymore in the current line, it is put into the next one.
Title: Re: slice text? <.<
Post by: Apidcloud on February 16, 2011, 02:01:08 pm
Well, it is the same point i think..But i dont understand it that well...

I don't get what this line does:

result, current_text = [], words.shift


--' I've never seen anything like that XD

And then, there is the problem with:
self.text_size("#{current_text} #{words}").width

text_size returns the rect of the self.contents.draw_text... but I don't know what's the meaning of that stuff inside ( )

see u
Title: Re: slice text? <.<
Post by: Blizzard on February 16, 2011, 03:29:58 pm
This here:

current_text = "#{current_text} #{words[i]}"


is basically the same as this here:

current_text = current_text + " " + words[i]


And text_size returns a Rect that has the dimensions of the area how much is needed to draw that text. When I use width, I know exactly how much the whole string needs. If it's bigger than the given width, I cut off the newest word and put it into the next line instead.
Title: Re: slice text? <.<
Post by: Apidcloud on February 16, 2011, 03:35:40 pm
Quote from: Blizzard on February 16, 2011, 03:29:58 pm
This here:

current_text = "#{current_text} #{words[i]}"


is basically the same as this here:

current_text = current_text + " " + words[i]


And text_size returns a Rect that has the dimensions of the area how much is needed to draw that text. When I use width, I know exactly how much the whole string needs. If it's bigger than the given width, I cut off the newest word and put it into the next line instead.


That doesnt make sense at all --'
current_text = current_text ?  :O.o:

What about that line
result, current_text = [], words.shift ?

See you
Title: Re: slice text? <.<
Post by: Blizzard on February 16, 2011, 03:47:39 pm
No, not this:

current_text = current_text


but this:

current_text = current_text + " " + words[i]


Of course it makes sense. It adds a space to current_text at the end and words[i].
Title: Re: slice text? <.<
Post by: Apidcloud on February 16, 2011, 03:52:29 pm
hmmmmmmm I think i get it now...
but where's current_text initialized?
it goes direct to the else of the branch then?
Title: Re: slice text? <.<
Post by: Blizzard on February 16, 2011, 03:53:52 pm
This piece must be a little confusing for you:

result, current_text = [], words.shift


It's the same as:

result = []
current_text = words.shift


current_text is initialized to the first word that was found in the string.
Title: Re: slice text? <.<
Post by: Apidcloud on February 16, 2011, 04:13:06 pm
Quote from: Blizzard on February 16, 2011, 03:53:52 pm
This piece must be a little confusing for you:

result, current_text = [], words.shift


It's the same as:

result = []
current_text = words.shift


current_text is initialized to the first word that was found in the string.



Lol xD i get it now (: thanks a lot ;D im going to try new stuff here ^^

ok, i understood everything XD
There was that problem of that 2 lines that u explained to me, thanks