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

Centering text on a monitor

Started by rpg4mc, 18 April 2014 - 09:17 PM
rpg4mc #1
Posted 18 April 2014 - 11:17 PM
Hello,

I'm trying to make a statistics screen on a 3x2 monitor but my text doesn't want to behave.


local mon = peripheral.wrap("right")
local x,y = mon.getSize()
mon.clear()
mon.setTextColor(colors.red)
mon.setTextScale(1)
text = "Stats"
mon.setCursorPos(math.floor(x - text:len())/2, 3)
mon.write(text)

With this code, everything is perfectly centered but if I want to make the words bigger (textScale 2) it's obviously not centered anymore. Multiplying the length by 2 did not work.

Thanks in advance
CometWolf #2
Posted 18 April 2014 - 11:21 PM
The problem is you store the current monitor size in the variables x and y, then you change the size. The variables won't change to reflect this new size, unless you again store the values returned by mon.getSize() in them. Basically you should change the size, then store the size, not the other way around.
rpg4mc #3
Posted 18 April 2014 - 11:23 PM
Ooh, by changing the TextScale, I also change the Size() ?
rpg4mc #4
Posted 18 April 2014 - 11:36 PM
Ok I change it around but it seems not fixed:


local mon = peripheral.wrap("right")
mon.setTextScale(2)
x,y = mon.getSize()
print(x..","..y)
mon.clear()
mon.setBackgroundColor(colors.black)
mon.setTextColor(colors.red)
text = "Stats"
mon.setCursorPos(math.floor((x - text:len())/2), 2)
mon.write(text)
mon.setCursorPos(1,6)
mon.setTextColor(colors.white)
mon.setTextScale(1)
x,y = mon.getSize()
mon.write("Logs collected: " .. "test")
mon.setCursorPos(1,7)
mon.write("Amount of trees chopped: " .. "test")
mon.setCursorPos(1,8)
mon.write("% logs per tree: " .. "test")

http://prntscr.com/3bax3r
CometWolf #5
Posted 18 April 2014 - 11:40 PM
What you seem to not understand, is that setTextScale affects the entire monitor, not just the text you are currently writing. Basically if you change it to 2, write something, then change it back to 1, what you wrote will revert to a scale of 1. Only way around this would be to use multple monitors.
rpg4mc #6
Posted 18 April 2014 - 11:42 PM
That's unfortunate. Anyway, thanks for the help :)/>