Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local oldPull = os.pullEvent
- os.pullEvent = os.pullEventRaw
- term.clear = function(c) if c then term.setBackgroundColor(c) end term.native.clear() term.setCursorPos(1,1) end
- setColors = function(b,t) if b then term.setBackgroundColor(b) end if t then term.setTextColor(t) end end
- clickedIn = function(mx,my,x,y,w,h) return (mx >= x and mx <= x + w and my >= y and my <= y + h) end
- startsWith = function(str, prefix) prefix = tostring( prefix ) return str:sub( 1, prefix:len()) == prefix end
- local localGui = {
- ["New button"] = { x = 1, y = 1, w = 5, h = 6, action = new },
- ["Generate Code"] = { x = 1, y = 1, w = 5, h = 6, action = generate },
- ["Cancel"] = { x = 1, y = 1, w = 5, h = 6, action = function() os.pullEvent = oldPull error() end },
- }
- local genericFuncs = [[local function checkButtonClicked(mouseX, mouseY)
- for _,v in pairs(buttons) do
- if mouseX >= v.x and mouseX <= v.x + v.w and mouseY >= v.y and mouseY <= v.y + v.h then
- v.action()
- return true
- end
- end
- return false
- end
- local function drawButtons()
- for k, v in pairs(buttons) do
- term.setBackgroundColor(v.bgColor)
- term.setTextColor(v.txtColor)
- for i = 0, v.h - 1 do
- term.setCursorPos(v.x,v.y+i)
- write(string.rep(" ", v.w))
- end
- term.setCursorPos(v.x,v.y)
- write(k)
- end
- end
- -- example main loop
- while true do
- drawButtons()
- local event = { os.pullEvent() }
- if event[1] == "mouse_click" then
- if event[2] == 1 then -- left click
- checkButtonClicked(event[3]. event[4])
- end
- end
- end
- ]]
- local progButtons = {}
- local progFuncs = {}
- local function getInput(text, validationFunc, compareVal)
- local output
- while true do
- write(text)
- local input = read()
- local ok, val, err = validationFunc(input, compareVal)
- if ok then
- if err then
- if startsWith(err, "Error:") then
- term.setTextColor(colors.red)
- elseif startsWith(err, "Warning:") then
- term.setTextColor(colors.yellow)
- end
- print(err)
- term.setTextColor(colors.white)
- write("Continue Anyway? (y/n) ")
- local con = read()
- if con:lower() == "y" or con:lower() == "yes" then
- output = val
- break
- end
- else
- output = val
- break
- end
- else
- if startsWith(err, "Error:") then
- term.setTextColor(colors.red)
- elseif startsWith(err, "Warning:") then
- term.setTextColor(colors.yellow)
- end
- print(err)
- term.setTextColor(colors.white)
- end
- end
- return output
- end
- local function validateColor(icol, notused)
- local col = loadstring("return colors."..icol)
- col = col()
- if not col then
- return false, nil, "Error: "..icol.." is not a valid color."
- end
- return true, col, nil
- end
- local function new()
- local bText = getInput("Button Text: ", function(text)
- if progButtons[text] then
- return true, text, "Error: Button with that text already exists"
- end
- return true, text, nil
- end, nil)
- local bWidth = getInput("Button Width: ", function(num, text)
- local sw = term.getSize()
- num = tonumber(num)
- if not num then
- return false, nil, "Error: Not a number"
- else
- if num > sw then return true, num, "Warning: Button width is wider than current screens width" end
- if num < 0 then return false, nil, "Error: Button width cannot be less than 1" end
- if num < #text then return false, nil, "Error: Button width must be greater than text width" end
- return true, num, nil
- end
- end, bText)
- local bHeight = getInput("Button Height: ", function(num, notused)
- local sw,sh = term.getSize()
- num = tonumber(num)
- if not num then
- return false, nil, "Error: Not a number"
- else
- if num > sh then return true, num, "Warning: Button height is taller than current screens height" end
- if num < 0 then return false, nil, "Error: Button height cannot be less than 1" end
- return true, num, nil
- end
- end, nil)
- local bx = getInput("Button X: ", function(num, compare)
- local sw,sh = term.getSize()
- num = tonumber(num)
- if not num then
- return false, nil, "Error: Not a number"
- else
- if num > sw then return true, num, "Warning: Button X position would be off the current screens width" end
- if num + compare > sw then
- return true, num, "Warning: Button X position and width would make part of the button off the current screen"
- end
- return true, num, nil
- end
- end, bWidth)
- local by = getInput("Button Y: ", function(num, compare)
- local sw,sh = term.getSize()
- num = tonumber(num)
- if not num then
- return false, nil, "Error: Not a number"
- else
- if num > sh then return true, num, "Warning: Button Y position would be off the current screens height" end
- if num + compare > sh then
- return true, num, "Warning: Button Y position and height would make part of the button off the current screen"
- end
- return true, num, nil
- end
- end, bHeight)
- local bTextCol = getInput("Text Color (eg white, red, etc.): ", validateColor, nil)
- local bBgCol = getInput("Button Color (eg white, red, etc.): ", validateColor, nil)
- local bAction = getInput("Function name to call when button clicked:\n", function(text, notused) return true, text, nil end, nil) -- allow anything to be entered
- progButtons[bText] = { x = bx, y = by, w = bWidth, h = bHeight, txtColor = bTextCol, bgColor = bBgCol, action = bAction }
- progFuncs[#progFuncs+1] = bAction
- end
- local function generate( progname )
- local file = fs.open(progname,"w")
- file.write("local buttons = {\n")
- for text,buttoninfo in pairs(progButtons) do
- file.write("\t[\""..text.."\"] = {")
- for k,v in pairs(buttoninfo) do
- file.write(" "..k.." = "..v..",")
- end
- file.write("},\n")
- end
- file.write("}\n\n")
- for k,v in pairs(progFuncs) do
- if v ~= "error" and v ~= "os.shutdown" and v ~= "os.reboot" then
- file.write("local function "..v.."()\n\t\nend\n\n" )
- end
- end
- file.write(genericFuncs)
- file.close()
- end
- term.clear()
- while true do
- new()
- write("File name: ")
- generate( read() )
- localGui.Cancel.action()
- end
Add Comment
Please, Sign In to add comment