Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Scrolling Text Box

edited September 2017 in Code Sharing Posts: 194

It seems to black out at some limit. Can anyone explain why this happens and how to fix it?
EDIT: Fixed, in a sort of lame way. It now breaks the text into chunks every newline, and displays them separately. It still won't work for long, unbroken paragraphs.

--# ScrollText ScrollText = class() function ScrollText:init(text,x,y,w,h) -- you can accept and set parameters here self.x = x self.y = y self.w = w self.h = h self.text = text self.scroll = 0 self.scrollSlide = 0 self.scrolling = false end local function lines(str) local t = {} local function helper(line) table.insert(t, line) return "" end helper((str:gsub("(.-)\r?\n", helper))) return t end function ScrollText:drawLines() local prev = "" local function helper(line) local _,th = textSize(prev) local _,ch = textSize(line) text(line,self.x,self.y+self.h-th-ch) prev = prev..line.."\n" return "" end helper((self.text:gsub("(.-)\r?\n", helper))) end function ScrollText:draw() -- Codea does not automatically call this method pushStyle() clip(self.x,self.y,self.w,self.h) pushMatrix() translate(0,-self.scroll) textWrapWidth(self.w) textMode(CORNER) self:drawLines() popMatrix() clip() local _,th = textSize(self.text) popStyle() self.scroll = self.scroll - self.scrollSlide self.scrollSlide = self.scrollSlide * 0.95 min = self.h-th max = 0 if th < self.h then min = 0 end if not self.scrolling then if self.scroll < min then self.scroll = self.scroll + ( min - self.scroll ) / 10 elseif self.scroll > max then self.scroll = self.scroll + ( max - self.scroll ) / 10 end else if self.scroll < min then self.scrollSlide = self.scrollSlide / (100*(min - self.scroll)) elseif self.scroll > max then self.scrollSlide = self.scrollSlide / (100*(max - self.scroll)) end end end local function inBounds(px,py,x,y,w,h) return px > x and py > y and px < x + w and py < y + h end function ScrollText:touched(touch) if inBounds(touch.x,touch.y,self.x,self.y,self.w,self.h) then self.scrollSlide = (self.scrollSlide + touch.deltaY)/2 if touch.state == MOVING then self.scrolling = true else self.scrolling = false end end end --# Main -- Scrolling Text -- Use this function to perform your initial setup function setup() print("Hello Text!") tc = [[Codea for iPad lets you create games and simulations — or just about any visual idea you have. Turn your thoughts into interactive creations that make use of iPad features like Multi-Touch and the accelerometer. We think Codea is the most beautiful code editor you'll use, and it's easy. Codea is designed to let you touch your code. Want to change a color? Just tap and drag it. How about an image or a sound? Tapping will bring up visual editors that let you choose exactly what you want. Codea is built on the Lua programming language. A simple, elegant language that doesn't rely too much on symbols — a perfect match for iPad. Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description. Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode with a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping. Lua has been used in many industrial applications (e.g., Adobe's Photoshop Lightroom), with an emphasis on embedded systems (e.g., the Ginga middleware for digital TV in Brazil) and games (e.g., World of Warcraft and Angry Birds). Lua is currently the leading scripting language in games. Lua has a solid reference manual and there are several books about it. Several versions of Lua have been released and used in real applications since its creation in 1993. Lua featured in HOPL III, the Third ACM SIGPLAN History of Programming Languages Conference, in 2007. Lua won the Front Line Award 2011 from the Game Developers Magazine.]] ts = ScrollText(tc,WIDTH/8,HEIGHT/8,WIDTH*3/4,HEIGHT*3/4) parameter.integer("TextLength",1,#tc,1221,function(v) ts.text = tc:sub(1,v) end) end -- This function gets called once every frame function draw() -- This sets a dark background color background(40, 40, 50) -- This sets the line thickness strokeWidth(5) fill(255) -- Do your drawing here fontSize(30) ts:draw() end function touched(touch) ts:touched(touch) end


  • dave1707dave1707 Mod
    Posts: 7,528

    The text function only works for a certain width and height. If you exceed that, it doesn't display anything. I posted an explanation and example somewhere in the forum a long time ago. I'm not sure what to search for to find it.

  • dave1707dave1707 Mod
    Posts: 7,528

    @em2 I couldn't find my original post on this, so here's a small example showing what happens. Slide the parameter to increase the number of characters shown. At the top of the screen is the pixel width of the string. On my iPad Air, the text disappears once the pixel width exceeds 1024 pixels. 1024 is the max pixels of my screen width or height. The same thing happens with height. If the total pixel height of the text exceeds 1024, it will disappear.

    function setup()
        for z=1,400 do
    function draw()
        background(40, 40, 50)
        text("text width  "..w.."  pixels.",100,HEIGHT-50)
  • em2em2
    Posts: 194

    Hmm... that would mean I would have to implement my own line breaking mechanism. I wonder why it does that.

  • It almost certainly does it because behind the scenes it would need to be building a single texture to render the text into before it can draw it. 1024x1024 was, at one time in the not-too-distant-past, the largest supported texture size on a whole lot of iOS devices.

Sign In or Register to comment.