Advertisement
Guest User

reactorGUI.lua

a guest
Sep 21st, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.00 KB | None | 0 0
  1. --imports
  2. local component = require("component")
  3. local event = require("event")
  4.  
  5. --from screen, find gpu, or allocate gpu to screen
  6. local scherm = "d97deff7-86d4-4834-81af-8c2d45db7eb3"
  7. local gpu = nil
  8. local w,h
  9. for address, name in component.list("gpu")
  10. do
  11.   if component.proxy(address).getScreen() == scherm
  12.   then
  13.     gpu = component.proxy(address)
  14.   end
  15. end
  16. if gpu == nil
  17. then
  18.   for address, name in component.list("gpu")
  19.   do
  20.     if component.proxy(address).getScreen() == nil
  21.     then
  22.       component.proxy(address).bind(scherm)
  23.       gpu = component.proxy(address)
  24.     end
  25.   end
  26.  
  27.   if gpu == nil
  28.   then
  29.     for address,name in component.list("gpu") do
  30.       for address2,name2 in component.list("gpu") do
  31.         if (address ~= address2) then
  32.           if component.proxy(address).getScreen() == component.proxy(address2).getScreen() then
  33.             component.proxy(address2).bind(scherm)
  34.             gpu = component.proxy(address2)
  35.           end
  36.         end
  37.       end
  38.     end
  39.   end
  40. end
  41.  
  42. w,h = gpu.getResolution()
  43.  
  44. local function clearScreen()
  45.   gpu.setForeground(0xFFFFFF)
  46.   gpu.setBackground(0x000000)
  47.   gpu.fill(1,1,w,h,' ')
  48.   --erase all components
  49.   components = {}
  50. end
  51.  
  52. components = {}
  53. local componentIdentifier = 1
  54.  
  55. local function addToTable(table,items)
  56.   table[#table + 1] = items
  57. end
  58.  
  59. local function addComponent(typ,param)
  60.   table.insert(param,1,typ)
  61.   table.insert(param,1,componentIdentifier)
  62.   componentIdentifier = componentIdentifier + 1
  63.   addToTable(components,param)
  64. end
  65.  
  66. local function addButton(func,x,y,xtot,ytot)
  67.   addComponent("button",{func,x,y,xtot,ytot})
  68. end
  69.  
  70. local function addLabel(func,x,y,xtot,ytot)
  71.   addComponent("label",{func,x,y,xtot,ytot})
  72. end
  73.  
  74. local function clearComponent(id)
  75.   local index = 0
  76.   for i=1,#components do
  77.     local component = components[i]
  78.     if(component[2] == "button") then
  79.       gpu.fill(component[3],component[4],component[5]-component[3]+1,component[6]-component[4]+1)
  80.     elseif(component[2] == "label") then
  81.       gpu.fill(component[3],component[4],component[5]-component[3]+1,component[6]-component[4]+1)
  82.     end
  83.     if component[1] == id then
  84.       index = i
  85.     end
  86.   end
  87.   if index > 0 then
  88.     table.remove(components,index)
  89.   end
  90. end
  91.  
  92. local function registerButton(func,backgr,foregr,x,y,width,fixedwidth,height,fixedheight,text)
  93.   local bgr = gpu.getBackground()
  94.   local fgr = gpu.getForeground()
  95.  
  96.   gpu.setBackground(backgr)
  97.   gpu.setForeground(foregr)
  98.   local xtot = x
  99.   local ytot = y
  100.   if (type(text) == "table") then
  101.     local i = 0
  102.     local textmaxlength = 0
  103.     --calculate maximum length of text in table
  104.     for j=1,#text do
  105.       if string.len(text[j]) > textmaxlength then
  106.         textmaxlength = string.len(text[j])
  107.       end
  108.     end
  109.  
  110.     if (fixedwidth == true) then
  111.       if (fixedheight == true) then
  112.         gpu.fill(x,y,width,height," ")
  113.         ytot = y + height - 1
  114.       else
  115.         gpu.fill(x,y,width,height*2+#text," ")
  116.         ytot = y + height*2 + #text - 1
  117.       end
  118.       xtot = x + width - 1
  119.     else
  120.       if (fixedheight == true) then
  121.         gpu.fill(x,y,width*2 + textmaxlength,height," ")
  122.         ytot = y + height - 1
  123.       else
  124.         gpu.fill(x,y,width*2 + textmaxlength,height*2+#text," ")
  125.         ytot = y + height*2 + #text - 1
  126.       end
  127.       xtot = x + (width*2 + textmaxlength) - 1
  128.     end
  129.  
  130.     for j=1,#text do
  131.       if(fixedwidth == true) then
  132.         if (fixedheight == true) then
  133.           gpu.set(x + math.floor((width - textmaxlength)/2),y + math.floor((height - #text)/2) - i,text[j])
  134.         else
  135.           gpu.set(x + math.floor((width - textmaxlength)/2),y + height + i,text[j])
  136.         end
  137.       else
  138.         if(fixedheight == true) then
  139.           gpu.set(x + floor(width/2),y + math.floor((height - #text)/2),text[j])
  140.         else
  141.           gpu.set(x + floor(width/2),y + height + i,text[j])
  142.         end
  143.       end
  144.       i = i + 1
  145.     end
  146.   else
  147.     --all on one line
  148.  
  149.     if (fixedwidth == true) then
  150.       if(fixedheight == true) then
  151.         gpu.fill(x,y,width,height," ")
  152.         gpu.set(x + math.floor((width - string.len(text)) / 2),y + math.floor((height - 1)/2),text)
  153.         xtot = x + width - 1
  154.         ytot = y + height - 1
  155.       else
  156.         gpu.fill(x,y,width,height*2+1," ")
  157.         gpu.set(x + math.floor((width - string.len(text)) / 2),y + height,text)
  158.         xtot = x + width - 1
  159.         ytot = y + height*2
  160.       end
  161.     else
  162.       if(fixedheight == true) then
  163.         gpu.fill(x,y,width*2 + string.len(text),height," ")
  164.         gpu.set(x + width,y + math.floor((height - 1)/2),text)
  165.         xtot = x + (width*2 + string.len(text)) - 1
  166.         ytot = y + height - 1
  167.       else
  168.         gpu.fill(x,y,width*2 + string.len(text),height*2 + 1," ")
  169.         gpu.set(x + width,y + height,text)
  170.         xtot = x + (width*2 + string.len(text)) - 1
  171.         ytot = y + height*2
  172.       end
  173.     end
  174.   end
  175.  
  176.   --add button to loop
  177.   addButton(func,x,y,xtot,ytot)
  178.  
  179.   --restore original back and foreground colors
  180.   gpu.setBackground(bgr)
  181.   gpu.setForeground(fgr)
  182. end
  183.  
  184. local stop = false
  185.  
  186. local functions = {}
  187.  
  188. functions["exit"] = function()
  189.   stop = true
  190. end
  191.  
  192. --start of program(in functions)...
  193.  
  194. local loggedin = false
  195. local player = nil
  196.  
  197. local function init()
  198.   functions["login"]()
  199. end
  200.  
  201. functions["login"] = function()
  202.   clearScreen()
  203.   registerButton("logincheck",0x000000,0xFFFFFF,1,1,80,true,25,true,"Please login using your fingerprint...")
  204.   loggedin = false
  205.   player = nil
  206. end
  207.  
  208. functions["logincheck"] = function()
  209.   if player == "DoctaShizzle" or player == "KrazyMack" then
  210.     loggedin = true
  211.     clearScreen()
  212.     --create main screen
  213.     print("creating main screen")
  214.     registerButton("login",0xFF0000,0xFFFFFF,1,1,25,true,1,false,"Logoff " .. player)
  215.     registerButton("exit",0x00FF00,0x0000FF,27,1,1,false,1,false,"Quit")
  216.   else
  217.     clearScreen()
  218.     registerButton("logincheck",0x000000,0xFFFFFF,1,1,80,true,25,true,{"Login Incorrect!","Please retry to login using your fingerprint..."})
  219.   end
  220. end
  221.  
  222. local function redraw()
  223.   --TODO: fill in fixed redraw events here
  224. end
  225.  
  226.  
  227. local function handleEvents(e)
  228.   --TODO: filter and handle received events
  229. end
  230.  
  231.  
  232. --main...
  233. --do initialisation
  234. init()
  235. --start draw and event loop
  236. while stop == false do
  237.   --read events(with a timeout) and act on those
  238.   local events = {}
  239.   local e = 0
  240.   while e ~= nil
  241.   do
  242.     e = {event.pull(0.1)}
  243.     if (e ~= nil) then
  244.       if (#e == 0) then
  245.         e = nil
  246.       else
  247.         addToTable(events,e)
  248.       end
  249.     end
  250.   end
  251.  
  252.   for i=1,#events do
  253.     handleEvents(events[i])
  254.     if events[i][1] == "touch" and events[i][2] == scherm then
  255.       local xco = events[i][3]
  256.       local yco = events[i][4]
  257.       player = events[i][6]
  258.       k = 1
  259.       for k=1,#components do
  260.         if components[k][2] == "button" then
  261.           if (xco >= components[k][4]) and (xco <= components[k][6]) and (yco >= components[k][5]) and (yco <= components[k][7]) then
  262.             --call custom function if it is no nil
  263.             local f = (components[k][3])
  264.             if f ~= nil then
  265.               functions[f]()
  266.             end
  267.             break
  268.           end
  269.         end
  270.       end
  271.     end
  272.     redraw()
  273.   end
  274. end
  275. clearScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement