Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / textwrap long words
- By simonj Date 2009-08-31 04:03 Edited 2009-09-03 00:13
i have a text_input and I need to limit the text wrap onto lines for loooong words but the above its not ideal, it works kind of but is not quite, any suggestions.

width is the size of the text box given in draw_quad....
thanks!!

see later post for an example to use...
- By jlnr (dev) Date 2009-08-31 06:32
Wow, I think you may be the first one to do that.

It would be easier to just re-render the text using Image#from_text on every change (but more wasteful, resource-wise). But that would also make it harder to draw the caret on the right position. What exactly are the problems with your solution? Can you post a minimal full program to toy around with it? :)
- By simonj Date 2009-08-31 12:30 Edited 2009-08-31 12:55
Thanks Julian

I'll attach an example for your to see, i cut it down for you to see but it should show you when you select create manager profile from the manin menu

I';ve got tons of menus to create and lots of input forms, text boxes, of various sizes, shades, colors, but seeing the code uploaded you get the idea when you enter a long string into a box it over runs out side of the box if say someone presses "WWWWWW"  as a name it will over run the size of the box, i want to scroll the text horitizontally or put it on to the next available text line in the box, assuming that I';ve defined a box big enough to have multiple lines, i;ve also lower the fps on the menu / forms pages to around 15/17 so it is on the CPU, 60 fps isnt really needed

i know its not resourceful the way I do it
- By simonj Date 2009-09-02 06:33 Edited 2009-09-02 08:52
ok, got it working nicely!
- By jlnr (dev) Date 2009-09-02 12:59
Good to hear! Maybe you can post the final solution for other people to see. I still didn't even get to fixing the new Snow Leopard bugs, so much to do :(
- By simonj Date 2009-09-03 00:11 Edited 2009-09-03 01:31
i've got to re post it in the morning , soemthing wierd is going on!!
- By simonj Date 2009-09-03 03:35 Edited 2009-09-03 11:23
here a working version for multil line and looooooooooooooong words....and normal spaced words into a box

#######single line box, only horizontally scroll.....

  def wordwrap_single_line(textinput,width,font,maxsize)
  offset=0
  text=textinput.text
  ##first we are going for opion -1 - scroll the text in the current box until text reaches the maxlength size
  text=text.slice(0,maxsize)
  if font.text_width(text) > width
    temptext=text
    while font.text_width(temptext) > width
      temptext=text.slice(offset,text.length)
      if font.text_width(temptext) < width
        return temptext
      else
        offset+=1
      end
    end
  else
    return text
  end 
  end

##########multi line box with horitonzal and vertical scroll of text to keep it inside the box dimensions - as long as the window.text_input has focus someway...
  def wordwrap_multi_line(textinput,width,font,maxsize,nolines)
  text=textinput.text.slice(0,maxsize)
   lines=[]
  newlines=[]
  text_length=font.text_width(text)
#  if text.index(' ').nil?
    if font.text_width(text) > width
      store=''
      text.each_char{|char|
        store+=char
        if font.text_width(store)>width -10 # 10 is padding for the box change accordingly!! o
          lines.push(store)
          store=''
        end
      }
      lines.push(store) if store.length > 0
      nolines.times do newlines.push(lines.pop) end
      return newlines.compact.reverse
    else
      lines=[text]
    end 
# else
  #  words = textinput.text.split(' ')
  #  lines = [words.shift]
   # words.each do |word|
      ###issue to be fixed - if a long word has been entered and they then add a space, it needs to chop the word up, TODO
    #  if font.text_width("#{lines[-1]} #{word}") < width
     #   lines[-1] << ' ' << word
    #  else
     #   lines.push(word)
    #  end
   # end
   # return lines
# end
end

more than likely can be condensed - there can be things done better but it gives the idea for form entries and games that require text input to be limited to a confined space
Brett(SimonJ)
- By simonj Date 2009-09-06 07:50
still learning but cant work this out

when initialized - @email_address is given from a previous text_input

@email_address="someverylooooooooooooooooongemailaddress@someverylongdomain.com",
@img2=Gosu::Image::from_text(window,@email_address, $normal_font,NormalSz,1,50,:center)

then in draw

@img2.draw(20, 50, 0, 1, 1, 0xffffffff)

-this hangs the application in  nasty way - any ideas? doesnt allow me to collect any errors just get an abrupt windows error saying ruby.exe has close, unknown fault( typical MS message)

i can make this work as in the prev post but trying other ways to handle text input for a game so does anyone know how to see what is going wrong? I know the string shouldnt contain spaces, i..e email address for a user.
Up Topic Gosu / Gosu Exchange / textwrap long words

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill