Advertisement
Phoenix_Tekkit

gooey

Jul 28th, 2013
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. -- API for building a basic GUI on advanced monitors
  2.  
  3. function drawBox(startX,startY,endX,endY,boxColour,display)
  4.     display = display or term
  5.     display.setBackgroundColour(boxColour)
  6.     for Y = startY, endY do
  7.         for X = startX, endX do
  8.             display.setCursorPos(X,Y)
  9.             display.write(" ")
  10.         end
  11.     end
  12. end
  13.  
  14. function writeAt(X,Y,message,display)
  15.     display = display or term
  16.     display.setCursorPos(X,Y)
  17.     display.write(message)
  18. end
  19.  
  20. function drawButton(startX,startY,endX,endY,message,boxColour,clickFunc,clickParams,display)
  21.  
  22.     display = display or term
  23.     clickParams = clickParams or {}
  24.     display.setBackgroundColour(boxColour)
  25.     triggerZones = {}
  26.     for Y = startY, endY do
  27.         for X = startX, endX do
  28.             display.setCursorPos(X,Y)
  29.             display.write(" ")
  30.             triggerZones[X .. "," .. Y] = {["func"] = clickFunc,["params"] = clickParams}
  31.         end
  32.     end
  33.     writeAt(math.floor(startX + (endX - startX  - #message)/2),math.floor(startY + (endY-startY)/2),message,display)
  34.     return triggerZones
  35.  
  36. end
  37.  
  38. function drawImage(startX,startY,image,display)
  39.     display = display or term
  40.     for Y, line in pairs(image) do
  41.         for X, col in pairs(line) do
  42.             display.setBackgroundColour(col)
  43.             display.setCursorPos(X,Y)
  44.             display.write(" ")
  45.         end
  46.     end
  47. end
  48.  
  49. function buttonHandler(triggerZones, monitor, time)
  50.     notPressed = true
  51.     while notPressed do
  52.         if monitor == nil or monitor == term then
  53.             event, button, xPos, yPos = os.pullEvent("mouse_click")
  54.         else
  55.             event, side, xPos, yPos = os.pullEvent("monitor_touch")
  56.         end
  57.         index = (xPos .. "," .. yPos)
  58.         if triggerZones[index] ~= nil
  59.         then
  60.             p = triggerZones[index].params
  61.             triggerZones[index].func(p[1],p[2],p[3],p[4],p[5],p[6])
  62.             notPressed = false
  63.         end
  64.     end
  65. end
  66.  
  67. function clickMenu(menuTitle,menuItems, display)
  68.     display = display or term
  69.     w, h = display.getSize()
  70.     display.setTextColor(colours.black)
  71.     display.setBackgroundColor(colours.black)
  72.     display.clear()
  73.     drawBox(2,2,w - 7,2,colours.lightGrey,display)
  74.     writeAt(math.floor((w - #menuTitle)/2),2,menuTitle,display)
  75.     buttons = {}
  76.     for i = 1,#menuItems do
  77.         buttons = commonFunctions.combineTables(buttons,drawButton(2,i * 2 + 2,w - 7,i * 2 + 2,menuItems[i].text,colours.lightGrey,menuItems[i].func,menuItems[i].params,display))
  78.     end
  79.     buttonHandler(buttons,display)
  80. end
  81.  
  82. --[[ function drawBar(horizontal, position, width, data, colour, backColour, display) -- Draws a bar as part of a bar chart right across the display
  83.     display = display or term
  84.    
  85.     -- these two bo
  86.     h, w = display.getSize()
  87.     WW = {}
  88.     Pos = {["X"] = 0, ["Y"] = 0}
  89.     if horizontal
  90.     then
  91.         WW["along"] = "X"
  92.         WW["wide"] = "Y"
  93.         step = math.floor((w - 1) / 10)
  94.     else
  95.         WW["along"] = "X"
  96.         WW["wide"] = "Y"
  97.         step = math.floor((h - 1) / 10)
  98.     end
  99.     for Pos[WW.along] = 2,(10 * step) do
  100.         for Pos[WW.wide] = position, (position + width)
  101.         if step > 1 and (Pos[WW.along] % step) == 0 then
  102.             display.setBackgroundColor(backColour)
  103.         else
  104.             display.setBackgroundColor(colour)
  105.         end
  106.         if ((Pos[WW.along]/(step * 10)) * 100) > data.percentage
  107.         then
  108.             display.setCursorPos(Pos.X,Pos.Y)
  109.             display.write(" ")
  110.         end
  111.     end
  112.     if data.label ~= nil
  113.     then
  114.         Pos[WW.along] = 1
  115.         for i = 1,width do
  116.             Pos[WW.wide] = position + (i - 1)
  117.             display.setCursorPos(Pos.X,Pos.Y)
  118.             display.write(data.label:sub(i,i))
  119.         end
  120.     end
  121.     if data.rawNumber ~= nil
  122.     then
  123.         numberText = tostring(data.rawNumber)
  124.         if horizontal
  125.         then
  126.             display.setCursorPos((position + width /2), ((w - numberText:len)/2))
  127.             display.write(numberText)
  128.         end
  129.     end
  130. end
  131.  
  132. function barCharts(data, title, horizontal, display) -- data should be a table of the data for the bars, which should themselves be a table containing .percentage, .rawNumber (optional) and .label (optional)
  133.    
  134.     -- set stuff up
  135.     display = display or term
  136.     horizontal = horizontal or false
  137.     colourToUse = 1
  138.     background = colour.black
  139.     h, w = display.getSize()
  140.     if horizontal
  141.     then
  142.         width = math.floor(h / len(data))
  143.         pos = 2
  144.     else
  145.         width = math.floor(w / len(data))
  146.         pos = 1
  147.     end
  148.    
  149.     -- Get with the drawing
  150.     display.setBackgroundColor(background)
  151.     display.clear()
  152.     for i = 1,#data do
  153.         drawBar(horizontal, pos, width, data[i], colourToUse, background, display)
  154.         pos = pos + width
  155.         colourToUse = colourToUse * 2
  156.     end
  157.     display.setCursorPos((w - len(title))/2,1)
  158.     display.setBackgroundColor(background)
  159.     display.write(title)
  160. end ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement