Sample Scripts (Control Unit Interaction)
These scripts show some simple examples of Control Units interacting with screen units.
Light Control (Screen to Programming Board)
This example demonstrates clickable buttons on a screen unit, that set the screens output to a value, the programming board reads that value and then clears it and toggles lights based on that data. (See the top section for the programming board code)
--[[----------------------------------------------------------------------------
-- Add the following section to a programming boards System.Update()
-- Link the screen unit and three lights to the programming board
-- Name the slots in the programming board respectively: screen, light0, light1, light2
local handlers = {}
handlers['Light 0'] = function() light0.toggle() end
handlers['Light 1'] = function() light1.toggle() end
handlers['Light 2'] = function() light2.toggle() end
local output = screen.getScriptOutput()
if #output > 0 then
screen.clearScriptOutput()
if handlers[output] then handlers[output]() end
end
--]]----------------------------------------------------------------------------
-- Screen render script below
--------------------------------------------------------------------------------
font = loadFont('Play-Bold', 32)
rx, ry = getResolution()
cx, cy = getCursor()
layer = createLayer()
click = getCursorPressed()
--------------------------------------------------------------------------------
if not Button then
local mt = {}
mt.__index = mt
function Button (text, x, y)
return setmetatable({
text = text,
x = x,
y = y,
}, mt)
end
function mt:draw ()
local sx, sy = self:getSize()
local x0 = self.x - sx/2
local y0 = self.y - sy/2
local x1 = x0 + sx
local y1 = y0 + sy
local r, g, b = 0.3, 0.7, 1.0
if cx >= x0 and cx <= x1 and cy >= y0 and cy <= y1 then
r, g, b = 1.0, 0.0, 0.4
if click then setOutput(self.text) end
end
setNextShadow(layer, 64, r, g, b, 0.3)
setNextFillColor(layer, 0.1, 0.1, 0.1, 1)
setNextStrokeColor(layer, r, g, b, 1)
setNextStrokeWidth(layer, 2)
addBoxRounded(layer, self.x - sx/2, self.y - sy/2, sx, sy, 4)
setNextFillColor(layer, 1, 1, 1, 1)
setNextTextAlign(layer, AlignH_Center, AlignV_Middle)
addText(layer, font, self.text, self.x, self.y)
end
function mt:getSize ()
local sx, sy = getTextBounds(font, self.text)
return sx + 32, sy + 16
end
function mt:setPos (x, y)
self.x, self.y = x, y
end
end
function drawFree (elems)
for i, v in ipairs(elems) do v:draw() end
end
function drawListV (elems, x, y)
for i, v in ipairs(elems) do
local sx, sy = v:getSize()
v:setPos(x, y)
v:draw()
y = y + sy + 4
end
end
function drawUsage ()
local font = loadFont('FiraMono', 16)
setNextTextAlign(layer, AlignH_Center, AlignV_Top)
addText(layer, font, "Activate board to enable buttons!", rx/2, ry - 32)
end
function drawCursor ()
if cx < 0 then return end
addLine(layer, cx - 12, cy - 12, cx + 12, cy + 12)
addLine(layer, cx + 12, cy - 12, cx - 12, cy + 12)
end
--------------------------------------------------------------------------------
local buttons = {
Button('Light 0', 128, 90),
Button('Light 1', 128, 290),
Button('Light 2', 128, 490),
}
drawFree(buttons)
drawUsage()
drawCursor()
requestAnimationFrame(5)
Light Control (Screen to Programming Board)
This allows you to create and use toggle buttons for boolean values, the examples flip not only their own state, but also the horizontally neighbouring toggle.
--[[----------------------------------------------------------------------------
-- Add to programming board's unit.start() and connect it to this screen.
local messages = {
"Hi, I am a message!",
"Yes hello this is Lua Screen.",
"We love Pops the Hamster!",
"WARNING: No warnings found.",
"I am a screen unit.",
"Are you enjoying this?",
"Pending Screen Operations",
"Lua Screen Units o/",
"It is NOT NQ-Node\'s fault.",
"Knock knock...",
"Who's there?",
"Ran out of Knock knock jokes.",
"It is all NQ-Deckard\'s fault."
}
local message = messages[math.random(1, #messages)]
local params = {
message = message,
testNumber = 1.337,
testStr = 'hello I am a string',
testTable = {x = 1, y = 0, k = {1, 2, 3, 4}},
}
screen.setScriptInput(json.encode(params))
unit.exit()
--]]----------------------------------------------------------------------------
-- Screen render script below
--------------------------------------------------------------------------------
local json = require('dkjson')
local params = json.decode(getInput()) or {}
message = params.message or '[ no message ]'
fontSize = params.fontSize or 64
color = params.color or {r=1.0,g=0,b=0.3}
--------------------------------------------------------------------------------
local font = loadFont('Play-Bold', fontSize)
local rx, ry = getResolution()
local layer = createLayer()
local cx, cy = getCursor()
local sx, sy = getTextBounds(font, message)
setDefaultStrokeColor(layer, Shape_Line, 1, 1, 1, 0.5)
setNextShadow(layer, 64, color.r, color.g, color.b, 0.4)
setNextFillColor(layer, color.r, color.g, color.b, 1.0)
addBoxRounded(layer, (rx-sx-16)/2, (ry-sy-16)/2, sx+16, sy+16, 8)
setNextFillColor(layer, 0, 0, 0, 1)
setNextTextAlign(layer, AlignH_Center, AlignV_Middle)
addText(layer, font, message, rx/2,ry/2)
--------------------------------------------------------------------------------
local fontCache = {}
function getFont (font, size)
local k = font .. '_' .. tostring(size)
if not fontCache[k] then fontCache[k] = loadFont(font, size) end
return fontCache[k]
end
function drawUsage ()
local font = getFont('FiraMono', 16)
setNextTextAlign(layer, AlignH_Center, AlignV_Top)
addText(layer, font, "Activate for an exciting new message!", rx/2, ry - 32)
end
function drawCursor ()
if cx < 0 then return end
if getCursorDown() then
setDefaultShadow(layer, Shape_Line, 32, color.r, color.g, color.b, 0.5)
end
addLine(layer, cx - 12, cy - 12, cx + 12, cy + 12)
addLine(layer, cx + 12, cy - 12, cx - 12, cy + 12)
end
function prettyStr (x)
if type(x) == 'table' then
local elems = {}
for k, v in pairs(x) do
table.insert(elems, string.format('%s = %s', prettyStr(k), prettyStr(v)))
end
return string.format('{%s}', table.concat(elems, ', '))
else
return tostring(x)
end
end
function drawParams ()
local font = getFont('RobotoMono', 11)
setNextTextAlign(layer, AlignH_Left, AlignV_Bottom)
addText(layer, font, "Script Parameters", 16, 16)
local y = 32
for k, v in pairs(params) do
setNextTextAlign(layer, AlignH_Left, AlignV_Bottom)
addText(layer, font, k .. ' = ' .. prettyStr(v), 24, y)
y = y + 12
end
end
--------------------------------------------------------------------------------
drawUsage()
drawCursor()
drawParams()
requestAnimationFrame(1)