Advertisement
GnoX

gxs

Aug 22nd, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.88 KB | None | 0 0
  1. local buttons              = {}
  2. local labels               = {}
  3. local screen               = {}
  4.  
  5. local forcedEvent          = false
  6.  
  7. local w, h = term.getSize()
  8.  
  9. local defaultActiveColor   = colors.lime
  10. local defaultInactiveColor = colors.red
  11. local defaultTBColor       = colors.blue
  12. local defaultTTColor       = colors.white
  13.  
  14. initialize = function()
  15.     screen = setScreen()
  16. end
  17.  
  18. register = function( name, xpos, ypos, width, height, func, text, activeColor, inactiveColor )
  19.     buttons[name]               = {}
  20.     buttons[name].name          = name
  21.     buttons[name].alive         = true
  22.     buttons[name].active        = false
  23.     buttons[name].shown         = true
  24.     buttons[name].x             = xpos
  25.     buttons[name].y             = ypos
  26.     buttons[name].width         = width - 1
  27.     buttons[name].height        = height - 1
  28.     buttons[name].func          = func
  29.     buttons[name].text          = text          or name
  30.     buttons[name].inactiveColor = inactiveColor or defaultInactiveColor
  31.     buttons[name].activeColor   = activeColor   or defaultActiveColor
  32.  
  33.     return buttons[name]
  34. end
  35.  
  36. array = function ( reg, sum, xCol, xOffset, yOffset )
  37.     local x, y = reg.x, reg.y
  38.     buttons[reg.name] = nil
  39.     xCol = xCol or 1
  40.     xOffset = xOffset or 2
  41.     yOffset = yOffset or 1
  42.  
  43.     for i = 1, sum do
  44.         local str = string.format("%s%d", reg.name, i)
  45.         func      = convertFunc(reg.func, str, i)
  46.  
  47.         if type(reg.inactiveColor) == "table" then
  48.             inactiveColor = reg.inactiveColor[i]
  49.         else
  50.             inactiveColor = reg.inactiveColor
  51.         end
  52.  
  53.         if type(reg.text) == "table" then
  54.             text = reg.text[i]
  55.         elseif reg.text ~= reg.name then
  56.             text = reg.text
  57.         else
  58.             text =  i
  59.         end
  60.  
  61.         register(str, x, y, reg.width + 1, reg.height + 1, func, text, reg.activeColor, inactiveColor)
  62.  
  63.         if xOffset then
  64.             x = x + xOffset
  65.         end
  66.  
  67.         if i % xCol == 0 then
  68.             x = x - xCol * xOffset
  69.  
  70.             if yOffset then
  71.                 y = y + yOffset
  72.             end
  73.         end
  74.     end
  75. end
  76.  
  77. render = function()
  78.     term.setBackgroundColor(colors.black)
  79.     term.clear()
  80.     renderScreen()
  81.     renderLabels()
  82.     renderButtons()
  83.     term.setCursorPos(1, 1)
  84.     term.setBackgroundColor(colors.black)
  85. end
  86.  
  87. renderScreen = function()
  88.     for y, data in pairs(screen) do
  89.         for x, let in pairs(data) do
  90.             if screen[y][x] ~= "" then
  91.                 if screen.color and screen.color[y] and screen.color[y][x] then
  92.                     term.setBackgroundColor(screen.color[y][x])
  93.                 else
  94.                     term.setBackgroundColor(colors.black)
  95.                 end
  96.                 if type(x) == "number" and type(y) == "number" then
  97.                     term.setCursorPos(x, y)
  98.                     term.write(tostring(let))
  99.                 end
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. renderLabels = function()
  106.     for _, data in pairs(labels) do
  107.         if data.shown then
  108.             term.setBackgroundColor(data.color)
  109.             term.setTextColor(data.textColor)
  110.             term.setCursorPos(data.x, data.y)
  111.             term.write(tostring(data.text))
  112.         end
  113.     end
  114. end
  115.  
  116. renderButtons = function()
  117.     for name, data in pairs(buttons) do
  118.         data.text = tostring(data.text)
  119.  
  120.         if data.active then
  121.             term.setBackgroundColor(data.activeColor)
  122.         else
  123.             term.setBackgroundColor(data.inactiveColor)
  124.         end
  125.  
  126.         if data.alive and data.shown then
  127.             height = data.height + data.y
  128.             width = data.width + data.x
  129.             xText = math.floor((width - data.x - data.text:len() ) / 2) + 1
  130.             yText = math.floor(data.y + data.height / 2)
  131.  
  132.             if data.text:len() > data.width + 1 then
  133.                 term.setCursorPos(1, 1)
  134.                 term.setBackgroundColor(colors.black)
  135.                 term.clear()
  136.                 error("Problem with button: \""..name.."\". Text is longer than button!")
  137.             end
  138.  
  139.             for y = data.y, height do
  140.                 term.setCursorPos(data.x, y)
  141.                 if y == yText then
  142.                     for x = 0, width - data.x - data.text:len() + 1 do
  143.                         if x == xText then
  144.                             term.write(data.text)
  145.                         else
  146.                             term.write(" ")
  147.                         end
  148.                     end
  149.                 else
  150.                     for x = data.x, width do
  151.                         term.write(" ")
  152.                     end
  153.                 end
  154.             end
  155.         end
  156.     end
  157. end
  158.  
  159. local getClickEvent = function()
  160.     if term.setTextScale then
  161.         return "monitor_touch"
  162.     elseif forcedEvent then
  163.         return forcedEvent
  164.     else
  165.         return "mouse_click"
  166.     end
  167. end
  168.  
  169. setForcedEvent = function( forceEvent )
  170.     forcedEvent = forceEvent
  171. end
  172.  
  173. handleEvents = function()
  174.     eventData = {os.pullEvent()}
  175.     event = eventData[1]
  176.     if event == getClickEvent() then
  177.         local click, xPos, yPos = eventData[2], eventData[3], eventData[4]
  178.         for _, data in pairs(buttons) do
  179.             if data.alive and data.shown and xPos >= data.x and xPos <= data.x + data.width
  180.             and yPos >= data.y and yPos <= data.y + data.height then
  181.                 if type(data.func) == "string" then
  182.                     loadstring(data.func)()
  183.                 else
  184.                     data.func()
  185.                 end
  186.             end
  187.         end
  188.     end
  189. end
  190.  
  191. exists = function( name )
  192.     if buttons[name] and buttons[name].alive then
  193.         return true
  194.     end
  195.     return error("Button \""..tostring(name).."\" does not exist!")
  196. end
  197.  
  198. show = function( name, state )
  199.     if buttons[name] then
  200.         buttons[name].shown = state or true
  201.     elseif labels[name] then
  202.         labels[name].shown = state or true
  203.     elseif interactiveLabels[name] then
  204.         interactiveLabels[name].shown = state or true
  205.     end
  206. end
  207.  
  208. hide = function ( name )
  209.     if exists(name) then
  210.         buttons[name].shown = false
  211.     end
  212. end
  213.  
  214. hideAll = function ( name, ... )
  215.     if name then
  216.         for name, data in pairs(buttons) do
  217.             for i=1, #arg + 1 do
  218.                 if name ~= arg[i] then
  219.                     if name:match("[%a%s]+") then
  220.                         data.shown = false
  221.                     end
  222.                 end
  223.             end
  224.         end
  225.     else
  226.         for _, data in pairs(buttons) do
  227.             data.shown = false
  228.         end
  229.     end
  230. end
  231.  
  232. changePosition = function( name, xpos, ypos, width, height )
  233.     if exists(name) then
  234.         if xpos then
  235.             buttons[name].x = xpos
  236.         end
  237.  
  238.         if ypos then
  239.             buttons[name].y = ypos
  240.         end
  241.  
  242.         if width then
  243.             buttons[name].width = width
  244.         end
  245.  
  246.         if height then
  247.             buttons[name].height = height
  248.         end
  249.     end
  250. end
  251.  
  252. changeArrayText = function( arrayname, textTable )
  253.     if type(textTable) ~= "table" then error("Argument 'textTable' must be a table!") end
  254.     for name, data in pairs(buttons) do
  255.         if string.gsub(name, "%d*", "") == arrayname then
  256.             num = string.gsub(name, "%a*", "")
  257.             data.text = textTable[tonumber(num)]
  258.         end
  259.     end
  260. end
  261.  
  262. changeArrayColor = function( arrayname, colorTable )
  263.     for name, data in pairs(buttons) do
  264.         if string.gsub(name, "%d*", "") == arrayname then
  265.             num = string.gsub(name, "%a*", "")
  266.             data.inactiveColor = colorTable[tonumber(num)]
  267.         end
  268.     end
  269. end
  270.  
  271. changeColor = function ( name, activeColor, inactiveColor )
  272.     if exists(name) then
  273.         buttons[name].activeColor    = activeColor
  274.         buttons[name].inactiveColor  = inactiveColor
  275.     end
  276. end
  277.  
  278. changeText = function( name, text )
  279.     if exists(name) then
  280.         buttons[name].text = text
  281.     end
  282. end
  283.  
  284. toggle = function( name )
  285.     if exists( name ) then
  286.         if lock then
  287.             buttons[name].active = true
  288.         else
  289.             buttons[name].active = not buttons[name].active
  290.  
  291.         end
  292.     end
  293. end
  294.  
  295. toggleAll = function ( name, lock )
  296.     name = name:match("[%a%s]+")
  297.     for bName, data in pairs(buttons) do
  298.         if bName:match(name) then
  299.             if lock then
  300.                 data.active = true
  301.             elseif lock == nil then
  302.                 data.active = not data.active
  303.             else
  304.                 data.active = false
  305.             end
  306.         end
  307.     end
  308. end
  309.  
  310. flash = function ( name, time )
  311.     time = time or 0.15
  312.     toggle( name )
  313.     renderButtons()
  314.     sleep( time )
  315.     toggle( name )
  316. end
  317.  
  318. label = function( text, x, y, color, textColor )
  319.     newId                   = #labels + 1
  320.     labels[newId]           = {}
  321.     labels[newId].text      = text
  322.     labels[newId].x         = x
  323.     labels[newId].y         = y
  324.     labels[newId].color     = color     or defaultTBColor
  325.     labels[newId].textColor = textColor or defaultTTColor
  326.     labels[newId].shown     = true
  327. end
  328.  
  329. infoText = function ( text, x, y, bgColor, textColor )
  330.     bgColor     = bgColor   or defaultBTColor
  331.     textColor   = textColor or defaultTTColor
  332.     term.setBackgroundColor(bgColor)
  333.     term.setTextColor(textColor)
  334. end
  335.  
  336. isToggled = function( name )
  337.     if exists(name) then
  338.         return buttons[name].active
  339.     end
  340. end
  341.  
  342. isShown = function( name )
  343.     if exists(name) then
  344.         return buttons[name].shown
  345.     end
  346. end
  347.  
  348. convertFunc = function( func, self, iterator )
  349.     if type(func) == "string" then
  350.         func = func:gsub(":self:", "\""..self.."\"")
  351.         func = func:gsub(":iterator:", iterator)
  352.     end
  353.     return func
  354. end
  355.  
  356. thinHLine = function( x, y, length, color )
  357.     bColor = color or colors.black
  358.     for i = x, length + x do
  359.         screen.color[y][i] = bColor
  360.         screen[y][i] = "_"
  361.     end
  362. end
  363.  
  364. printf = function( x, y, text )
  365.     local i = 0
  366.     for j = x, tostring(text):len() + x - 1 do
  367.         i = i + 1
  368.         screen[y][j] = text:sub(i, i)
  369.     end
  370. end
  371.  
  372. drawLine = function ( xo, yo, xt, yt, color )
  373.     local cColor
  374.     if color then cColor = color else cColor = colors.white end
  375.     screen.color                  = {}
  376.     for i = yo, yt do
  377.         screen.color[i]           = {}
  378.         for j = xo, xt do
  379.             screen[i][j]          = " "
  380.             screen.color[i][j]    = cColor
  381.         end
  382.     end
  383. end
  384.  
  385. setScreen = function()
  386.     for y = 1, h do
  387.         screen[y] = {}
  388.         for x = 1, w do
  389.             screen[y][x] = ""
  390.         end
  391.     end
  392.     return screen
  393. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement