Advertisement
Terminator_NL

Better paint

Feb 27th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.00 KB | None | 0 0
  1. transparentIcon = "-"
  2. args = {...}
  3. tX, tY = term.getSize()
  4.  
  5. function import(source,dest)
  6.  init()
  7.  if fs.exists(source) == false then
  8.   print("File "..source.." doesnt exists!")
  9.   error()
  10.  elseif not dest then
  11.   print("Provide an output file.")
  12.   error()
  13.  elseif fs.exists(dest) == true then
  14.   print("File "..dest.." already exists!")
  15.   error()
  16.  end
  17.  --if mode == "import" then
  18.   h = fs.open(source,"r")
  19.   lines = {}
  20.   count = 0
  21.   repeat
  22.    count = count + 1
  23.    lines[count] = h.readLine(count)  
  24.   until lines[count] == nil
  25.   h.close()
  26.   data = {}
  27.   if not painted then
  28.    painted = {}
  29.   end
  30.   for i=1,#lines do
  31.    data[i] = {}
  32.    string.gsub(lines[i],".",function(char) table.insert(data[i],char) end)
  33.   end
  34.   x=0
  35.   y=0
  36.   for i=1,#data do
  37.    y = y + 1
  38.    x = 0
  39.    for a=1,#data[i] do
  40.     x = x + 1
  41.     if not painted[x] then
  42.      painted[x] = {}
  43.     end
  44.     if not painted[x][y] then
  45.      painted[x][y] = {}
  46.     end
  47.     rawColor = data[i][x]
  48.     if rawColor == "0" then
  49.      colorLoaded = colors.white
  50.     elseif rawColor == "1" then
  51.      colorLoaded = colors.orange
  52.     elseif rawColor == "2" then
  53.      colorLoaded = colors.magenta
  54.     elseif rawColor == "3" then
  55.      colorLoaded = colors.lightBlue
  56.     elseif rawColor == "4" then
  57.      colorLoaded = colors.yellow
  58.     elseif rawColor == "5" then
  59.      colorLoaded = colors.lime
  60.     elseif rawColor == "6" then
  61.      colorLoaded = colors.pink
  62.     elseif rawColor == "7" then
  63.      colorLoaded = colors.gray
  64.     elseif rawColor == "8" then
  65.      colorLoaded = colors.lightGray
  66.     elseif rawColor == "9" then
  67.      colorLoaded = colors.cyan
  68.     elseif rawColor == "a" then
  69.      colorLoaded = colors.purple
  70.     elseif rawColor == "b" then
  71.      colorLoaded = colors.blue
  72.     elseif rawColor == "c" then
  73.      colorLaoded = colors.brown
  74.     elseif rawColor == "d" then
  75.      colorLoaded = colors.green
  76.     elseif rawColor == "e" then
  77.      colorLoaded = colors.red
  78.     elseif rawColor == "f" then
  79.      colorLoaded = colors.black
  80.     elseif rawColor == " " then
  81.      -- Do nothing, allow it
  82.     else
  83.      error("File: \""..source.."\" has an incorrect format!")
  84.     end
  85.     if rawColor ~= " " then
  86.      painted[x][y] = {colorLoaded," ",colorLoaded}  
  87.     else
  88.      painted[x][y] = nil
  89.     end
  90.    end
  91.   end
  92.  --end
  93.  args[1] = dest
  94.  save()
  95. end
  96.  
  97.  
  98. function drawImage(file,xSet,ySet,redirection)
  99.  init()
  100.  lastImage = file
  101.  lastX = xSet
  102.  lastY = ySet
  103.  lastRedirection = redirection
  104.  if redirection then
  105.   current = term.current()
  106.   term.redirect(redirection)
  107.  end
  108.  drawData(xSet,ySet,file)
  109.  if redirection then
  110.   term.redirect(current)
  111.  end
  112. end
  113.  
  114. function overWrite(textSet,xSet,ySet,colorSet)
  115.  init()
  116.  exists = true
  117.  if not lastImage then
  118.   error("Use drawImage first!")
  119.  end
  120.  if not writeBuffer then
  121.   writeBuffer = {}
  122.  end
  123.  if not writeBuffer[lastImage] then
  124.   writeBuffer[lastImage] = {}
  125.  end
  126.  plusPos = 0
  127.  for char in string.gmatch(textSet,".") do
  128.   if not writeBuffer[lastImage][xSet+plusPos] then
  129.    writeBuffer[lastImage][xSet+plusPos] = {}
  130.   end
  131.   if not writeBuffer[lastImage][xSet+plusPos][ySet] then
  132.    writeBuffer[lastImage][xSet+plusPos][ySet] = {colors.black," ",colors.white}
  133.   end
  134.   writeBuffer[lastImage][xSet+plusPos][ySet][2] = char
  135.   writeBuffer[lastImage][xSet+plusPos][ySet][3] = colorSet
  136.   plusPos = plusPos + 1
  137.  end
  138.  drawImage(lastImage,lastX,lastY,lastRedirection)
  139. end
  140.  
  141. function init()
  142. function eventHandler()
  143.  while true do
  144.   event = {os.pullEvent()}
  145.   if event[1] == "key" then
  146.    if event[2] == keys.leftCtrl or event[2] == 157 then
  147.     menuStatus = not menuStatus
  148.     writeMenuBar(menuStatus)
  149.    end
  150.    if menuStatus == true then
  151.     if event[2] == keys.right or event[2] == keys.left then
  152.      if menuItemSelected == 1 then
  153.       menuItemSelected = 2
  154.      else
  155.       menuItemSelected = 1
  156.      end
  157.      writeMenuBar(menuStatus)
  158.     elseif event[2] == keys.enter then
  159.      if menuItemSelected == 1 then
  160.       save()
  161.       writeMenuBar(false)
  162.      else
  163.       term.setTextColor(colors.yellow)
  164.       term.setBackgroundColor(colors.black)
  165.       term.clear()
  166.       term.setCursorPos(1,1)
  167.       error()
  168.      end
  169.     end
  170.    else  
  171.     if event[2] == keys.right then
  172.      drawData(offSetX-1,offSetY)
  173.      drawMenu()
  174.      writeMenuBar(menuStatus)
  175.     elseif event[2] == keys.left then
  176.      drawData(offSetX+1,offSetY)
  177.      drawMenu()
  178.      writeMenuBar(menuStatus)
  179.     elseif event[2] == keys.up then
  180.      drawData(offSetX,offSetY+1)
  181.      drawMenu()
  182.      writeMenuBar(menuStatus)
  183.     elseif event[2] == keys.down then
  184.      drawData(offSetX,offSetY-1)
  185.      drawMenu()
  186.      writeMenuBar(menuStatus)
  187.     end
  188.    end
  189.   end
  190.   if event[1] == "mouse_click" or event[1] == "mouse_drag" then
  191.    if event[3] > 2 and event[4] ~= tY then
  192.     insertItem(event[3],event[4],event[2])
  193.    elseif event[4] < 18 and event[4] > 1 then
  194.     if event[3] == 1 then
  195.      bgSelected = 2^(event[4]-2)
  196.     elseif event[3] == 2 then
  197.      tSelected = 2^(event[4]-2)
  198.     end
  199.     drawMenu()
  200.    elseif event[4] == tY - 1 and event[3] == 1 then
  201.     setLetter()
  202.     drawData(offSetX,offSetY)
  203.     drawMenu()
  204.    elseif event[3] == tX and event[4] == tY and menuStatus == false then
  205.     writeHelp()
  206.    end
  207.   end
  208.   if event[1] == "char" then
  209.    textSelected = string.sub(event[2],1,1)
  210.    drawMenu()
  211.   end
  212.   --drawData(offSetX,offSetY)
  213.  end
  214. end
  215.  
  216. function writeHelp()
  217.  term.setBackgroundColor(colors.black)
  218.  term.setTextColor(colors.green)
  219.  term.clear()
  220.  term.setCursorPos(1,1)
  221.  term.write("Help:")
  222.  term.setTextColor(colors.white)
  223.  term.setCursorPos(1,3)
  224.  print("Usage:")
  225.  term.setTextColor(colors.lightGray)
  226.  print("  Select color: Click on the color on the left")
  227.  print("  Change draw char: Press a key on the keyboard")
  228.  print("  Change transparent icon: Click on the icon's char in the menu")
  229.  print("  Change text color: Click on a color in the menu on the right side")
  230.  print("  Change background color: Click on a color in the menu on the left side")
  231.  term.setTextColor(colors.white)
  232.  print()
  233.  print("Controls:")
  234.  term.setTextColor(colors.lightGray)
  235.  print("  Arrow keys to pan")
  236.  print("  Left mouse button to select and draw")
  237.  print("  Right mouse button to delete")
  238.  print("  Ctrl to open menu")
  239.  print()
  240.  term.setTextColor(colors.white)
  241.  term.write("Click a mouse button to exit.")
  242.  term.setTextColor(colors.orange)
  243.  term.setCursorPos(tX-9,1)
  244.  term.write("API help >")
  245.  event = {os.pullEvent("mouse_click")}
  246.  if event[3] > tX-10 and event[4] == 1 then
  247.   drawAPIhelp()
  248.  end
  249.  drawData(offSetX,offSetY)
  250.  drawMenu()
  251.  writeMenuBar(menuStatus)
  252. end
  253.  
  254. function drawAPIhelp()
  255.  term.clear()
  256.  term.setCursorPos(1,1)
  257.  term.setTextColor(colors.orange)
  258.  print("API help menu:")
  259.  term.setTextColor(colors.white)
  260.  print("Drawing an image: ")
  261.  term.setTextColor(colors.lightGray)
  262.  print(shell.getRunningProgram(),".drawImage(<file name>,<x pos>,<y pos>,[redirection object])")
  263.  print()
  264.  term.setTextColor(colors.white)
  265.  print("Overlaying text on the last image:")
  266.  term.setTextColor(colors.lightGray)
  267.  print(shell.getRunningProgram(),".overWrite(<Text>,<x pos>,<y pos>,<text color>)")
  268.  print()
  269.  term.setTextColor(colors.white)
  270.  print("Convert a paintutils file to betterpaint file:")
  271.  term.setTextColor(colors.lightGray)
  272.  print(shell.getRunningProgram(),".import(<source>,<destination>)")
  273.  term.setTextColor(colors.red)
  274.  print()
  275.  print("Overwriting text will only work AFTER drawing an image!")
  276.  term.setTextColor(colors.white)
  277.  print()
  278.  print("Example:")
  279.  term.setTextColor(colors.lightGray)
  280.  print("os.loadAPI(\"",shell.getRunningProgram(),"\")")
  281.  print(shell.getRunningProgram(),".drawImage(\"myPicture\",1,1)")
  282.  term.write((shell.getRunningProgram()..".overWrite(\"Hello!\",2,3,colors.orange)"))
  283.  os.pullEvent("mouse_click")
  284. end
  285.  
  286. function setLetter()
  287.  term.setBackgroundColor(colors.red)
  288.  term.setTextColor(colors.black)
  289.  for i=1,4 do
  290.   term.setCursorPos(tX/2-11,(tY/2-4)+i)
  291.   term.write("                     ")
  292.  end
  293.  term.setCursorPos(tX/2-10,tY/2-2)
  294.  term.write("Change transparancy")
  295.  term.setCursorPos(tX/2-10,tY/2-1)
  296.  term.write("character to: (key)")
  297.  event = {os.pullEvent("char")}
  298.  transparentIcon = event[2]
  299. end
  300.  
  301. function insertItem(xPos,yPos,modeSet)
  302.  if saved == true then
  303.   saved = false
  304.   writeMenuBar(false)
  305.  end
  306. --bgSelected
  307. --tSelected
  308. --textSelected
  309.  if not painted then
  310.   painted = {}
  311.  end
  312.  if not painted[xPos-offSetX] then
  313.   painted[xPos-offSetX] = {}
  314.  end
  315.  if modeSet == 1 then
  316.    if not textSelected then
  317.     textSelected = " "
  318.    end
  319.    TMPtextSelected = textSelected
  320.    painted[xPos-offSetX][yPos-offSetY] = {bgSelected,textSelected,tSelected}
  321.    term.setBackgroundColor(bgSelected)
  322.    term.setTextColor(tSelected)
  323.   else
  324.    TMPtextSelected = transparentIcon
  325.    term.setBackgroundColor(colors.black)
  326.    term.setTextColor(colors.gray)
  327.    painted[xPos-offSetX][yPos-offSetY] = nil
  328.  end
  329.  term.setCursorPos(xPos,yPos)
  330.  term.write(TMPtextSelected)
  331. end
  332.  
  333. --if #args ~= 1 then
  334. -- print("Usage: "..shell.getRunningProgram().." <path>")
  335. -- return
  336. --end
  337.  
  338. if args[1] and fs.exists(args[1]) == true then
  339.  buff = fs.open(args[1],"r")
  340.  previousData = buff.readAll()
  341.  buff.close()
  342.  processed = string.sub(previousData,43)
  343.  painted = textutils.unserialize(processed)
  344. else
  345.  painted = {}
  346. end
  347.  
  348. function save()
  349.  file = fs.open(args[1],"w")
  350.  file.write("error('This is an image, not a program!')\n"..textutils.serialize(painted))
  351.  file.close()
  352.  saved = true
  353. end
  354.  
  355. function drawData(xStart, yStart, file)
  356.  offSetX = xStart
  357.  offSetY = yStart
  358.  if not file then
  359.   term.setBackgroundColor(colors.black)
  360.   term.setTextColor(colors.gray)
  361.   transparentLine = ""
  362.   for x=1,tX-2 do
  363.    transparentLine = transparentIcon..transparentLine
  364.   end
  365.   for y=1,tY-1 do
  366.    term.setCursorPos(3,y)
  367.    term.write(transparentLine)
  368.   end
  369.  else
  370.   if fs.exists(file) == false then
  371.    error("File given doesnt exists! file name: "..file)
  372.   else
  373.    local fileD = fs.open(file,"r")
  374.    raw = fileD.readAll()
  375.    --ignoreL = string.len(fileD.readLine(1))
  376.    processed = string.sub(raw,43)
  377.    --term.redirect(term.native())
  378.   -- textutils.pagedPrint(processed)
  379.    painted = textutils.unserialize(processed)
  380.    fileD.close()
  381.   end
  382.  end
  383.  if not painted then
  384.   painted = {}
  385.  end
  386.  paintedF = painted
  387.  count = 0
  388.  repeat ---------
  389.  count = count + 1
  390.  for xPos,v in pairs(paintedF) do
  391.   for yPos in pairs (paintedF[xPos]) do
  392.     overWrite = true
  393.     if not lastImage or not writeBuffer or not writeBuffer[lastImage] then
  394.      overWrite = false
  395.     end
  396.     if overWrite == true then
  397.      if not writeBuffer[lastImage][xPos] then
  398.       overWrite = false
  399.      end
  400.     end
  401.     if overWrite == true then
  402.      if not writeBuffer[lastImage][xPos][yPos] then
  403.       overWrite = false
  404.      end
  405.     end
  406.     if overWrite == false then
  407.      bgColor = paintedF[xPos][yPos][1]
  408.      text = paintedF[xPos][yPos][2]
  409.      tColor = paintedF[xPos][yPos][3]
  410.     else
  411.      if painted and painted[xPos] and painted[xPos][yPos] and painted[xPos][yPos][1] then
  412.       bgColor = painted[xPos][yPos][1]
  413.       else
  414.       bgColor = colors.black
  415.      end
  416.      --if not bgColor then
  417.     --  bgColor = colors.black
  418.      --end
  419.      text = writeBuffer[lastImage][xPos][yPos][2]
  420.      tColor = writeBuffer[lastImage][xPos][yPos][3]
  421.     end
  422.     if not tColor then
  423.      tColor = colors.white
  424.     end
  425.     if not text then
  426.      text = " "
  427.     end
  428.     term.setCursorPos(xPos+xStart,yPos+yStart)
  429.     term.setBackgroundColor(bgColor)
  430.     term.setTextColor(tColor)
  431.     term.write(text)
  432.    end
  433.   end
  434.   if count == 1 and writeBuffer and lastImage then
  435.    paintedF = writeBuffer[lastImage]
  436.   elseif count == 1 and not lastImage or not writeBuffer then
  437.    count = 2
  438.   end
  439.  until count == 2
  440.  term.setCursorPos(1,tY)
  441. end
  442.  
  443. function drawMenu()
  444.  term.setCursorPos(1,1)
  445.  term.setTextColor(colors.white)
  446.  if not bgSelected then
  447.   bgSelected = colors.black
  448.  elseif bgSelected == colors.white then
  449.   term.setTextColor(colors.black)
  450.  end
  451.  if not tSelected then
  452.   tSelected = colors.white
  453.  elseif tSelected == colors.white then
  454.   term.setTextColor(colors.black)
  455.  end
  456.  if not textSelected then
  457.   textSelected = " "
  458.  end
  459.  term.setBackgroundColor(bgSelected)
  460.  term.write("B")
  461.  term.setBackgroundColor(tSelected)
  462.  term.write("T")
  463.  for i=1,16 do
  464.   i=i-1
  465.   term.setCursorPos(1,i+2)
  466.   term.setBackgroundColor(2^i)
  467.   term.write("  ")
  468.  end
  469.  term.setCursorPos(1,18)
  470.  term.setBackgroundColor(colors.black)
  471.  if not textSelected then
  472.   textSelected = " "
  473.  elseif string.len(textSelected) > 1 then
  474.   textSelected = string.sub(textSelected,1,1)
  475.  end
  476.  term.setTextColor(colors.gray)
  477.  term.setBackgroundColor(colors.black)
  478.  term.write(transparentIcon)
  479.  term.setTextColor(tSelected)
  480.  term.setBackgroundColor(bgSelected)
  481.  term.write(textSelected)
  482. end
  483.  
  484. function writeMenuBar(booly)
  485.  menuStatus = booly
  486.  term.setBackgroundColor(colors.black)
  487.  if booly == true then
  488.   term.setCursorPos(1,tY)
  489.   term.clearLine()
  490.   if not menuItemSelected then
  491.    menuItemSelected = 1
  492.   end
  493.   term.setTextColor(colors.white)
  494.   term.write(" Save  Exit ")
  495.   term.setCursorPos(6*menuItemSelected-5,tY)
  496.   term.setTextColor(colors.yellow)
  497.   term.write("[")
  498.   term.setCursorPos(6*menuItemSelected,tY)
  499.   term.write("]")
  500.  elseif booly == false then
  501.   term.setCursorPos(1,tY)
  502.   term.setTextColor(colors.yellow)
  503.   term.clearLine()
  504.   if saved == true then
  505.    term.write("Saved to "..args[1])
  506.   else
  507.    term.write("Press Ctrl to access menu")
  508.   end
  509.   term.setCursorPos(tX,tY)
  510.   term.setTextColor(colors.lightGray)
  511.   term.write("?")
  512.  end
  513. end
  514. end
  515.  
  516. if args[1] == "import" then -- or args[1] == "export" then
  517.  init()
  518.  mode = args[1]
  519.  source = args[2]
  520.  destination = args[3]
  521.  if not args[2] then
  522.   print("Usage: "..shell.getRunningProgram().." "..mode.." <source> <destination>")
  523.   error()
  524.  end
  525.  import(source,destination)
  526.  if mode == "import" then
  527.   print("File imported.")
  528.   error()
  529.  end
  530. elseif #args > 0 then
  531.  init()
  532.  menuStatus = false
  533.  saved = false
  534.  writeMenuBar(menuStatus)
  535.  menuItemSelected = 1
  536.  drawData(3,0)
  537.  drawMenu()
  538.  eventHandler()
  539. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement