Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- (c) ProgramCrafter
- -- Program that creates and shows different banners.
- local s = require('serialization')
- local c = require('component')
- local u = require('unicode')
- local e = require('event')
- local args = {...}
- local banner_path = args[1] or '/tmp/last_banner.dat'
- -- configured for OCEmu
- local arrows_behavior = {
- [200] = {0, -1}, [203] = {-1, 0},
- [205] = {1, 0}, [208] = {0, 1}
- }
- local colors = {
- 0x0000FF, 0x00FFFF, 0x00FF00, 0xFFFF00, 0xFF0000, 0xFF00FF,
- 0x000000, 0x333333, 0x666666, 0x999999, 0xAAAAAA, 0xFFFFFF
- }
- local screen_bcol = 7
- local gpu = c.gpu
- local w,h = gpu.getResolution()
- if args[2] and tonumber(args[2]) then w = tonumber(args[2]) end
- if args[3] and tonumber(args[3]) then h = tonumber(args[3]) end
- gpu.setResolution(w, h)
- local function changeColors(fcol, bcol)
- return gpu.setForeground(fcol), gpu.setBackground(bcol)
- end
- local function drawText(x, y, fcol, bcol, txt)
- fcol, bcol = changeColors(fcol, bcol)
- gpu.set(x, y, txt)
- changeColors(fcol, bcol)
- end
- local function clearScreen()
- gpu.fill(1, 1, w, h, ' ')
- end
- local messages = {back = {x=w+1,y=h+1,text='',backcolor=screen_bcol}}
- local selection = nil
- local editing = false
- messages.back.w = w
- messages.back.h = h
- local function msgIntersectXY(msg, x,y)
- return x >= msg.x and x < msg.x + msg.w and y >= msg.y and y < msg.y + msg.h
- end
- local function msgUnderXY(x,y)
- for k,v in pairs(messages) do
- if msgIntersectXY(v, x, y) and k ~= 'back' then return v end
- end
- end
- local function drawMessage(msg)
- drawText(msg.x, msg.y, colors[msg.fcol or 1], colors[msg.bcol or 1], msg.text)
- end
- local function drawMessages()
- for _,v in pairs(messages) do
- if type(v) == 'table' then drawMessage(v) end
- end
- end
- local function removeMessage(msg)
- for k,v in pairs(messages) do
- if v == selection and type(k) == 'number' and k > 0 then
- table.remove(messages, k)
- elseif v == selection then
- messages[k] = nil
- end
- end
- end
- local function loadMessages()
- local f = io.open(banner_path, 'r')
- if not f then return end
- messages = s.unserialize(f:read())
- f:close()
- gpu.setBackground(colors[messages.back.backcolor or 1])
- end
- local function dumpMessages()
- local tmp_msg = messages[-1]
- messages[-1] = nil
- local f = io.open(banner_path, 'w')
- if not f then return end
- f:write(s.serialize(messages))
- f:close()
- messages[-1] = tmp_msg
- end
- local admin_nick = nil
- drawText(3, h, 0xFF9999, 0, 'WAITING FOR ADMIN')
- admin_nick = ( {e.pull("touch")} )[6]
- drawText(3, h, 0xFF9999, 0, 'ADMIN FOUND ')
- os.sleep(1)
- clearScreen()
- -- print(messages.back.w)
- -- io.read()
- -- clearScreen()
- loadMessages()
- clearScreen()
- drawMessages()
- while true do
- local e = {e.pull()}
- if e[1] == 'touch' and e[6] == admin_nick then
- local x,y = e[3], e[4]
- if e[5] == 1 and editing then
- messages[-1] = nil
- editing = false
- if selection then
- selection.bcol = selection.obcol
- selection = nil
- end
- elseif e[5] == 1 then
- messages[-1] = {x=3, y=h, w=13, h=1, fcol=9, bcol=7, text='-- Editing --'}
- editing = true
- elseif editing then
- if selection then selection.bcol = selection.obcol end
- selection = msgUnderXY(x,y)
- if not selection then
- selection = {x=x, y=y, w=3, h=1, text='Abc', fcol=#colors, bcol=screen_bcol}
- table.insert(messages, selection)
- end
- selection.obcol = selection.bcol
- selection.bcol = 8
- end
- elseif e[1] == 'key_up' and e[5] == admin_nick and editing then
- if selection then
- local txt = selection.text
- local t_len = u.len(txt)
- if e[3] == 8 and t_len then -- backspace
- selection.text = u.sub(txt, 1, t_len - 1)
- selection.w = u.len(selection.text)
- elseif e[4] == 211 then
- removeMessage(selection)
- selection = nil
- elseif e[3] == 9 then
- if selection == messages[-1] then
- local bcol = screen_bcol
- bcol = bcol + 1
- while bcol > #colors do bcol = bcol - #colors end
- for k,v in pairs(messages) do
- if type(v) == 'table' then
- v.bcol = bcol
- v.obcol = bcol
- end
- end
- selection.bcol = 8
- screen_bcol = bcol
- messages.back.backcolor = bcol
- gpu.setBackground(colors[bcol])
- else
- local fcol = selection.fcol
- fcol = fcol + 1
- while fcol > #colors do fcol = fcol - #colors end
- selection.fcol = fcol
- end
- elseif arrows_behavior[e[4]] then
- local cx, cy = selection.x, selection.y
- local move = arrows_behavior[e[4]]
- local nx, ny = cx + move[1], cy + move[2]
- local posValid = true
- if nx + selection.w - 1 > w then posValid = false end
- if ny + selection.h - 1 > h then posValid = false end
- if nx < 1 then posValid = false end
- if ny < 1 then posValid = false end
- for tx = nx, nx + selection.w - 1 do
- for ty = ny, ny + selection.h - 1 do
- local m = msgUnderXY(tx, ty)
- if m and m ~= selection then posValid = false break end
- end
- if not posValid then break end
- end
- if posValid then selection.x, selection.y = nx, ny end
- elseif e[3] ~= 0 then
- selection.text = txt .. string.gsub(u.char(e[3]), '%c', '')
- selection.w = u.len(selection.text)
- end
- elseif e[3] == 9 then
- local bcol = screen_bcol
- bcol = bcol + 1
- while bcol > #colors do bcol = bcol - #colors end
- for k,v in pairs(messages) do
- if type(v) == 'table' then
- v.bcol = bcol
- v.obcol = bcol
- end
- end
- screen_bcol = bcol
- messages.back.backcolor = bcol
- gpu.setBackground(colors[bcol])
- end
- elseif e[1] == 'interrupted' then break end
- clearScreen()
- drawMessages()
- dumpMessages()
- end
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0)
- gpu.setResolution(40, 16)
- clearScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement