Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --imports
- local component = require("component")
- local event = require("event")
- --from screen, find gpu, or allocate gpu to screen
- local scherm = "d97deff7-86d4-4834-81af-8c2d45db7eb3"
- local gpu = nil
- local w,h
- for address, name in component.list("gpu")
- do
- if component.proxy(address).getScreen() == scherm
- then
- gpu = component.proxy(address)
- end
- end
- if gpu == nil
- then
- for address, name in component.list("gpu")
- do
- if component.proxy(address).getScreen() == nil
- then
- component.proxy(address).bind(scherm)
- gpu = component.proxy(address)
- end
- end
- if gpu == nil
- then
- for address,name in component.list("gpu") do
- for address2,name2 in component.list("gpu") do
- if (address ~= address2) then
- if component.proxy(address).getScreen() == component.proxy(address2).getScreen() then
- component.proxy(address2).bind(scherm)
- gpu = component.proxy(address2)
- end
- end
- end
- end
- end
- end
- w,h = gpu.getResolution()
- local function clearScreen()
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- gpu.fill(1,1,w,h,' ')
- --erase all components
- components = {}
- print("components: " .. #components)
- end
- components = {}
- local componentIdentifier = 1
- local function addToTable(table,items)
- table[#table + 1] = items
- end
- local function addComponent(typ,param)
- table.insert(param,1,typ)
- table.insert(param,1,componentIdentifier)
- componentIdentifier = componentIdentifier + 1
- addToTable(components,param)
- end
- local function addButton(func,x,y,xtot,ytot)
- addComponent("button",{func,x,y,xtot,ytot})
- end
- local function clearComponent(id)
- local index = 0
- for i=1,#components do
- local component = components[i]
- if(component[2] == "button") then
- gpu.fill(component[3],component[4],component[5]-component[3]+1,component[6]-component[4]+1)
- end
- if component[1] == id then
- index = i
- end
- end
- if index > 0 then
- table.remove(components,index)
- end
- end
- local function registerButton(func,backgr,foregr,x,y,width,fixedwidth,height,fixedheight,text)
- print("registring buttons, components: " .. #components)
- local bgr = gpu.getBackground()
- local fgr = gpu.getForeground()
- gpu.setBackground(backgr)
- gpu.setForeground(foregr)
- local xtot = x
- local ytot = y
- if (type(text) == "table") then
- local i = 0
- local textmaxlength = 0
- --calculate maximum length of text in table
- for j=1,#text do
- if string.len(text[j]) > textmaxlength then
- textmaxlength = string.len(text[j])
- end
- end
- if (fixedwidth == true) then
- if (fixedheight == true) then
- gpu.fill(x,y,width,height," ")
- ytot = y + height - 1
- else
- gpu.fill(x,y,width,height*2+#text," ")
- ytot = y + height*2 + #text - 1
- end
- xtot = x + width - 1
- else
- if (fixedheight == true) then
- gpu.fill(x,y,width*2 + textmaxlength,height," ")
- ytot = y + height - 1
- else
- gpu.fill(x,y,width*2 + textmaxlength,height*2+#text," ")
- ytot = y + height*2 + #text - 1
- end
- xtot = x + (width*2 + textmaxlength) - 1
- end
- for j=1,#text do
- if(fixedwidth == true) then
- if (fixedheight == true) then
- gpu.set(x + math.floor((width - textmaxlength)/2),y + math.floor((height - #text)/2) - i,text[j])
- else
- gpu.set(x + math.floor((width - textmaxlength)/2),y + height + i,text[j])
- end
- else
- if(fixedheight == true) then
- gpu.set(x + floor(width/2),y + math.floor((height - #text)/2),text[j])
- else
- gpu.set(x + floor(width/2),y + height + i,text[j])
- end
- end
- i = i + 1
- end
- else
- --all on one line
- if (fixedwidth == true) then
- if(fixedheight == true) then
- gpu.fill(x,y,width,height," ")
- gpu.set(x + math.floor((width - string.len(text)) / 2),y + math.floor((height - 1)/2),text)
- xtot = x + width - 1
- ytot = y + height - 1
- else
- gpu.fill(x,y,width,height*2+1," ")
- gpu.set(x + math.floor((width - string.len(text)) / 2),y + height,text)
- xtot = x + width - 1
- ytot = y + height*2
- end
- else
- if(fixedheight == true) then
- gpu.fill(x,y,width*2 + string.len(text),height," ")
- gpu.set(x + width,y + math.floor((height - 1)/2),text)
- xtot = x + (width*2 + string.len(text)) - 1
- ytot = y + height - 1
- else
- gpu.fill(x,y,width*2 + string.len(text),height*2 + 1," ")
- gpu.set(x + width,y + height,text)
- xtot = x + (width*2 + string.len(text)) - 1
- ytot = y + height*2
- end
- end
- end
- --add button to loop
- addButton(func,x,y,xtot,ytot)
- --restore original back and foreground colors
- gpu.setBackground(bgr)
- gpu.setForeground(fgr)
- end
- local stop = false
- local functions = {}
- functions["exit"] = function()
- stop = true
- end
- --start of program(in functions)...
- local loggedin = false
- local player = nil
- local function init()
- functions["login"]()
- end
- functions["login"] = function()
- clearScreen()
- registerButton("logincheck",0x000000,0xFFFFFF,1,1,80,true,25,true,"Please login using your fingerprint...")
- loggedin = false
- player = nil
- end
- functions["logincheck"] = function()
- if player == "DoctaShizzle" or player == "KrazyMack" then
- loggedin = true
- clearScreen()
- --create main screen
- print("creating main screen")
- registerButton("login",0xFF0000,0xFFFFFF,1,1,25,true,1,false,"Logoff " .. player)
- registerButton("exit",0x00FF00,0x0000FF,27,1,1,false,1,false,"Quit")
- else
- clearScreen()
- registerButton("logincheck",0x000000,0xFFFFFF,1,1,80,true,25,true,{"Login Incorrect!","Please retry to login using your fingerprint..."})
- end
- end
- local function redraw()
- --TODO: fill in fixed redraw events here
- end
- local function handleEvents(e)
- --TODO: filter and handle received events
- end
- --main...
- --do initialisation
- init()
- --start draw and event loop
- while stop == false do
- --read events(with a timeout) and act on those
- local events = {}
- local e = 0
- while e ~= nil
- do
- e = {event.pull(0.1)}
- if (e ~= nil) then
- if (#e == 0) then
- e = nil
- else
- addToTable(events,e)
- end
- end
- end
- if #events > 0 then
- print(#events .. " number of events received. " .. "components: " .. #components)
- end
- for i=1,#events do
- handleEvents(events[i])
- if events[i][1] == "touch" and events[i][2] == scherm then
- local xco = events[i][3]
- local yco = events[i][4]
- player = events[i][6]
- k = 1
- for k=1,#components do
- print("#components: " .. #components .. " index: " .. k)
- print(components[k])
- if components[k][2] == "button" then
- if (xco >= components[k][4]) and (xco <= components[k][6]) and (yco >= components[k][5]) and (yco <= components[k][7]) then
- --call custom function if it is no nil
- local f = (components[k][3])
- if f ~= nil then
- print("touch received, calling function " .. f)
- functions[f]()
- end
- break
- end
- end
- end
- end
- redraw()
- end
- end
- clearScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement