Advertisement
pepeknamornik

malovani plochy 1.0

Sep 15th, 2014
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.76 KB | None | 0 0
  1. local w,h = term.getSize()
  2.  
  3. -- The selected colours on the left and right mouse button, and the colour of the canvas
  4. local leftColour, rightColour = colours.white, nil
  5. local canvasColour = colours.black
  6.  
  7. -- The values stored in the canvas
  8. local canvas = {}
  9.  
  10. -- The menu options
  11. local mChoices = { "Save","Exit" }
  12.  
  13. -- The message displayed in the footer bar
  14. local fMessage = "Press Ctrl to access menu"
  15.  
  16. -------------------------
  17. -- Initialisation --
  18. -------------------------
  19.  
  20. -- Determine if we can even run this
  21. if not term.isColour() then
  22.     print("Requires an Advanced Computer")
  23.     return
  24. end
  25.  
  26. -- Determines if the file exists, and can be edited on this computer
  27. local tArgs = {...}
  28. if #tArgs == 0 then
  29.     print("Usage: paint <path>")
  30.     return
  31. end
  32. local sPath = shell.resolve(tArgs[1])
  33. local bReadOnly = fs.isReadOnly(sPath)
  34. if fs.exists(sPath) and fs.isDir(sPath) then
  35.     print("Cannot edit a directory.")
  36.     return
  37. end
  38.  
  39. ---------------
  40. -- Functions --
  41. ---------------
  42.  
  43. local function getCanvasPixel( x, y )
  44.     if canvas[y] then
  45.         return canvas[y][x]
  46.     end
  47.     return nil
  48. end
  49.  
  50. --[[
  51.     Converts a colour value to a text character
  52.     params: colour = the number to convert to a hex value
  53.     returns: a string representing the chosen colour
  54. ]]
  55. local function getCharOf( colour )
  56.     -- Incorrect values always convert to nil
  57.     if type(colour) == "number" then
  58.         local value = math.floor( math.log(colour) / math.log(2) ) + 1
  59.         if value >= 1 and value <= 16 then
  60.             return string.sub( "0123456789abcdef", value, value )
  61.         end
  62.     end
  63.     return " "
  64. end
  65.  
  66. --[[
  67.     Converts a text character to colour value
  68.     params: char = the char (from string.byte) to convert to number
  69.     returns: the colour number of the hex value
  70. ]]
  71. local tColourLookup = {}
  72. for n=1,16 do
  73.     tColourLookup[ string.byte( "0123456789abcdef",n,n ) ] = 2^(n-1)
  74. end
  75. local function getColourOf( char )
  76.     -- Values not in the hex table are transparent (canvas coloured)
  77.     return tColourLookup[char]
  78. end
  79.  
  80. --[[
  81.     Loads the file into the canvas
  82.     params: path = the path of the file to open
  83.     returns: nil
  84. ]]
  85. local function load(path)
  86.     -- Load the file
  87.     if fs.exists(path) then
  88.         local file = fs.open(sPath, "r")
  89.         local sLine = file.readLine()
  90.         while sLine do
  91.             local line = {}
  92.             for x=1,w-2 do
  93.                 line[x] = getColourOf( string.byte(sLine,x,x) )
  94.             end
  95.             table.insert( canvas, line )
  96.             sLine = file.readLine()
  97.         end
  98.         file.close()
  99.     end
  100. end
  101.  
  102. --[[  
  103.     Saves the current canvas to file  
  104.     params: path = the path of the file to save
  105.     returns: true if save was successful, false otherwise
  106. ]]
  107. local function save(path)
  108.     -- Open file
  109.     local sDir = string.sub(sPath, 1, #sPath - #fs.getName(sPath))
  110.     if not fs.exists(sDir) then
  111.         fs.makeDir(sDir)
  112.     end
  113.  
  114.     local file = fs.open( path, "w" )
  115.     if not file then
  116.         return false
  117.     end
  118.  
  119.     -- Encode (and trim)
  120.     local tLines = {}
  121.     local nLastLine = 0
  122.     for y=1,h-1 do
  123.         local sLine = ""
  124.         local nLastChar = 0
  125.         for x=1,w-2 do
  126.             local c = getCharOf( getCanvasPixel( x, y ) )
  127.             sLine = sLine .. c
  128.             if c ~= " " then
  129.                 nLastChar = x
  130.             end
  131.         end
  132.         sLine = string.sub( sLine, 1, nLastChar )
  133.         tLines[y] = sLine
  134.         if string.len( sLine ) > 0 then
  135.             nLastLine = y
  136.         end
  137.     end
  138.  
  139.     -- Save out
  140.     for n=1,nLastLine do
  141.         file.writeLine( tLines[ n ] )
  142.     end
  143.     file.close()
  144.     return true
  145. end
  146.  
  147. --[[  
  148.     Draws colour picker sidebar, the pallette and the footer
  149.     returns: nil
  150. ]]
  151. local function drawInterface()
  152.     -- Footer
  153.     term.setCursorPos(1, h)
  154.     term.setBackgroundColour(colours.black)
  155.     term.setTextColour(colours.yellow)
  156.     term.clearLine()
  157.     term.write(fMessage)
  158.    
  159.     -- Colour Picker
  160.     for i=1,16 do
  161.         term.setCursorPos(w-1, i)
  162.         term.setBackgroundColour( 2^(i-1) )
  163.         term.write("  ")
  164.     end
  165.  
  166.     term.setCursorPos(w-1, 17)
  167.     term.setBackgroundColour( canvasColour )
  168.     term.setTextColour( colours.grey )
  169.     term.write("XX")
  170.            
  171.     -- Left and Right Selected Colours
  172.     for i=18,18 do
  173.         term.setCursorPos(w-1, i)
  174.         if leftColour ~= nil then
  175.             term.setBackgroundColour( leftColour )
  176.             term.write(" ")
  177.         else
  178.             term.setBackgroundColour( canvasColour )
  179.             term.setTextColour( colours.grey )
  180.             term.write("X")
  181.         end
  182.         if rightColour ~= nil then
  183.             term.setBackgroundColour( rightColour )
  184.             term.write(" ")
  185.         else
  186.             term.setBackgroundColour( canvasColour )
  187.             term.setTextColour( colours.grey )
  188.             term.write("X")
  189.         end
  190.     end
  191.  
  192.     -- Padding
  193.     term.setBackgroundColour( canvasColour )
  194.     for i=20,h-1 do
  195.         term.setCursorPos(w-1, i)
  196.         term.write("  ")
  197.     end
  198. end
  199.  
  200. --[[  
  201.     Converts a single pixel of a single line of the canvas and draws it
  202.     returns: nil
  203. ]]
  204. local function drawCanvasPixel( x, y )
  205.     local pixel = getCanvasPixel( x, y )
  206.     if pixel then
  207.         term.setBackgroundColour( pixel or canvasColour )
  208.         term.setCursorPos(x, y)
  209.         term.write(" ")
  210.     else
  211.         term.setBackgroundColour( canvasColour )
  212.         term.setTextColour( colours.grey )
  213.         term.setCursorPos(x, y)
  214.         term.write("-")
  215.     end
  216. end
  217.  
  218. --[[  
  219.     Converts each colour in a single line of the canvas and draws it
  220.     returns: nil
  221. ]]
  222. local function drawCanvasLine( y )
  223.     for x = 1, w-2 do
  224.         drawCanvasPixel( x, y )
  225.     end
  226. end
  227.  
  228. --[[  
  229.     Converts each colour in the canvas and draws it
  230.     returns: nil
  231. ]]
  232. local function drawCanvas()
  233.     for y = 1, h-1 do
  234.         drawCanvasLine( y )
  235.     end
  236. end
  237.  
  238. --[[
  239.     Draws menu options and handles input from within the menu.
  240.     returns: true if the program is to be exited; false otherwise
  241. ]]
  242. local function accessMenu()
  243.     -- Selected menu option
  244.     local selection = 1
  245.    
  246.     term.setBackgroundColour(colours.black)
  247.     while true do
  248.         -- Draw the menu
  249.         term.setCursorPos(1,h)
  250.         term.clearLine()
  251.         term.setTextColour(colours.white)
  252.         for k,v in pairs(mChoices) do
  253.             if selection==k then
  254.                 term.setTextColour(colours.yellow)
  255.                 local ox,_ = term.getCursorPos()
  256.                 term.write("["..string.rep(" ",#v).."]")
  257.                 term.setCursorPos(ox+1,h)
  258.                 term.setTextColour(colours.white)
  259.                 term.write(v)
  260.                 term.setCursorPos(term.getCursorPos()+1,h)
  261.             else
  262.                 term.write(" "..v.." ")
  263.             end
  264.         end
  265.        
  266.         -- Handle input in the menu
  267.         local id,key = os.pullEvent("key")
  268.         if id == "key" then
  269.             -- S and E are shortcuts
  270.             if key == keys.s then
  271.                 selection = 1
  272.                 key = keys.enter
  273.             elseif key == keys.e then
  274.                 selection = 2
  275.                 key = keys.enter
  276.             end
  277.        
  278.             if key == keys.right then
  279.                 -- Move right
  280.                 selection = selection + 1
  281.                 if selection > #mChoices then
  282.                     selection = 1
  283.                 end
  284.                
  285.             elseif key == keys.left and selection > 1 then
  286.                 -- Move left
  287.                 selection = selection - 1
  288.                 if selection < 1 then
  289.                     selection = #mChoices
  290.                 end
  291.                
  292.             elseif key == keys.enter then
  293.                 -- Select an option
  294.                 if mChoices[selection]=="Save" then
  295.                     if bReadOnly then
  296.                         fMessage = "Access Denied"
  297.                         return false
  298.                     end
  299.                     local success = save(sPath)
  300.                     if success then
  301.                         fMessage = "Saved to "..sPath
  302.                     else
  303.                         fMessage = "Error saving to "..sPath
  304.                     end
  305.                     return false
  306.                 elseif mChoices[selection]=="Exit" then
  307.                     return true
  308.                 end
  309.             elseif key == keys.leftCtrl or keys == keys.rightCtrl then
  310.                 -- Cancel the menu
  311.                 return false
  312.             end
  313.         end
  314.     end
  315. end
  316.  
  317. --[[  
  318.     Runs the main thread of execution. Draws the canvas and interface, and handles
  319.     mouse and key events.
  320.     returns: nil
  321. ]]
  322. local function handleEvents()
  323.     local programActive = true
  324.     while programActive do
  325.         local id,p1,p2,p3 = os.pullEvent()
  326.         if id=="mouse_click" or id=="mouse_drag" then
  327.             if p2 >= w-1 and p3 >= 1 and p3 <= 17 then
  328.                 if id ~= "mouse_drag" then
  329.                     -- Selecting an items in the colour picker
  330.                     if p3 <= 16 then
  331.                         if p1==1 then
  332.                             leftColour = 2^(p3-1)
  333.                         else
  334.                             rightColour = 2^(p3-1)
  335.                         end
  336.                     else
  337.                         if p1==1 then
  338.                             leftColour = nil
  339.                         else
  340.                             rightColour = nil
  341.                         end
  342.                     end
  343.                     --drawCanvas()
  344.                     drawInterface()
  345.                 end
  346.             elseif p2 < w-1 and p3 <= h-1 then
  347.                 -- Clicking on the canvas
  348.                 local paintColour = nil
  349.                 if p1==1 then
  350.                     paintColour = leftColour
  351.                 elseif p1==2 then
  352.                     paintColour = rightColour
  353.                 end
  354.                 if not canvas[p3] then
  355.                     canvas[p3] = {}
  356.                 end
  357.                 canvas[p3][p2] = paintColour
  358.  
  359.                 drawCanvasPixel( p2, p3 )
  360.             end
  361.         elseif id=="key" then
  362.             if p1==keys.leftCtrl or p1==keys.rightCtrl then
  363.                 programActive = not accessMenu()
  364.                 drawInterface()
  365.             end
  366.         elseif id=="term_resize" then
  367.             w,h = term.getSize()
  368.             drawCanvas()
  369.             drawInterface()
  370.         end
  371.     end
  372. end
  373.  
  374. -- Init
  375. load(sPath)
  376. drawCanvas()
  377. drawInterface()
  378.  
  379. -- Main loop
  380. handleEvents()
  381.  
  382. -- Shutdown
  383. term.setBackgroundColour(colours.black)
  384. term.setTextColour(colours.white)
  385. term.clear()
  386. term.setCursorPos(1,1)
  387. plocha ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement