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

Scrollable Pages Program Help

Started by ZombieSteve999, 21 May 2015 - 03:20 PM
ZombieSteve999 #1
Posted 21 May 2015 - 05:20 PM
Hey guys, I made a code for my server, it's basically optimised to scroll pages .

Spoilerargs = {…}

–Main "packet" table DO NOT CHANGE
–Haupt "Paket" table NICHT VER壟F�83?0壟F�NDERN
info = {}
info.func = {}
info.pages = {}

function info:addPage(text)

self.pages[self.func.maxPage + 1] = text
self.func.maxPage = self.func.maxPage + 1
self.func.minPage = 1
self.func.currPage = self.func.minPage

end



function info.func:getBackPos()
return {x=1, y=1}
end

function info.func:getFrontPos()
w, h = term.getSize()
return {x=w, y=1}
end

–File loading function
–Datei lade funktion
function info.func:setPages(path)

if(path ~= nil and fs.isDir(path)) then

files = fs.list(path)

for k, v in ipairs(files) do

–Parameters for the file, you can add more
–Parameter f壟F�83壟F�BCr die datei, du kannst mehr einf壟F�83壟F�BCgen
if(v ~= nil and not fs.isDir(v)) then

f = fs.open(path.."/"..v, "r")

cont = {}

i = 1

while(true) do

local l = f.readLine()

if(l == nil) then break end

cont = l

i = i + 1

end

cont.minScroll = 1

cont.maxScroll = i - 17
if cont.maxScroll < 1 then
cont.maxScroll = 0
end

info:addPage(cont)

f.close()
end
end



end

end

–Main function a lot of hardcoding
–Hauptfunktion viel hardgecodetes
function info.func:redraw()

w, h = term.getSize()

bp = self:getBackPos()
fp = self:getFrontPos()

term.clear()

term.setCursorPos(bp.x, bp.y)
term.write("<")

toWrite = self.currPage.."/"..self.maxPage
term.setCursorPos((w / 2) - (string.len(toWrite) / 2), 1)
term.write(toWrite)

term.setCursorPos(fp.x, fp.y)
term.write(">")

term.setCursorPos(1, 2)
term.write("^")

term.setCursorPos(w, 2)
term.write("v")

term.setCursorPos(1, 3)

local page = info.pages[self.currPage]

rLine = 3

count = self.pageScroll

toPrint = #page

if toPrint > 17 then
toPrint = 17
end

for i=1,toPrint do

term.setCursorPos(1, rLine)
term.write(page[count])
rLine = rLine + 1
count = count + 1

end

event, i, x, y = os.pullEvent()

if event == "mouse_scroll" then

if(page.maxScroll > 0) then

self.pageScroll = self.pageScroll + i

if(self.pageScroll < page.minScroll) then
self.pageScroll = page.minScroll
end

if self.pageScroll > page.maxScroll then
self.pageScroll = page.maxScroll
end

end

elseif event == "monitor_touch" or event == "mouse_click" then

if x == bp.x and y == bp.y then

self.currPage = self.currPage - 1

if self.currPage < self.minPage then
self.currPage = self.maxPage
end

elseif x == fp.x and y == fp.y then

self.currPage = self.currPage + 1

if self.currPage > self.maxPage then
self.currPage = self.minPage
end

elseif x == 1 and y == 2 then

if(page.maxScroll > 0) then

self.pageScroll = self.pageScroll - 1

if(self.pageScroll < 1) then
self.pageScroll = 1
end

end

elseif x == w and y == 2 then

if(page.maxScroll > 0) then

self.pageScroll = self.pageScroll + 1

if self.pageScroll > page.maxScroll then
self.pageScroll = page.maxScroll
end

end

end

end

end

–Base parameters DO NOT CHANGE
–Grund Parameter NICHT VER壟F�83?0壟F�NDERN
info.func.minPage = 0
info.func.maxPage = 0
info.func.currPage = info.func.minPage;
info.func.pageScroll = 1

–The path to the folder with the informational files
–Der pfad zum Verzeichnis mit den Dateien die gezeigt werden sollen
toSearchPath = "disk/info"

info.func:setPages(toSearchPath)

–You can add other stuff to the loop
–In diesem loop kann man auch andere dinge tun
while(true) do
info.func:redraw()
end


If you run it you'll see '<' and '>' on the top left and right respectively. I'm a bit new to computercraft so I was wondering if anyone could help me with the following:

1] How do I display this stuff on an advanced monitor (I did this once, it worked perfectly but I do not remember how I did it, and the 'arrows' weren't clickable really clickable)
2] How do I make it clickable on the advanced monitor?

Please don't offer any alternatives like adding a password program for it :D/> . Alternative programs aren't really welcome either :/

Thanks :3
Edited on 21 May 2015 - 03:49 PM
KingofGamesYami #2
Posted 21 May 2015 - 06:06 PM
1) wrap the monitor using peripheral.wrap, then use term.redirect to redirect to it. After that, run your program normally
2) You could use the monitor program (monitor <side> program), or you could pull monitor_touch events instead of mouse_click.
Bomb Bloke #3
Posted 22 May 2015 - 01:16 AM
Sorta worth noting that using the "monitor" script will solve both questions at the same time.
ZombieSteve999 #4
Posted 22 May 2015 - 08:14 AM
1) wrap the monitor using peripheral.wrap, then use term.redirect to redirect to it. After that, run your program normally
2) You could use the monitor program (monitor <side> program), or you could pull monitor_touch events instead of mouse_click.

I already tried that, for some reason it doesn't work.

Sorta worth noting that using the "monitor" script will solve both questions at the same time.

Sorry I'm quite new to lua and I got how to do this from some forum, what do you mean?
Lyqyd #5
Posted 22 May 2015 - 03:16 PM
Show us the code you tried that "didn't work" and go in to more detail on how it "didn't work", please.
HPWebcamAble #6
Posted 24 May 2015 - 08:13 AM
Sorta worth noting that using the "monitor" script will solve both questions at the same time.

Sorry I'm quite new to lua and I got how to do this from some forum, what do you mean?

All computers have a 'monitor' program that lets you run another program, and display everything on a monitor

To run it:

monitor <monitor side> <other program name> <any arguments for the other program>