Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Advanced GUI by Henness
- -- Version 1.2 2/5/2013
- -- Config
- local program = "Advanced Program's"
- local version = "v1.2"
- local author = "Henness"
- screenw,screenh = term.getSize()
- local shell = {}
- local tEnv = {
- ["shell"] = shell,
- }
- local select, bError, sError
- local enderChestMode = true
- local safeMode = true
- local questionInput = {
- new = {
- {input = {{text = "", sx = 15, sy = 5}, {text = "", sx = 15, sy = 6}, {text = "", sx = 15, sy = 7}}}
- },
- preset = {
- {input = {{text = "", sx = 15, sy = 4}, {text = "", sx = 15, sy = 6}, {text = "", sx = 15, sy = 7}, {text = "", sx = 15, sy = 8}}},
- {input = {{text = "", sx = 15, sy = 4}, {text = "", sx = 15, sy = 6}, {text = "", sx = 15, sy = 7}, {text = "", sx = 15, sy = 8}}},
- {input = {{text = "", sx = 15, sy = 4}, {text = "", sx = 15, sy = 6}, {text = "", sx = 15, sy = 7}, {text = "", sx = 15, sy = 8}}}
- }
- }
- -- Functions
- function saveTable(table,name)
- local file = fs.open(name,"w")
- file.write(textutils.serialize(table))
- file.close()
- end
- function loadTable(name)
- local file = fs.open(name,"r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- function printReverse(str, xpos, ypos)
- term.setCursorPos(xpos - (#str-1), ypos)
- term.write (str)
- end
- function printForward(str, xpos, ypos)
- term.setCursorPos(xpos, ypos)
- term.write (str)
- end
- function printCentered(str, ypos)
- term.setCursorPos((screenw/2 - #str/2) + 1, ypos)
- term.write(str)
- end
- function printRight(str, ypos)
- term.setCursorPos(screenw - #str, ypos)
- term.write(str)
- end
- function printLeft(str, ypos)
- term.setCursorPos(1, ypos)
- term.write(str)
- end
- function readLines( sLines, _sReplaceChar )
- term.setCursorBlink( true )
- local w, h = term.getSize()
- local nPos = 0
- local nSelect = 1
- local vSelect = 1
- local function redraw( _sCustomReplaceChar )
- local nScroll = 0
- if sLines[nSelect].sx + nPos >= w then
- nScroll = (sLines[nSelect].sx + nPos) - w
- end
- term.setCursorPos( sLines[nSelect].sx, sLines[nSelect].sy )
- local sReplace = _sCustomReplaceChar or _sReplaceChar
- if sReplace then
- term.write( string.rep(sReplace, string.len(sLines[nSelect].text) - nScroll) )
- else
- term.write( string.sub( sLines[nSelect].text, nScroll + 1 ) )
- end
- term.setCursorPos( sLines[nSelect].sx + nPos - nScroll, sLines[nSelect].sy )
- end
- for nVar, tVar in ipairs(sLines) do
- nSelect = nVar
- redraw()
- if tVar.text ~= "" then
- vSelect = nVar
- end
- end
- nSelect = vSelect
- nPos = #sLines[nSelect].text
- redraw()
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos ) .. param .. string.sub( sLines[nSelect].text, nPos + 1 )
- nPos = nPos + 1
- redraw()
- elseif sEvent == "key" then
- if param == keys.enter then
- -- Enter
- if nSelect < #sLines then
- nSelect = nSelect + 1
- if sLines[nSelect].text == "" then
- nPos = 0
- else
- nPos = #sLines[nSelect].text
- end
- redraw()
- else
- break
- end
- elseif param == keys.left then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.right then
- -- Right
- if nPos < string.len(sLines[nSelect].text) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == keys.up or param == keys.down then
- -- Up or down
- if param == keys.up then
- -- Up
- if nSelect > 1 then
- nSelect = nSelect - 1
- if sLines[nSelect].text == "" then
- nPos = 0
- else
- nPos = #sLines[nSelect].text
- end
- redraw()
- end
- else
- -- Down
- if nSelect < #sLines then
- nSelect = nSelect + 1
- if sLines[nSelect].text == "" then
- nPos = 0
- else
- nPos = #sLines[nSelect].text
- end
- redraw()
- else
- break
- end
- end
- redraw()
- elseif param == keys.backspace then
- -- Backspace
- if nPos > 0 then
- redraw(" ");
- sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos - 1 ) .. string.sub( sLines[nSelect].text, nPos + 1 )
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.home then
- -- Home
- nPos = 0
- redraw()
- elseif param == keys.delete then
- if nPos < string.len(sLines[nSelect].text) then
- redraw(" ");
- sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos ) .. string.sub( sLines[nSelect].text, nPos + 2 )
- redraw()
- end
- elseif param == keys["end"] then
- -- End
- nPos = string.len(sLines[nSelect].text)
- redraw()
- end
- end
- end
- term.setCursorBlink( false )
- return sLines
- end
- local function drawHeader()
- printCentered(program, 1)
- printLeft(string.rep("-", screenw), 2)
- printLeft("["..version.."]"..string.rep("-", screenw-#author-#version-7).."[by "..author.."]", screenh)
- end
- function drawMain()
- term.clear()
- drawHeader()
- printLeft(" OreFinder", 4)
- printLeft(" Tunnel", 5)
- printLeft(" Exit", screenh-2)
- local ypos = 4
- if select == 2 then ypos = 5
- elseif select == 3 then ypos = screenh-2 end
- printLeft(">", ypos)
- end
- function drawOreFinder()
- term.clear()
- drawHeader()
- if fs.exists("ofpreset") then
- preset = loadTable("ofpreset")
- else
- preset = {
- [1] = {
- values = {"000", "000", "00", "00", "PRESET #1", "0", true, true}
- },
- [2] = {
- values = {"000", "000", "00", "00", "PRESET #2", "0", true, true}
- },
- [3] = {
- values = {"000", "000", "00", "00", "PRESET #3", "0", true, true}
- }
- }
- saveTable(preset, "ofpreset")
- end
- local namea = preset[1].values[5]
- local nameb = preset[2].values[5]
- local namec = preset[3].values[5]
- printLeft(" Continue", 4)
- printLeft(" New", 5)
- printLeft(" "..namea, 7)
- printLeft(" "..nameb, 8)
- printLeft(" "..namec, 9)
- printLeft(" Back", screenh-2)
- for i=1,screenh-3 do
- printRight("|"..string.rep(" ", 21), i+2)
- end
- if select == 1 then
- printLeft(">", 4)
- elseif select == 2 then
- printLeft(">", 5)
- elseif select == 3 or select == 4 or select == 5 then
- local vPreset = select - 2
- local width = preset[vPreset].values[1]
- local length = preset[vPreset].values[2]
- local hours = preset[vPreset].values[3]
- local minutes = preset[vPreset].values[4]
- local ignore = preset[vPreset].values[6]
- local safemode = preset[vPreset].values[7]
- local enderchest = preset[vPreset].values[8]
- if select == 3 then
- printRight("| "..namea..string.rep(" ", 19-#namea), 4)
- printLeft(">", 7)
- elseif select == 4 then
- printRight("| "..nameb..string.rep(" ", 19-#nameb), 4)
- printLeft(">", 8)
- elseif select == 5 then
- printRight("| "..namec..string.rep(" ", 19-#namec), 4)
- printLeft(">", 9)
- end
- printRight("| Size: "..string.rep("0", 3-#width)..width.."x"..string.rep("0", 3-#length)..length..string.rep(" ", 3), 6)
- printRight("| Time: "..string.rep("0", 2-#hours)..hours..":"..string.rep("0", 2-#minutes)..minutes..string.rep(" ", 4), 7)
- printRight("| Ignore: "..string.rep(" ", 2-#ignore)..ignore..string.rep(" ", 6), 9)
- if safemode then
- printRight("| SafeMode Yes ", 10)
- else
- printRight("| SafeMode No ", 10)
- end
- if enderchest then
- printRight("| EnderChest Yes ", 11)
- else
- printRight("| EnderChest No ", 11)
- end
- elseif select == 6 then
- printLeft(">", screenh-2)
- end
- end
- function drawOreFinderContinue()
- local continueList = {
- "+-------------------------------+",
- "| |",
- "| Would you like to continue |",
- "| your previous excavation? |",
- "| |",
- "| YES NO |",
- "+-------------------------------+"
- }
- for i=1,#continueList do
- if i == #continueList-1 then
- if select == 1 then
- printCentered("| [YES] NO |", 3 + i)
- elseif select == 2 then
- printCentered("| YES [NO] |", 3 + i)
- end
- else
- printCentered(continueList[i], 3 + i)
- end
- end
- end
- function drawOreFinderStart()
- local startList = {
- "+-------------------------------+",
- "| |",
- "| Are you sure you would like |",
- "| to start a new excavation? |",
- "| |",
- "| YES NO |",
- "+-------------------------------+"
- }
- for i=1,#startList do
- if i == #startList-1 then
- if select == 1 then
- printCentered("| [YES] NO |", 3 + i)
- elseif select == 2 then
- printCentered("| YES [NO] |", 3 + i)
- end
- else
- printCentered(startList[i], 3 + i)
- end
- end
- end
- function drawOreFinderNoFile()
- local nofileList = {
- "+-------------------------------+",
- "| |",
- "| ERROR! |",
- "| No save file was found. |",
- "| |",
- "| [OK] |",
- "+-------------------------------+"
- }
- for i=1,#nofileList do
- printCentered(nofileList[i], 3 + i)
- end
- end
- function drawOreFinderInputError()
- local inputerrorList = {
- "+-------------------------------+",
- "| |",
- "| ERROR! |",
- "| Please input valid variables! |",
- "| |",
- "| [OK] |",
- "+-------------------------------+"
- }
- for i=1,#inputerrorList do
- printCentered(inputerrorList[i], 3 + i)
- end
- end
- function drawOreFinderPreset()
- local presetList = {
- "+-------------------------------+",
- "| |",
- "| |",
- "| |",
- "| |",
- "| START CANCEL EDIT |",
- "+-------------------------------+"
- }
- Name = preset[presetstate].values[5]
- Width = preset[presetstate].values[1]
- Length = preset[presetstate].values[2]
- Hours = preset[presetstate].values[3]
- Minutes = preset[presetstate].values[4]
- Ignore = preset[presetstate].values[6]
- for i=1,#presetList do
- if i == 2 or i == 5 then
- printCentered("| |", 3 + i)
- if i == 2 then
- printCentered(Name, 3 + i)
- end
- elseif i == 3 then
- printCentered("| Size: " .. string.rep("0", 3-#Width) .. Width .. "x" .. string.rep("0", 3-#Length) .. Length .. string.rep(" ", 12) .. "|", 3 + i)
- elseif i == 4 then
- printCentered("| Time: " .. string.rep("0", 2-#Hours) .. Hours .. ":" .. string.rep("0", 2-#Minutes) .. Minutes .. string.rep(" ", 13) .. "|", 3 + i)
- elseif i == #presetList-1 then
- if select == 1 then
- printCentered("| [START] CANCEL EDIT |", 3 + i)
- elseif select == 2 then
- printCentered("| START [CANCEL] EDIT |", 3 + i)
- elseif select == 3 then
- printCentered("| START CANCEL [EDIT] |", 3 + i)
- end
- else
- printCentered(presetList[i], 3 + i)
- end
- end
- end
- function drawOreFinderNew()
- local sLine8 = " Ender Chest "
- local sLine9 = " Safe Mode "
- term.clear()
- drawHeader()
- printCentered("Input variables for the excavation.", 3)
- printLeft(" Width: " .. questionInput.new[1].input[1].text, 5)
- printLeft(" Length: " .. questionInput.new[1].input[2].text, 6)
- printLeft(" Ignore: " .. questionInput.new[1].input[3].text, 7)
- if enderChestMode then
- printLeft(sLine8 .. " Yes ", 8)
- else
- printLeft(sLine8 .. " No ", 8)
- end
- if safeMode then
- printLeft(sLine9 .. " Yes ", 9)
- else
- printLeft(sLine9 .. " No ", 9)
- end
- printLeft(string.rep("-", screenw), screenh-2)
- printCentered(" DONE ", screenh-1)
- if select == 1 then
- readLines(questionInput.new[1].input)
- Width,Length,Ignore = tonumber(questionInput.new[1].input[1].text),tonumber(questionInput.new[1].input[2].text),tonumber(questionInput.new[1].input[3].text)
- if Width ~= nil and Length ~= nil and Ignore ~= nil and Width > 0 and Length > 0 and Ignore >= 0 and Ignore < 15 then
- select = 2
- mopt[menustate].draw()
- else
- menustate = mopt[menustate].options[select]
- mopt[menustate].draw()
- end
- elseif select == 2 then
- if enderChestMode then
- printLeft(sLine8 .. "[Yes]", 8)
- else
- printLeft(sLine8 .. "[No] ", 8)
- end
- elseif select == 3 then
- if safeMode then
- printLeft(sLine9 .. "[Yes]", 9)
- else
- printLeft(sLine9 .. "[No] ", 9)
- end
- elseif select == 4 then
- printCentered("[DONE]", screenh-1)
- end
- end
- function drawOreFinderEdit()
- local sLine9 = " Ender Chest "
- local sLine10 = " Safe Mode "
- term.clear()
- drawHeader()
- printCentered("Input variables for preset " .. presetname .. ".", 3)
- printLeft(" Name: " .. questionInput.preset[presetstate].input[1].text, 4)
- printLeft(" Width: " .. questionInput.preset[presetstate].input[2].text, 6)
- printLeft(" Length: " .. questionInput.preset[presetstate].input[3].text, 7)
- printLeft(" Ignore: " .. questionInput.preset[presetstate].input[4].text, 8)
- if enderChestMode then
- printLeft(sLine9 .. " Yes ", 9)
- else
- printLeft(sLine9 .. " No ", 9)
- end
- if safeMode then
- printLeft(sLine10 .. " Yes ", 10)
- else
- printLeft(sLine10 .. " No ", 10)
- end
- printLeft(string.rep("-", screenw), screenh-2)
- printCentered(" DONE ", screenh-1)
- if select == 1 then
- readLines(questionInput.preset[presetstate].input)
- Name,Width,Length,Ignore = tostring(questionInput.preset[presetstate].input[1].text),tonumber(questionInput.preset[presetstate].input[2].text),tonumber(questionInput.preset[presetstate].input[3].text),tonumber(questionInput.preset[presetstate].input[4].text)
- if Name ~= nil and #Name < 15 and Width ~= nil and Length ~= nil and Ignore ~= nil and Width > 0 and Length > 0 and Ignore >= 0 and Ignore < 15 then
- preset[presetstate].values[5] = Name
- preset[presetstate].values[1] = tostring(Width)
- preset[presetstate].values[2] = tostring(Length)
- preset[presetstate].values[6] = tostring(Ignore)
- preset[presetstate].values[7] = enderChestMode
- preset[presetstate].values[8] = safeMode
- saveTable(preset, "ofpreset")
- select = 2
- mopt[menustate].draw()
- else
- menustate = mopt[menustate].options[select]
- mopt[menustate].draw()
- end
- elseif select == 2 then
- if enderChestMode then
- printLeft(sLine9 .. "[Yes]", 9)
- else
- printLeft(sLine9 .. "[No] ", 9)
- end
- elseif select == 3 then
- if safeMode then
- printLeft(sLine10 .. "[Yes]", 10)
- else
- printLeft(sLine10 .. "[No] ", 10)
- end
- elseif select == 4 then
- printCentered("[DONE]", screenh-1)
- end
- end
- function drawErrorMenu()
- term.clear()
- drawHeader()
- printLeft(string.rep("-", screenw), screenh-2)
- printCentered("[OK]", screenh-1)
- term.setCursorPos(1, 3)
- print(sError)
- end
- function gui()
- term.clear()
- select = 1
- menustate = "main"
- mopt = {
- ["main"] = {
- options = {"orefinder", "tunnel", "exit"},
- draw = drawMain
- },
- ["orefinder"] = {
- options = {"orefindercontinue", "orefindernew", "orefinderpreset", "orefinderpreset", "orefinderpreset", "main"},
- draw = drawOreFinder
- },
- ["orefindercontinue"] = {
- options = {"orefinderrun", "orefinder"},
- draw = drawOreFinderContinue
- },
- ["orefindernofile"] = {
- options = {"orefinder"},
- draw = drawOreFinderNoFile
- },
- ["orefindernew"] = {
- options = {"orefinderinputerror", "enderchest", "safemode", "orefinderstart"},
- draw = drawOreFinderNew
- },
- ["orefinderpreset"] = {
- options = {"orefinderstart", "orefinder", "orefinderedit"},
- draw = drawOreFinderPreset
- },
- ["orefinderstart"] = {
- options = {"orefinderrun", "orefinder"},
- draw = drawOreFinderStart
- },
- ["orefinderedit"] = {
- options = {"orefinderpreseterror", "enderchest", "safemode", "orefinder"},
- draw = drawOreFinderEdit
- },
- ["orefinderinputerror"] = {
- options = {"orefindernew"},
- draw = drawOreFinderInputError
- },
- ["orefinderpreseterror"] = {
- options = {"orefinderedit"},
- draw = drawOreFinderInputError
- },
- ["errormenu"] = {
- options = {"main"},
- draw = drawErrorMenu
- }
- }
- while true do
- mopt[menustate].draw()
- local id, key = os.pullEvent("key")
- -- DOWN = 208 UP = 200 LEFT = 203 RIGHT = 205 ENTER = 28
- if key == 200 and select > 1 then
- select = select-1
- elseif key == 208 and select < #mopt[menustate].options then
- select = select+1
- elseif key == 203 and select > 1 then
- select = select-1
- elseif key == 205 and select < #mopt[menustate].options then
- select = select+1
- elseif key == 28 then
- if mopt[menustate].options[select] == "exit" then
- break
- elseif mopt[menustate].options[select] == "orefindercontinue" then
- if fs.exists("orefinder.save") then
- menustate = "orefindercontinue"
- else
- menustate = "orefindernofile"
- end
- select = 1
- elseif mopt[menustate].options[select] == "orefinderrun" then
- if menustate ~= "orefindercontinue" then
- local _tSave = {
- w = 1,
- l = 1,
- width = Width,
- length = Length,
- ignore = Ignore,
- safemode = safeMode,
- enderchestmode = enderChestMode
- }
- saveTable(_tSave, "orefinder.save")
- end
- bError, sError = pcall(loadfile("advancedorefinder", "orefinder.save"))
- if not bError then
- menustate = "errormenu"
- else
- menustate = "main"
- end
- select = 1
- elseif mopt[menustate].options[select] == "tunnel" then
- -- Run Tunnel
- bError, sError = pcall(loadfile("advancedtunnel"))
- if not bError then
- menustate = "errormenu"
- else
- menustate = "main"
- end
- select = 1
- elseif mopt[menustate].options[select] == "enderchest" then
- if enderChestMode then
- enderChestMode = false
- else
- enderChestMode = true
- end
- elseif mopt[menustate].options[select] == "safemode" then
- if safeMode then
- safeMode = false
- else
- safeMode = true
- end
- else
- if mopt[menustate].options[select] == "orefinderpreset" then
- drawOreFinder()
- if select == 3 then
- presetstate = 1
- presetname = "one"
- elseif select == 4 then
- presetstate = 2
- presetname = "two"
- elseif select == 5 then
- presetstate = 3
- presetname = "three"
- end
- end
- menustate = mopt[menustate].options[select]
- select = 1
- end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- end
- -- RUN
- gui()
Advertisement
Add Comment
Please, Sign In to add comment