Advertisement
Guest User

reactorControl.lua

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