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

help with text on monitors

Started by haskell911, 08 July 2012 - 10:29 AM
haskell911 #1
Posted 08 July 2012 - 12:29 PM
so i have this code and need to add more lines and center it….


monitor = peripheral.wrap("bottom")
monitor
.setTextScale(5)
monitor.setCursorPos(1,1)
monitor.write("welcome")
my server need a opener so i made this but also want to have it display that for about 10 seconds then repeat the same code with new text. any one know a code i can use for this idea?
1lann #2
Posted 08 July 2012 - 02:32 PM
Alright then, first we want to know the size of the monitor. So we use
mX,mY = monitor.getSize()
Then we want to know where to position the y axis
y = mY/2
And where to position the x axis. Positioning the x axis, i suppose is harder as it requires more calculations.
To calculate where the X position needs to be, you divide the length of the string (welcome) by 2 and round it (floor or ceil it). Then you get the center of the monitor and divide that by 2 and round it (floor or ceil). Subtract the 1st calculation (half of welcome) from the 2nd calculation (half of the monitor's x size). That is where you need to position the x axis. So it'll look like
x =  math.floor(mX/2) - math.floor(string.len("welcome")/2)
Now all we have to do is set the cursor position

monitor.setCursorPos(x, y)

As for your server opener. Try using this function and call it (I made it so you have to specify the Y axis because that's probably better for longer texts):

function showMonitor(text, y)
monitor.clear()
mX = monitor.getSize()
x =  math.floor(mX/2) - math.floor(string.len(text)/2)
monitor.setCursorPos(x, y)
monitor.write(text)
end

Hope that helped!
haskell911 #3
Posted 08 July 2012 - 03:35 PM
would you know how to add lines of text to this code by chance?
localfunction centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
write(text)

end
monitor = peripheral.wrap("bottom")
monitor.setTextScale(5)
monitor.setCursorPos(1,1)



centerText("Hello World")

and yes the above worked thank you very much :)/>/>
KevinW1998 #4
Posted 08 July 2012 - 03:46 PM

monitor.write("yourtext")
or what do you mean with add lines of text?
haskell911 #5
Posted 08 July 2012 - 04:25 PM

monitor.write("yourtext")
or what do you mean with add lines of text?
i would like to a more lines of text but one line down centered under hello world
KevinW1998 #6
Posted 08 July 2012 - 05:01 PM

term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2 + 1)
monitor.write("hello world")