blunty666

advancedTiles

May 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.80 KB | None | 0 0
  1. --===== UTILITIES =====--
  2. local function makeMetatable(methodsTable)
  3.     return {
  4.         __index = function(t, k)
  5.             return methodsTable[k] or t.tile[k]
  6.         end,
  7.     }
  8. end
  9.  
  10. local function createBackground(tile, width, height, opacity)
  11.     return tile:AddBox(0, 0, width, height, 0x000000, opacity)
  12. end
  13.  
  14. local function createAxis(tile, width, height, opacity)
  15.     local points = {
  16.         {x = 0, y = 0},
  17.         {x = 0, y = 0 + height},
  18.         {x = 0 + width, y = 0 + height},
  19.     }
  20.     local axis = tile:AddLineList(0x000000, opacity, points)
  21.     axis:SetZ(2)
  22.     axis:SetClickable(false)
  23.     axis:SetWidth(2)
  24.     return axis
  25. end
  26.  
  27. --===== SIMPLE BAR =====--
  28. local simpleBarMethods = {
  29.     GetTile = function(self)
  30.         return self.tile
  31.     end,
  32.     GetBackground = function(self)
  33.         return self.background
  34.     end,
  35.     GetBar = function(self)
  36.         return self.bar
  37.     end,
  38.     GetWidth = function(self)
  39.         return self.background:GetWidth()
  40.     end,
  41.     SetWidth = function(self, width)
  42.         local newWidth = math.max(0, width)
  43.         self.background:SetWidth(newWidth)
  44.         self.bar:SetWidth(math.max(0, newWidth - 2)*self.percent)
  45.     end,
  46.     GetHeight = function(self)
  47.         return self.background:GetHeight()
  48.     end,
  49.     SetHeight = function(self, height)
  50.         local newHeight = math.max(0, height)
  51.         self.background:SetHeight(newHeight)
  52.         self.bar:SetHeight(math.max(0, newHeight - 2))
  53.     end,
  54.     GetPercent = function(self)
  55.         return self.percent
  56.     end,
  57.     SetPercent = function(self, percent)
  58.         if type(percent) == "number" then
  59.             self.percent = math.max(0, math.min(1, percent))
  60.             self.bar:SetWidth((self:GetWidth() - 2)*self.percent)
  61.             return true
  62.         end
  63.         return false
  64.     end,
  65. }
  66. local simpleBarMetatable = makeMetatable(simpleBarMethods)
  67.  
  68. local function createSimpleBar(tile, width, height, opacity, bar, percent)
  69.     local simpleBar = {
  70.         tile = tile,
  71.         percent = 0,
  72.         background = createBackground(tile, width, height, opacity),
  73.         bar = bar,
  74.     }
  75.     bar:SetZ(1)
  76.     bar:SetClickable(false)
  77.     setmetatable(simpleBar, simpleBarMetatable)
  78.     if percent then
  79.         simpleBar:SetPercent(percent)
  80.     end
  81.     return simpleBar
  82. end
  83.  
  84. function addSimpleBoxBar(tile, xPos, yPos, zPos, width, height, colour, opacity, percent)
  85.     local subTile = tile:AddSubTile(xPos, yPos, zPos)
  86.     local bar = subTile:AddBox(1, 1, 0, height - 2, colour, opacity)
  87.     return createSimpleBar(subTile, width, height, opacity/2, bar, percent)
  88. end
  89.  
  90. function addSimpleGradientBoxBar(tile, xPos, yPos, zPos, width, height, colour1, opacity1, colour2, opacity2, gradient, percent)
  91.     local subTile = tile:AddSubTile(xPos, yPos, zPos)
  92.     local bar = subTile:AddGradientBox(1, 1, 0, height - 2, colour1, opacity1, colour2, opacity2, gradient)
  93.     return createSimpleBar(subTile, width, height, (opacity1 + opacity2)/4, bar, percent)
  94. end
  95.  
  96. function addSimpleFluidBar(tile, xPos, yPos, zPos, width, height, fluid, alpha, percent)
  97.     local subTile = tile:AddSubTile(xPos, yPos, zPos)
  98.     local bar = subTile:AddFluid(1, 1, 0, height - 2, fluid)
  99.     bar:SetAlpha(alpha)
  100.     return createSimpleBar(subTile, width, height, alpha/2, bar, percent)
  101. end
  102.  
  103. --===== COMPLEX BAR =====--
  104. local complexBarMethods = {
  105.     GetTile = function(self)
  106.         return self.tile
  107.     end,
  108.     GetBackground = function(self)
  109.         return self.background
  110.     end,
  111.     GetWidth = function(self)
  112.         return self.background:GetWidth()
  113.     end,
  114.     --[[
  115.     SetWidth = function(self, width)
  116.         local newWidth = math.max(0, width)
  117.         self.background:SetWidth(newWidth)
  118.         --self.bar:SetWidth(math.max(0, newWidth - 2)*self.percent)
  119.     end,
  120.     ]]
  121.     GetHeight = function(self)
  122.         return self.background:GetHeight()
  123.     end,
  124.     --[[
  125.     SetHeight = function(self, height)
  126.         local newHeight = math.max(0, height)
  127.         self.background:SetHeight(newHeight)
  128.         --self.bar:SetHeight(math.max(0, newHeight - 2))
  129.     end,
  130.     ]]
  131.     GetPercent = function(self)
  132.         return self.percent
  133.     end,
  134.     SetPercent = function(self, percent)
  135.         if type(percent) == "number" then
  136.             self.percent = math.max(0, math.min(1, percent))
  137.             local segmentsDrawn = self.percent*#self.segments
  138.             segmentsDrawn = math.floor(segmentsDrawn) + math.floor(2*(segmentsDrawn%1))
  139.             for i, segment in ipairs(self.segments) do
  140.                 segment:SetVisible(i <= segmentsDrawn)
  141.             end
  142.             return true
  143.         end
  144.         return false
  145.     end,
  146. }
  147. local complexBarMetatable = makeMetatable(complexBarMethods)
  148.  
  149. function addComplexBar(tile, xPos, yPos, zPos, width, height, colour, opacity, percent)
  150.     local subTile = tile:AddSubTile(xPos, yPos, zPos)
  151.     local complexBar = {
  152.         tile = subTile,
  153.         background = createBackground(subTile, width, height, opacity/2),
  154.         segments = {},
  155.     }
  156.    
  157.     local segmentWidth = math.ceil((width - 2)/40)
  158.     local xStart = 1
  159.     while xStart < width - 2 do
  160.         local thisSegmentWidth = segmentWidth
  161.         if xStart + segmentWidth > width - 1 then
  162.             thisSegmentWidth = thisSegmentWidth - (xStart + segmentWidth) + (width - 1)
  163.         end
  164.         local segment = subTile:AddGradientBox(xStart, 1, thisSegmentWidth, height - 2, colour/3, opacity, colour, opacity, 2)
  165.         segment:SetZ(1)
  166.         segment:SetVisible(false)
  167.         segment:SetClickable(false)
  168.         table.insert(complexBar.segments, segment)
  169.         xStart = xStart + segmentWidth
  170.     end
  171.    
  172.     setmetatable(complexBar, complexBarMetatable)
  173.     if percent then
  174.         complexBar:SetPercent(percent)
  175.     end
  176.     return complexBar
  177. end
  178.  
  179. --===== BAR GRAPH =====--
  180. local barGraphMethods = {
  181.     GetTile = function(self)
  182.         return self.tile
  183.     end,
  184.     GetBackground = function(self)
  185.         return self.background
  186.     end,
  187.     GetWidth = function(self)
  188.         return self.background:GetWidth()
  189.     end,
  190.     --[[
  191.     SetWidth = function(self, width)
  192.         local newWidth = math.max(0, width)
  193.         self.background:SetWidth(newWidth)
  194.         --self.bar:SetWidth(math.max(0, newWidth - 2)*self.percent)
  195.     end,
  196.     ]]
  197.     GetHeight = function(self)
  198.         return self.background:GetHeight()
  199.     end,
  200.     --[[
  201.     SetHeight = function(self, height)
  202.         local newHeight = math.max(0, height)
  203.         self.background:SetHeight(newHeight)
  204.         --self.bar:SetHeight(math.max(0, newHeight - 2))
  205.     end,
  206.     ]]
  207.     Update = function(self, value)
  208.         if type(value) == "number" then
  209.             table.insert(self.values, math.min(1, math.max(0, value)))
  210.             table.remove(self.values, 1)
  211.             for i, bar in ipairs(self.bars) do
  212.                 bar:SetHeight(self:GetHeight()*self.values[i])
  213.             end
  214.             return true
  215.         end
  216.         return false
  217.     end,
  218. }
  219. local barGraphMetatable = makeMetatable(barGraphMethods)
  220.  
  221. local function createBarGraph(tile, width, height, opacity, bars)
  222.     local barGraph = {
  223.         tile = tile,
  224.         values = {},
  225.         bars = bars,
  226.         background = createBackground(tile, width, height, opacity/4),
  227.         axis = createAxis(tile, width, height, opacity),
  228.     }
  229.     for i = 1, width do
  230.         barGraph.values[i] = 0
  231.     end
  232.     return setmetatable(barGraph, barGraphMetatable)
  233. end
  234.  
  235. function addBoxBarGraph(tile, xPos, yPos, zPos, width, height, colour, opacity)
  236.     local subTile = tile:AddSubTile(xPos, yPos, zPos)
  237.     local bars = {}
  238.     for x = 1, width do
  239.         local bar = subTile:AddBox(x - 1, height, 1, 0, colour, opacity)
  240.         bar:SetZ(1)
  241.         bar:SetObjectAnchor("LEFT", "BOTTOM")
  242.         bar:SetClickable(false)
  243.         table.insert(bars, bar)
  244.     end
  245.     return createBarGraph(subTile, width, height, opacity, bars)
  246. end
  247.  
  248. function addGradientBoxBarGraph(tile, xPos, yPos, zPos, width, height, colour, opacity)
  249.     local subTile = tile:AddSubTile(xPos, yPos, zPos)
  250.     local bars = {}
  251.     for x = 1, width do
  252.         local bar = subTile:AddGradientBox(x - 1, height, 1, 0, colour/3, opacity, colour, opacity, 2)
  253.         bar:SetZ(1)
  254.         bar:SetObjectAnchor("LEFT", "BOTTOM")
  255.         bar:SetClickable(false)
  256.         table.insert(bars, bar)
  257.     end
  258.     return createBarGraph(subTile, width, height, opacity, bars)
  259. end
  260.  
  261. function addFluidBarGraph(tile, xPos, yPos, zPos, width, height, fluid, alpha)
  262.     local subTile = tile:AddSubTile(xPos, yPos, zPos)
  263.     local bars = {}
  264.     for x = 1, width do
  265.         local bar = subTile:AddFluid(x - 1, height, 1, 0, fluid)
  266.         bar:SetZ(1)
  267.         bar:SetObjectAnchor("LEFT", "BOTTOM")
  268.         bar:SetClickable(false)
  269.         bar:SetAlpha(alpha)
  270.         table.insert(bars, bar)
  271.     end
  272.     return createBarGraph(subTile, width, height, alpha, bars)
  273. end
  274.  
  275. --===== LINE GRAPH =====--
  276. local lineGraphMethods = {
  277.     GetTile = function(self)
  278.         return self.tile
  279.     end,
  280.     GetBackground = function(self)
  281.         return self.background
  282.     end,
  283.     GetWidth = function(self)
  284.         return self.background:GetWidth()
  285.     end,
  286.     --[[
  287.     SetWidth = function(self, width)
  288.         local newWidth = math.max(0, width)
  289.         self.background:SetWidth(newWidth)
  290.         --self.bar:SetWidth(math.max(0, newWidth - 2)*self.percent)
  291.     end,
  292.     ]]
  293.     GetHeight = function(self)
  294.         return self.background:GetHeight()
  295.     end,
  296.     --[[
  297.     SetHeight = function(self, height)
  298.         local newHeight = math.max(0, height)
  299.         self.background:SetHeight(newHeight)
  300.         --self.bar:SetHeight(math.max(0, newHeight - 2))
  301.     end,
  302.     ]]
  303.     Update = function(self, value)
  304.         if type(value) == "number" then
  305.             table.insert(self.values, math.min(1, math.max(0, value)))
  306.             table.remove(self.values, 1)
  307.             for i, point in ipairs(self.points) do
  308.                 point.y = -(self:GetHeight()*self.values[i])
  309.             end
  310.             self.line:SetPoints(self.points)
  311.             return true
  312.         end
  313.         return false
  314.     end,
  315. }
  316. local lineGraphMetatable = makeMetatable(lineGraphMethods)
  317.  
  318. local function createLineGraph(tile, width, height, opacity, points, line)
  319.     local lineGraph = {
  320.         tile = tile,
  321.         values = {},
  322.         points = points,
  323.         line = line,
  324.         background = createBackground(tile, width, height, opacity/4),
  325.         axis = createAxis(tile, width, height, opacity),
  326.     }
  327.     for i = 1, width do
  328.         lineGraph.values[i] = 0
  329.     end
  330.     return setmetatable(lineGraph, lineGraphMetatable)
  331. end
  332.  
  333. function addLineGraph(tile, xPos, yPos, zPos, width, height, colour, opacity)
  334.     local subTile = tile:AddSubTile(xPos, yPos, zPos)
  335.     local pointsTile = subTile:AddSubTile(0, height - 1, 1)
  336.     local points = {}
  337.     for x = 1, width do
  338.         table.insert(points, {x = x, y = 0})
  339.     end
  340.     local line = pointsTile:AddLineList(colour, opacity, points)
  341.     line:SetZ(1)
  342.     line:SetWidth(2)
  343.     line:SetClickable(false)
  344.     return createLineGraph(subTile, width, height, opacity, points, line)
  345. end
Add Comment
Please, Sign In to add comment