[resolved]slice text? <.<

Started by Apidcloud, February 16, 2011, 10:58:43 am

Previous topic - Next topic

Apidcloud

February 16, 2011, 10:58:43 am Last Edit: February 16, 2011, 06:10:29 pm by Apidcloud
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~



Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Blizzard

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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Apidcloud

February 16, 2011, 02:01:08 pm #2 Last Edit: February 16, 2011, 02:12:59 pm by Apidcloud
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
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Blizzard

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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Apidcloud

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
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Blizzard

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].
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Apidcloud

hmmmmmmm I think i get it now...
but where's current_text initialized?
it goes direct to the else of the branch then?
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit

Blizzard

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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Apidcloud

February 16, 2011, 04:13:06 pm #8 Last Edit: February 16, 2011, 06:10:10 pm by Apidcloud
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
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."


Gibbo Glast 2D Engine - The sky is no longer a limit