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 = "e943b65b-af38-4d0d-b3e6-53af346dd1fa"
- 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 = {}
- end
- local components = {}
- local componentIdentifier = 1
- local function addToTable(table,items)
- if type(items) == "table" then
- for i = 1,#items do
- table[#table + 1] = items[i]
- end
- else
- table[#table + 1] = items
- end
- end
- local function addComponent(typ,param)
- table.insert(param,1,typ)
- table.insert(param,1,componentIdentifier)
- componentIdentifier = componentIdentifier + 1
- 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)
- 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
- gpu.fill(x,y,width,ud_edge*2+1," ")
- gpu.set(x + math.floor((width - string.len(text)) / 2),y + ud_edge,text)
- xtot = x + width - 1
- ytot = y + ud_edge*2
- else
- gpu.fill(x,y,width*2 + string.len(text),ud_edge*2+1," ")
- gpu.set(x + width,y + ud_edge,text)
- xtot = x + (width*2 + string.len(text)) - 1
- ytot = y + ud_edge*2
- 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 exit = false
- --start of program(in functions)...
- local loggedin = false
- local player = nil
- local function login()
- print("logging in...")
- if player == "DoctaShizzle" then
- loggedin = true
- print("logged in succesfully, refreshing screen...")
- clearScreen()
- end
- end
- local function init()
- --login screen
- --clear screen and set standard colors
- clearScreen()
- --start writing
- registerButton(nil,0xFF0000,0xFFFFFF,1,1,w,true,h,"Please login using your fingerprint...")
- --print events on log screen
- loggedin = false
- player = nil
- --[[
- while loggedin == false do
- _,_,_,_,_,player = event.pull("touch")
- if (player == "DoctaShizzle") then
- loggedin = true
- end
- --if (loggedin == false) then
- --clearScreen()
- --registerButton(nil,0x000000,0xFFFFFF,30,24,0,false,0,"Login Incorrect!")
- --registerButton(nil,0x000000,0xFFFFFF,30,25,0,false,0,"Please retry to login using your fingerprint...")
- --end
- end
- --]]
- end
- local function redraw()
- --TODO: fill in fixed redraw events here
- end
- --[[
- ::main::
- clearScreen()
- registerButton(0xFF0000,0xFFFFFF,1,1,25,true,1,"Logoff " .. player)
- registerButton(0x00FF00,0x0000FF,27,1,1,false,1,"Quit")
- local x,y
- while loggedin == true do
- _,_,x,y,_,player = event.pull("touch")
- if (y == 1) and (x >= 1) and (x <= 25) then
- loggedin = false
- goto login
- end
- if (y == 1 and (x >= 27) and (x <= 32)) then
- goto quit
- end
- end
- ::quit::
- clearScreen()
- --]]
- local function handleEvents(e)
- --TODO: filter and handle received events
- end
- --main...
- --do initialisation
- init()
- --start draw and event loop
- while exit == false do
- --read events(with a timeout) and act on those
- local e = {event.pull(0.1)}
- handleEvents(e)
- if e[1] == "touch" then
- local xco = e[3]
- local xco = e[4]
- player = e[5]
- for i=1,#components do
- local comp = components[i]
- if components[i][2] == "button" then
- if xco >= components[i][4] and xco <= components[i][6] and yco > components[i][5] and components[i][7] then
- --call custom function if it is no nill
- local f = components[i][3]
- if f ~= nil then
- assert(f .. "()")()
- end
- end
- end
- end
- end
- redraw()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement