@FearMe2142 looking at this again, you can in fact accurately determine the height of the text you are going to print. You need to grab a table of fontMetrics
and then add together ascent
and descent
to get the total line height. This means that you can, if you wish, keep the text together in a continuous string, which does simplify the code slightly. In the code below I’ve also added typewriter scrolling, so that what you’ve written doesn’t disappear off the edge of the screen or beneath the keyboard. I was also able to use ascent and descent to draw two lines, a thicker one for the text to “sit” on, and a paler one as the separator between each line. One disadvantage of this method compared with the above is that Codea doesn’t tell us where, horizontally, the block of text ends, so our cursor options are limited to text-based cursors, such as the below (so if you wanted, say, a cursor that was a different colour to the text, that’d take a rethink).
Edit: tidied up the code a bit
-- Notepad
-- by Yojimbo2000
displayMode(OVERLAY)
function setup()
inkey=[[Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam eget hendrerit ligula. Quisque vitae mauris aliquet risus efficitur hendrerit at nec quam. Nulla ut sollicitudin dolor, ut interdum nulla. Quisque malesuada fermentum erat vitae mattis. Suspendisse vitae cursus purus, sit amet dictum risus.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam eget hendrerit ligula. Quisque vitae mauris aliquet risus efficitur hendrerit at nec quam. Nulla ut sollicitudin dolor, ut interdum nulla. Quisque malesuada fermentum erat vitae mattis. Suspendisse vitae cursus purus, sit amet dictum risus.]]
font("Zapfino")
fill(31, 31, 88, 255)
fontSize(20)
strokeWidth(1)
textMode(CORNER) --necessary to stop cursor displacing text
showKeyboard()
metrics(CurrentOrientation)
--measure the font
local fon = fontMetrics()
for k,v in pairs(fon) do
print(k,v)
end
ascent = fon.ascent
descent = fon.descent
size = ascent + descent
screeny, targety = 0,0
end
function metrics(orientation)
width = WIDTH*0.8
textWrapWidth(width)
border = HEIGHT - 50
pos = vec2((WIDTH - width)*0.5, HEIGHT*0.5)
if orientation==PORTRAIT or orientation==PORTRAIT_UPSIDE_DOWN then
typeWriter = HEIGHT * 0.3 --the height at which we want the last line to stay at, to prevent what we're typing disappearing off the page, or being covered by the keyboard
else
typeWriter = HEIGHT * 0.45 --keyboard takes up more space in landscape
end
end
function draw()
background(223, 215, 165, 255)
--cursor
local cursorSpeed = 4
local cursorLen = (ElapsedTime*cursorSpeed%2)//1 --a number that regularly alternates between 0 and 1
local cursor=string.rep("\\u{25ae}", cursorLen)..string.rep("\\u{25af}", 1-cursorLen) --cursor symbol..empty cursor symbol. The empty symbol is necessary otherwise the cursor blinking will push a word past the word wrap limit if you're at the end of a line. One of the disadvantages of a text-based cursor
--Establish y pos of text and scroll the screen/ move the camera accordingly
local w,h = textSize(inkey..cursor)
pos.y = border - h
targety = math.max(0, typeWriter-pos.y)
screeny = screeny + (targety - screeny) * 0.1
translate(0,screeny)
--draw lines
for y = border-ascent, pos.y, - size do
stroke(39, 26, 26, 128)
line(pos.x-10,y,pos.x+width+10,y) --line that text "sits on"
stroke(68, 50, 50, 48)
line(pos.x-10,y-descent,pos.x+width+10,y-descent) --separator between ascendors and descendors
end
text(inkey..cursor, pos.x, pos.y)
end
--The three built-in user-input functions that Codea ships with:
function keyboard(key)
if key==BACKSPACE then
inkey=string.sub(inkey, 1, -2)
else
inkey=inkey..key
end
end
function touched(t)
if t.state==BEGAN then --toggle keyboard
if isKeyboardShowing() then hideKeyboard() else showKeyboard() end
end
end
function orientationChanged(orientation)
metrics(orientation) --re-measure width, height
end