Advertisement
kaibochan

whiteboard.lua

Mar 13th, 2023 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.41 KB | None | 0 0
  1. require "gui"
  2.  
  3. local buffer
  4. local canvas
  5.  
  6. local penButton
  7. local fillButton
  8. local clearButton
  9. local undoButton
  10. local redoButton
  11. local saveButton
  12. local loadButton
  13. local promptText
  14. local promptTextbox
  15.  
  16. local whiteboardFolder = "whiteboards"
  17. local whiteboardExtension = ".wtbd"
  18.  
  19. --ordered colors table with both name and color value available
  20. local orderedColors = {
  21.     {["name"] = "red",      ["color"] = colors.red},
  22.     {["name"] = "orange",   ["color"] = colors.orange},
  23.     {["name"] = "yellow",   ["color"] = colors.yellow},
  24.     {["name"] = "lime",     ["color"] = colors.lime},
  25.     {["name"] = "green",    ["color"] = colors.green},
  26.     {["name"] = "cyan",     ["color"] = colors.cyan},
  27.     {["name"] = "blue",     ["color"] = colors.blue},
  28.     {["name"] = "purple",   ["color"] = colors.purple},
  29.     {["name"] = "magenta",  ["color"] = colors.magenta},
  30.     {["name"] = "pink",     ["color"] = colors.pink},
  31.     {["name"] = "brown",    ["color"] = colors.brown},
  32.     {["name"] = "white",    ["color"] = colors.white},
  33.     {["name"] = "lightGray",["color"] = colors.lightGray},
  34.     {["name"] = "gray",     ["color"] = colors.gray},
  35.     {["name"] = "black",    ["color"] = colors.black},
  36. }
  37.  
  38. local function save(fileName)
  39.     local fullFileName = whiteboardFolder.."/"..fileName..whiteboardExtension
  40.    
  41.     local overwrite
  42.  
  43.     if fs.exists(fullFileName) then
  44.         local confirmationRecieved = false
  45.  
  46.         promptText:setText(fileName.." already exists. Overwrite?")
  47.  
  48.         promptTextbox:setText("")
  49.         promptTextbox.allAutoCompleteChoices = {"yes", "no"}
  50.         promptTextbox.onEnter = function(txb)
  51.             if txb.text == "yes" or txb.text == "no" then
  52.                 confirmationRecieved = true
  53.                 overwrite = (txb.text == "yes")
  54.             end
  55.         end
  56.  
  57.         repeat
  58.             buffer:draw()
  59.             parallel.waitForAny(handleInputEvents)
  60.         until confirmationRecieved
  61.     end
  62.  
  63.     promptText:setText("")
  64.     promptText.visible = false
  65.  
  66.     promptTextbox:setText("")
  67.     promptTextbox.allAutoCompleteChoices = {}
  68.     promptTextbox.visible = false
  69.  
  70.     if not fs.exists(fullFileName) or overwrite then
  71.         local file = fs.open(fullFileName, "w")
  72.         file.write(textutils.serialise(whiteboard.cells))
  73.     end
  74. end
  75.  
  76. local function initializeElements()
  77.     local device = peripheral.find("monitor")
  78.  
  79.     if not device then
  80.         device = term
  81.     end
  82.  
  83.     buffer = createBuffer(device, "buffer", colors.white)
  84.  
  85.     whiteboard = Canvas:new {
  86.         name = "whiteboard",
  87.         parent = buffer,
  88.         buffer = buffer,
  89.         width = buffer.width,
  90.         height = buffer.height,
  91.     }
  92.  
  93.     for index, color in ipairs(orderedColors) do
  94.         Button:new {
  95.             name = color.name,
  96.             parent = buffer,
  97.             buffer = buffer,
  98.             backgroundColor = color.color,
  99.             x = index,
  100.             y = buffer.height,
  101.             onClickName = color.name,
  102.             onClick = function(self)
  103.                 whiteboard.currentColor = color.color
  104.             end,
  105.         }
  106.     end
  107.  
  108.     penButton = Button:new {
  109.         name = "pen",
  110.         parent = buffer,
  111.         buffer = buffer,
  112.         text = "pen",
  113.         x = buffer.width - 12,
  114.         y = buffer.height,
  115.         width = 3,
  116.         height =  1,
  117.         onClickName = "switchToPen",
  118.         onClick = function(self)
  119.             whiteboard.currentDrawAction = whiteboard.pen
  120.         end,
  121.     }
  122.  
  123.     fillButton = Button:new {
  124.         name = "fill",
  125.         parent = buffer,
  126.         buffer = buffer,
  127.         text = "fill",
  128.         x = buffer.width - 17,
  129.         y = buffer.height,
  130.         width = 4,
  131.         height =  1,
  132.         onClickName = "switchToFill",
  133.         onClick = function(self)
  134.             whiteboard.currentDrawAction = whiteboard.fill
  135.         end,
  136.     }
  137.  
  138.     clearButton = Button:new {
  139.         name = "clear",
  140.         parent = buffer,
  141.         buffer = buffer,
  142.         text = "clear",
  143.         x = buffer.width - 5,
  144.         y = 1,
  145.         width = 5,
  146.         height =  1,
  147.         onClickName = "clear",
  148.         onClick = function(self)
  149.             whiteboard:clear()
  150.         end,
  151.     }
  152.  
  153.     undoButton = Button:new {
  154.         name = "undo",
  155.         parent = buffer,
  156.         buffer = buffer,
  157.         text = "undo",
  158.         x = buffer.width - 10,
  159.         y = 1,
  160.         width = 4,
  161.         height =  1,
  162.         onClickName = "undo",
  163.         onClick = function(self)
  164.             whiteboard:undo()
  165.         end,
  166.     }
  167.  
  168.     redoButton = Button:new {
  169.         name = "redo",
  170.         parent = buffer,
  171.         buffer = buffer,
  172.         text = "redo",
  173.         x = buffer.width - 15,
  174.         y = 1,
  175.         width = 4,
  176.         height =  1,
  177.         onClickName = "redo",
  178.         onClick = function(self)
  179.             whiteboard:redo()
  180.         end,
  181.     }
  182.  
  183.     promptText = Text:new {
  184.         name = "promptText",
  185.         parent = buffer,
  186.         buffer = buffer,
  187.         x = math.ceil(buffer.width / 10),
  188.         y = math.floor(buffer.height / 2) - 2,
  189.         width = math.floor(8 * buffer.width / 10),
  190.         height = 1,
  191.         visible = false,
  192.     }
  193.  
  194.     promptTextbox = Textbox:new {
  195.         name = "prompt",
  196.         parent = buffer,
  197.         buffer = buffer,
  198.         x = promptText.x,
  199.         y = promptText.y + 1,
  200.         width = promptText.width,
  201.         height = 3,
  202.         padding = 1,
  203.         backgroundColor = colors.red,
  204.         textColor = colors.orange,
  205.         cursorBackgroundColor = colors.white,
  206.         cursorTextColor = colors.red,
  207.         selectionBackgroundColor = colors.yellow,
  208.         selectionTextColor = colors.red,
  209.         visible = false,
  210.     }
  211.  
  212.     saveButton = Button:new {
  213.         name = "save",
  214.         parent = buffer,
  215.         buffer = buffer,
  216.         text = "save",
  217.         x = buffer.width - 4,
  218.         y = buffer.height,
  219.         width = 4,
  220.         height = 1,
  221.         onClickName = "save",
  222.         onClick = function(self)
  223.             promptText.visible = true
  224.             promptText:setText("Enter filename to save whiteboard as:")
  225.  
  226.             promptTextbox.visible = true
  227.             promptTextbox.onEnter = function(txb)
  228.                 save(txb.text)
  229.             end
  230.         end,
  231.     }
  232. end
  233.  
  234. local function main()
  235.     initializeElements()
  236.  
  237.     while true do
  238.         buffer:draw()
  239.         parallel.waitForAny(handleInputEvents)
  240.     end
  241. end
  242.  
  243. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement