Advertisement
GravityCube

TableAPI

Mar 20th, 2019
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.63 KB | None | 0 0
  1. --[[
  2.    
  3.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--   
  4.             --*            Table Display API         *--
  5.             --*     https://pastebin.com/tn4Uuddp    *--
  6.             --*            by: GravityCube           *--
  7.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  8.            
  9.     Changelog:
  10.         1.0.0 First release,
  11.         1.1.0 Highlights added.
  12.             1.1.1 A method to know wish row is being touch was added.
  13.         1.2.0 Sorting added.
  14.             1.2.1 Added text to know which column is being sorted.
  15.             1.2.2 Changed ">" to "v" and "<" to "^".
  16.         1.3.0 UP/DOWN buttons were added. They allow more rows in the same screen.
  17.         1.4.0 Changed scroll system for pages system.
  18.             1.4.1 Added red response in case of no next/previous page.
  19.             1.4.2 Method updateRow was added.
  20.             1.4.3 Replace "_" -> " "
  21.         1.5.0 Added custom colors functions for columns
  22.        
  23.         ]]--
  24. local TableInstance = {
  25.        
  26.     draw = function(self)
  27.         local mon = self.mon
  28.         mon.setBackgroundColor(colors.white)
  29.         mon.clear()
  30.         local max_x, max_y = mon.getSize()
  31.    
  32.         local x_string = ""
  33.         for x=1, max_x do
  34.             x_string = x_string .. " "
  35.         end
  36.    
  37.         self:updatePositions() 
  38.        
  39.         mon.setBackgroundColor(colors.cyan)
  40.         mon.setCursorPos(1,1)
  41.         mon.write(x_string)
  42.        
  43.         for i,column in pairs(self.columns) do
  44.             local x_pos = self.positions[i]
  45.             mon.setCursorPos(x_pos, 1)
  46.             local column_txt = string.upper(string.sub(column, 1, 1)) .. string.sub(column, 2)
  47.             column_txt = string.gsub(column_txt, "_", "")
  48.             if self.sort_column == column then
  49.                 column_txt = column_txt .. " " .. ((self.sort_direction and "^") or "v")
  50.             end
  51.             mon.write(tostring(column_txt))
  52.         end
  53.        
  54.        
  55.         mon.setBackgroundColor(colors.white)
  56.         mon.setTextColor(colors.black)
  57.        
  58.         for y=2,max_y-1,1 do
  59.             self:updateRow(y)          
  60.         end
  61.         self:displayButtons()
  62.     end,
  63.    
  64.     updateRow = function (self, y)
  65.         --SCROLL
  66.         local mon = self.mon
  67.         local max_x, max_y = mon.getSize()
  68.         local row_data = getRowData(self.data, y, self.scrollInt)  
  69.        
  70.         local x_string = ""
  71.         for x=1, max_x do
  72.             x_string = x_string .. " "
  73.         end
  74.        
  75.         if row_data ~= nil then
  76.            
  77.             --COSA PARA NO PERDERSE
  78.             if (y + self.scrollInt) % 2 == 0 then
  79.                 mon.setBackgroundColor(0x0100)
  80.             end
  81.            
  82.             --HighLight
  83.             if self:isHighlight(row_data) then
  84.                 mon.setBackgroundColor(colors.orange)
  85.             end
  86.            
  87.             mon.setCursorPos(1,y)
  88.             mon.write(x_string)
  89.            
  90.             for i,column in pairs(self.columns) do
  91.                 local x_pos = self.positions[i]
  92.                 mon.setCursorPos(x_pos, y)
  93.                 local txt = tostring(row_data[column])
  94.                 if column == "price" then
  95.                     txt = "$" .. txt
  96.                 end
  97.                 local cf = self.custom_colors_func[column]
  98.                 if cf then
  99.                     cf(mon, txt)
  100.                     mon.write(txt)
  101.                     mon.setTextColor(colors.black)
  102.                 else
  103.                     mon.write(txt)
  104.                 end
  105.             end
  106.            
  107.             mon.setBackgroundColor(colors.white)
  108.         end
  109.     end,
  110.    
  111.     highlight = function (self, column, value, state)
  112.         if not self.highlights[column] then
  113.             self.highlights[column] = {}
  114.         end
  115.        
  116.         local last_state = self.highlights[column][value]
  117.         local new_state = (state ~= nil and state) or (not last_state)
  118.        
  119.         self.highlights[column][value] = new_state
  120.     end,
  121.    
  122.     isHighlight = function (self, row_data)
  123.         for column, value in pairs(row_data) do
  124.             if self.highlights[column] and self.highlights[column][value] then
  125.                 return true
  126.             end
  127.         end
  128.         return false
  129.     end,
  130.    
  131.     clearHighlights = function(self)
  132.         for column, values in pairs(self.highlights) do
  133.             self.highlights[column] = {}
  134.         end
  135.     end,
  136.    
  137.     getRowOnEvent = function (self, tEvent)
  138.         if tEvent[1] == "monitor_touch" then
  139.             local max_x, max_y = self.mon.getSize()
  140.             local x = tEvent[3]
  141.             local y = tEvent[4]
  142.            
  143.             self:sortingRow(x,y)
  144.             self:pageButtons(x,y)
  145.            
  146.             if y > 1 and y < max_y then
  147.                 local i = y-1+self.scrollInt
  148.                 return self.data[i], y
  149.             end
  150.             if self.playSound then
  151.                 self:playSound()
  152.             end
  153.         end
  154.     end,
  155.    
  156.     pageButtons = function(self, x, y)
  157.         local max_x, max_y = self.mon.getSize()
  158.         if y ~= max_y then return end
  159.        
  160.         local center_x = getCenter(max_x)
  161.        
  162.         local new_scroll_int = 0
  163.         local button_state_name = nil
  164.         if x < center_x then
  165.             new_scroll_int = self.scrollInt - (max_y - 2)
  166.             button_state_name = "button1_state"
  167.         else
  168.             new_scroll_int = self.scrollInt + (max_y - 2)
  169.             button_state_name = "button2_state"
  170.         end
  171.        
  172.         if getRowData(self.data, 2,  new_scroll_int) then
  173.             self.scrollInt = new_scroll_int
  174.             self[button_state_name] = true
  175.         else
  176.             self[button_state_name] = colors.red
  177.         end
  178.        
  179.         self:displayButtons()
  180.         sleep(0.05)
  181.         self[button_state_name] = false
  182.         self:draw()
  183.     end,
  184.    
  185.     displayButtons = function(self)
  186.         local mon = self.mon
  187.         local max_x, max_y = self.mon.getSize()
  188.        
  189.         local b1 = self.button1_state
  190.         local b2 = self.button2_state
  191.        
  192.         local x_string = ""
  193.         for x=1, max_x do
  194.             x_string = x_string .. " "
  195.         end
  196.        
  197.         local max_x, max_y = self.mon.getSize()
  198.         local center_x = getCenter(max_x)
  199.        
  200.         local x_b1 = getCenter(center_x, "Previous Page")
  201.         local x_b2 = getCenter(max_x-center_x, "Next Page") + center_x
  202.        
  203.         mon.setBackgroundColor(colors.cyan)
  204.         if b1 then mon.setBackgroundColor( ( (b1 == true) and colors.orange ) or b1) end
  205.         mon.setCursorPos(1, max_y)
  206.         mon.write(x_string)
  207.         mon.setCursorPos(x_b1, max_y)
  208.         mon.write("Previous Page")
  209.        
  210.         mon.setBackgroundColor(colors.cyan)
  211.         if b2 then mon.setBackgroundColor( ( (b2 == true) and colors.orange ) or b2) end
  212.         mon.setCursorPos(center_x, max_y)
  213.         mon.write(x_string)
  214.         mon.setCursorPos(x_b2, max_y)
  215.         mon.write("Next Page")     
  216.     end,
  217.    
  218.     sortingRow = function (self, x, y)
  219.         if y ~= 1 then return end
  220.         --** Sorting
  221.         local positions = self.positions
  222.         for i,column in pairs(self.columns) do
  223.             local x_pos = positions[i]
  224.             if x >= x_pos and x <= (string.len(column) + x_pos) then
  225.                 self:sort(column)
  226.                 return true
  227.             end
  228.         end
  229.         return false
  230.     end,
  231.    
  232.     sort = function(self, column)
  233.         if self.sort_column ~= column then
  234.             self.sort_direction = false
  235.         end
  236.         if self.sort_direction then
  237.             table.sort(self.data, function(a,b) return (a[column] > b[column]) end)
  238.         else
  239.             table.sort(self.data, function(a,b) return (a[column] < b[column]) end)                
  240.         end
  241.         self.sort_column = column
  242.         self.sort_direction = (not self.sort_direction)
  243.        
  244.         self:draw()
  245.     end,
  246.    
  247.     getLengths = function(self)
  248.         local max_x, max_y = self.mon.getSize()
  249.         local max_x_table = (max_x - #self.columns) - 1
  250.         local lengths = {}
  251.         lengths[0] = 0
  252.         for i,column in pairs(self.columns) do
  253.             lengths[i] = getMaxLength(self.data, column)
  254.         end
  255.        
  256.         local a = 1
  257.         while totalLength(lengths) < max_x_table do
  258.             if a > #self.columns then
  259.                 a = 1
  260.             end
  261.             lengths[a] = lengths[a] + 1
  262.             a = a + 1
  263.         end
  264.         return lengths
  265.     end,
  266.    
  267.     updatePositions = function (self)
  268.         local lengths = self:getLengths()
  269.         local positions = {}
  270.         local x_pos = 1
  271.         for i,column in pairs(self.columns) do
  272.             x_pos = x_pos + lengths[(i-1)]+1
  273.             positions[i] = x_pos
  274.         end    
  275.         self.positions = positions
  276.    
  277.     end,
  278. }
  279. --------------------------------------
  280. -->               Utils            <--
  281. --------------------------------------
  282. function getMaxLength(eTable, column)
  283.     local length = 1
  284.     for k,v in pairs(eTable) do
  285.         if length < string.len(v[column]) then
  286.             length = string.len(v[column])
  287.         end
  288.     end
  289.     if length < string.len(column) then
  290.         length = string.len(column)
  291.     end
  292.     return length
  293. end
  294. function getRowData(eTable, y, scrollInt)
  295.     local i = y-1+scrollInt
  296.     return eTable[i]
  297. end
  298. function totalLength(lengths)
  299.     tLength = 0
  300.     for _,le in pairs(lengths) do
  301.         tLength = tLength + le
  302.     end
  303.     return tLength
  304. end
  305. function getCenter(f, varP)
  306.     if not varP then
  307.         varP = 0
  308.     end
  309.     local lengthOfVar = 0
  310.     if type(varP) == "number" then
  311.         lengthOfVar = varP
  312.     elseif type(varP) == "string" then
  313.         lengthOfVar = string.len(varP)
  314.     else
  315.         error("For function gcapi.getCenter expected number or string in argument #2")
  316.     end
  317.     if (f - string.len(varP) % 2) == 0 then
  318.         return math.floor((f - lengthOfVar)/2)
  319.     end
  320.     return math.floor((f - lengthOfVar)/2)+1
  321.    
  322. end
  323. --------------------------------------
  324. -->         Public functions       <--
  325. --------------------------------------
  326. function new(monitor, data_table, columns_ins, x, y, width, heigth)
  327.    
  328.     local calculated_columns = {}
  329.     if not columns_ins then
  330.         for k,v in pairs(data_table[1]) do
  331.             table.insert(calculated_columns, k)
  332.         end
  333.     else
  334.         calculated_columns = columns_ins
  335.     end
  336.    
  337.     local instance = {
  338.         highlights = {},
  339.         mon = monitor,
  340.         columns = calculated_columns,
  341.         data = data_table,
  342.         scrollInt = 0,
  343.         custom_colors_func = {}
  344.     }
  345.     setmetatable(instance, {__index = TableInstance})
  346.    
  347.     instance:sort(instance.columns[1])
  348.     return instance
  349. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement