Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function CenterCursor(mode, monitor)
- if (monitor == nil) then
- local w,h = term.getSize()
- local cw, ch = term.getCursorPos()
- if (mode == "width") then
- term.setCursorPos(w/2, ch)
- elseif (mode == "height") then
- term.setCursorPos(cw, h/2)
- else
- term.setCursorPos(w/2, h/2)
- end
- return term.getCursorPos()
- else
- local w,h = monitor.getSize()
- local cw, ch = monitor.getCursorPos()
- if (mode == "width") then
- monitor.setCursorPos(w/2, ch)
- elseif (mode == "height") then
- monitor.setCursorPos(cw, h/2)
- else
- monitor.setCursorPos(w/2, h/2)
- end
- return monitor.getCursorPos()
- end
- end
- function MoveCursor(modw, modh, monitor)
- if (monitor == nil) then
- local cw, ch = term.getCursorPos()
- term.setCursorPos(cw+modw, ch+modh)
- else
- local cw, ch = monitor.getCursorPos()
- monitor.setCursorPos(cw+modw, ch+modh)
- end
- end
- function WriteCentered(text, mode, monitor)
- if (monitor == nil) then
- local modif = #text/2
- CenterCursor(mode)
- MoveCursor(0-modif, 0)
- write(text)
- else
- local modif = #text/2
- CenterCursor(mode, monitor)
- MoveCursor(0-modif, 0, monitor)
- monitor.write(text)
- end
- end
- function PrintCentered(text, mode, monitor)
- if (monitor == nil) then
- local modif = #text/2
- CenterCursor(mode)
- MoveCursor(0-modif, 0)
- print(text)
- else
- local modif = #text/2
- CenterCursor(mode, monitor)
- MoveCursor(0-modif, 0, monitor)
- monitor.write(text .. "\n")
- end
- end
- -- program main functions
- function RenderMainMenu(monitor)
- if (monitor == nil) then
- term.clear()
- term.setCursorPos(0,0)
- CenterCursor()
- MoveCursor(0, -2)
- WriteCentered("Continue", "width")
- MoveCursor(0, 2)
- WriteCentered("Options", "width")
- MoveCursor(0, 2)
- WriteCentered("Exit", "width")
- else
- monitor.clear()
- monitor.setCursorPos(0,0)
- CenterCursor(monitor)
- MoveCursor(0, -2, monitor)
- WriteCentered("Continue", "width", monitor)
- MoveCursor(0, 2, monitor)
- WriteCentered("Options", "width", monitor)
- MoveCursor(0, 2, monitor)
- WriteCentered("Exit", "width", monitor)
- end
- end
- function Setup(monitor)
- local config = settings.get("IEMPconfig")
- print("Please click the top left and bottom right corners of each button until they are configured")
- RenderMainMenu(monitor)
- print("Please configure \"Continue\"")
- local event, side, x, y = os.pullEvent("monitor_touch")
- config.continueButtonPos[1] = x
- config.continueButtonPos[2] = y
- local event, side, x, y = os.pullEvent("monitor_touch")
- config.continueButtonPos[3] = x
- config.continueButtonPos[4] = y
- print("Please configure \"Options\"")
- local event, side, x, y = os.pullEvent("monitor_touch")
- config.optionsButtonPos[1] = x
- config.optionsButtonPos[2] = y
- local event, side, x, y = os.pullEvent("monitor_touch")
- config.optionsButtonPos[3] = x
- config.optionsButtonPos[4] = y
- print("Please configure \"Exit\"")
- local event, side, x, y = os.pullEvent("monitor_touch")
- config.exitButtonPos[1] = x
- config.exitButtonPos[2] = y
- local event, side, x, y = os.pullEvent("monitor_touch")
- config.exitButtonPos[3] = x
- config.exitButtonPos[4] = y
- settings.set("IEMPconfig", config)
- settings.save()
- print("Setup complete!")
- end
- --define settings for program
- settings.define("IEMPconfig", {
- description = "Configurations for Icons Energy Monitor Program",
- default = {
- newInstall = true,
- continueButtonPos = {0, 0, 0, 0},
- optionsButtonPos = {0, 0, 0, 0},
- exitButtonPos = {0, 0, 0, 0}
- },
- type = "table"
- })
- --program start
- -- start by finding monitor to use and loading settings as "config"
- mon = peripheral.find("monitor")
- settings.load()
- config = settings.get("IEMPconfig")
- -- if this is the first time the program is launched (or the settings are loaded with the default values for any other reason)
- if (config.newInstall == true) then
- Setup(mon)
- config = settings.get("IEMPconfig")
- config.newInstall = false
- settings.set("IEMPconfig", config)
- settings.save()
- end
- mon.clear()
- WriteCentered("Welcome", "none", mon)
- sleep(3)
- mon.clear()
- sleep(0.5)
- local loop = true
- while loop do
- RenderMainMenu(mon)
- local event, side, x, y = os.pullEvent("monitor_touch")
- if (x >= config.continueButtonPos[1] and x <= config.continueButtonPos[2] and y >= config.continueButtonPos[3] and config.continueButtonPos[4]) then
- write("Continue")
- elseif (x >= config.optionsButtonPos[1] and x <= config.optionsButtonPos[2] and y >= config.optionsButtonPos[3] and config.optionsButtonPos[4]) then
- write("Options")
- elseif (x >= config.exitButtonPos[1] and x <= config.exitButtonPos[2] and y >= config.exitButtonPos[3] and config.exitButtonPos[4]) then
- write("Exit")
- loop = false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment