Advertisement
pedrosgali

PedrOS drawAPI

Nov 21st, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.31 KB | None | 0 0
  1. --GLOBAL VARIABLES--
  2.  
  3. x, y = term.getSize()
  4.  
  5. --Colours
  6. white = 1
  7. orange = 2
  8. magenta = 4
  9. lightBlue = 8
  10. yellow = 16
  11. lime = 32
  12. pink = 64
  13. grey = 128
  14. lightGrey = 256
  15. cyan = 512
  16. purple = 1024
  17. blue = 2048
  18. brown = 4096
  19. green = 8192
  20. red = 16384
  21. black = 32768
  22.  
  23. --TEXT FUNCTIONS--
  24.  
  25. function writeText(tX, tY, bCol, tCol, text)
  26.     term.setCursorPos(tX, tY)
  27.     term.setBackgroundColor(bCol)
  28.     term.setTextColor(tCol)
  29.     write(text)
  30. end
  31.  
  32. function centerText(stX, maX, tY, bCol, tCol, text)
  33.     boundry = maX - stX
  34.     width = (boundry / 2) - (string.len(text) / 2)
  35.     writeText(width + stX, tY, bCol, tCol, text)
  36. end
  37.  
  38. --DRAW FUNCTIONS--
  39.  
  40. function box(stX, stY, maX, maY, color)
  41.     term.setBackgroundColor(color)
  42.     for i = stY, maY do
  43.         term.setCursorPos(stX, i)
  44.         for j = stX, maX do
  45.             write(" ")
  46.         end
  47.     end
  48. end
  49.  
  50. function button(xPos, yPos, sizeX, sizeY, bCol, tCol, text) --Needs writing
  51.     box(xPos, xPos + sizeX, yPos, yPos + sizeY, colour)
  52.     midLine = yPos + (sizeY / 2)
  53.     centerText(xPos, xPos + sizeX, midLine, bCol, tCol, text)
  54. end
  55.  
  56. function window(sizeX, sizeY, bCol, hCol, tCol, header)
  57.     --fakeScreen()
  58.     height = (y / 2) - (sizeY / 2)
  59.     width = (x / 2) - (sizeX / 2)
  60.     box(width, height, (width + sizeX), (height + sizeY), bCol)
  61.     box(width, height, (width + sizeX), height, hCol)
  62.     writeText((width + sizeX), height, bCol, tCol, "X")
  63.     writeText(width + 1, height, hCol, tCol, header)
  64. end
  65.  
  66. function dataWindow(bCol, hCol, tCol, header)
  67.     fakeScreen()
  68.     box(1, 1, x, y-1, bCol)
  69.     box(1, 1, x, 1, hCol)
  70.     writeText(2, 1, hCol, tCol, header)
  71.     writeText(x, 1, bCol, tCol, "X")
  72.     writeText(2, 2, bCol, tCol, "File Edit Help")
  73.     box(2, 3, x-1, y-2, 1)
  74.     term.setCursorPos(3, 3)
  75. end
  76.  
  77. function serialize(data, header, bCol, hCol, tCol)
  78.     table = textutils.unserialize(data)
  79.     dataWindow(bCol, hCol, tCol, header)
  80.     term.setBackgroundColor(white)
  81.     term.setTextColor(black)
  82.     yPos = 3
  83.     for i = 1, #table do
  84.         print(table[i][3])
  85.         yPos = yPos + 1
  86.         term.setCursorPos(3, yPos)
  87.     end
  88. end
  89.  
  90. function optionBox(header, mainText, opt1, opt2)
  91.     fakeScreen()
  92.     boxHeight = 5
  93.     centerY = 12
  94.     if opt1 == nil then
  95.         opt1 = "Ok"
  96.         opt2 = false
  97.     elseif opt2 == nil then
  98.         opt2 = false
  99.     end
  100.     width = string.len(mainText) + 2
  101.     if not opt2 then
  102.         optBoxes = string.len(opt1)
  103.     else
  104.         optBoxes = string.len(opt1) + string.len(opt2)
  105.     end
  106.     if optBoxes > width then
  107.         width = optBoxes
  108.     end
  109.     window(width, boxHeight, lightGrey, magenta, black, header)
  110.     centerText(1, x, (centerY - 2), lightGrey, black, mainText)
  111.     if not opt2 then
  112.         optWidth = math.floor((x / 2) - (string.len(opt1) / 2))
  113.         writeText(optWidth, centerY, magenta, black, opt1)
  114.     else
  115.         optWidth = (string.len(opt1) + string.len(opt2) + 3) / 2
  116.         writeText(width, centerY, magenta, black, opt1)
  117.         newX = (width + string.len(opt1)) + 1
  118.         writeText(newX, centerY, magenta, black, opt2)
  119.     end
  120.     opt1Size = string.len(opt1)
  121.     if opt2 then
  122.         opt2Size = string.len(opt2)
  123.     end
  124.     clickTrue = false
  125.     repeat
  126.         event, button, cX, cY = os.pullEvent()
  127.         if event == "mouse_click" then
  128.             --writeText(1, 1, black, white, optWidth.." "..opt1Size)
  129.             if not opt2 then
  130.                 if cX >= optWidth and cX <= (optWidth + opt1Size) then
  131.                     if cY == centerY and button == 1 then
  132.                         click = "button1"
  133.                         clickTrue = true
  134.                     end
  135.                 end
  136.             else
  137.                 if cX >= width and cX <= (width + opt1Size) then
  138.                     if cY == centerY and button == 1 then
  139.                         click = "button1"
  140.                         clickTrue = true
  141.                     end
  142.                 end
  143.                 if cX >= newX and cX <= (newX + opt2Size) then
  144.                     if cY == centerY and button == 1 then
  145.                         click = "button2"
  146.                         clickTrue = true
  147.                     end
  148.                 end
  149.             end
  150.         end
  151.     until clickTrue == true
  152.     return(click)
  153. end
  154.  
  155. function inputBox(header)
  156.     --fakeScreen()
  157.     window(40, 4, lightGrey, magenta, black, header)
  158.     box(((x/2)-19), ((y/2)+1), ((x/2)+19), ((y/2)+1), white)
  159.     term.setCursorPos(((x/2)-19), ((y/2)+1))
  160. end
  161.  
  162. --GRAPH FUNCTIONS--
  163.  
  164. --Draws a verticle bar chart
  165. function bar100(startX, startY, width, height,
  166. currValue, maxValue, barCol, bckCol, fntCol, label)
  167.  
  168.     barSeg = "_"
  169.     barLbl = string.sub(label, 1, math.min(width, string.len(label)))
  170.     barPer = currValue / maxValue
  171.     barSegs = round(height * barPer, 0)
  172.     bckSegs = height - barSegs
  173.    
  174.     for i = 2, width do
  175.         barSeg = barSeg.." "
  176.     end
  177.    
  178.     terminal.centerText(startX, startX + width,
  179.     startY + 1, black, white, barLbl)
  180.    
  181.     for j = 0, barSegs - 1 do
  182.         terminal.writeText(startX, startY - j,
  183.         barCol, fntCol, barSeg)
  184.     end
  185.    
  186.     for k = 0, bckSegs - 1 do
  187.         terminal.writeText(startX,
  188.         (startY - barSegs) - k, bckCol, fntCol,
  189.         barSeg)
  190.     end
  191.    
  192. end
  193.  
  194. --UTILITY FUNCTIONS--
  195.  
  196. function clear(col, line, colour)
  197.     if col == nil then
  198.            col = 1
  199.             line = 1
  200.     end
  201.     if colour == nil then
  202.            term.setBackgroundColor(black)
  203.     else
  204.         term.setBackgroundColor(colour)
  205.     end
  206.     term.clear()
  207.     term.setCursorPos(col, line)
  208. end
  209.  
  210. function loginScreen(header)
  211.     term.setBackgroundColor(lightBlue)
  212.     term.clear()
  213.     window(40, 4, lightGrey, magenta, black, header)
  214.     box(((x/2)-19), ((y/2)+1), ((x/2)+19), ((y/2)+1), white)
  215.     term.setCursorPos(((x/2)-19), ((y/2)+1))
  216. end
  217.  
  218. function fakeScreen()
  219.     os.loadAPI("OS/Users/curUser")
  220.     bg = paintutils.loadImage(curUser.background)
  221.     paintutils.drawImage(bg, 1, 1)
  222.     box(1, x, y, y, lightGrey)
  223.     writeText(1, y, lightGrey, white, "Start")
  224.     time = os.time()
  225.     time = textutils.formatTime(time, true)
  226.     tLen = string.len(time)
  227.     writeText((x - 1) - tLen, y, lightGrey, white, time)
  228. end
  229.  
  230. --Rounds 'num' to 'dec' decimal places
  231. function round(num, dec)
  232.     mult = 10^(dec or 0)
  233.     return math.floor(num * mult + 0.5) / mult
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement