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

Making a certain area of a screen scroll.

Started by Roboguy99, 25 November 2013 - 11:42 AM
Roboguy99 #1
Posted 25 November 2013 - 12:42 PM
I've been working on a chat system which works with Rednet and the actual chat bit works fine. My problem is, after about 10 or so messages, the screen runs out of room. I've made a very basic scrolling system which is extremely buggy (1 message will show, then nothing will, and then a large line will show) and I don't really know what to do any more.

Here is all my code so far:

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

lastMsg = 0
rednet.open("top")
write("Enter the ID of your chatting partner: ")
id = tonumber(read())

write("Enter their name too: ")
name = read()

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

local rMsg = ""
local sMsg = ""

function gui()
	width, height = term.getSize()
	term.setCursorPos(1, height - 3)
	term.setTextColor(colors.white)
	
	for i=1, width do
		write("-")
	end
end

function display(isReceived)
	if isReceived == true then
		nametag = name
	else
		nametag = "You"
	end
	
	term.setCursorPos(1, lastMsg+1)
	
	x, y = term.getCursorPos()
	
	if y == height-5 then
		term.scroll(1)
		term.setCursorPos(1, height-5)
		term.clearLine()
	end
	
	term.setTextColor(colors.red)
	
	if sMsg ~= "" then
		write("[" .. nametag .. "]")
		
		term.setTextColor(colors.lime)
		
			if isReceived == true then
					write(rMsg)
			else
					write(sMsg)
			end

		if lastMsg < height-4 then
			lastMsg = lastMsg + 1
		else
			lastMsg = height-6
		end
	end
end

function send()
	term.setTextColor(colors.red)
	write("Message: ")
	term.setTextColor(colors.white)
	sMsg = read()
	rednet.send(id, sMsg)
	display(false)
end

function receive()
	id, rMsg = rednet.receive()
	display(true)
end

gui()

while true do
	gui()
	parallel.waitForAny(send, receive)
	
	term.setCursorPos(1, height - 2)
	term.clearLine()
	term.setCursorPos(1, height - 1)
	term.clearLine()
	term.setCursorPos(1, height)
	term.clearLine()
	
end
Bomb Bloke #2
Posted 26 November 2013 - 06:35 AM
function display(isReceived)
        if sMsg == "" then return end  -- End the function early if there's no message to write.

        if isReceived then  -- "isReceived == true" is pointless. The "if" statement is already checking for a "true" condition.
                nametag = name
        else
                nametag = "You"
        end

        term.setCursorPos(1, nextMsg)  -- Set "nextMsg" to 1 at the start of your script.

        term.setTextColor(colors.red)

        write("[" .. nametag .. "]")

        term.setTextColor(colors.lime)

        if isReceived then
        	write(rMsg)
        else
                write(sMsg)
        end

        x, y = term.getCursorPos()

        nextMsg = y + 1  -- We have to base this on where the last message ended, 'cause that might've covered multiple lines.

        -- Check to see if we just reached the GUI at the bottom of the screen:
        if y > height-4 then

          -- Clear the rest of the last line we just wrote to (as it may be overlapping the ----'s or something).
          write( string.rep(" ",width-x+1) )

          -- Scroll up until we're clear of the GUI, then redraw the GUI.
          term.scroll(y-height+4)
          gui()

          -- 'cause we just scrolled, the next line must be rendered at:
          nextMsg = height - 3
        end
end
Roboguy99 #3
Posted 26 November 2013 - 11:46 AM
Bomb Bloke you are a genius! It works perfectly! Thank you for all your help!