Advertisement
Guest User

reactorGUI.lua

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