Advertisement
john9912

drawLight

Oct 23rd, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. args = {...}
  2. if fs.isDir("/drawLight/") == false then --fs, like I/O APIs
  3. fs.makeDir("/.drawLight/")
  4. end
  5. term.clear()
  6. os.loadAPI("frame") -- loads API "frame", custom API
  7. frame.init()
  8. local menuColor = "7"
  9. local c = {}
  10. c[0] = "0"; c[1] = "1"; c[2] = "2"; c[3] = "3"; c[4] = "4"; c[5] = "5"; c[6] = "6"; c[7] = "7"; c[8] = "8"; c[9] = "9"; c[10] = "a"; c[11] = "b"; c[12] = "c"; c[13] = "d"; c[14] = "e" c[15] = "f";
  11. local d = {}
  12. for i=0,15 do
  13. d[c[i]] = true
  14. end
  15. local MaxX,MaxY = term.getSize()
  16. local file
  17. local canvas = {}
  18. local current = {}
  19. local rainbow = false
  20. current.brushSize = 2
  21. do
  22. local y = 1
  23. repeat
  24. local x = 1
  25. repeat
  26. canvas[frame.totable(x,y)] = " "
  27. x = x + 1
  28. until x > MaxX
  29. y = y + 1
  30. until y > MaxY
  31. end
  32. local function load(filename) -- load a file
  33. local file = fs.open(args[1],"r")
  34. canvas = textutils.unserialize(file.readAll())
  35. end
  36. local function save(filename) -- save a file
  37. if fs.exists(filename) then
  38. filename = filename.."(1)"
  39. end
  40. local a = 2
  41. while fs.exists(filename) do
  42. filename = filename:sub(1,#filename).."("..a..")"
  43. end
  44. local file = fs.open(filename,"w")
  45. file.write(textutils.serialize(canvas))
  46. file.close()
  47. end
  48. local function import(filename) -- import a file
  49. if not fs.exists(filename) then
  50. error(filename.." does not exist!",9000)
  51. end
  52. --save("/.drawLight/drawLight_temp"..os.day()..os.time())
  53. local w = 1
  54. local h = 1
  55. local file = fs.open(filename,"r")
  56. local line = " "
  57. frame.setBkg("f")
  58. while h < MaxY+1 do
  59. line = file.readLine()
  60. w = 1
  61. if line ~= nil then
  62. repeat
  63. canvas[frame.totable(w,h)] = line:sub(w,w)
  64. if d[line:sub(w,w)] ~= true then
  65. canvas[frame.totable(w,h)] = "f"
  66. end
  67. w = w + 1
  68. until w > MaxX
  69. end
  70. h = h + 1
  71. end
  72. end
  73. local function export(filename) -- export a file
  74. local file = fs.open(filename,"w")
  75. local xc = 1
  76. local yc = 1
  77. while yc < MaxY + 1 do
  78. xc = 1
  79. while xc < MaxX + 1 do
  80. file.write(canvas[frame.totable(xc,yc)])
  81. xc = xc + 1
  82. end
  83. file.write("\n")
  84. yc = yc + 1
  85. end
  86. file.close()
  87. end
  88. current.color = "0"
  89. local drawColor = "0"
  90. if #args < 1 or #args > 2 then
  91. error("Usage: drawLight (filename) <fancy>",9000)
  92. elseif fs.exists(args[1]) then
  93. local test = fs.open(args[1],"r")
  94. local line = test.readLine()
  95. if line:sub(1,1) == "{" then
  96. load(args[1])
  97. else
  98. import(args[1])
  99. end
  100. end
  101. current.r = 0
  102. local function paint(x,y) -- main drawing function
  103. if x > 0 and x < MaxX+1 and y > 0 and y < MaxY+1 then
  104. current.r = current.r + 1
  105. if current.r == 15 then
  106. current.r = 0
  107. end
  108. if rainbow then
  109. drawColor = c[current.r]
  110. end
  111. frame.setb(x,y,current.color)
  112. frame.draw(x,y)
  113. canvas[frame.totable(x,y)] = current.color
  114. end
  115. end
  116. local function paintSize(x,y,n) -- paintbrush
  117. paint(x,y)
  118. repeat
  119. paint(x-n+1,y-n+1)
  120. paint(x-n+1,y+n-1)
  121. paint(x+n-1,y-n+1)
  122. paint(x+n-1,y+n-1)
  123. n = n - 1
  124. until n == 0
  125. end
  126. local function drawCanvas() -- initialize the drawing area
  127. frame.setCol("f")
  128. local y = 1
  129. repeat
  130. local x = 1
  131. repeat
  132. frame.setc(x,y,"f")
  133. frame.setb(x,y,canvas[frame.totable(x,y)])
  134. frame.sett(x,y," ")
  135. x = x + 1
  136. until x == MaxX + 1
  137. y = y + 1
  138. until y == MaxY + 1
  139. local t = 1
  140. if args[2] == "fancy" then
  141. local a = math.floor(MaxX/MaxY)
  142. repeat
  143. frame.drawPart(a*t,t,MaxX,t+1)
  144. frame.drawPart(a*t-1,t,a*t,MaxY)
  145. t = t + 1
  146. until t == MaxY+1
  147. frame.drawPart(1,1,1,MaxY)
  148. else
  149. frame.drawAll()
  150. end
  151. frame.setCol(menuColor)
  152. frame.setText(MaxX-14,MaxY,"Press left Ctrl")
  153. frame.drawPart(MaxX-15,MaxY,MaxX,MaxY)
  154. end
  155. local function drawMenu() -- initialize the menu
  156. frame.init()
  157. frame.setCol("4")
  158. local TopScreen = "drawLight: "..args[1]
  159. frame.setText(math.floor((MaxX-#TopScreen)/2)+1,1,TopScreen)
  160. frame.setText(1,MaxY-4,"Current Color:")
  161. frame.setText(1,MaxY-3,"Colors:")
  162. frame.setText(1,MaxY-2,"Save - Export - Import")
  163. frame.setText(1,MaxY-1,"Quit")
  164. frame.setText(MaxX-14,MaxY,"Press left Ctrl")
  165. frame.setBkg(menuColor)
  166. local a = 0
  167. local x = 9
  168. frame.setb(15,MaxY-4,drawColor)
  169. repeat
  170. frame.setb(x,MaxY-3,c[a])
  171. a = a + 1
  172. x = x + 1
  173. until a == 16
  174. frame.setText(25,MaxY-3,"Rainbow")
  175. if args[2] == "fancy" then
  176. local t = MaxY
  177. local a = math.floor(MaxX/MaxY)
  178. repeat
  179. frame.drawPart(a*t,t,MaxX,t+1)
  180. frame.drawPart(a*t-1,t,a*t,MaxY)
  181. t = t - 1
  182. until t == -1
  183. frame.drawPart(1,1,1,MaxY)
  184. else
  185. frame.drawAll()
  186. end
  187. end
  188. local function cleanUp(msg) -- for exiting the program
  189. term.clear()
  190. term.setCursorPos(1,1)
  191. error(msg,9000)
  192. end
  193. local function input() -- main event handler
  194. e = {os.pullEvent()}
  195. if menu and e[1] == "mouse_click" then
  196. if e[4] == MaxY-3 and e[3] > 8 and e[3] < 25 then
  197. drawColor = c[e[3]-9]
  198. rainbow = false
  199. elseif e[4] == MaxY-3 and e[3] > 24 and e[3] < 32 then
  200. rainbow = true
  201. elseif e[4] == MaxY-2 and e[3] < 5 then
  202. save(args[1])
  203. elseif e[4] == MaxY-2 and e[3] > 16 and e[3] < 23 then
  204. term.setCursorPos(MaxX/2-5,2)
  205. term.setBackgroundColor(colors.gray)
  206. import(read())
  207. elseif e[4] == MaxY-2 and e[3] > 6 and e[3] < 16 then
  208. export(args[1])
  209. elseif e[4] == MaxY-1 and e[3] < 6 then
  210. local userID
  211. if os.computerLabel() ~= nil then
  212. userID = "\""..os.getComputerLabel().."\""
  213. else
  214. userID = "computer"
  215. end
  216. save("/.drawLight/drawLight_temp"..os.day()..os.time())
  217. cleanUp(shell.getRunningProgram().." Quit by "..userID.." user.")
  218. end
  219. end
  220. if menu == false then
  221. if e[1] == "mouse_click" or e[1] == "mouse_drag" then
  222. if e[2] == 1 then
  223. current.color = drawColor
  224. paintSize(e[3],e[4],current.brushSize)
  225. elseif e[2] == 2 then
  226. current.color = "f"
  227. paintSize(e[3],e[4],current.brushSize)
  228. end
  229. end
  230. end
  231. --frame.drawPart(MaxX-10,MaxY,MaxX,MaxY)
  232. if e[1] == "key_up" and e[2] == keys.leftCtrl then
  233. if menu == true then
  234. menu = false
  235. else
  236. menu = true
  237. end
  238. end
  239. if e[1] == "key_up" and e[2] == keys.f1 then
  240. save("/.drawLight/drawLight_temp"..os.day()..os.time())
  241. local userID
  242. if os.computerLabel() ~= nil then
  243. userID = "\""..os.getComputerLabel().."\""
  244. else
  245. userID = "computer"
  246. end
  247. cleanUp(shell.getRunningProgram().." Quit by "..userID.." user.")
  248. end
  249. if e[1] == "mouse_scroll" then
  250. if e[2] == -1 and current.brushSize < 15 then
  251. current.brushSize = current.brushSize + 1
  252. elseif e[2] == 1 and current.brushSize > 1 then
  253. current.brushSize = current.brushSize - 1
  254. end
  255. end
  256. end
  257. while true do -- main loop structure
  258. drawMenu() -- menu
  259. repeat
  260. input()
  261. frame.setb(15,MaxY-4,drawColor)
  262. frame.draw(15,MaxY-4)
  263. until menu == false
  264. drawCanvas() -- drawing area
  265. repeat
  266. input()
  267. until menu
  268. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement