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

Text Adapting To Any Gui Size

Started by FuzzyPurp, 11 February 2012 - 12:06 AM
FuzzyPurp #1
Posted 11 February 2012 - 01:06 AM
I'd like for the next version of Redworks to have all programs ascii adjust to any screen size a user has. I played around with the syntax, but i'm a pure noob at getting it to work. Any snippets or examples of how i should do it, please submit.

Heres a quick program for example. How would i have this Fit to any GUI size a user has.

Spoiler

  term.setCursorBlink( false )

function clear()
  term.clear()
  term.setCursorPos(1,1)
end

function topBar()
print("		**"..os.version() .."0 [+] Redworks "..redworks.version().."**")
end

function mainMenu()
redworks.showWindow(7,4,36,10,"Redworks Control Panel","n 1.XXXXX	4.XXXXX	 7.XXXXXnn 2.XXXXX	5.XXXXX	8.XXXXXnn 3.XXXXX	  6.XXXXX	9.XXXXX")
end

function bottomBar()
term.setCursorPos(1,17)
print("PC-ID# " .. os.getComputerID())
term.setCursorPos(18,17)
print("[".. redworks.getUser().."]")
term.setCursorPos(40,17)
print("["..textutils.formatTime( os.time(), false ).."]")
end

clear()
topBar()
mainMenu()
bottomBar()

term.setCursorPos(1,9)
print"PREVnn<---"
term.setCursorPos(46,9)
print"NEXT"
print"nn--->"

while true do
bottomBar()
fault, event, param1, param2, param3 = pcall(os.pullEvent)
  if fault == false then
  elseif event == "key" and param1 == 2 then
  clear()
  break end
end
Espen #2
Posted 11 February 2012 - 02:20 AM
You can make use of term.getSize() which returns two values: the maximum x and y value of the terminal.
I hope this isn't too vague. If it is then I'd be happy to give you an example.
Edited on 11 February 2012 - 01:22 AM
FuzzyPurp #3
Posted 11 February 2012 - 02:38 AM
You can make use of term.getSize() which returns two values: the maximum x and y value of the terminal.
I hope this isn't too vague. If it is then I'd be happy to give you an example.

Yes i' m aware of that. And yes, too vague :D/>/>
Casper7526 #4
Posted 11 February 2012 - 02:48 AM

ScreenX, ScreenY = term.getSize()

-- lets say you wanted a line across the top

print (string.rep("-",ScreenX - 1))

-- Basically you have to change everywhere in your code that does something at a certain part of the screen.

term.setCursorPos(1,9)
term.setCursorPos(1,math.ceil(ScreenY / 2))

term.setCursorPos(46,17)
term.setCursorPos(ScreenX - 4, ScreenY - 3)
Espen #5
Posted 11 February 2012 - 02:52 AM
This is a function I'm using for my codes atm:

-- Centers text for given string 's' and given 'y'-coordinate.
-- Also if the third parameter is 'true' the line is cleard before writing to it.
function writeCentered( s, y, ... )
	local sizeX, sizeY = term.getSize()
	local x = math.ceil( ((sizeX - 1) / 2) ) - math.ceil( #s / 2 ) + 1  -- Favors left-aligned centering if the length of the string is an odd number.
	term.setCursorPos( x, y )
	if arg[1] then term.clearLine() end
	write( s )
end
-- Test Inputs
term.clear()
writeCentered( ">......................>MM<.................1234<", 4 )	 -- The terminal is x = 50 wide, but you can only write x - 1 = 49 characters without a forced new-line.
writeCentered( ">......................>MM<.................123<", 5 )
writeCentered( ">......................>MM<.................12<", 6 )
writeCentered( ">......................>MM<.................1<", 7 )
writeCentered( ">......................>MM<.................<", 8 )
print()

EDIT: Casper ninja'd me. :D/>/>
Note to self: Copy text and refresh page before posting.^^
Edited on 11 February 2012 - 01:53 AM
Casper7526 #6
Posted 11 February 2012 - 03:21 AM
Dont be mad, and I liked the profile pic I set for you alot better!
FuzzyPurp #7
Posted 11 February 2012 - 04:03 AM
:D/>/>