This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Reciprocaterman's profile picture

Auto-centering text.

Started by Reciprocaterman, 03 May 2014 - 07:53 PM
Reciprocaterman #1
Posted 03 May 2014 - 09:53 PM
I was going to use the read function but decided to try and write one myself that auto centers as you type the text. I've come across a weird error that I don't recognize.

[string "term"]:9:bad argument # 2 to '?' (number expected, got no value)

Here's the code.


term.setBackgroundColor(colors.white)
term.clear()
term.setTextColor(colors.white)
sizeX, sizeY = term.getSize()
button = "Start"

term.setCursorPos(math.floor((sizeX - #button) / 2),math.floor((sizeY / 2) + 1))
term.setBackgroundColor(colors.blue)
write(button)

function checkPass(name)
    term.clear()
    print(name)
end

function getUser()
    local askUser = "Please enter your username."
    term.setCursorPos(math.floor((sizeX - #askUser) / 2), math.floor((sizeY / 2) - 1))
    term.setBackgroundColor(colors.white)
    term.setTextColor(colors.blue)
    textutils.slowPrint(askUser)
    term.setCursorPos(math.floor(sizeX / 2), math.floor(sizeY / 2))
    while true do
        --term.clearLine()
        if user then
            term.setCursorPos(math.floor((sizeX - #user) / 2))
        else
            term.setCursorPos(math.floor(sizeX / 2))
        end
        print(user)
        local event, pressed = os.pullEvent()
        if event == "char" then
            if user then
                user = "user" .. pressed
            else
                user = pressed
            end
        elseif event == "key" then
            if pressed == 8 then
                user = string.sub(user, 1, #user-1)
            elseif pressed == 13 then
                return user
            end
        end
    end
    checkPass(user)
end

function buttonCheck(x,y)
    if x > math.floor((sizeX - #button) / 2) and x < math.floor((sizeX + #button) / 2) then
        if y == math.floor((sizeY / 2) + 1) then
            term.setBackgroundColor(colors.white)
            -- term.setCursorPos(1,1)
            term.clear()
            getUser()
        end
    end
end

function getClick()
    event, t, x, y = os.pullEvent("mouse_click")
    buttonCheck(x,y)
end

while true do
    getClick()
end

Just hoping to tap into the vast knowledge of the pros. :)/>

edit: original code spacing didn't show up, fixed now.
Edited on 03 May 2014 - 07:59 PM
Dog #2
Posted 03 May 2014 - 10:27 PM
Well, I can see two places where you use term.setCursorPos but only provide one number (look around lines 26 and 28). I'd bet that's where your problem is.
Reciprocaterman #3
Posted 03 May 2014 - 10:32 PM
That was it, Thank you.

Can't believe I missed that. :blink:/>
Reciprocaterman #4
Posted 03 May 2014 - 11:17 PM
messed around with it a bit more and I'm quite proud of one of the functions I made in it.


function newRead(mod)
while true do
  term.setCursorPos(1,math.floor(sizeY / 2))
  term.clearLine()
  if info then
   term.setCursorPos(math.floor((sizeX - #info) / 2), math.floor(sizeY / 2))
  else
   term.setCursorPos(math.floor(sizeX / 2),math.floor(sizeY / 2))
  end
  if info and mod then
   for i = 1, #info do
	write(mod)
   end
  else
   print(info)
  end
  local event, pressed = os.pullEvent()
  if event == "char" then
   if info then
	info = info .. pressed
   else
	info = pressed
   end
  elseif event == "key" then
   if pressed == 14 then
	info = string.sub(info, 1, #info-1)
   elseif pressed == 28 then
	return info
   end
  end
end
end

Basically the read function wasn't able to do what I wanted so I made my own. :)/>

Full code is available http://pastebin.com/6Fx25AT9

Any ideas for optimization are greatly appreciated.
Edited on 03 May 2014 - 09:19 PM
Agoldfish #5
Posted 04 May 2014 - 05:38 AM
The CC highlighting package for sublime text 2 has a center print function…
Reciprocaterman #6
Posted 04 May 2014 - 04:03 PM
I'll have to check that out, but this function centers your text as you type it in. And you can put a mod in like * but you can use any character you want. So as you type yournamehere it comes out !!!!!!!!!!!!. or however else you want it.
blipman17 #7
Posted 04 May 2014 - 07:42 PM
always fun if your own way is more complicated that someone else's