Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pastebin get v5mRmNm2 textureEditor
- local component = require("component")
- local gpu = component.gpu
- local event = require("event")
- local term = require("term")
- local unicode = require("unicode")
- local serialization = require("serialization")
- filler = "▀"
- filler1 = "▒"
- filler2 = "▓"
- filler3 = "█"
- pixels = {}
- foregroundChars = {}
- backgroundChars = {}
- textures = {}
- textureColors = {}
- resultLeft = false
- resultRight = false
- valueLeftColor = 0xFFFFFF
- valueRightColor = 0xFFFFFF
- texturePosition = 1
- texturePosition1 = 21
- startTexturePosition = 1
- gpu.setForeground(0x000000)
- gpu.setBackground(0x555555)
- gpu.fill(63, 17, 36, 18, " ")
- gpu.setForeground(0x555555)
- gpu.setBackground(0x000000)
- --gpu.fill(73, 21, 16, 8, " ")
- function drawArea()
- gpu.setForeground(0x555555)
- gpu.setBackground(0x000000)
- gpu.fill(65, 18, 32, 16, filler2)
- for i = 1, 16 do
- for l = 1, 32, 2 do
- if i%2 == 1 and math.ceil(l/2)%2 == 1 then
- gpu.set(64+l, 17+i, filler1..filler1)
- elseif i%2 == 0 and math.ceil(l/2)%2 == 0 then
- gpu.set(64+l, 17+i, filler1..filler1)
- end
- end
- end
- end
- --for i = 1, 16, 2 do
- --for l = 1, 8 do
- --gpu.set(72+i, 20+l, filler)
- --end
- --end
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x555555)
- gpu.fill(1, 3, 24, 21, " ")
- gpu.fill(103, 24, 20, 10, " ")
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x777777)
- gpu.fill(105, 25, 16, 8, " ")
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- --fileTextures1 = io.open("Textures", "w")
- --fileTextures1:write("--First start".."\n".."--Succesfully done")
- --fileTextures1:close()
- fileTextures = io.open("Textures", "r")
- for line in fileTextures:lines() do table.insert(textures, serialization.unserialize(line)) end
- fileTextures:close()
- print(#textures)
- function insertTextures()
- if #textures >= 22 then
- for i = 1, 21 do
- 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})
- end
- else
- for i = 1, #textures do
- 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})
- end
- end
- drawButtons()
- end
- fileTextureColors = io.open("Texture colors", "r")
- if fileTextureColors then
- for line in fileTextureColors:lines() do table.insert(textureColors, tonumber(line)) end
- fileTextureColors:close()
- end
- function drawTextureColors()
- h = 4
- l = 3
- for i = 1, #textureColors do
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(textureColors[i])
- gpu.set(l, h, " ")
- l = l+2
- if l == 21 then
- l = 3
- h = h+1
- end
- if i == #textureColors then
- break
- end
- end
- end
- function setColor()
- if leftColor ~= nil then
- gpu.setBackground(tonumber(leftColor))
- gpu.set(4, 43, " ")
- valueLeftColor = tonumber(leftColor)
- end
- if rightColor ~= nil then
- gpu.setBackground(tonumber(rightColor))
- gpu.set(4, 44, " ")
- valueRightColor = tonumber(rightColor)
- end
- end
- function colorValue(cValue)
- if cValue == "Left color" then
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- gpu.set(7+unicode.len(cValue), 43, " ")
- term.setCursor(7+unicode.len(cValue), 43)
- leftColor = io.read()
- --valueLeftColor = tonumber(leftColor)
- setColor()
- elseif cValue == "Right color" then
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- gpu.set(7+unicode.len(cValue), 44, " ")
- term.setCursor(7+unicode.len(cValue), 44)
- rightColor = io.read()
- --valueRightColor = tonumber(rightColor)
- setColor()
- end
- end
- function exit()
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- fileTextures = io.open("Textures", "w")
- for i = 1, #textures do
- fileTextures:write("\n"..serialization.serialize(textures[i]))
- end
- fileTextures:close()
- fileTextureColors1 = io.open("Texture colors", "w")
- for i = 1, #textureColors do
- fileTextureColors1:write(tostring(textureColors[i]).."\n")
- end
- fileTextureColors1:close()
- term.clear()
- os.exit()
- end
- function fileName(actFile)
- local resultSave = false
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- if actFile == "Open file" then
- term.setCursor(7, 48)
- nameOfFile = io.read()
- for kTexture,vTexture in pairs(textures) do
- if nameOfFile == vTexture[1] then
- pixels = vTexture[2]
- drawPixels()
- break
- --else
- --print("Wrong name of texture")
- end
- end
- nameOfFile = nil
- elseif actFile == "Save file" then
- term.setCursor(18, 48)
- nameOfFile = io.read()
- if nameOfFile ~= nil then
- pixels = {}
- for i = 18, 33, 2 do
- for l = 65, 96, 2 do
- _,_,colorUp,_,_ = gpu.get(l,i)
- _,_,colorDown,_,_ = gpu.get(l,i+1)
- table.insert(pixels, {xPixel=math.ceil((l-64)/2), yPixel=math.ceil((i-17)/2), fPixel=colorUp, bPixel=colorDown})
- end
- end
- for kSave,vSave in pairs(textures) do
- if nameOfFile == vSave[1] then
- resultSave = true
- indx = kSave
- end
- end
- end
- if resultSave then
- table.remove(textures, indx)
- table.insert(textures, {nameOfFile, pixels})
- else
- table.insert(textures, {nameOfFile, pixels})
- --print(textures[1][1])
- end
- end
- insertTextures()
- end
- function drawPixels()
- if #pixels >= 1 then
- for kPixels, vPixels in pairs(pixels) do
- --if vPixels.yPixel%2 == 1 then
- gpu.setBackground(vPixels.fPixel)
- gpu.set((vPixels.xPixel*2-1)+64, (vPixels.yPixel*2-1)+17, " ")
- --elseif vPixels.yPixel%2 == 0 then
- gpu.setBackground(vPixels.bPixel)
- gpu.set((vPixels.xPixel*2-1)+64, vPixels.yPixel*2+17, " ")
- --end
- end
- else
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- gpu.fill(73, 21, 16, 8, " ")
- end
- end
- function addColor()
- for i = 1, #textureColors do
- if valueLeftColor == textureColors[i] then
- resultLeft = true
- break
- else
- resultLeft = false
- end
- end
- for i = 1, #textureColors do
- if valueRightColor == textureColors[i] then
- resultRight = true
- break
- else
- resultRight = false
- end
- end
- if not resultLeft then
- table.insert(textureColors, valueLeftColor)
- drawTextureColors()
- end
- if not resultRight then
- table.insert(textureColors, valueRightColor)
- drawTextureColors()
- end
- end
- function newFile()
- pixels = {}
- --drawPixels()
- drawArea()
- end
- function chooseColor()
- while true do
- local _,_,x,y,key,name = event.pull("touch")
- if (x >= 65 and x <= 96) and (y >= 18 and y <= 33) then
- if key == 0 then
- _,_,leftColor,_,_ = gpu.get(x,y)
- setColor()
- elseif key == 1 then
- _,_,rightColor,_,_ = gpu.get(x,y)
- setColor()
- end
- else
- break
- end
- end
- end
- function drawPreview()
- if #pixels >= 1 then
- for kPreview, vPreview in pairs(pixels) do
- gpu.setForeground(vPreview.fPixel)
- gpu.setBackground(vPreview.bPixel)
- gpu.set(vPreview.xPixel+104, vPreview.yPixel+24, filler)
- end
- end
- end
- function scrollTexturesList(listPosition, multiple)
- if multiple == -1 then
- if #textures == 22 then
- startTexturePosition = 2
- elseif #textures == 23 then
- startTexturePosition = 3
- elseif #textures >= 24 then
- startTexturePosition = startTexturePosition + listPosition
- end
- elseif multiple == 1 then
- if #textures == 22 then
- startTexturePosition = 2
- elseif #textures == 23 then
- startTexturePosition = 3
- elseif #textures >= 24 then
- startTexturePosition = startTexturePosition - listPosition
- end
- end
- insertTextures()
- end
- function openTexture(tbl)
- pixels = tbl
- drawPixels()
- end
- buttons = {{x=7, y=45, text="Choose color", func=chooseColor, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=7, y=43, text="Left color", func=colorValue, active=true, cFore = 0x000000, cBack = 0xFFFFFF},
- {x=7, y=44, text="Right color", func=colorValue, active=true, cFore = 0x000000, cBack = 0xFFFFFF},
- {x=158, y=1, text=" X ", func=exit, active=true, cFore = 0xFFFFFF, cBack = 0xFF0000},
- {x=7, y=47, text="Open file", func=fileName, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=18, y=47, text="Save file", func=fileName, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=7, y=46, text="Add color", func=addColor, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=7, y=49, text="New file", func=newFile, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=105, y=22, text="Preview", func=drawPreview, active=true, cFore = 0xFFFFFF, cBack = 0x000000}}
- scrollAreas = {{x=141, x1=160, y=4, y1=24, func=scrollTexturesList, count=3}}
- function drawButtons()
- for k,v in pairs(buttons) do
- if v.active then
- gpu.setForeground(v.cFore)
- gpu.setBackground(v.cBack)
- gpu.set(v.x, v.y, v.text)
- end
- end
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- end
- function main()
- while true do
- local event,_,x,y,key,name = event.pull()
- if event == "touch" then
- for k1,v1 in pairs(buttons) do
- if (x >= v1.x and x < v1.x+unicode.len(v1.text)) and y == v1.y and v1.active then
- if v1.text == "Left color" or v1.text == "Right color" or v1.text == "Open file" or v1.text == "Save file" then
- v1.func(v1.text)
- elseif v1.pxls ~= nil then
- v1.func(v1.pxls)
- else
- v1.func()
- end
- end
- end
- if (x >= 65 and x <= 96) and (y >= 18 and y <= 33) then
- gpu.setForeground(0xFFFFFF)
- --gpu.setBackground(valueBackground)
- if key == 0 then
- if gpu.getBackground() ~= valueLeftColor then
- gpu.setBackground(valueLeftColor)
- end
- elseif key == 1 then
- if gpu.getBackground() ~= valueRightColor then
- gpu.setBackground(valueRightColor)
- end
- end
- if x%2 == 1 then
- gpu.set(x,y, " ")
- elseif x%2 == 0 then
- gpu.set(x-1,y, " ")
- --table.insert(pixels, {xPixel=x-72, yPixel=y-20, fPixel=valueForeground, bPixel=valueBackground})
- end
- end
- if (x >= 3 and x <= 21) and (y >= 4 and y <= 20) then
- if key == 0 then
- _,_,leftColor,_,_ = gpu.get(x,y)
- setColor()
- elseif key == 1 then
- _,_,rightColor,_,_ = gpu.get(x,y)
- setColor()
- end
- end
- elseif event == "scroll" then
- for k2,v2 in pairs(scrollAreas) do
- if (x >= v2.x and x <= v2.x1) and (y >= v2.y and y <= v2.y1) then
- v2.func(v2.count, key)
- end
- end
- end
- end
- end
- drawButtons()
- drawTextureColors()
- drawArea()
- insertTextures()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement