Advertisement
sosochka

artSubsEntry

Jun 17th, 2022 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.50 KB | None | 0 0
  1. -----FUNCTIONS-----
  2.  
  3. local function len(a)
  4.     local length = 0
  5.     for i in pairs(a) do
  6.         length = length + 1
  7.     end
  8.     return length
  9. end
  10.  
  11. --MAIN SCREEN
  12.  
  13. local function drawMainScreen(createButton, editButton, deleteButton, width)
  14.     term.clear()
  15.  
  16.     term.setCursorPos(width/2-7, 4)
  17.     term.write("Art Submissions")
  18.  
  19.     term.setCursorPos(createButton.x, createButton.y)
  20.     term.write(createButton.content)
  21.  
  22.     term.setCursorPos(editButton.x, editButton.y)
  23.     term.write(editButton.content)
  24.  
  25.     term.setCursorPos(deleteButton.x, deleteButton.y)
  26.     term.write(deleteButton.content)
  27. end
  28.  
  29. --CREATE SCREEN
  30.  
  31. local function drawNameScreen(width)
  32.     term.clear()
  33.  
  34.     term.setCursorPos(width/2-6, 4)
  35.     term.write("Name your art")
  36.  
  37.     term.setCursorPos(width/2-13, 10)
  38.     term.write("Enter an empty name to exit")
  39.  
  40.     term.setCursorPos(width/2-10, 6)
  41.     term.blit("00000000000000000000", "00000000000000000000", "00000000000000000000")
  42.     term.setCursorPos(width/2-10, 6)
  43. end
  44.  
  45. local function createButtons(width)
  46.     local createButton = {}
  47.     local editButton = {}
  48.     local deleteButton = {}
  49.  
  50.     createButton.x = 3
  51.     createButton.y = 18
  52.     createButton.content = "Create"
  53.  
  54.     editButton.x = 46
  55.     editButton.y = 18
  56.     editButton.content = "Edit"
  57.  
  58.     deleteButton.x = width/2-3
  59.     deleteButton.y = 18
  60.     deleteButton.content = "Delete"
  61.  
  62.     return createButton, editButton, deleteButton
  63. end
  64.  
  65. --Sync
  66.  
  67. local function syncImages()
  68.     local images = fs.list("artSubmissions")
  69.     local length = len(images)
  70.     for i=1, length do
  71.         local image = io.open("artSubmissions/" .. images[i], "r")
  72.         local data = {}
  73.         data.data = image.read(image, "a")
  74.         data.name = images[i]
  75.         data.protocol = "Input"
  76.         rednet.broadcast(data, "Import")
  77.         image.close(image)
  78.     end
  79. end
  80.  
  81. --LIST SCREEN
  82.  
  83. local function drawList(amt)
  84.     local width, height = term.getSize()
  85.     local images = fs.list("artSubmissions")
  86.     term.clear()
  87.     local length = len(images)
  88.     local curY = 1
  89.     for i=height*(amt-1)+1, height*amt do
  90.         term.setCursorPos(1, curY)
  91.         if images[i] then
  92.             term.write(i .. ": " .. images[i])
  93.         end
  94.         curY = curY + 1
  95.     end
  96.     term.setCursorPos(52-17, 1)
  97.     term.write("Backspace to exit")
  98.     return images, length
  99. end
  100.  
  101. ------MAIN CODE------
  102.  
  103. --Setting up path
  104. if not fs.exists("artSubmissions") then
  105.     fs.makeDir("artSubmissions")
  106. end
  107. shell.setDir("artSubmissions")
  108.  
  109. local width, height = term.getSize()
  110.  
  111. local createButton, editButton, deleteButton = createButtons(width)
  112.  
  113. rednet.open("bottom")
  114.  
  115. while true do
  116.     drawMainScreen(createButton, editButton, deleteButton, width)
  117.     syncImages()
  118.     local event, button, x, y = os.pullEvent("mouse_click")
  119.  
  120.     --CREATE BUTTON
  121.     if x >= createButton.x and x <= createButton.x + 6 and y == createButton.y and button == 1 then
  122.         drawNameScreen(width)
  123.         term.setBackgroundColor(colors.white)
  124.         term.setTextColor(colors.black)
  125.         local artName = read()
  126.         term.setBackgroundColor(colors.black)
  127.         term.setTextColor(colors.white)
  128.         local artName =  string.gsub(artName, " ", "")
  129.         shell.execute("paint", artName)
  130.     --EDIT BUTTON
  131.     elseif x >=editButton.x and x <= editButton.x + 4 and y == editButton.y and button  == 1 then
  132.         local amt = 1
  133.         while true do
  134.             local images, length = drawList(amt)
  135.             term.setCursorPos(50, 18)
  136.             print(amt)
  137.             local eventData = {os.pullEvent()}
  138.             if eventData[1] == "mouse_click" then
  139.                 if eventData[2] == 1 and eventData[4] <= length - height * (amt-1) and length > 0 then
  140.                     shell.execute("paint", images[eventData[4]+(height*(amt-1))])
  141.                     break
  142.                 end
  143.             elseif eventData[1] == "key" then
  144.                 if keys.getName(eventData[2]) == "backspace" then
  145.                     break
  146.                 elseif keys.getName(eventData[2]) == "down" then
  147.                     if (length > height * amt) then
  148.                         amt = amt + 1
  149.                     end
  150.                 elseif keys.getName(eventData[2]) == "up" then
  151.                     if (amt > 1) then
  152.                         amt = amt - 1
  153.                     end
  154.                 end
  155.             elseif eventData[1] == "mouse_scroll" then
  156.                 if (tonumber(eventData[2]) == 1) then
  157.                     if (length > height * amt) then
  158.                         amt = amt + 1
  159.                     end
  160.                 else
  161.                     if (amt > 1) then
  162.                         amt = amt - 1
  163.                     end
  164.                 end
  165.             end
  166.         end
  167.  
  168.     --DELETE BUTTON
  169.     elseif x >= deleteButton.x and x <= deleteButton.x + 6 and y == deleteButton.y and button ==1 then
  170.         local amt = 1
  171.         while true do
  172.             local images, length = drawList(amt)
  173.             term.setCursorPos(50, 18)
  174.             print(amt)
  175.             local eventData = {os.pullEvent()}
  176.             if eventData[1] == "mouse_click" then
  177.                 if eventData[2] == 1 and eventData[4] <= length - height * (amt-1) and length > 0 then
  178.                     fs.delete("artSubmissions/" .. images[eventData[4]+(height*(amt-1))])
  179.                     local data = {}
  180.                     data.data = nil
  181.                     data.name = images[eventData[4]+(height*(amt-1))]
  182.                     data.protocal = "Delete"
  183.                     rednet.broadcast(data)
  184.                     break
  185.                 end
  186.             elseif eventData[1] == "key" then
  187.                 if keys.getName(eventData[2]) == "backspace" then
  188.                     break
  189.                 elseif keys.getName(eventData[2]) == "down" then
  190.                     if (length > height * amt) then
  191.                         amt = amt + 1
  192.                     end
  193.                 elseif keys.getName(eventData[2]) == "up" then
  194.                     if (amt > 1) then
  195.                         amt = amt - 1
  196.                     end
  197.                 end
  198.             elseif eventData[1] == "mouse_scroll" then
  199.                 if (tonumber(eventData[2]) == 1) then
  200.                     if (length > height * amt) then
  201.                         amt = amt + 1
  202.                     end
  203.                 else
  204.                     if (amt > 1) then
  205.                         amt = amt - 1
  206.                     end
  207.                 end
  208.             end
  209.         end
  210.     end
  211. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement