Advertisement
Guest User

Simple UI API v4

a guest
Mar 18th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.74 KB | None | 0 0
  1. --[[ Computercraft UI API by ThePH]]
  2.  
  3. -- functions comonly used, suing targets (being a monitor or term)
  4. function cur(target, x, y) target.setCursorPos(x, y) end
  5. function color(target,fg,bg) if fg > -1 then    target.setTextColor(fg) end if bg > -1 then target.setBackgroundColor(bg) end end
  6. function drawPixel(target, x, y, nColour) if nColour then term.setBackgroundColor( nColour ) end cur(target, x, y) v.write(' ') end
  7.  
  8. --Function to check if item is contained in tbl and return en key or 0 if not in tbl
  9. function inTable(tbl, item)
  10.     if type(item) == 'table' then
  11.         for i, v in pairs(item) do
  12.             for key,value in pairs(tbl) do
  13.                 if value == v then return key end
  14.             end
  15.         end
  16.     elseif type(item) == 'string' then
  17.         for key,value in pairs(tbl) do
  18.             if value == item then return key end    
  19.         end
  20.     end
  21.     return 0
  22. end
  23.  
  24. -- Reset the the UI list to make it fit the 'format'
  25. function reset(list)
  26.     list.ui = {}
  27.     list.shape = {}
  28.     return list
  29. end
  30.  
  31.  
  32. -- Function to create and edit the lists
  33.  
  34. -- Adding a UI element
  35. function addUI(uiList, keyName, x, y, text, fg, bg, action, pages, output, enabled, visible)
  36.     if enabled == nil then enabled = true end
  37.     if visible == nil then visible = true end
  38.     uiList.ui[keyName] = {x, y, text, fg, bg, action, pages, output, enabled, visible}
  39.     return uiList
  40. end
  41.  
  42. -- Adding a shape (p3 is not used yet, but its here just in case)
  43. function addShape(uiList, keyName, shape, x, y, p1, p2, p3, color, pages, output, enabled)
  44.     if enabled == nil then enabled = true end
  45.     uiList.shape[keyName] = {shape, x, y, p1, p2, p3, color, pages, output, enabled}
  46.     return uiList
  47. end
  48.  
  49. -- Editing a UI element
  50. function editUI(uiList, key, id, value)
  51.     local ids = {'x','y','text','fg','bg','action','pages','output', 'enabled', 'visible'}
  52.     local index = inTable(ids, id)
  53.     uiList.ui[key][index] = value
  54.     return uiList
  55. end
  56.  
  57. -- Draws the UI and shapes (shapes  before)
  58. function draw(uiList, pageList, dFG, dBG)
  59.     for index, value in pairs(uiList.shape) do
  60.         local shape, x, y, p1, p2, p3, color, pagesIn, output, enabled = unpack(value)
  61.         local pageOk = false
  62.         if inTable(pagesIn, pageList) > 0 then
  63.             pageOk = true
  64.         end
  65.         if pageOk and enabled then
  66.             if shape == 'line' then
  67.                 drawLine(x, y, p1, p2, output, color)
  68.             elseif shape == 'outline' then
  69.                 drawOutline(x, y, p1, p2, output, color)
  70.             elseif shape == 'box' then
  71.                 drawBox(x, y, p1, p2, output, color)
  72.             end
  73.         end
  74.     end
  75.     for index, value in pairs(uiList.ui) do
  76.         local x, y, text, fg, bg, action, pagesIn, output, enabled, visible = unpack(value)
  77.         local pageOk = false
  78.         if inTable(pagesIn, pageList) > 0 then
  79.             pageOk = true
  80.         end
  81.         if pageOk and visible and enabled then
  82.             for i,v in pairs(output) do
  83.                 color(v, dFG, dBG)
  84.                 cur(v, x, y)
  85.                 color(v, fg, bg)
  86.                 v.write(text)
  87.             end
  88.         end
  89.     end
  90. end
  91.  
  92. -- Function to draw something not used for interaction (captions etc)
  93. function drawSingle(x, y, text, fg, bg, output)
  94.     for i, v in pairs(output) do
  95.         cur(v, x, y)
  96.         color(v, fg, bg)
  97.         v.write(text)
  98.     end
  99. end
  100.  
  101. -- Checks mouse function (using the ui list, x, y pages and (optional, the targets))
  102. -- returns the action, the key in the ui list and the position on the string
  103. function mouse(uiList, mx, my, pageList, target)
  104.     for index, value in pairs(uiList.ui) do
  105.         local x, y, text, fg, bg, action, pagesIn, output, enabled, visible = unpack(value)
  106.         pageOk = false
  107.         if inTable(pagesIn, pageList) > 0 then pageOk = true end
  108.         if (not(target) or inTable(output, target) > 0) and not(action == 'nil') and pageOk and enabled then
  109.             for i, v in pairs(pagesIn) do
  110.                 if inTable(pageList, v) then
  111.                     if my == y then
  112.                         if mx >= x and mx <= (x + text:len() - 1) then
  113.                             return action, index, (mx - x + 1)
  114.                         end
  115.                     end
  116.                     break
  117.                 end
  118.             end
  119.         end
  120.     end
  121.     return nil, nil, nil
  122. end
  123.  
  124. -- function the draw an empty box on target
  125. function drawOutline(x1,y1, x2, y2, target, c)
  126.     drawLine(x1,y1, x1, y2, target, c)
  127.     drawLine(x1,y2, x2, y2, target, c)
  128.     drawLine(x2,y1, x2, y2, target, c)
  129.     drawLine(x1,y1, x2, y1, target, c)
  130. end
  131.  
  132. -- function from paintutils ported for using targets
  133. local function drawPixelInternal( xPos, yPos, target )
  134.     target.setCursorPos(xPos, yPos)
  135.     target.write(" ")
  136. end
  137.  
  138. -- function to draw line using targets
  139. function drawLine(startX, startY, endX, endY, target, nColour)
  140.     for i, v in pairs(target) do
  141.         line( startX, startY, endX, endY, v, nColour )
  142.     end
  143. end
  144.  
  145. -- function to draw a filled box on target
  146. function drawBox(startX, startY, endX, endY, target, nColour)
  147.     if startY <= endY then s = 1 else s= -1 end
  148.     for index=startY, endY, s do
  149.         drawLine( startX, index, endX, index, target, nColour )
  150.     end
  151. end
  152.  
  153. -- function from paintutils ported for using targets
  154. function line( startX, startY, endX, endY, v, nColour )
  155.     if nColour then
  156.         v.setBackgroundColor( nColour )
  157.     end
  158.  
  159.     startX = math.floor(startX)
  160.     startY = math.floor(startY)
  161.     endX = math.floor(endX)
  162.     endY = math.floor(endY)
  163.    
  164.     if startX == endX and startY == endY then
  165.         drawPixelInternal( startX, startY, v )
  166.         return
  167.     end
  168.    
  169.     local minX = math.min( startX, endX )
  170.     if minX == startX then
  171.         minY = startY
  172.         maxX = endX
  173.         maxY = endY
  174.     else
  175.         minY = endY
  176.         maxX = startX
  177.         maxY = startY
  178.     end
  179.        
  180.     local xDiff = maxX - minX
  181.     local yDiff = maxY - minY
  182.            
  183.     if xDiff > math.abs(yDiff) then
  184.         local y = minY
  185.         local dy = yDiff / xDiff
  186.         for x=minX,maxX do
  187.             drawPixelInternal( x, math.floor( y + 0.5 ), v )
  188.             y = y + dy
  189.         end
  190.     else
  191.         local x = minX
  192.         local dx = xDiff / yDiff
  193.         if maxY >= minY then
  194.             for y=minY,maxY do
  195.                 drawPixelInternal( math.floor( x + 0.5 ), y, v )
  196.                 x = x + dx
  197.             end
  198.         else
  199.             for y=minY,maxY,-1 do
  200.                 drawPixelInternal( math.floor( x + 0.5 ), y, v )
  201.                 x = x - dx
  202.             end
  203.         end
  204.     end
  205. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement