Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- m = peripheral.wrap("right")
- m.setBackgroundColor(32768)
- m.setTextColor(colors.white)
- m.setTextScale(0.5)
- m.clear()
- state = "Grid"
- x, y = m.getSize()
- tempButtons = {}
- buttons = {}
- current = {}
- last = {}
- info = {}
- info["buttons"] = {}
- info["config"] = {}
- wide = {["36"] = 5,["57"] = 8,["79"] = 6,["100"] = 9,["121"] = 6,["143"] = 7,["164"] = 9}
- high = {["24"] = 7,["38"] = 7,["52"] = 7}
- colors = {["white"] = 1,["orange"] = 2,["magenta"] = 4,["lightblue"] = 8,["yellow"] = 16,["lime"] = 32,["pink"] = 64,["gray"] = 128,["lightgray"] = 256,["cyan"] = 512,["purple"] = 1024,["blue"] = 2048,["brown"] = 4096,["green"] = 8192,["red"] = 16384,["black"] = 32768}
- if(x == 15 or y == 15) then
- state = "Wrong_monitor_size"
- else
- w = wide[tostring(x)]
- h = high[tostring(y)]
- end
- function clear()
- m.setBackgroundColor(colors.black)
- m.clear()
- end
- function copy(data1,data2)
- data2["x1"] = data1["x1"]
- data2["x2"] = data1["x2"]
- data2["y1"] = data1["y1"]
- data2["y2"] = data1["y2"]
- end
- function tempButton(name,x1,x2,y1,y2)
- tempButtons[name] = {}
- tempButtons[name]["x1"] = x1
- tempButtons[name]["x2"] = x2
- tempButtons[name]["y1"] = y1
- tempButtons[name]["y2"] = y2
- end
- function checkxy(x, y, button)
- for name, data in pairs(button) do
- if y >= data["y1"] and y <= data["y2"] then
- if x >= data["x1"] and x <= data["x2"] then
- return name
- end
- end
- end
- return "none"
- end
- function checkx(x,y1,y2)
- for i = y1, y2 do
- if checkxy(x,i,buttons) ~= "none" then
- return false
- end
- end
- return true
- end
- function checky(y,x1,x2)
- for i = x1, x2 do
- if checkxy(i,y,buttons) ~= "none" then
- return false
- end
- end
- return true
- end
- function checkLine(a,b)
- if a%w == 1 or b%h == 3 or a == 1 or a == x or b == 1 or b == 2 or b == y then
- return true
- elseif(x == 143) and (a == 142) then
- return true
- else
- return false
- end
- end
- function find(a,b)
- while not checkLine(a,0) do
- a = a - 1
- end
- a = a + 1
- while not checkLine(0,b) do
- b = b - 1
- end
- b = b + 1
- return a, b
- end
- function finish()
- m.setBackgroundColor(colors.black)
- m.clear()
- drawButtons()
- good = false
- while not good do
- term.clear()
- term.setCursorPos(1,1)
- print("[white/orange/magenta/lightblue/yellow/lime/pink/")
- print("gray/lightgray/cyan/purple/blue/brown/green/red/")
- print("black]")
- print("Which color do you want the background to have? ")
- color = io.read()
- if(colors[color] ~= nil) then
- info["config"]["background"] = colors[color]
- good = true
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- print("All the buttons the same colors?[Y/n]")
- result = io.read()
- if(result ~= "n") then
- all = true
- good = false
- while not good do
- term.clear()
- term.setCursorPos(1,1)
- print("[white/orange/magenta/lightblue/yellow/lime/pink/")
- print("gray/lightgray/cyan/purple/blue/brown/green/red/")
- print("black]")
- print("What color should the buttons be? ")
- buttonC = io.read()
- if(colors[buttonC] ~= nil) then
- buttonInC = colors[buttonC]
- good = true
- end
- end
- good = false
- while not good do
- term.clear()
- term.setCursorPos(1,1)
- print("[white/orange/magenta/lightblue/yellow/lime/pink/")
- print("gray/lightgray/cyan/purple/blue/brown/green/red/")
- print("black]")
- print("What color should the border be?")
- print("Leave empty to use the same as the button")
- buttonC = io.read()
- if(buttonC == "") then
- buttonOutC = buttonInC
- good = true
- elseif(colors[buttonC] ~= nil) then
- buttonOutC = colors[buttonC]
- good = true
- end
- end
- good = false
- while not good do
- term.clear()
- term.setCursorPos(1,1)
- print("[white/orange/magenta/lightblue/yellow/lime/pink/")
- print("gray/lightgray/cyan/purple/blue/brown/green/red/")
- print("black]")
- print("What color should text be?")
- buttonC = io.read()
- if(colors[buttonC] ~= nil) then
- textC = colors[buttonC]
- good = true
- end
- end
- else
- all = false
- end
- for name, data in pairs(buttons) do
- if(all) then
- buttons[name]["cIn"] = buttonInC
- buttons[name]["cOut"] = buttonOutC
- buttons[name]["text"] = textC
- else
- good = false
- while not good do
- term.clear()
- term.setCursorPos(1,1)
- print("[white/orange/magenta/lightblue/yellow/lime/pink/")
- print("gray/lightgray/cyan/purple/blue/brown/green/red/")
- print("black]")
- print("What color should '"..name.."' be? ")
- buttonC = io.read()
- if(colors[buttonC] ~= nil) then
- buttons[name]["cIn"] = colors[buttonC]
- good = true
- end
- end
- good = false
- while not good do
- term.clear()
- term.setCursorPos(1,1)
- print("[white/orange/magenta/lightblue/yellow/lime/pink/")
- print("gray/lightgray/cyan/purple/blue/brown/green/red/")
- print("black]")
- print("What color should the border be?")
- print("Leave empty to use the same as the button")
- buttonC = io.read()
- if(buttonC == "") then
- buttons[name]["cOut"] = buttons[name]["cIn"]
- good = true
- elseif(colors[buttonC] ~= nil) then
- buttons[name]["cOut"] = colors[buttonC]
- good = true
- end
- end
- good = false
- while not good do
- term.clear()
- term.setCursorPos(1,1)
- print("[white/orange/magenta/lightblue/yellow/lime/pink/")
- print("gray/lightgray/cyan/purple/blue/brown/green/red/")
- print("black]")
- print("What color should the text be? ")
- textC = io.read()
- if(colors[textC] ~= nil) then
- buttons[name]["text"] = colors[textC]
- good = true
- end
- end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- m.setBackgroundColor(info["config"]["background"])
- m.clear()
- drawButtons()
- print("Is this the way you wanted it?[Y/n]")
- result = io.read()
- if(result ~= "n") then
- return true
- else
- return false
- end
- end
- function drawTempButton(a,a2,b,b2,color,name)
- for i = a, a2 do
- for j = b, b2 do
- m.setBackgroundColor(color)
- m.setCursorPos(i,j)
- m.write(" ")
- end
- end
- yspot = math.floor((b + b2) /2)
- xspot= math.floor((a + a2 - string.len(name))/2)
- m.setCursorPos(xspot,yspot)
- m.write(name)
- end
- function drawGrid()
- for i = 1, x do
- for j = 1, y do
- m.setCursorPos(i,j)
- if checkLine(i,j) then
- m.setBackgroundColor(colors.black)
- else
- m.setBackgroundColor(colors.red)
- end
- m.write(" ")
- end
- end
- end
- function drawButton(name,data)
- for i = data["x1"], data["x2"] do
- for j = data["y1"], data["y2"] do
- m.setCursorPos(i,j)
- if(i == data["x1"] or i == data["x2"] or j == data["y1"] or j == data["y2"]) then
- m.setBackgroundColor(data["cOut"])
- else
- m.setBackgroundColor(data["cIn"])
- end
- m.write(" ")
- end
- end
- yspot = math.floor((data["y1"] + data["y2"]) /2)
- xspot= math.floor((data["x1"] + data["x2"] - string.len(name))/2) + 1
- m.setCursorPos(xspot,yspot)
- m.setBackgroundColor(data["cIn"])
- m.setTextColor(data["text"])
- m.write(name)
- end
- function drawButtons()
- for name, data in pairs(buttons) do
- drawButton(name,data)
- end
- end
- function drawCurrentButton()
- drawTempButton(current["x1"],current["x2"],current["y1"],current["y2"],colors.lime,"")
- if (current["x1"] > 2 and checkx(current["x1"]-w,current["y1"],current["y2"])) then
- tempButton("1",current["x1"] - w,current["x1"] - 1,current["y1"],current["y2"])
- drawTempButton(tempButtons["1"]["x1"],tempButtons["1"]["x2"],tempButtons["1"]["y1"],tempButtons["1"]["y2"],colors.red,"")
- end
- if (current["y1"] > 4 and checky(current["y1"]-h,current["x1"],current["x2"])) then
- tempButton("2",current["x1"],current["x2"],current["y1"] - h,current["y1"]-1)
- drawTempButton(tempButtons["2"]["x1"],tempButtons["2"]["x2"],tempButtons["2"]["y1"],tempButtons["2"] ["y2"],colors.red,"")
- end
- if (current["x2"] < (x - 4) and checkx(current["x2"]+w,current["y1"],current["y2"])) then
- tempButton("3",current["x2"]+1,current["x2"] + w,current["y1"],current["y2"])
- drawTempButton(tempButtons["3"]["x1"],tempButtons["3"]["x2"],tempButtons["3"]["y1"],tempButtons["3"]["y2"],colors.red,"")
- end
- if (current["y2"] < (y - 4) and checky(current["y2"]+h,current["x1"],current["x2"])) then
- tempButton("4",current["x1"],current["x2"],current["y2"]+1,current["y2"]+h)
- drawTempButton(tempButtons["4"]["x1"],tempButtons["4"]["x2"],tempButtons["4"]["y1"],tempButtons["4"]["y2"],colors.red,"")
- end
- end
- function makeCurrentButton()
- startx, starty = find(event[3],event[4])
- current["x1"] = startx
- current["x2"] = startx + w - 2
- current["y1"] = starty
- current["y2"] = starty + h - 2
- copy(current,last)
- end
- function makeButton(name)
- buttons[name] = {}
- buttons[name]["x1"] = current["x1"]
- buttons[name]["x2"] = current["x2"]
- buttons[name]["y1"] = current["y1"]
- buttons[name]["y2"] = current["y2"]
- buttons[name]["cIn"] = 2048
- buttons[name]["cOut"] = 2048
- buttons[name]["text"] = 1
- end
- function editButton(r)
- if r == "1" then
- copy(current,last)
- current["x1"] = tempButtons["1"]["x1"]
- elseif r == "2" then
- copy(current,last)
- current["y1"] = tempButtons["2"]["y1"]
- elseif r == "3" then
- copy(current,last)
- current["x2"] = tempButtons["3"]["x2"]
- elseif r == "4" then
- copy(current,last)
- current["y2"] = tempButtons["4"]["y2"]
- end
- end
- tempButton("Clear",1,12,1,3)
- tempButton("Undo",14,22,1,3)
- tempButton("Confirm",24,36,1,3)
- while state ~= "Ready" do
- clear()
- if(state == "Editing") then
- drawTempButton(tempButtons["Clear"]["x1"],tempButtons["Clear"]["x2"],tempButtons["Clear"]["y1"],tempButtons["Clear"]["y2"],colors.gray,"Clear")
- drawTempButton(tempButtons["Undo"]["x1"],tempButtons["Undo"]["x2"],tempButtons["Undo"]["y1"],tempButtons["Undo"]["y2"],colors.gray,"Undo")
- drawTempButton(tempButtons["Confirm"]["x1"],tempButtons["Confirm"]["x2"],tempButtons["Confirm"]["y1"],tempButtons["Confirm"]["y2"],colors.gray,"Confirm")
- drawCurrentButton()
- drawButtons()
- elseif(state == "Grid") then
- drawGrid()
- drawButtons()
- elseif(state == "Wrong_monitor_size") then
- m.setCursorPos(1,1)
- m.write("This size")
- m.setCursorPos(1,2)
- m.write("isnt supported")
- elseif(state == "Monitor_resize") then
- a,b = m.getSize()
- yspot = math.floor(b/2)
- xspot = math.floor((1 + a - 25)/2) + 1
- m.setCursorPos(xspot,yspot-1)
- m.write("You resized the monitor,")
- m.setCursorPos(xspot,yspot)
- m.write("return to the old size")
- m.setCursorPos(xspot,yspot+1)
- m.write("or restart this program")
- end
- event = { os.pullEvent() }
- if(event[1] == "monitor_resize") then
- a,b = m.getSize()
- if(a == 15 or b == 15) then
- state = "Wrong_monitor_size"
- elseif(a ~= x or b ~= y) then
- state = "Monitor_resize"
- elseif(a == x and b == y) then
- state = "Grid"
- end
- elseif(event[1] == "monitor_touch") then
- if(state == "Grid") then
- if not checkLine(event[3],event[4]) then
- makeCurrentButton()
- state = "Editing"
- end
- elseif(state == "Editing") then
- result = checkxy(event[3], event[4], tempButtons)
- if(result == "Clear") then
- state = "Grid"
- elseif(result == "Undo") then
- copy(last,current)
- drawCurrentButton()
- elseif(result == "Confirm") then
- term.clear()
- clear()
- term.setCursorPos(1,1)
- yspot = math.floor(y/2)
- xspot = math.floor((1 + x - 25)/2) + 1
- m.setCursorPos(15,15)
- m.write("Plz fill the name into the terminal")
- write("Whats the label of the button? ")
- name = io.read()
- makeButton(name)
- term.setCursorPos(1,2)
- write("Do you want to add another button?[Y/n] ")
- another = io.read()
- if(another ~= "n") then
- state = "Grid"
- else
- state = "Ready"
- result = false
- while not result do
- result = finish()
- end
- end
- elseif(result ~= "none") then
- editButton(result)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement