Advertisement
Hikooshi

Редактор текстур

Sep 28th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. --pastebin get v5mRmNm2 textureEditor
  2. local component = require("component")
  3. local gpu = component.gpu
  4. local event = require("event")
  5. local term = require("term")
  6. local unicode = require("unicode")
  7. local serialization = require("serialization")
  8.  
  9.  
  10.  
  11. filler = "▀"
  12. filler1 = "▒"
  13. filler2 = "▓"
  14. filler3 = "█"
  15. pixels = {}
  16. foregroundChars = {}
  17. backgroundChars = {}
  18. textures = {}
  19. textureColors = {}
  20. resultLeft = false
  21. resultRight = false
  22. valueLeftColor = 0xFFFFFF
  23. valueRightColor = 0xFFFFFF
  24. texturePosition = 1
  25. texturePosition1 = 21
  26. startTexturePosition = 1
  27.  
  28.  
  29.  
  30. gpu.setForeground(0x000000)
  31. gpu.setBackground(0x555555)
  32. gpu.fill(63, 17, 36, 18, " ")
  33. gpu.setForeground(0x555555)
  34. gpu.setBackground(0x000000)
  35. --gpu.fill(73, 21, 16, 8, " ")
  36.  
  37. function drawArea()
  38.  
  39. gpu.setForeground(0x555555)
  40. gpu.setBackground(0x000000)
  41. gpu.fill(65, 18, 32, 16, filler2)
  42. for i = 1, 16 do
  43. for l = 1, 32, 2 do
  44. if i%2 == 1 and math.ceil(l/2)%2 == 1 then
  45. gpu.set(64+l, 17+i, filler1..filler1)
  46. elseif i%2 == 0 and math.ceil(l/2)%2 == 0 then
  47. gpu.set(64+l, 17+i, filler1..filler1)
  48. end
  49. end
  50. end
  51.  
  52. end
  53.  
  54. --for i = 1, 16, 2 do
  55. --for l = 1, 8 do
  56. --gpu.set(72+i, 20+l, filler)
  57. --end
  58. --end
  59. gpu.setForeground(0xFFFFFF)
  60. gpu.setBackground(0x555555)
  61. gpu.fill(1, 3, 24, 21, " ")
  62. gpu.fill(103, 24, 20, 10, " ")
  63. gpu.setForeground(0xFFFFFF)
  64. gpu.setBackground(0x777777)
  65. gpu.fill(105, 25, 16, 8, " ")
  66. gpu.setForeground(0xFFFFFF)
  67. gpu.setBackground(0x000000)
  68.  
  69. --fileTextures1 = io.open("Textures", "w")
  70. --fileTextures1:write("--First start".."\n".."--Succesfully done")
  71. --fileTextures1:close()
  72.  
  73. fileTextures = io.open("Textures", "r")
  74. for line in fileTextures:lines() do table.insert(textures, serialization.unserialize(line)) end
  75. fileTextures:close()
  76. print(#textures)
  77.  
  78. function insertTextures()
  79.  
  80. if #textures >= 22 then
  81. for i = 1, 21 do
  82. table.insert(buttons, {x=130, y=4+i-1, text=textures[i+startTexturePosition-1][1], pxls=textures[i+startTexturePosition-1][2], func=openTexture, cFore = 0xFFFFFF, cBack = 0x000000})
  83. end
  84. else
  85. for i = 1, #textures do
  86. table.insert(buttons, {x=130, y=4+i-1, text=textures[i][1], pxls=textures[i][2], func=openTexture, active=true, cFore = 0xFFFFFF, cBack = 0x000000})
  87. end
  88. end
  89. drawButtons()
  90.  
  91. end
  92.  
  93. fileTextureColors = io.open("Texture colors", "r")
  94. if fileTextureColors then
  95. for line in fileTextureColors:lines() do table.insert(textureColors, tonumber(line)) end
  96. fileTextureColors:close()
  97. end
  98.  
  99. function drawTextureColors()
  100.  
  101. h = 4
  102. l = 3
  103. for i = 1, #textureColors do
  104. gpu.setForeground(0xFFFFFF)
  105. gpu.setBackground(textureColors[i])
  106. gpu.set(l, h, " ")
  107. l = l+2
  108. if l == 21 then
  109. l = 3
  110. h = h+1
  111. end
  112. if i == #textureColors then
  113. break
  114. end
  115. end
  116.  
  117. end
  118.  
  119. function setColor()
  120.  
  121. if leftColor ~= nil then
  122. gpu.setBackground(tonumber(leftColor))
  123. gpu.set(4, 43, " ")
  124. valueLeftColor = tonumber(leftColor)
  125. end
  126. if rightColor ~= nil then
  127. gpu.setBackground(tonumber(rightColor))
  128. gpu.set(4, 44, " ")
  129. valueRightColor = tonumber(rightColor)
  130. end
  131.  
  132. end
  133.  
  134. function colorValue(cValue)
  135.  
  136. if cValue == "Left color" then
  137. gpu.setForeground(0xFFFFFF)
  138. gpu.setBackground(0x000000)
  139. gpu.set(7+unicode.len(cValue), 43, " ")
  140. term.setCursor(7+unicode.len(cValue), 43)
  141. leftColor = io.read()
  142. --valueLeftColor = tonumber(leftColor)
  143. setColor()
  144. elseif cValue == "Right color" then
  145. gpu.setForeground(0xFFFFFF)
  146. gpu.setBackground(0x000000)
  147. gpu.set(7+unicode.len(cValue), 44, " ")
  148. term.setCursor(7+unicode.len(cValue), 44)
  149. rightColor = io.read()
  150. --valueRightColor = tonumber(rightColor)
  151. setColor()
  152. end
  153.  
  154. end
  155.  
  156. function exit()
  157.  
  158. gpu.setForeground(0xFFFFFF)
  159. gpu.setBackground(0x000000)
  160. fileTextures = io.open("Textures", "w")
  161. for i = 1, #textures do
  162. fileTextures:write("\n"..serialization.serialize(textures[i]))
  163. end
  164. fileTextures:close()
  165. fileTextureColors1 = io.open("Texture colors", "w")
  166. for i = 1, #textureColors do
  167. fileTextureColors1:write(tostring(textureColors[i]).."\n")
  168. end
  169. fileTextureColors1:close()
  170. term.clear()
  171. os.exit()
  172.  
  173. end
  174.  
  175. function fileName(actFile)
  176.  
  177. local resultSave = false
  178. gpu.setForeground(0xFFFFFF)
  179. gpu.setBackground(0x000000)
  180. if actFile == "Open file" then
  181. term.setCursor(7, 48)
  182. nameOfFile = io.read()
  183. for kTexture,vTexture in pairs(textures) do
  184. if nameOfFile == vTexture[1] then
  185. pixels = vTexture[2]
  186. drawPixels()
  187. break
  188. --else
  189. --print("Wrong name of texture")
  190. end
  191. end
  192. nameOfFile = nil
  193. elseif actFile == "Save file" then
  194. term.setCursor(18, 48)
  195. nameOfFile = io.read()
  196. if nameOfFile ~= nil then
  197. pixels = {}
  198. for i = 18, 33, 2 do
  199. for l = 65, 96, 2 do
  200. _,_,colorUp,_,_ = gpu.get(l,i)
  201. _,_,colorDown,_,_ = gpu.get(l,i+1)
  202. table.insert(pixels, {xPixel=math.ceil((l-64)/2), yPixel=math.ceil((i-17)/2), fPixel=colorUp, bPixel=colorDown})
  203. end
  204. end
  205. for kSave,vSave in pairs(textures) do
  206. if nameOfFile == vSave[1] then
  207. resultSave = true
  208. indx = kSave
  209. end
  210. end
  211. end
  212. if resultSave then
  213. table.remove(textures, indx)
  214. table.insert(textures, {nameOfFile, pixels})
  215. else
  216. table.insert(textures, {nameOfFile, pixels})
  217. --print(textures[1][1])
  218. end
  219. end
  220. insertTextures()
  221.  
  222. end
  223.  
  224. function drawPixels()
  225.  
  226. if #pixels >= 1 then
  227. for kPixels, vPixels in pairs(pixels) do
  228. --if vPixels.yPixel%2 == 1 then
  229. gpu.setBackground(vPixels.fPixel)
  230. gpu.set((vPixels.xPixel*2-1)+64, (vPixels.yPixel*2-1)+17, " ")
  231. --elseif vPixels.yPixel%2 == 0 then
  232. gpu.setBackground(vPixels.bPixel)
  233. gpu.set((vPixels.xPixel*2-1)+64, vPixels.yPixel*2+17, " ")
  234. --end
  235. end
  236. else
  237. gpu.setForeground(0xFFFFFF)
  238. gpu.setBackground(0x000000)
  239. gpu.fill(73, 21, 16, 8, " ")
  240. end
  241.  
  242. end
  243.  
  244. function addColor()
  245.  
  246. for i = 1, #textureColors do
  247. if valueLeftColor == textureColors[i] then
  248. resultLeft = true
  249. break
  250. else
  251. resultLeft = false
  252. end
  253. end
  254. for i = 1, #textureColors do
  255. if valueRightColor == textureColors[i] then
  256. resultRight = true
  257. break
  258. else
  259. resultRight = false
  260. end
  261. end
  262. if not resultLeft then
  263. table.insert(textureColors, valueLeftColor)
  264. drawTextureColors()
  265. end
  266. if not resultRight then
  267. table.insert(textureColors, valueRightColor)
  268. drawTextureColors()
  269. end
  270.  
  271. end
  272.  
  273. function newFile()
  274.  
  275. pixels = {}
  276. --drawPixels()
  277. drawArea()
  278.  
  279. end
  280.  
  281. function chooseColor()
  282.  
  283. while true do
  284. local _,_,x,y,key,name = event.pull("touch")
  285. if (x >= 65 and x <= 96) and (y >= 18 and y <= 33) then
  286. if key == 0 then
  287. _,_,leftColor,_,_ = gpu.get(x,y)
  288. setColor()
  289. elseif key == 1 then
  290. _,_,rightColor,_,_ = gpu.get(x,y)
  291. setColor()
  292. end
  293. else
  294. break
  295. end
  296. end
  297.  
  298. end
  299.  
  300. function drawPreview()
  301.  
  302. if #pixels >= 1 then
  303. for kPreview, vPreview in pairs(pixels) do
  304. gpu.setForeground(vPreview.fPixel)
  305. gpu.setBackground(vPreview.bPixel)
  306. gpu.set(vPreview.xPixel+104, vPreview.yPixel+24, filler)
  307. end
  308. end
  309.  
  310. end
  311.  
  312. function scrollTexturesList(listPosition, multiple)
  313.  
  314. if multiple == -1 then
  315. if #textures == 22 then
  316. startTexturePosition = 2
  317. elseif #textures == 23 then
  318. startTexturePosition = 3
  319. elseif #textures >= 24 then
  320. startTexturePosition = startTexturePosition + listPosition
  321. end
  322. elseif multiple == 1 then
  323. if #textures == 22 then
  324. startTexturePosition = 2
  325. elseif #textures == 23 then
  326. startTexturePosition = 3
  327. elseif #textures >= 24 then
  328. startTexturePosition = startTexturePosition - listPosition
  329. end
  330. end
  331. insertTextures()
  332.  
  333. end
  334.  
  335. function openTexture(tbl)
  336.  
  337. pixels = tbl
  338. drawPixels()
  339.  
  340. end
  341.  
  342. buttons = {{x=7, y=45, text="Choose color", func=chooseColor, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  343. {x=7, y=43, text="Left color", func=colorValue, active=true, cFore = 0x000000, cBack = 0xFFFFFF},
  344. {x=7, y=44, text="Right color", func=colorValue, active=true, cFore = 0x000000, cBack = 0xFFFFFF},
  345. {x=158, y=1, text=" X ", func=exit, active=true, cFore = 0xFFFFFF, cBack = 0xFF0000},
  346. {x=7, y=47, text="Open file", func=fileName, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  347. {x=18, y=47, text="Save file", func=fileName, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  348. {x=7, y=46, text="Add color", func=addColor, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  349. {x=7, y=49, text="New file", func=newFile, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
  350. {x=105, y=22, text="Preview", func=drawPreview, active=true, cFore = 0xFFFFFF, cBack = 0x000000}}
  351.  
  352. scrollAreas = {{x=141, x1=160, y=4, y1=24, func=scrollTexturesList, count=3}}
  353.  
  354. function drawButtons()
  355.  
  356. for k,v in pairs(buttons) do
  357. if v.active then
  358. gpu.setForeground(v.cFore)
  359. gpu.setBackground(v.cBack)
  360. gpu.set(v.x, v.y, v.text)
  361. end
  362. end
  363. gpu.setForeground(0xFFFFFF)
  364. gpu.setBackground(0x000000)
  365.  
  366. end
  367.  
  368. function main()
  369.  
  370. while true do
  371. local event,_,x,y,key,name = event.pull()
  372. if event == "touch" then
  373. for k1,v1 in pairs(buttons) do
  374. if (x >= v1.x and x < v1.x+unicode.len(v1.text)) and y == v1.y and v1.active then
  375. if v1.text == "Left color" or v1.text == "Right color" or v1.text == "Open file" or v1.text == "Save file" then
  376. v1.func(v1.text)
  377. elseif v1.pxls ~= nil then
  378. v1.func(v1.pxls)
  379. else
  380. v1.func()
  381. end
  382. end
  383. end
  384. if (x >= 65 and x <= 96) and (y >= 18 and y <= 33) then
  385. gpu.setForeground(0xFFFFFF)
  386. --gpu.setBackground(valueBackground)
  387. if key == 0 then
  388. if gpu.getBackground() ~= valueLeftColor then
  389. gpu.setBackground(valueLeftColor)
  390. end
  391. elseif key == 1 then
  392. if gpu.getBackground() ~= valueRightColor then
  393. gpu.setBackground(valueRightColor)
  394. end
  395. end
  396. if x%2 == 1 then
  397. gpu.set(x,y, " ")
  398. elseif x%2 == 0 then
  399. gpu.set(x-1,y, " ")
  400. --table.insert(pixels, {xPixel=x-72, yPixel=y-20, fPixel=valueForeground, bPixel=valueBackground})
  401. end
  402. end
  403.  
  404. if (x >= 3 and x <= 21) and (y >= 4 and y <= 20) then
  405. if key == 0 then
  406. _,_,leftColor,_,_ = gpu.get(x,y)
  407. setColor()
  408. elseif key == 1 then
  409. _,_,rightColor,_,_ = gpu.get(x,y)
  410. setColor()
  411. end
  412. end
  413. elseif event == "scroll" then
  414. for k2,v2 in pairs(scrollAreas) do
  415. if (x >= v2.x and x <= v2.x1) and (y >= v2.y and y <= v2.y1) then
  416. v2.func(v2.count, key)
  417. end
  418. end
  419. end
  420. end
  421.  
  422. end
  423.  
  424. drawButtons()
  425. drawTextureColors()
  426. drawArea()
  427. insertTextures()
  428. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement