Figuring out best values for scrolling text, based on length

Hi. I’m clueless on how to figure out the best values for a scrolling headline thing for my game. I use it to sort-of communicate to users to let them know if the server is down or if there’s a bug. I have absolutely no clue. Can someone try and give me any ideas? Also, here’s the code for the text:

self.newsSequence = self.lastNews.posInterval(T,(-X,0,self.lastNews.getZ()),startPos=(X,0,self.lastNews.getZ()))

T is the time it takes to complete the interval, and X is the X co-ordinate. I need something to calculate what X should be, so that it goes off screen before restarting, and make T be a slow, yet sort-of quick speed so that people can read it. Example: If X is too big, and T is right, it will go too fast because X is far away, and it only knows it has to get there in that time. But, if X is right and T is too quick, it will zoom across the screen. And if T is too slow, it will have you on the edge of your seat with suspense and anticipation of what word will come next. Anybody have any ideas?
if this is the wrong forum, please move this post to the correct one :smiley:

You can get the size of a given text using TextNode.calcWidth() for example like this:

def get_text_length(text, font=None, size=None):
    t=TextNode('')
    if font:
        t.setFont(font)
    if size:
        t.setGlyphScale(size)
    return t.calcWidth(text)

I think you can do the same with TextNode.getWidth() if you already have a TextNode of some sort.

You can get the size of the window using base.win.getXSize() or base.getSize() or WindowProperties.getXSize()

You should be able to get the timing right knowing the size of both the text and the window.

Thank you for your help. I just need to figure out what to plus base.win.getXSize() (because that looks like it gets the x size lol) by. Different text lengths would call for a bigger value I need to add. I think I can find that value with base.win.getXSize() multiplied by 2, minus half of the text length. How do I get one half of a strings length is all I need to know now, and a google search could do that. But if the user goes fullscreen, it will break. That can be fixed by restarting the cycle with updated properties once a window size change is detected, so I need to also know how to detect window size changes. Do you know how to detect window size changes?

If you have a class that inherits from DirectObject you can do:

class MyThingObject (DirectObject):
    def __init__(self):
        #send an event when something happens to the window
        self.accept( 'window-event', self.on_window_event)
        self.window_x=base.win.getXSize()
        self.window_y=base.win.getYSize()

   def on_window_event(self, window=None):
        if window is not None: # window is None if there is no open window
            if self.window_x != base.win.getXSize() or self.window_y != base.win.getYSize():
                self.window_x=base.win.getXSize()
                self.window_y=base.win.getYSize()
                self.on_window_resize()

    def on_window_resize(self):
        print "window has changed size!"
        #do some other stuff

…or you can check in a task each frame if the current window size is the same as the last recorded window size - simpler, but I somehow find the solution above neat.

I was incorrect about my maths. Do you know someone who might know what maths to do? Also, here’s what I did:

leng = len(self.launcher.getOnlineNews())
X = base.win.getXSize() * 2 - leng / 2

If I did this incorrect, can someone fix it?
P.S Thank you for that code wezu. Conveniently, the class of the GUI the news thing is meant to be in IS inherited from DirectObject.

You seem to calculate “leng” as the number of characters in the text; instead, you should either use the code wezu gave you earlier if you’re using a TextNode, or you could call get_tight_bounds on an OnscreenText if that’s what you’re using to display the text.
Also note that base.win.getXSize gives you the window width in pixels, but if you parented your TextNode or OnscreenText to aspect2d, then you need to calculate the window width differently.
The following might help:

self.lastNews = OnscreenText(text="The latest news", scale=.07)
win_props = base.win.get_properties()
win_x_size = win_props.get_x_size()
win_y_size = win_props.get_y_size()
win_width = 2. * max(win_x_size, win_y_size) / win_y_size
text_min, text_max = self.lastNews.get_tight_bounds()
text_width = text_max.x - text_min.x
dist = win_width + text_width
X = dist * .5
speed = .75
T = dist / speed
self.newsSequence = self.lastNews.posInterval(T, (-X, 0., 0.), startPos=(X, 0., 0.))

How more convenient can this day get. My text node is parented to aspect2d. Thank you for this code, I will try it out. :smiley:

Thank you very much. This code worked PERFECT. I don’t know how to thank you, really. Thank you sooooooooo very much!!!