Guest User

SpriteMog1.2

a guest
Aug 9th, 2026
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.23 KB | None | 0 0
  1. local camera = {x = 30,y = 30}
  2. local selectedColour = 1
  3. local width,height = term.getSize()
  4. local palette = {}
  5. local originalPalette = {}
  6. local sprite = {}
  7. local keysDown = {}
  8. local colourSelected = colors.black
  9. local backgroundColour = colors.purple
  10. local spriteWindow = window.create(term.current(),1,4,width,height-3)
  11. local guiWindow = window.create(term.current(),1,1,width,3)
  12. local brightness = 1
  13. term.clear()
  14.  
  15. local function writeAt(contents,x,y,text,background,win)
  16.     win.setBackgroundColor(background)
  17.     win.setTextColor(text)
  18.     win.setCursorPos(x,y)
  19.     win.write(contents)
  20. end
  21.  
  22. local function clear(win,colour)
  23.     win.setBackgroundColor(colour)
  24.     win.clear()
  25. end
  26.  
  27. local function loadPalette()
  28.    
  29.     if not fs.isDir(shell.resolve("palettes")) then
  30.         fs.makeDir(shell.resolve("palettes"))
  31.         fs.makeDir(shell.resolve("palettes/default"))
  32.         fs.makeDir(shell.resolve("palettes/default/sprites"))
  33.         file = fs.open(shell.resolve("palettes/default/default.plt"),"w")
  34.         file.writeLine("{0xF0F0F0,0xF2B233,0xE57FD8,0x99B2F2,0xDEDE6C,0x7FCC19,0xF2B2CC,0x4C4C4C,0x999999,0x4C99B2,0xB266E5,0x3366CC,0x7F664C,0x57A64E,0xCC4C4C,0x111111}")
  35.         file.close()
  36.     end
  37.    
  38.     local function drawHeader()
  39.         clear(paletteSelectionWindow,colors.white)
  40.         writeAt("-SELECT PALETTE-",2,2,colors.gray,colors.white,paletteSelectionWindow)    
  41.     end
  42.    
  43.     local paletteList = fs.list(shell.resolve("palettes"))
  44.    
  45.     paletteSelectionWindow = window.create(term.current(),3,3,18,#paletteList + 3)
  46.    
  47.     drawHeader()
  48.    
  49.     for i = 1,#paletteList do
  50.         writeAt(paletteList[i],3,i+2,colors.blue,colors.white,paletteSelectionWindow)
  51.     end
  52.  
  53.     while true do
  54.         event,key,x,y = os.pullEvent()
  55.        
  56.         if event == "mouse_click" and key == 1 and x > 3 and x < 21 and y > 4 and y < #paletteList + 5 then
  57.        
  58.             drawHeader()
  59.            
  60.             for i = 1,#paletteList do
  61.            
  62.                 if i + 4 == y then
  63.                     writeAt(paletteList[i],3,i+2,colors.blue,colors.white,paletteSelectionWindow)
  64.                 else
  65.                     writeAt(paletteList[i],3,i+2,colors.lightGray,colors.white,paletteSelectionWindow)
  66.                 end
  67.  
  68.             end
  69.            
  70.             sleep(0.15)
  71.            
  72.             file = fs.open(shell.resolve("palettes/"..paletteList[y-4].."/"..paletteList[y-4]..".plt"),"r")
  73.            
  74.             palette = textutils.unserialize(file.readLine(),{compact = true})
  75.             palette.name = paletteList[y-4]
  76.             file.close()
  77.             -- Snapshot for dirty-checking
  78.             originalPalette = {}
  79.             for i = 1,16 do originalPalette[i] = palette[i] end
  80.             paletteSelectionWindow.setBackgroundColor(colors.black)
  81.             paletteSelectionWindow.reposition(3,3,18,16)
  82.            
  83.                 for i = 0,15 do
  84.                     term.setPaletteColor(2^i,palette[i+1]) 
  85.                 end
  86.            
  87.             break
  88.         end
  89.     end
  90. end
  91.  
  92. local function loadSprite()
  93.    
  94.     local function drawHeader()
  95.         clear(spriteSelectionWindow,colors.white)
  96.         writeAt("-SELECT SPRITE-",2,2,colors.gray,colors.white,spriteSelectionWindow)
  97.     end
  98.    
  99.     local spriteList = fs.list(shell.resolve("palettes/"..palette.name.."/sprites"))
  100.    
  101.     spriteList[#spriteList + 1] = "newSprite"
  102.    
  103.     spriteSelectionWindow = window.create(term.current(),3,3,40,#spriteList + 3)
  104.    
  105.     drawHeader()
  106.    
  107.     for i = 1,#spriteList do
  108.         writeAt(spriteList[i],3,i+2,colors.blue,colors.white,spriteSelectionWindow)
  109.     end
  110.  
  111.     while true do
  112.         event,key,x,y = os.pullEvent()
  113.        
  114.         if event == "mouse_click" and key == 1 and x > 3 and x < 21 and y > 4 and y < #spriteList + 5 then
  115.        
  116.             drawHeader()
  117.            
  118.             for i = 1,#spriteList do
  119.            
  120.                 if i + 4 == y then
  121.                     writeAt(spriteList[i],3,i+2,colors.blue,colors.white,spriteSelectionWindow)
  122.                 else
  123.                     writeAt(spriteList[i],3,i+2,colors.lightGray,colors.white,spriteSelectionWindow)
  124.                 end
  125.                
  126.             end
  127.            
  128.             sleep(0.15)
  129.            
  130.             if spriteList[y-4] == "newSprite" then
  131.                 sprite = {{1,1,colors.black},{16,1,colors.black},{1,16,colors.black},{16,16,colors.black}}
  132.                 sprite.name = "unnamedSprite"
  133.             else
  134.                 file = fs.open(shell.resolve("palettes/"..palette.name.."/sprites/"..spriteList[y-4]),"r")
  135.                 sprite = textutils.unserialize(file.readLine(),{compact = true})
  136.                 sprite.name = spriteList[y-4]
  137.                 file.close()
  138.             end
  139.             break
  140.         end
  141.     end
  142. end
  143.  
  144. local function drawSprite()
  145.    
  146.     for i = 1,#sprite do
  147.  
  148.             for y = 1,2 do
  149.                 spriteWindow.setCursorPos   (      
  150.                                     math.floor(sprite[i][1] * 3 + camera.x),
  151.                                     math.floor(sprite[i][2] * 2 + camera.y) + y
  152.                                     )
  153.                                    
  154.                 spriteWindow.setBackgroundColor(sprite[i][3])
  155.                 if sprite[i][1] == 0 and sprite[i][2] == 0 and y == 2 then
  156.                     spriteWindow.write("--+")
  157.                 elseif sprite[i][1] == 0 then
  158.                     spriteWindow.write("  \124")
  159.                 elseif sprite[i][2] == 0 and y == 2 then
  160.                     spriteWindow.write("---")
  161.                 else
  162.                     spriteWindow.write("   ")
  163.                 end
  164.             end
  165.  
  166.     end
  167. end
  168.  
  169. local function drawHeader()
  170.  
  171.     clear(guiWindow,colors.white)
  172.     writeAt("SpriteMog V1.2".."        Brightness: "..brightness,2,2,colors.black,colors.white,guiWindow)
  173.  
  174.     writeAt(":               :              :           :",width-43,1,colors.black,colors.white,guiWindow)
  175.     writeAt(": COLOUR SELECT : PALETTE EDIT : SAVE/EXIT :",width-43,2,colors.black,colors.white,guiWindow)
  176.     writeAt(":               :              :           :",width-43,3,colors.black,colors.white,guiWindow)
  177.  
  178. end
  179.  
  180. local function drawScreen()
  181.    
  182.     clear(spriteWindow,backgroundColour)
  183.     drawHeader()
  184.    
  185.     for i = 1,width do
  186.         writeAt("\124",2+camera.x,i,colors.white,backgroundColour,spriteWindow)
  187.         writeAt("-",i,camera.y+2,colors.white,backgroundColour,spriteWindow)
  188.     end
  189.    
  190.     writeAt("+",2+camera.x,camera.y+2,colors.white,backgroundColour,spriteWindow)
  191.     drawSprite()
  192. end
  193.  
  194. local function colourSelect()
  195.     local colourSelectWindow = window.create(term.current(),width-43,1,17,36)
  196.  
  197.     for i = 1,35 do
  198.         writeAt(":               :",1,i,colors.white,colors.lightGray,colourSelectWindow)
  199.     end
  200.    
  201.     writeAt(": COLOUR SELECT :",1,2,colors.white,colors.lightGray,colourSelectWindow)
  202.     writeAt("\96---------------'",1,36,colors.white,colors.lightGray,colourSelectWindow)
  203.  
  204.    
  205.     for i = 0,15 do
  206.         writeAt("               ",2,2*i+4,2^i,2^i,colourSelectWindow)
  207.         writeAt("               ",2,2*i+5,2^i,2^i,colourSelectWindow)  
  208.     end
  209.    
  210.     while true do
  211.         local event,key,x,y = os.pullEvent()
  212.        
  213.         if event == "mouse_click" then
  214.             if x > width - 43 and x < width - 43 + 16 and y > 3 and y < 36 then
  215.                 if key == 1 then
  216.                     colourSelected = 2^math.floor((y - 4) / 2)
  217.                 elseif key == 2 then
  218.                     backgroundColour = 2^math.floor((y - 4) / 2)
  219.                 end
  220.             end
  221.             break
  222.         end
  223.     end
  224.     timer = os.startTimer(0.05)
  225. end
  226.  
  227. local function paletteEdit()
  228.     local listWinX = width - 27
  229.     local listWinY = 1
  230.     local listWinW = 16
  231.     local listWinH = 36
  232.     local paletteEditWindow = window.create(term.current(), listWinX, listWinY, listWinW, listWinH)
  233.     local iW = listWinW - 2  -- 14 inner chars
  234.  
  235.     local function drawPaletteList()
  236.         for i = 1, listWinH - 1 do
  237.             writeAt(":              :", 1, i, colors.white, colors.lightGray, paletteEditWindow)
  238.         end
  239.         writeAt("\96--------------'", 1, listWinH, colors.white, colors.lightGray, paletteEditWindow)
  240.         writeAt(": PALETTE EDIT :", 1, 2, colors.white, colors.lightGray, paletteEditWindow)
  241.         for i = 0, 15 do
  242.             writeAt("              ", 2, 2*i+4, 2^i, 2^i, paletteEditWindow)
  243.             writeAt("              ", 2, 2*i+5, 2^i, 2^i, paletteEditWindow)
  244.         end
  245.     end
  246.  
  247.     local function openColourEditor(colorKey)
  248.         local colourIndex = colorKey + 1
  249.         local r, g, b
  250.  
  251.         local function loadRGB()
  252.             local rf, gf, bf = colors.unpackRGB(palette[colourIndex])
  253.             r = math.floor(0.5 + rf * 255)
  254.             g = math.floor(0.5 + gf * 255)
  255.             b = math.floor(0.5 + bf * 255)
  256.         end
  257.  
  258.         local function applyRGB()
  259.             palette[colourIndex] = colors.packRGB(r / 255, g / 255, b / 255)
  260.             term.setPaletteColor(2^colorKey, palette[colourIndex])
  261.         end
  262.  
  263.         local blank = string.rep(" ", iW)
  264.  
  265.         local function fmtVal(v)
  266.             local s = tostring(v)
  267.             return string.rep(" ", 3 - #s) .. s
  268.         end
  269.  
  270.         local function drawEditor()
  271.             -- Full border redraw
  272.             for i = 1, listWinH - 1 do
  273.                 writeAt(":              :", 1, i, colors.white, colors.lightGray, paletteEditWindow)
  274.             end
  275.             writeAt("\96--------------'", 1, listWinH, colors.white, colors.lightGray, paletteEditWindow)
  276.             writeAt(": PALETTE EDIT :", 1, 2, colors.white, colors.lightGray, paletteEditWindow)
  277.  
  278.             -- Rows 4-5: colour preview
  279.             local pc = 2^colorKey
  280.             writeAt(blank, 2, 4, pc, pc, paletteEditWindow)
  281.             writeAt(blank, 2, 5, pc, pc, paletteEditWindow)
  282.  
  283.             -- Rows 7-9: UP button (3 tall), mid row = ^ labels
  284.             writeAt(blank,              2,  7, colors.white, colors.gray, paletteEditWindow)
  285.             writeAt("  ^    ^    ^ ", 2,  8, colors.white, colors.gray, paletteEditWindow)
  286.             writeAt(blank,              2,  9, colors.white, colors.gray, paletteEditWindow)
  287.  
  288.             -- Row 10: R G B labels on lightGray, values in black text below them
  289.             writeAt("  R    G    B ", 2, 10, colors.black, colors.lightGray, paletteEditWindow)
  290.  
  291.             -- Row 11: numeric values (lightGray bg, black text)
  292.             local valStr = " " .. fmtVal(r) .. "  " .. fmtVal(g) .. "  " .. fmtVal(b)
  293.             writeAt(valStr,             2, 11, colors.black, colors.lightGray, paletteEditWindow)
  294.  
  295.             -- Rows 12-14: DOWN button (3 tall), mid row = v labels
  296.             writeAt(blank,              2, 12, colors.white, colors.gray, paletteEditWindow)
  297.             writeAt("  v    v    v ", 2, 13, colors.white, colors.gray, paletteEditWindow)
  298.             writeAt(blank,              2, 14, colors.white, colors.gray, paletteEditWindow)
  299.  
  300.             -- Rows 16-18: HEX button
  301.             writeAt(blank,           2, 16, colors.white, colors.gray, paletteEditWindow)
  302.             writeAt("  INPUT HEX   ", 2, 17, colors.white, colors.gray, paletteEditWindow)
  303.             writeAt(blank,           2, 18, colors.white, colors.gray, paletteEditWindow)
  304.  
  305.             -- Rows 20-22: DONE button
  306.             writeAt(blank,           2, 20, colors.white, colors.gray, paletteEditWindow)
  307.             writeAt("     DONE     ", 2, 21, colors.white, colors.gray, paletteEditWindow)
  308.             writeAt(blank,           2, 22, colors.white, colors.gray, paletteEditWindow)
  309.         end
  310.  
  311.         loadRGB()
  312.         drawEditor()
  313.  
  314.         local function inWinX(ax)
  315.             local lx = ax - listWinX + 1
  316.             return lx >= 2 and lx <= listWinW - 1
  317.         end
  318.  
  319.         -- Divide inner 14 cols into thirds: cols 1-4=R, 5-10=G, 11-14=B
  320.         local function channelCol(ax)
  321.             local lx = ax - listWinX  -- 1 = first inner col
  322.             if lx <= 5 then return "R"
  323.             elseif lx <= 10 then return "G"
  324.             else return "B"
  325.             end
  326.         end
  327.  
  328.         while true do
  329.             local event, btn, ax, ay = os.pullEvent()
  330.  
  331.             if event == "mouse_click" and btn == 1 then
  332.                 local ly = ay - listWinY + 1
  333.  
  334.                 if not inWinX(ax) then break end
  335.  
  336.                 if ly >= 7 and ly <= 9 then
  337.                     -- UP button
  338.                     local ch = channelCol(ax)
  339.                     if     ch == "R" then r = math.min(r + 1, 255)
  340.                     elseif ch == "G" then g = math.min(g + 1, 255)
  341.                     else                   b = math.min(b + 1, 255)
  342.                     end
  343.                     applyRGB() ; drawEditor()
  344.  
  345.                 elseif ly >= 12 and ly <= 14 then
  346.                     -- DOWN button
  347.                     local ch = channelCol(ax)
  348.                     if     ch == "R" then r = math.max(r - 1, 0)
  349.                     elseif ch == "G" then g = math.max(g - 1, 0)
  350.                     else                   b = math.max(b - 1, 0)
  351.                     end
  352.                     applyRGB() ; drawEditor()
  353.  
  354.                 elseif ly >= 16 and ly <= 18 then
  355.                     -- HEX input: clear mid row to black for typing
  356.                     writeAt(blank, 2, 17, colors.white, colors.black, paletteEditWindow)
  357.                     term.setCursorPos(listWinX + 1, listWinY + 16)
  358.                     term.setBackgroundColor(colors.black)
  359.                     term.setTextColor(colors.white)
  360.                     local hex = read()
  361.                     if hex and #hex >= 6 then
  362.                         hex = hex:gsub("^#", "")
  363.                         local rv = tonumber(hex:sub(1,2), 16)
  364.                         local gv = tonumber(hex:sub(3,4), 16)
  365.                         local bv = tonumber(hex:sub(5,6), 16)
  366.                         if rv and gv and bv then
  367.                             r, g, b = rv, gv, bv
  368.                             applyRGB()
  369.                         end
  370.                     end
  371.                     drawEditor()
  372.  
  373.                 elseif ly >= 20 and ly <= 22 then
  374.                     -- DONE
  375.                     break
  376.  
  377.                 elseif ly >= 23 then
  378.                     break
  379.                 end
  380.             end
  381.         end
  382.     end
  383.  
  384.     drawPaletteList()
  385.  
  386.     while true do
  387.         local event, key, x, y = os.pullEvent()
  388.  
  389.         if event == "mouse_click" then
  390.             if x >= listWinX and x < listWinX + listWinW and y >= 4 and y < listWinH then
  391.                 if key == 1 then
  392.                     local colorKey = math.floor((y - 4) / 2)
  393.                     if colorKey >= 0 and colorKey <= 15 then
  394.                         openColourEditor(colorKey)
  395.                         drawPaletteList()
  396.                     end
  397.                 end
  398.             else
  399.                 break
  400.             end
  401.         end
  402.     end
  403.  
  404.     timer = os.startTimer(0.05)
  405. end
  406.  
  407. local function paletteModified()
  408.     for i = 1, 16 do
  409.         if palette[i] ~= originalPalette[i] then return true end
  410.     end
  411.     return false
  412. end
  413.  
  414. local function savePalette(asNew)
  415.     if asNew then
  416.         -- Prompt for new palette name
  417.         term.setCursorPos(1, height)
  418.         term.setBackgroundColor(colors.black)
  419.         term.setTextColor(colors.white)
  420.         term.write("New palette name: ")
  421.         local newName = read()
  422.         if not newName or newName == "" then return end
  423.         local dir = shell.resolve("palettes/" .. newName)
  424.         fs.makeDir(dir)
  425.         fs.makeDir(dir .. "/sprites")
  426.         local file = fs.open(dir .. "/" .. newName .. ".plt", "w")
  427.         local data = {}
  428.         for i = 1, 16 do data[i] = palette[i] end
  429.         file.writeLine(textutils.serialize(data, {compact = true}))
  430.         file.close()
  431.         palette.name = newName
  432.     else
  433.         local file = fs.open(shell.resolve("palettes/" .. palette.name .. "/" .. palette.name .. ".plt"), "w")
  434.         local data = {}
  435.         for i = 1, 16 do data[i] = palette[i] end
  436.         file.writeLine(textutils.serialize(data, {compact = true}))
  437.         file.close()
  438.     end
  439.     -- Update snapshot
  440.     for i = 1, 16 do originalPalette[i] = palette[i] end
  441. end
  442.  
  443. local function saveSprite(name)
  444.     local path = shell.resolve("palettes/"..palette.name.."/sprites/"..name)
  445.     local file = fs.open(path,"w")
  446.     local spriteData = {}
  447.     for i = 1,#sprite do
  448.         spriteData[i] = sprite[i]
  449.     end
  450.     file.writeLine(textutils.serialize(spriteData,{compact = true}))
  451.     file.close()
  452. end
  453.  
  454. local function saveExit()
  455.     -- Layout: row1 top-border, row2 title, row3 sep,
  456.     --         rows 4-6 btn1, row7 sep,
  457.     --         rows 8-10 btn2, row11 sep,
  458.     --         rows 12-14 btn3, row15 closer
  459.     -- winW=15: 1 left border + 13 inner + 1 right border
  460.     -- Anchored so right edge = width (winX + winW - 1 = width)
  461.     local winW = 15
  462.     local innerW = winW - 2  -- 13 chars
  463.     local winH = 15
  464.     local winX = width - winW + 1
  465.     local winY = 1
  466.     local saveExitWindow = window.create(term.current(), winX, winY, winW, winH)
  467.  
  468.     local function drawWindow()
  469.         -- Full border frame
  470.         for i = 1, winH - 1 do
  471.             writeAt(":"..string.rep(" ", innerW)..":", 1, i, colors.white, colors.lightGray, saveExitWindow)
  472.         end
  473.         writeAt("\96"..string.rep("-", innerW).."'", 1, winH, colors.white, colors.lightGray, saveExitWindow)
  474.  
  475.         -- Title on row 2 (": SAVE/EXIT :" = 13 chars, fits innerW exactly)
  476.         writeAt(":  SAVE/EXIT  :", 1, 2, colors.white, colors.lightGray, saveExitWindow)
  477.  
  478.         -- Buttons: 3 rows tall, label centred on middle row, blank rows fill full innerW
  479.         local btnBlank = string.rep(" ", innerW)
  480.         local buttons = {
  481.             {topY = 4,  midY = 5,  botY = 6,  label = "    SAVE     "},
  482.             {topY = 8,  midY = 9,  botY = 10, label = "   SAVE AS   "},
  483.             {topY = 12, midY = 13, botY = 14, label = " SAVE & EXIT "},
  484.         }
  485.         for _, btn in ipairs(buttons) do
  486.             writeAt(btnBlank,  2, btn.topY, colors.white, colors.gray, saveExitWindow)
  487.             writeAt(btn.label, 2, btn.midY, colors.white, colors.gray, saveExitWindow)
  488.             writeAt(btnBlank,  2, btn.botY, colors.white, colors.gray, saveExitWindow)
  489.         end
  490.     end
  491.  
  492.     -- If palette was modified, ask what to do with it first
  493.     local function promptSavePalette()
  494.         -- Small popup: 2 buttons, anchored just left of the save/exit panel
  495.         -- ": PALETTE MODIFIED :" = 20 chars + 2 borders = 22 wide
  496.         local pW = 22
  497.         local pH = 12
  498.         local pX = winX - pW
  499.         local pY = 1
  500.         local pWin = window.create(term.current(), pX, pY, pW, pH)
  501.         local piW = pW - 2
  502.  
  503.         for i = 1, pH - 1 do
  504.             writeAt(":" .. string.rep(" ", piW) .. ":", 1, i, colors.white, colors.lightGray, pWin)
  505.         end
  506.         writeAt("\96" .. string.rep("-", piW) .. "'", 1, pH, colors.white, colors.lightGray, pWin)
  507.         writeAt(": PALETTE MODIFIED :", 1, 2, colors.white, colors.lightGray, pWin)
  508.  
  509.         -- btn1: SAVE MODIFIED  rows 4-6
  510.         -- btn2: CREATE NEW      rows 8-10
  511.         local blank = string.rep(" ", piW)
  512.         writeAt(blank,              2, 4,  colors.white, colors.gray, pWin)
  513.         writeAt("   SAVE MODIFIED    ", 2, 5,  colors.white, colors.gray, pWin)
  514.         writeAt(blank,              2, 6,  colors.white, colors.gray, pWin)
  515.         writeAt(blank,              2, 8,  colors.white, colors.gray, pWin)
  516.         writeAt("    CREATE NEW      ", 2, 9,  colors.white, colors.gray, pWin)
  517.         writeAt(blank,              2, 10, colors.white, colors.gray, pWin)
  518.  
  519.         while true do
  520.             local ev, mb, mx, my = os.pullEvent()
  521.             if ev == "mouse_click" and mb == 1 then
  522.                 local lx = mx - pX + 1
  523.                 local ly = my - pY + 1
  524.                 if lx >= 2 and lx <= pW - 1 then
  525.                     if ly >= 4 and ly <= 6 then
  526.                         savePalette(false)
  527.                         break
  528.                     elseif ly >= 8 and ly <= 10 then
  529.                         savePalette(true)
  530.                         break
  531.                     end
  532.                 end
  533.                 -- Click outside = skip saving palette
  534.                 break
  535.             end
  536.         end
  537.  
  538.         -- Erase popup
  539.         pWin.setBackgroundColor(colors.black)
  540.         pWin.clear()
  541.     end
  542.  
  543.     drawWindow()
  544.  
  545.     -- Hit detection: absolute screen coords → button index
  546.     local function getButtonIndex(ax, ay)
  547.         local lx = ax - winX      -- 0-indexed local x
  548.         local ly = ay - winY + 1  -- 1-indexed local y
  549.         if lx >= 1 and lx <= innerW then
  550.             if ly >= 4  and ly <= 6  then return 1 end
  551.             if ly >= 8  and ly <= 10 then return 2 end
  552.             if ly >= 12 and ly <= 14 then return 3 end
  553.         end
  554.         return nil
  555.     end
  556.  
  557.     while true do
  558.         local event, btn, x, y = os.pullEvent()
  559.  
  560.         if event == "mouse_click" and btn == 1 then
  561.             local idx = getButtonIndex(x, y)
  562.  
  563.             if idx == 1 then
  564.                 -- Save
  565.                 if paletteModified() then promptSavePalette() end
  566.                 saveSprite(sprite.name)
  567.                 break
  568.  
  569.             elseif idx == 2 then
  570.                 -- Save As
  571.                 if paletteModified() then promptSavePalette() end
  572.                 local inputRow = winH - 1
  573.                 writeAt(string.rep(" ", innerW), 2, inputRow, colors.white, colors.black, saveExitWindow)
  574.                 term.setCursorPos(winX + 1, winY + inputRow - 1)
  575.                 term.setBackgroundColor(colors.black)
  576.                 term.setTextColor(colors.white)
  577.                 local newName = read()
  578.                 if newName and newName ~= "" then
  579.                     sprite.name = newName
  580.                     saveSprite(newName)
  581.                 end
  582.                 break
  583.  
  584.             elseif idx == 3 then
  585.                 -- Save and exit
  586.                 if paletteModified() then promptSavePalette() end
  587.                 saveSprite(sprite.name)
  588.                 programEnded = true
  589.                 break
  590.  
  591.             else
  592.                 -- Clicked outside buttons — dismiss
  593.                 break
  594.             end
  595.         end
  596.     end
  597.  
  598.     timer = os.startTimer(0.05)
  599. end
  600.  
  601. local function brightnessModifier()
  602.     local function rgbToHsv(r, g, b)
  603.         local max = math.max(r, g, b)
  604.         local min = math.min(r, g, b)
  605.         local delta = max - min
  606.  
  607.         local h = 0
  608.         if delta ~= 0 then
  609.             if max == r then
  610.                 h = ((g - b) / delta) % 6
  611.             elseif max == g then
  612.                 h = ((b - r) / delta) + 2
  613.             else
  614.                 h = ((r - g) / delta) + 4
  615.             end
  616.             h = h / 6
  617.         end
  618.  
  619.         local s = (max == 0) and 0 or (delta / max)
  620.         local v = max
  621.  
  622.         return h, s, v
  623.     end
  624.  
  625.     local function hsvToRgb(h, s, v)
  626.         local i = math.floor(h * 6)
  627.         local f = (h * 6) - i
  628.         local p = v * (1 - s)
  629.         local q = v * (1 - f * s)
  630.         local t = v * (1 - (1 - f) * s)
  631.  
  632.         i = i % 6
  633.  
  634.         if i == 0 then return v, t, p
  635.         elseif i == 1 then return q, v, p
  636.         elseif i == 2 then return p, v, t
  637.         elseif i == 3 then return p, q, v
  638.         elseif i == 4 then return t, p, v
  639.         else return v, p, q
  640.         end
  641.     end
  642.  
  643.  
  644.     for i = 0,15 do
  645.         if brightness > 1 then
  646.             local r,g,b = colors.unpackRGB(palette[i+1])
  647.            
  648.             local nr = math.min(r + brightness - 1, 1)
  649.             local ng = math.min(g + brightness - 1, 1)
  650.             local nb = math.min(b + brightness - 1, 1)
  651.            
  652.            
  653.             term.setPaletteColor(2^i, colors.packRGB(nr,ng,nb))
  654.         else
  655.             local r,g,b = colors.unpackRGB(palette[i+1])
  656.  
  657.             local h,s,v = rgbToHsv(r,g,b)
  658.  
  659.             v = math.min(v * brightness, 1)
  660.  
  661.             local nr,ng,nb = hsvToRgb(h,s,v)
  662.  
  663.             term.setPaletteColor(2^i, colors.packRGB(nr,ng,nb))
  664.         end
  665.     end    
  666. end
  667.  
  668. local function keyCheck()
  669.     timer = os.startTimer(0.05)
  670.  
  671.     local changed = false
  672.  
  673.     if keysDown[keys.s] then
  674.         camera.y = camera.y - 1
  675.         changed = true
  676.     end
  677.     if keysDown[keys.d] then
  678.         camera.x = camera.x - 1.5
  679.         changed = true
  680.     end
  681.     if keysDown[keys.w] then
  682.         camera.y = camera.y + 1
  683.         changed = true
  684.     end
  685.     if keysDown[keys.a] then
  686.         camera.x = camera.x + 1.5
  687.         changed = true
  688.     end
  689.  
  690.     if keysDown[keys.up] then
  691.         brightness = brightness + 0.01
  692.         brightnessModifier()
  693.         changed = true
  694.     end
  695.  
  696.     if keysDown[keys.down] then
  697.         brightness = brightness - 0.01
  698.         brightnessModifier()
  699.         changed = true
  700.     end
  701.  
  702.     if changed then
  703.         drawScreen()
  704.     end
  705.  
  706.     if keysDown[keys.backspace] then
  707.         programEnded = true
  708.     end
  709.  
  710. end
  711.  
  712. local function mouseCheck(key,x,y)
  713.     local spriteX = math.floor(((x - 1 - camera.x) / 3) + 0.5)
  714.     local spriteY = math.floor(((y - 5 - camera.y) / 2) + 0.5)
  715.    
  716.     local function replacePixel(color)
  717.         for y = 1,2 do
  718.             spriteWindow.setCursorPos   (      
  719.                                 math.floor(spriteX * 3 + camera.x),
  720.                                 math.floor(spriteY * 2 + camera.y) + y
  721.                                 )
  722.                                
  723.             spriteWindow.setBackgroundColor(color)
  724.             spriteWindow.write("   ")
  725.         end
  726.     end
  727.    
  728.     local function checkSpriteTable()
  729.         for i = 1,#sprite do
  730.             if sprite[i][1] == spriteX and sprite[i][2] == spriteY then
  731.                 table.remove(sprite,i)
  732.                 break
  733.             end
  734.         end
  735.     end
  736.    
  737.     if y > 3 then
  738.         if key == 1 then
  739.             checkSpriteTable()
  740.             table.insert(sprite,{spriteX,spriteY,colourSelected})
  741.             replacePixel(colourSelected)
  742.         elseif key == 2 then
  743.             checkSpriteTable()
  744.             replacePixel(backgroundColour)
  745.         end
  746.     else
  747.         if x > width-43 and x < width-27 then
  748.             colourSelect()
  749.             drawScreen()
  750.         elseif x > width - 27 and x < width - 12 then
  751.             paletteEdit()
  752.             drawScreen()
  753.         elseif x >= width - 12 then
  754.             saveExit()
  755.             drawScreen()
  756.         end
  757.     end
  758. end
  759.  
  760. local function getUserInput()
  761.  
  762.     local event, key,x,y = os.pullEvent()
  763.    
  764.     if event == "key" then keysDown[key] = true
  765.     elseif event == "key_up" then keysDown[key] = nil
  766.     elseif event == "mouse_click" or event == "mouse_drag" then
  767.    
  768.         mouseCheck(key,x,y)
  769.        
  770.     elseif event == "timer" and key == timer then
  771.        
  772.         keyCheck()
  773.        
  774.     end
  775. end
  776.  
  777. loadPalette()
  778. loadSprite()
  779. drawScreen()
  780.  
  781. timer = os.startTimer(0.05)
  782.  
  783. while true do
  784.     getUserInput()
  785.     if programEnded == true then
  786.         break
  787.     end
  788. end
  789.  
  790. term.setBackgroundColor(colors.black)
  791. term.setTextColor(colors.white)
  792. term.clear()
  793. term.setCursorPos(1,1)
  794. print("Program Ended.")
Add Comment
Please, Sign In to add comment