LUA Welcome Screen

--[[

welcome screen for the factory

]]--


-- Locals
local running = true
local mon = ""
local buttons = {}
local window = {}
local outBundleSide = "left"

function initMon(side)
 mon = peripheral.wrap(side)
 mon.setTextScale(1)
 mon.setBackgroundColor(colors.black)
 mon.setTextColor(colors.white)
 window["o"] = {}
 window["o"]["x"] = 1
 window["o"]["y"] = 1
 window["w"], window["h"] = mon.getSize()
end

function updateButtonCalc( button )
 button["tlen"] = string.len( button["text"] )
 button["height"] = button["pady"] * 2 + 1
 button["width"] = button["padx"] * 2 + button["tlen"]
 button["xend"] = button["origx"] + button["width"] - 1
 button["yend"] = button["origy"] + button["height"] - 1
 button["tX"] = button["origx"] + (math.floor( button["width"] / 2 ) - math.floor( button["tlen"] / 2 )) - 1 + button["padx"]
 button["tY"] = button["origy"] + math.floor( button["height"] / 2 )
end

function addButton( name, text, func, state, origx, origy, padx, pady, fore, bact, binact )
 buttons[name] = {}
 buttons[name]["text"] = text
 buttons[name]["state"] = state
 buttons[name]["defstate"] = state
 buttons[name]["func"] = func
 buttons[name]["origx"] = origx
 buttons[name]["origy"] = origy
 buttons[name]["padx"] = padx
 buttons[name]["pady"] = pady
 buttons[name]["foreground"] = fore
 buttons[name]["bact"] = bact
 buttons[name]["binact"] = binact
 
 updateButtonCalc(buttons[name])
end

function addSimpleButton( name, text, func, origx, origy )
 addButton( name, text, func, false, origx, origy, 1, 1, colors.white, colors.lime, colors.red )
end

function drawButton( bData )
 local tLen = bData["tlen"]
 local h = bData["height"]
 local w = bData["width"]
 local xend = bData["xend"]
 local yend = bData["yend"]
 local tY = bData["tY"]
 local tX = bData["tX"]

 if bData["state"] == true then
 mon.setBackgroundColor( bData["bact"] )
 else
 mon.setBackgroundColor( bData["binact"] )
 end
 mon.setTextColor( bData["foreground"] )

 for i = bData["origy"], yend do
 mon.setCursorPos( bData["origx"], i )
 if i == tY then
 for j = bData["origx"], (xend - tLen + 1) do
 if j == tX then
 mon.write(bData["text"])
 else
 mon.write(" ")
 end
 end
 else
 for i = bData["origx"], xend do
 mon.write(" ")
 end
 end
 end
 mon.setBackgroundColor( colors.black )
end

function drawButtons()
 for name,data in pairs(buttons) do
 drawButton( data )
 end
end

function heading( text )
 w, h = mon.getSize()
 mon.setCursorPos( (w-string.len(text))/2+1, 1 )
 mon.write( text )
end

function justifiedPair( row, indent, k, v )
 w = window["w"]
 h = window["h"]
 plen = w - (indent * 2)
 local dots = plen - (string.len(k) + string.len(v))
 str = k

 if dots < 1 then
 dots = plen - string.len(k)
 for i=0, dots do
 str = str.."."
 end
 mon.setCursorPos( indent, row )
 mon.write(str)
 dots = plen - string.len(v)
 str = ""
 for i=0, dots do
 str = str.."."
 end
 str = str..v
 mon.setCursorPos( indent, row+1 )
 mon.write(str)
 else
 for i = 0, dots do
 str = str.."."
 end
 
 str = str..v
 mon.setCursorPos( indent, row )
 mon.write(str)
 end
end

function drawScreen( )
 mon.clear()
 heading("Welcome to AGA Industries")
 drawButtons()

 justifiedPair( 3, 1, "Floor 1", "Sorting Facility" )
 justifiedPair( 5, 1, "Floor 2", "Fabriciation" )
 justifiedPair( 7, 1, "Floor 3", "Power Plant" )
 justifiedPair( 9, 1, "Floor 4", "Liquid Processing" )
 justifiedPair(11, 1, "Floor 5", "empty" )
 justifiedPair(13, 1, "Floor 6", "empty" )
end

function toggleButton(name)
 buttons[name]["state"] = not buttons[name]["state"]
end

--[[ for alignment, horiz and vert can have 3 states:
 0 no change
 1 left/top
 2 right/bottom
 3 center
]]--

function alignButtonInBox( button, box, horiz, vert )
 local x = box["o"]["x"]
 local y = box["o"]["y"]
 
 if horiz > 0 then
 if horiz == 1 then
 button["origx"] = x
 elseif horiz == 2 then
 x = (x + box["w"] - 1) - button["width"]
 button["origx"] = x
 elseif horiz == 3 then
 x = x + (box["w"] / 2)
 button["origx"] = x
 end
 end

 if vert > 0 then
 if vert == 1 then
 button["origy"] = y
 elseif vert == 2 then
 y = (y + box["h"] - 1) - button["height"]
 button["origy"] = y
 elseif vert == 3 then
 y = y + (box["h"] / 2)
 button["origy"] = y
 end
 end

 updateButtonCalc( button )

end

function powerDown()
 print("shutting down...")
 running = false
end

function checkButtonHit( x, y )
 for name, data in pairs(buttons) do
 if y >= data["origy"] and y <= data["yend"] then
 if x >= data["origx"] and x <= data["xend"] then
 data["func"](name)
 end
 end
 end
end

function shutdownButtons()
 for name, data in pairs(buttons) do
 if data["state"] ~= data["defstate"] then
 data["func"](name)
 end
 end
end

-- current mon is 50x19

initMon("back")

while running == true do
 drawScreen()
 local e, side, x, y = os.pullEvent("monitor_touch")
 checkButtonHit( x, y )
 sleep( .1 )
end

shutdownButtons()
mon.clear()

About

Agathezol is the leader of the Lost Dorks guild, a father, programmer, and gamer.

Categories: Minecraft