Agent_Silence

Guide 2 [PRE ALPHA, ~30% DONE]

Jan 13th, 2015 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.47 KB | None | 0 0
  1. term.redirect(term.native())
  2. code = debuguide ~= nil and debuguide.code or {}
  3. curElement = 1
  4. curValue = 2
  5. curColor = 2^curValue
  6. term.width, term.height = term.getSize()
  7. term.clear()
  8. toolBarActive = false
  9. rcm = true
  10. editing = false
  11.  
  12. function findNext(sClass)
  13.     local totals = {rectangle = 0, text = 0, line = 0, sprite = 0, fill = 0}
  14.     if totals[sClass] == nil then
  15.         error("sClass in function findNext is not a valid class")
  16.     end
  17.     for i,v in pairs(code) do
  18.         totals[v.class] = totals[v.class] + 1
  19.     end
  20.     return totals[sClass] + 1
  21. end
  22.  
  23. function drawBox(x,y,endX,endY,color)
  24.     for i=y > endY and endY or y,y > endY and y or endY do
  25.         paintutils.drawLine(x,i,endX,i,color)
  26.     end
  27. end
  28.  
  29. function fullLimitWrite(text,limit)
  30.     term.write(#text < limit+1 and text..(string.rep(" ",limit-#text)) or string.sub(text,1,limit-3).."...")
  31. end
  32.  
  33. function toColor(nColor)
  34.     for i,v in pairs(colors) do
  35.         if nColor == v then
  36.             return "colors."..i
  37.         end
  38.     end
  39. end
  40.  
  41. function dualPull(...)
  42.     local args={...}
  43.     repeat
  44.         local event = {os.pullEvent()}
  45.         for i,v in pairs(args) do
  46.             if event[1] == v then
  47.                 return unpack(event)
  48.             end
  49.         end
  50.     until false
  51. end
  52.  
  53. function redraw()
  54.     if #code > 0 then
  55.     for i,v in pairs(code) do
  56.         if curElement ~= i then
  57.             if v.class == "sprite" and v.visible == true then
  58.                 for l,k in pairs(v.pixels) do
  59.                     paintutils.drawPixel(k.x,k.y,k.color)
  60.                 end
  61.             elseif v.class == "line" and v.visible == true then
  62.                 paintutils.drawLine(v.sX,v.sY,v.eX,v.eY,v.color)
  63.             elseif v.class == "text" and v.visible == true then
  64.                 term.setCursorPos(v.x,v.y)
  65.                 term.setTextColor(v.tColor)
  66.                 term.setBackgroundColor(v.bColor)
  67.                 term.write(v.text)
  68.             elseif v.class == "fill" and v.visible == true then
  69.                 term.setBackgroundColor(v.color)
  70.                 term.clear()
  71.             elseif v.class == "rectangle" and v.visible == true then
  72.                 drawBox(v.sX,v.sY,v.eX,v.eY,v.color)
  73.             end
  74.         elseif i == curElement then
  75.             if v.class == "sprite" then
  76.                 for l,k in pairs(v.pixels) do
  77.                     paintutils.drawPixel(k.x,k.y,k.color)
  78.                 end
  79.             elseif v.class == "line" then
  80.                 paintutils.drawLine(v.sX,v.sY,v.eX,v.eY,v.color ~= colors.blue and colors.blue or colors.lightBlue)
  81.             elseif v.class == "text" then
  82.                 term.setCursorPos(v.x,v.y)
  83.                 term.setTextColor(v.tColor ~= colors.lightBlue and colors.lightBlue or colors.blue)
  84.                 term.setBackgroundColor(v.bColor ~= blue and colors.blue or colors.lightBlue)
  85.                 term.write(v.text)
  86.             elseif v.class == "fill" then
  87.                 term.setBackgroundColor(v.color ~= colors.blue and colors.blue or colors.lightBlue)
  88.                 term.clear()
  89.             elseif v.class == "rectangle" then
  90.                 drawBox(v.sX,v.sY,v.eX,v.eY,v.color ~= colors.blue and colors.blue or colors.lightBlue)
  91.             end
  92.         end
  93.     end
  94.     end
  95.     if toolBarActive == true and editing == false then
  96.         drawBox(term.width-12,1,term.width,term.height,colors.white)
  97.         term.setCursorPos(term.width-11,1)
  98.         term.setTextColor(colors.black)
  99.         term.setBackgroundColor(colors.lightGray)
  100.         term.setCursorPos(term.width-12,1)
  101.         fullLimitWrite("Toolbar",13)
  102.         term.setCursorPos(term.width-12,7)
  103.         term.setTextColor(colors.lightBlue)
  104.         term.setBackgroundColor(colors.lightGray)
  105.         fullLimitWrite("Edit",13)
  106.         term.setTextColor(colors.white)
  107.         term.setBackgroundColor(colors.red)
  108.         term.setCursorPos(term.width,1)
  109.         term.write("X")
  110.         term.setTextColor(colors.black)
  111.         term.setBackgroundColor(colors.white)
  112.         for i=-2,2 do
  113.             term.setCursorPos(term.width-12,4+i)
  114.             if code[curElement + i] ~= nil then
  115.                 if code[curElement + i] == code[curElement] then
  116.                     term.setTextColor(colors.lightBlue)
  117.                     fullLimitWrite(code[curElement].name,13)
  118.                     term.setTextColor(colors.black)
  119.                 else
  120.                     fullLimitWrite(code[curElement + i].name,13)
  121.                 end
  122.             else
  123.                 fullLimitWrite(" ",13)
  124.             end
  125.         end
  126.     end
  127. end
  128.  
  129. function line(toX,toY,fromX,fromY,color,key,name)
  130.     term.setBackgroundColor(colors.black)
  131.     term.clear()
  132.     redraw()
  133.     if toX == nil and toY == nil and fromX == nil and fromY == nil then
  134.         event, button, fromX, fromY = os.pullEvent("mouse_click")
  135.         event, button, toX, toY = os.pullEvent("mouse_click")
  136.     end
  137.     if not color then
  138.         color = colors.red
  139.     end
  140.     local curPoint = "to"
  141.     while true do
  142.         redraw()
  143.         paintutils.drawLine(fromX,fromY,toX,toY,color)
  144.         paintutils.drawPixel(curPoint == "to" and toX or fromX,curPoint == "to" and toY or fromY,color ~= colors.lightBlue and colors.lightBlue or colors.blue)
  145.         local event, button, x, y = dualPull("mouse_click","mouse_drag")
  146.         if event == "mouse_drag" then
  147.             paintutils.drawLine(fromX,fromY,toX,toY,colors.black)
  148.             toX = curPoint == "to" and x or toX
  149.             toY = curPoint == "to" and y or toY
  150.             fromX = curPoint == "from" and x or fromX
  151.             fromY = curPoint == "from" and y or fromY
  152.             redraw()
  153.             paintutils.drawLine(fromX,fromY,toX,toY,color)
  154.         elseif event == "mouse_click" then
  155.             if x == fromX and y == fromY and curPoint ~= "from" then
  156.                 curPoint = "from"
  157.             elseif x == toX and y == toY and curPoint ~= "to" then
  158.                 curPoint = "to"
  159.             elseif x == toX and y == toY and curPoint == "to" then
  160.                 break
  161.             elseif x == fromX and y == fromY and curPoint == "from" then
  162.                 break
  163.             end
  164.         end
  165.     end
  166.     local template = {
  167.         class = "line",
  168.         sX = fromX,
  169.         sY = fromY,
  170.         eX = toX,
  171.         eY = toY,
  172.         visible = true,
  173.         color = color,
  174.         name = name or "line"..findNext("line")}
  175.     if key then
  176.         code[key] = template
  177.     else
  178.         table.insert(code,template)
  179.     end
  180.     redraw()
  181. end
  182.  
  183. function text(x,y,sText,bColor,tColor,key,name)
  184.     term.setBackgroundColor(colors.black)
  185.     term.clear()
  186.     redraw()
  187.     if not bColor and not tColor then
  188.         tColor = colors.black
  189.         bColor = colors.green
  190.     end
  191.     local tText = {}
  192.     if sText then
  193.         for i=1,#sText do
  194.             local letter = string.sub(sText,i,i)
  195.             table.insert(tText,letter)
  196.         end
  197.     end
  198.     local selected = #tText
  199.     if not x and not y then
  200.         event, button, x, y = os.pullEvent("mouse_click")
  201.     end
  202.     repeat
  203.         redraw()
  204.         term.setCursorPos(x,y)
  205.         term.setTextColor(tColor)
  206.         term.setBackgroundColor(bColor)
  207.         term.write(table.concat(tText))
  208.         local event, key = dualPull("key","char")
  209.         if event == "char" then
  210.             table.insert(tText,selected + 1,key)
  211.             selected = selected + 1
  212.         elseif event == "key" then
  213.             if key == keys.left and selected > 0 then
  214.                 selected = selected - 1
  215.             elseif key == keys.right and selected < #tText then
  216.                 selected = selected + 1
  217.             elseif key == keys.backspace and selected > 0 then
  218.                 local oldSel = selected
  219.                 selected = selected - 1
  220.                 table.remove(tText,oldSel)
  221.                 paintutils.drawPixel(x + #tText,y,colors.black)
  222.             end
  223.         end
  224.     until key == keys.enter
  225.     if #tText > 0 then
  226.         local template = {
  227.             class = "text",
  228.             text = table.concat(tText),
  229.             x = x,
  230.             y = y,
  231.             tColor = tColor,
  232.             visible = true,
  233.             bColor = bColor,
  234.             name = name or "text"..findNext("text")}
  235.         if key then
  236.             code[key] = template
  237.         else
  238.             table.insert(code,template)
  239.         end
  240.     end
  241.     redraw()
  242. end
  243.  
  244. function sprite(pixels,key,name)
  245.     term.setBackgroundColor(colors.black)
  246.     term.clear()
  247.     redraw()
  248.     local pTbl = pixels or {}
  249.     while true do
  250.         curColor = 2^curValue
  251.         redraw()
  252.         for i,v in pairs(pTbl) do
  253.             paintutils.drawPixel(v.x,v.y,v.color)
  254.         end
  255.         local event, button, x, y = dualPull("mouse_click","key","mouse_scroll","mouse_drag")
  256.         if event == "mouse_click" or event == "mouse_drag" then
  257.             local found = false
  258.             for i,v in pairs(pTbl) do
  259.                 if v.x == x and v.y == y then
  260.                     v.color = curColor
  261.                     found = true
  262.                 end
  263.             end
  264.             if found == false then
  265.                 table.insert(pTbl,{x = x, y = y, color = curColor})
  266.             end
  267.         elseif event == "key" then
  268.             if button == keys.enter then
  269.                 break
  270.             elseif button == 14 and #pTbl > 0 then
  271.                 paintutils.drawPixel(pTbl[#pTbl].x,pTbl[#pTbl].y,colors.black)
  272.                 table.remove(pTbl)
  273.                 redraw()
  274.             elseif button == keys.up then
  275.                 curValue = curValue < 15 and curValue + 1 or 0
  276.             elseif button == keys.down then
  277.                 curValue = curValue > 0 and curValue - 1 or 15
  278.             end
  279.         end
  280.     end
  281.     local template = {
  282.         class = "sprite",
  283.         pixels = pTbl,
  284.         visible = true,
  285.         name = name or "Pxls"..findNext("sprite")}
  286.     if key and #pTbl > 0 then
  287.         code[key] = template
  288.     elseif #pTbl > 0 then
  289.         table.insert(code,template)
  290.     end
  291.     redraw()
  292. end
  293.  
  294. function rectangle(fromX,fromY,toX,toY,color,key,name)
  295.     term.setBackgroundColor(colors.black)
  296.     term.clear()
  297.     redraw()
  298.     if not fromX and not fromY and not toX and not toY then
  299.         local event, button, fX, fY = os.pullEvent("mouse_click")
  300.         fromX = fX
  301.         fromY = fY
  302.         local event, button, tX, tY = os.pullEvent("mouse_click")
  303.         toX = tX
  304.         toY = fY
  305.     end
  306.     if not color then
  307.         color = colors.red
  308.     end
  309.     local curPoint = "to"
  310.     while true do
  311.         redraw()
  312.         drawBox(fromX,fromY,toX,toY,color)
  313.         paintutils.drawPixel(curPoint == "to" and toX or fromX, curPoint == "to" and toY or fromY, color ~= colors.lightBlue and colors.lightBlue or colors.blue)
  314.         local event, button, x, y = dualPull("mouse_click","mouse_drag")
  315.         if event == "mouse_drag" then
  316.             if curPoint == "to" then
  317.                 drawBox(fromX,fromY,toX,toY,colors.black)
  318.                 toX = x
  319.                 toY = y
  320.             elseif curPoint == "from" then
  321.                 drawBox(fromX,fromY,toX,toY,colors.black)
  322.                 fromX = x
  323.                 fromY = y
  324.             end
  325.         elseif event == "mouse_click" then
  326.             if x == fromX and y == fromY and curPoint ~= "from" then
  327.                 curPoint = "from"
  328.             elseif x == toX and y == toY and curPoint ~= "to" then
  329.                 curPoint = "to"
  330.             elseif x == toX and y == toY and curPoint == "to" then
  331.                 break
  332.             elseif x == fromX and y == fromY and curPoint == "from" then
  333.                 break
  334.             end
  335.         end
  336.     end
  337.     local template = {
  338.         class = "rectangle",
  339.         sX = fromX < toX and fromX or toX,
  340.         sY = fromY < toY and fromY or toY,
  341.         eX = fromX > toX and fromX or toX,
  342.         eY = fromY > toY and fromY or toY,
  343.         visible = true,
  344.         color = color,
  345.         name = name or "rect"..findNext("rectangle")}
  346.     if key then
  347.         code[key] = template
  348.     else
  349.         table.insert(code,template)
  350.     end
  351.     redraw()
  352. end
  353.  
  354. function delete(nKey)
  355.     if code[nKey] ~= nil then
  356.         local item = code[nKey]
  357.         if item.class == "line" then
  358.             paintutils.drawLine(item.sX,item.sY,item.eX,item.eY,colors.black)
  359.             table.remove(code, nKey)
  360.             redraw()
  361.         elseif item.class == "text" then
  362.             paintutils.drawLine(item.x,item.y,item.x + #item.text, item.y, colors.black)
  363.             table.remove(code, nKey)
  364.             redraw()
  365.         elseif item.class == "sprite" then
  366.             for i,v in pairs(item.pixels) do
  367.                 paintutils.drawPixel(v.x,v.y,colors.black)
  368.             end
  369.             table.remove(code, nKey)
  370.             redraw()
  371.         elseif item.class == "rectangle" then
  372.             drawBox(item.sX, item.sY, item.eX, item.eY, colors.black)
  373.             table.remove(code, nKey)
  374.             redraw()
  375.         end
  376.     end
  377. end
  378.  
  379.  
  380. function edit(key)
  381.     local item = code[key]
  382.     item.visible = false
  383.     if item.class == "line" then
  384.         line(item.sX,item.sY,item.eX,item.eY,item.color,key,item.name)
  385.     elseif item.class == "text" then
  386.         text(item.x,item.y,item.text,item.tColor,item.bColor,key,item.name)
  387.     elseif item.class == "sprite" then
  388.          sprite(item.pixels,key,item.name)
  389.     elseif item.class == "rectangle" then
  390.         rectangle(item.sX,item.sY,item.eX,item.eY,item.color,key,item.name)
  391.     end
  392. end
  393.  
  394. function rightClickMenu(x,y)
  395.     if x == nil or y == nil then
  396.         event,button,x,y = os.pullEvent("mouse_click")
  397.     end
  398.     local width, height = term.getSize()
  399.     redraw()
  400.     if toolBarActive == true then
  401.         width = width - 13
  402.     end
  403.     local browser
  404.     while rcm == true do
  405.         if y + 5 > height then
  406.             y = y - 5
  407.         end
  408.         if x + 8 > width then
  409.             x = x - (8 - (width-x))
  410.         end
  411.         drawBox(x,y,x+8,y+5,colors.white)
  412.         term.setTextColor(colors.black)
  413.         term.setCursorPos(x,y)
  414.         term.write("Line")
  415.         term.setCursorPos(x,y+1)
  416.         term.write("Text")
  417.         term.setCursorPos(x,y+2)
  418.         term.write("Box")
  419.         term.setCursorPos(x,y+3)
  420.         term.write("Sprite")
  421.         term.setCursorPos(x,y+4)
  422.         term.write("Delete")
  423.         term.setCursorPos(x,y+5)
  424.         term.write("Browser")
  425.         local event, button, clickX, clickY = os.pullEvent("mouse_click")
  426.         if button == 2 and (clickX > x+8 or clickX < x or clickY > y+5 or clickY < y) then
  427.             drawBox(x,y,x+8,y+5,colors.black)
  428.             redraw()
  429.             x = clickX
  430.             y = clickY
  431.             if y + 5 > height then
  432.                 y = y - 5
  433.             end
  434.             if x + 8 > width then
  435.                 x = x - (8 - (width-x))
  436.             end
  437.         elseif button == 1 then
  438.             if clickX >= x and clickX < x + 9 and clickY == y then
  439.                 drawBox(x,y,x+8,y+5,colors.black)
  440.                 redraw()
  441.                 line()
  442.             end
  443.             if clickX >= x and clickX < x + 9 and clickY == y+1 then
  444.                 drawBox(x,y,x+8,y+5,colors.black)
  445.                 redraw()
  446.                 text()
  447.             end
  448.             if clickX >= x and clickX < x + 9 and clickY == y+2 then
  449.                 drawBox(x,y,x+8,y+5,colors.black)
  450.                 redraw()
  451.                 rectangle()
  452.             end
  453.             if clickX >= x and clickX < x + 9 and clickY == y+3 then
  454.                 drawBox(x,y,x+8,y+5,colors.black)
  455.                 redraw()
  456.                 sprite()
  457.             end
  458.             if clickX >= x and clickX < x + 9 and clickY == y+4 then
  459.                 drawBox(x,y,x+8,y+5,colors.black)
  460.                 delete(#code)
  461.                 redraw()
  462.             end
  463.             if clickX >= x and clickX < x + 9 and clickY == y+5 then
  464.                 drawBox(x,y,x+8,y+5,colors.black)
  465.                 redraw()
  466.                 browser = true
  467.                 break
  468.             end
  469.             if clickX > x+8 or clickX < x then
  470.                 drawBox(x,y,x+8,y+5,colors.black)
  471.                 break
  472.             end
  473.             if clickY > y+5 or clickY < y then
  474.                 drawBox(x,y,x+8,y+5,colors.black)
  475.                 redraw()
  476.                 break
  477.             end
  478.         end
  479.     end
  480.     if browser then
  481.         elementBrowser()
  482.         browser = false
  483.     end
  484. end
  485.  
  486. function elementBrowser()
  487.     while true do
  488.         drawBox(term.width-12,1,term.width,term.height,colors.white)
  489.         term.setCursorPos(term.width-11,1)
  490.         term.setTextColor(colors.black)
  491.         term.setBackgroundColor(colors.lightGray)
  492.         term.setCursorPos(term.width-12,1)
  493.         fullLimitWrite("Toolbar",13)
  494.         term.setCursorPos(term.width-12,7)
  495.         term.setTextColor(colors.lightBlue)
  496.         term.setBackgroundColor(colors.lightGray)
  497.         fullLimitWrite("Edit",13)
  498.         term.setTextColor(colors.white)
  499.         term.setBackgroundColor(colors.red)
  500.         term.setCursorPos(term.width,1)
  501.         term.write("X")
  502.         term.setTextColor(colors.black)
  503.         term.setBackgroundColor(colors.white)
  504.         for i=-2,2 do
  505.             term.setCursorPos(term.width-12,4+i)
  506.             if code[curElement + i] ~= nil then
  507.                 if code[curElement + i] == code[curElement] then
  508.                     term.setTextColor(colors.lightBlue)
  509.                     fullLimitWrite(code[curElement].name,13)
  510.                     term.setTextColor(colors.black)
  511.                 else
  512.                     fullLimitWrite(code[curElement + i].name,13)
  513.                 end
  514.             else
  515.                 fullLimitWrite(" ",13)
  516.             end
  517.         end
  518.         local event, button, x, y = dualPull("mouse_click","mouse_scroll","key")
  519.         if event == "mouse_scroll" then
  520.             if button == -1 then
  521.                 curElement = curElement > 1 and curElement - 1 or #code
  522.                 sleep(0.01)
  523.                 redraw()
  524.             elseif button == 1 then
  525.                 curElement = curElement < #code and curElement + 1 or 1
  526.                 sleep(0.01)
  527.                 redraw()
  528.             end
  529.         elseif event == "key" then
  530.             if button == 211 or button == 14 then
  531.                 delete(curElement)
  532.                 curElement = (code[curElement] == nil and #code > 0) and curElement - 1 or curElement
  533.             elseif button == keys.up then
  534.                 curElement = curElement > 1 and curElement - 1 or #code
  535.             elseif button == keys.down then
  536.                 curElement = curElement < #code and curElement + 1 or 1
  537.             end
  538.         elseif event == "mouse_click" then
  539.             if x > term.width-12 and 7 > y then
  540.                 for i=-2,2 do
  541.                     if y == 4 + i and curElement + i > 0 and curElement + i < #code + 1 then
  542.                         curElement = curElement + i
  543.                         redraw()
  544.                     end
  545.                 end
  546.             elseif x > term.width-12 and y == 7 then
  547.                 term.setBackgroundColor(colors.black)
  548.                 term.clear()
  549.                 redraw()
  550.                 local svcur = curElement
  551.                 curElement = 0
  552.                 editing = true
  553.                 edit(svcur)
  554.                 curElement = svcur
  555.                 term.setBackgroundColor(colors.black)
  556.                 term.clear()
  557.                 redraw()
  558.             elseif (y == 1 and x == term.width) then
  559.                 drawBox(term.width-12,1,term.width,term.height,colors.black)
  560.                 redraw()
  561.                 toolBarActive = false
  562.                 rcm = false
  563.                 break
  564.             elseif term.width > x and button == 2 then
  565.                 toolBarActive = true
  566.                 rightClickMenu(x,y,true)
  567.             end
  568.         end
  569.     end
  570. end
  571.  
  572. function save()
  573.     local file = fs.open("test","a")
  574.     local rect = false
  575.     local currentBColor
  576.     local currentTColor
  577.     file.writeLine("function drawGUI()")
  578.     for i,v in pairs(code) do
  579.         if v.class == "rectangle" then
  580.             if not paintutils.drawFilledBox and not rect then
  581.                 file.writeLine([[
  582. local function drawBox(x,y,endX,endY,color)
  583.     for i=y > endY and endY or y,y > endY and y or endY do
  584.         paintutils.drawLine(x,i,endX,i,color)
  585.     end
  586. end]])
  587.                 rect = true
  588.             end
  589.             file.writeLine(rect and "   drawBox("..v.sX..","..v.sY..","..v.eX..","..v.eY..","..toColor(v.color)..")" or "   paintutils.drawFilledBox("..v.sX..","..v.sY..","..v.eX..","..v.eY..","..toColor(v.color)..")")
  590.             currentTColor = v.color
  591.             currentBColor = v.color
  592.         elseif v.class == "line" then
  593.             file.writeLine("    paintutils.drawLine("..v.sX..","..v.sY..","..v.eX..","..v.eY..","..toColor(v.color)..")")
  594.             currentTColor = v.color
  595.             currentBColor = v.color
  596.         elseif v.class == "text" then
  597.             file.writeLine("    term.setCursorPos("..v.x..","..v.y..")")
  598.             if currentTColor ~= v.tColor then
  599.                 file.writeLine("    term.setTextColor("..toColor(v.tColor)..")")
  600.                 currentTColor = v.tColor
  601.             end
  602.             if currentBColor ~= v.bColor then
  603.                 file.writeLine("    term.setBackgroundColor("..toColor(v.bColor)..")")
  604.                 currentBColor = v.bColor
  605.             end
  606.             file.writeLine('    term.write("'..v.text..'")')
  607.         elseif v.class == "sprite" then
  608.             file.writeLine(" ")
  609.             for l,k in pairs(v.pixels) do
  610.                 file.writeLine("    paintutils.drawPixel("..k.x..","..k.y..","..k.color..")")
  611.             end
  612.             currentTColor = v.color
  613.             currentBColor = v.color
  614.         end
  615.     end
  616.     file.writeLine("end")
  617.     file.close()
  618. end
  619.  
  620. rightClickMenu()
Advertisement
Add Comment
Please, Sign In to add comment