My question is simply optimization. I want to know if this programs runs as efficiently as possible.
Anyway here's the pastebin link:
http://pastebin.com/gf9ft4pu
And the code:
Spoiler
--Monitor Setup
local mon = peripheral.wrap("right")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)
--Variable Definition
buttonPush = false
option = 0
--Functions
function home()
shell.run("TitanOS")
end
function setTable(name, func, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = true
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
end
function fill(text, color, bData)
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
if k == xspot then
mon.write(text)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end
function screen()
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then currColor = colors.lime else currColor = colors.red end
fill(name, currColor, data)
end
end
function checkxy(x, y)
for name, data in pairs(button) do
if y>=data["ymin"] and y <= data["ymax"] then
if x>=data["xmin"] and x<= data["xmax"] then
data["func"]()
data["active"] = not data["active"]
print(name)
end
end
end
end
function heading(text,a)
w, h = mon.getSize()
mon.setCursorPos((w-string.len(text))/2+1,a)
mon.write(text)
end
function dummy()
buttonPush = not buttonPush
end
function menu1()
if buttonPush == true then
dummy()
end
button = {}
mon.clear()
sleep(.25)
sleep(.25)
heading("Please follow the On Screen Navigation Below",1)
while buttonPush == false do
setTable("Option1", one, 5, 15, 3, 5)
setTable("Option2", two, 5, 15, 7, 9)
setTable("Option3", three, 5, 15, 11, 13)
screen()
local e, side, x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x, y)
sleep(.1)
end
end
function menu2()
if buttonPush == true do
dummy()
end
button = {}
mon.clear()
sleep(.25)
while buttonPush == false then
heading("Button ",1)
mon.write(option.." was pressed.")
setTable("Back", menu1, 50, 55, 17, 19)
setTable("Home", home, 2, 6, 16, 18)
screen()
local e, side, x, y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x, y)
sleep(.1)
end
end
function one()
dummy()
option = 1
menu2()
end
function two()
dummy()
option = 2
menu2()
end
function three()
dummy()
option = 3
menu2()
end
--ASCII Variables
local Logo = {
[[ _ __ _ ___ ]],
[[ o O O | |/ / ___ _ __ _ _ __ _ __| | ___ / __| ___ ]],
[[ o | ' < / _ \ | ' \ | '_| / _' | / _' | / -_) ( |__ / _ \ ]],
[[ TS__[O] |_|\_\ \___/ |_|_|_| _|_|_ \__,_| \__,_| \___| \___| \___/ ]],
[[{======|__|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""| |"""""|_|"""""|]],
[[/o--000'""'-0-0-'"'-0-0-'"'-0-0-'"'-0-0-'"'-0-0-'"'-0-0-'"'-0-0-'.'-0-0-'"'-0-0-']],
}
--Program
--------------
--Opening Screen
while buttonPush == false do
mon.clear()
mon.setTextScale(1)
term.redirect(mon)
term.clear()
term.setCursorPos(1,1)
for i = 1, #Logo do
print(Logo[i])
end
heading("Welcome to Titan Tower!",12)
heading("A Komrade Co. Enterprise.",13)
term.restore()
setTable("Press to Begin", dummy, 30, 50, 15, 17)
screen()
local e,side,x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x,y)
sleep(.1)
end
menu1()
menu2()
With minor changes and more comments added. Home function modified to reboot computer (program set as startup).
Spoiler
--Monitor Setup
local mon = peripheral.wrap("right") --Monitor Wrap
mon.setTextScale(1)
mon.setTextColor(colors.white)
mon.setBackgroundColor(colors.black)
--Variable Definition
buttonPush = false
option = 0
local button={}
--Functions
function home()
os.reboot() --Program is set as startup
end
function setTable(name, func, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["func"] = func --Function to be called when button is pressed.
button[name]["active"] = true --Defualt active state.
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
end
function fill(text, color, bData) --Centers button text, colors button according to state
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - #text) /2) +1
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - #text +1 do
if k == xspot then
mon.write(text)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end
function screen() --Sets color according to state
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then currColor = colors.lime else currColor = colors.red end
fill(name, currColor, data)
end
end
function checkxy(x, y) --Prints x and y location of touch, calls function from the button.
for name, data in pairs(button) do
if y>=data["ymin"] and y <= data["ymax"] then
if x>=data["xmin"] and x<= data["xmax"] then
data["func"]()
data["active"] = not data["active"]
print(name)
end
end
end
end
function heading(text,a) --Centers given text on given line
w, h = mon.getSize()
mon.setCursorPos((w-string.len(text))/2+1,a)
mon.write(text)
end
function dummy() --Toggles buttonPush and allows exit of the while loop
buttonPush = not buttonPush --without needing a break command.
end
function menu1() --Main Menu containing 3 button options
if buttonPush == true then --Makes sure that every time you call menu1(),
dummy() --the buttons will be drawn.
end
button = {} --Clears the button table so only new buttons will,
mon.clear() --be displayed.
sleep(.25)
heading("Please follow the On Screen Navigation Below",1)
while buttonPush == false do
setTable("Option1", one, 5, 15, 3, 5)
setTable("Option2", two, 5, 15, 7, 9)
setTable("Option3", three, 5, 15, 11, 13)
screen()
local e, side, x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x, y) --Calls function from selected button
sleep(.1)
end
end
function menu2() --New menu telling you which button was pressed.
if buttonPush == true do --Makes sure buttons will always be drawn
dummy()
end
button = {} --Resetting button table again.
mon.clear()
sleep(.25)
while buttonPush == false then
heading("Button ",1) --Tells you which button was
mon.write(option.." was pressed.") --pressed.
setTable("Back", menu1, 50, 55, 17, 19)
setTable("Home", home, 2, 6, 16, 18)
screen()
local e, side, x, y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x, y)
sleep(.1)
end
end
function one() --Toggles buttonPush and sets option.
dummy()
option = 1
menu2()
end
function two() --Same as one
dummy()
option = 2
menu2()
end
function three() --Same as one and two
dummy()
option = 3
menu2()
end
--ASCII Variables
local Logo = {
[[ _ __ _ ___ ]],
[[ o O O | |/ / ___ _ __ _ _ __ _ __| | ___ / __| ___ ]],
[[ o | ' < / _ \ | ' \ | '_| / _' | / _' | / -_) | (__ / _ \ ]],
[[ TS__[O] |_|\_\ \___/ |_|_|_| _|_|_ \__,_| \__,_| \___| \___| \___/ ]],
[[{======|__|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""| |"""""|_|"""""|]],
[[/o--000'""'-0-0-'"'-0-0-'"'-0-0-'"'-0-0-'"'-0-0-'"'-0-0-'"'-0-0-'.'-0-0-'"'-0-0-']],
}
--Program
--Home Screen
while buttonPush == false do
mon.clear()
mon.setTextScale(1)
term.redirect(mon)
term.clear()
term.setCursorPos(1,1)
for i = 1, #Logo do --Printing ASCII Logo
print(Logo[i])
end
heading("Welcome to Titan Tower!",12)
heading("A Komrade Co. Enterprise.",13)
term.restore()
setTable("Press to Begin", dummy, 30, 50, 15, 17)
screen()
local e,side,x,y = os.pullEvent("monitor_touch")
print(x..":"..y)
checkxy(x,y)
sleep(.1)
end
menu1()
menu2()
Note: Original touchsreen button code is from Direwolf20. I've modified it to fit my needs, but the original code is not mine.