Advertisement
gravitowl

Paint App

Mar 11th, 2021 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. require("ui_api")
  2.  
  3. -- GLOBALS
  4. local colorButtons = {}
  5. local curColor = colors.white
  6. local showOptions = false
  7. local cWidth = termWidth - 4
  8. local cHeight = termWidth - 1
  9. local curDrawing = {}
  10. local running = true
  11.  
  12. for i = 1, cWidth, 1 do
  13. curDrawing[i] = {}
  14. for j = 1, cHeight, 1 do
  15. curDrawing[i][j] = colors.white
  16. end
  17. end
  18.  
  19. term.clear()
  20. term.setCursorPos(1, 1)
  21.  
  22. -- Functions & Stuff
  23.  
  24. function setColor(color)
  25. curColor = color
  26. end
  27.  
  28. function toggleOptions()
  29. if showOptions == true then showOptions = false elseif showOptions == false then showOptions = true end
  30. end
  31.  
  32. function quitApp()
  33. term.clear()
  34. term.setCursorPos(1,1)
  35. running = false
  36. end
  37.  
  38. function drawOptions()
  39. --drawRectangle(1, 2, 7, 10, colors.blue)
  40. saveButton:draw()
  41. end
  42.  
  43. function draw()
  44.  
  45. while running do
  46. term.clear()
  47.  
  48. -- Draw main shapes, topbar, sidebar.
  49. drawRectangle(1, 1, termWidth, 1, colors.green, "Paint Program by gravitowl", colors.white)
  50. drawRectangle(termWidth - 3, 2, 4, termHeight, colors.gray)
  51.  
  52. -- Draw canvas
  53.  
  54. for i = 1, cWidth, 1 do
  55. for j = 1, cHeight, 1 do
  56. drawRectangle(i, j + 1, 1, 1, curDrawing[i][j])
  57. end
  58. end
  59. -- Show currently selected color.
  60. drawRectangle(termWidth-2, 12, 2, 1, curColor)
  61.  
  62. if showOptions then
  63. for key, ob in pairs(optionsMenu) do
  64. ob:draw()
  65. end
  66. end
  67. -- Draw buttons
  68. for key, cb in pairs(colorButtons) do
  69. cb:draw()
  70. end
  71.  
  72. optionsButton:draw()
  73.  
  74. os.sleep(0.1)
  75. end
  76. end
  77.  
  78. function click()
  79. while running do
  80. local e, a, x, y = os.pullEvent("mouse_click")
  81. for key, cb in pairs(colorButtons) do
  82. cb:isClicked(x, y)
  83. end
  84. optionsButton:isClicked(x, y)
  85.  
  86. if showOptions then
  87. for key, ob in pairs(optionsMenu) do
  88. ob:isClicked(x, y)
  89. end
  90. end
  91. if x >= 1 and x <= cWidth and y > 1 and y < cHeight then
  92. curDrawing[x][y - 1] = curColor
  93. end
  94. end
  95. end
  96.  
  97. -- Buttons
  98.  
  99. local CBY = 3
  100. colorButtons = {
  101. white = Button:new(nil, termWidth-2, CBY, 0, 0, "", colors.white, colors.white, {setColor, colors.white}),
  102. orange = Button:new(nil, termWidth-2, CBY + 1, 0, 0, "", colors.white, colors.orange, {setColor, colors.orange}),
  103. magenta = Button:new(nil, termWidth-2, CBY + 2, 0, 0, "", colors.white, colors.magenta, {setColor, colors.magenta}),
  104. lightBlue = Button:new(nil, termWidth-2, CBY + 3, 0, 0, "", colors.white, colors.lightBlue, {setColor, colors.lightBlue}),
  105. yellow = Button:new(nil, termWidth-2, CBY + 4, 0, 0, "", colors.white, colors.yellow, {setColor, colors.yellow}),
  106. lime = Button:new(nil, termWidth-2, CBY + 5, 0, 0, "", colors.white, colors.lime, {setColor, colors.lime}),
  107. pink = Button:new(nil, termWidth-2, CBY + 6, 0, 0, "", colors.white, colors.pink, {setColor, colors.pink}),
  108. gray = Button:new(nil, termWidth-2, CBY + 7, 0, 0, "", colors.white, colors.gray, {setColor, colors.gray}),
  109. lightGray = Button:new(nil, termWidth-1, CBY, 0, 0, "", colors.white, colors.lightGray, {setColor, colors.lightGray}),
  110. blue = Button:new(nil, termWidth-1, CBY + 1, 0, 0, "", colors.white, colors.blue, {setColor, colors.blue}),
  111. purple = Button:new(nil, termWidth-1, CBY + 2, 0, 0, "", colors.white, colors.purple, {setColor, colors.purple}),
  112. cyan = Button:new(nil, termWidth-1, CBY + 3, 0, 0, "", colors.white, colors.cyan, {setColor, colors.cyan}),
  113. brown = Button:new(nil, termWidth-1, CBY + 4, 0, 0, "", colors.white, colors.brown, {setColor, colors.brown}),
  114. green = Button:new(nil, termWidth-1, CBY + 5, 0, 0, "", colors.white, colors.green, {setColor, colors.green}),
  115. red = Button:new(nil, termWidth-1, CBY + 6, 0, 0, "", colors.white, colors.red, {setColor, colors.red}),
  116. black = Button:new(nil, termWidth-1, CBY + 7, 0, 0, "", colors.white, colors.black, {setColor, colors.black}),
  117. }
  118.  
  119. optionsButton = Button:new(nil, 1, 1, 7, 1, "OPTIONS", colors.black, colors.lime, toggleOptions)
  120.  
  121. optionsMenu = {
  122. saveButton = Button:new(nil, 1, 2, 7, 1, "SAVE", colors.black, colors.blue, "return"),
  123. newButton = Button:new(nil, 1, 3, 7, 1, "NEW", colors.black, colors.blue, "return"),
  124. quitButton = Button:new(nil, 1, 4, 7, 1, "QUIT", colors.black, colors.blue, quitApp)
  125. }
  126.  
  127. parallel.waitForAll(draw, click)
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement