Posted 22 June 2012 - 11:06 AM
This is a simple prompt software that allows you to prompt the user for answers to questions spread over 3 lines.
It displays a nice and simple GUI, and allows for 3 different types of prompts - A simple display prompt, a question with text answer prompt, and a choice prompt with up to 6 items the user can choose from.
To use it, call the prompt function, and the arguments are:
- title - is the title displayed in the GUI for the prompt, appearing at the top of the screen
- type - is the type of the prompt (can either be "display" for displaying messages, "text" for getting text input, or "choice" to ask the user a question with multiple, selectable answers)
- q1, q2, q3 - are the 3 lines of question text provided
- … - is the list of choices to display, if you chose the "choice" type of prompt, otherwise just forget about putting anything there
It displays a nice and simple GUI, and allows for 3 different types of prompts - A simple display prompt, a question with text answer prompt, and a choice prompt with up to 6 items the user can choose from.
To use it, call the prompt function, and the arguments are:
- title - is the title displayed in the GUI for the prompt, appearing at the top of the screen
- type - is the type of the prompt (can either be "display" for displaying messages, "text" for getting text input, or "choice" to ask the user a question with multiple, selectable answers)
- q1, q2, q3 - are the 3 lines of question text provided
- … - is the list of choices to display, if you chose the "choice" type of prompt, otherwise just forget about putting anything there
-- ----------START PROMPT SOFTWARE----------
--Used to get and set the current selection
curSel = 0
function getCurSel() return curSel end
function setCurSel(new) curSel = new end
function center(string) return math.floor(25 - string.len(string)/2) end
--Used to select an option
alignment = "center"
function select(dir, ...)
local opt = {...}
local st = opt[getCurSel() + 1]
--Remove currently selected arrows
if alignment == "center" then
term.setCursorPos(center(st) - 1, getCurSel() + 10)
write(" ")
term.setCursorPos(center(st) + string.len(st), getCurSel() + 10)
write(" ")
elseif alignment == "left" then
term.setCursorPos(4, getCurSel() + 10)
write(" ")
term.setCursorPos(5 + string.len(st), getCurSel() + 10)
write(" ")
end
--Get next object number to select
if dir == "down" then
setCurSel(getCurSel() + 1)
if getCurSel() > table.getn(opt) - 1 then
setCurSel(table.getn(opt) - 1)
end
elseif dir == "up" then
setCurSel(getCurSel() - 1)
if getCurSel() < 0 then
setCurSel(0)
end
end
st = opt[getCurSel() + 1]
--Add arrows
if alignment == "center" then
term.setCursorPos(center(st) - 1, getCurSel() + 10)
write("[")
term.setCursorPos(center(st) + string.len(st), getCurSel() + 10)
write("]")
elseif alignment == "left" then
term.setCursorPos(4, getCurSel() + 10)
write("[")
term.setCursorPos(5 + string.len(st), getCurSel() + 10)
write("]")
end
end
--Title is the title of the selection panel
--q1, q1, q3 are the question lines
--o1, o2 are the options
--Type is prompt type out of:
-- Text - text
-- Multiple Choice - choice
-- Display - display
--Returns the selected option
function prompt(title, type, q1, q2, q3, ...)
term.clear()
--Configure questions
if string.len(q1) == 0 then
q1 = " "
elseif string.len(q2) == 0 then
q2 = " "
elseif string.len(q3) == 0 then
q3 = " "
end
--Draw top/bottom
term.setCursorPos(2, 2)
write("+----------------------------------------------+")
term.setCursorPos(2, 3)
write("|")
local tloc = center(title)
if alignment == "left" then
tloc = 4
end
term.setCursorPos(tloc, 3)
write(title)
term.setCursorPos(49, 3)
write("|")
term.setCursorPos(2, 4)
write("+----------------------------------------------+")
term.setCursorPos(2, 17)
write("+----------------------------------------------+")
--Sides
local i = 5
while i < 17 do
term.setCursorPos(2, i)
write("|")
term.setCursorPos(49, i)
write("|")
i = i + 1
end
--Write questions
if alignment == "center" then
term.setCursorPos(center(q1), 6)
write(q1)
term.setCursorPos(center(q2), 7)
write(q2)
term.setCursorPos(center(q3), 8)
write(q3)
elseif alignment == "left" then
term.setCursorPos(4, 6)
write(q1)
term.setCursorPos(4, 7)
write(q2)
term.setCursorPos(4, 8)
write(q3)
end
--Depending on prompt type
if type == "choice" then
--Write options
local opt = {...}
--Configure options
for i = 1, table.getn(opt) do
if string.len(opt[i]) == 0 then
opt[i] = " "
end
end
--Write
i = 1
while i < table.getn(opt) + 1 do
term.setCursorPos(center(opt[i]), i + 9)
print(opt[i])
i = i + 1
end
--Select current option
select("none", unpack(opt))
--Keys Pressed
local ret = ""
while ret == "" do
event, key = os.pullEvent("key")
if key == 200 then
--Up
select("up", unpack(opt))
elseif key == 208 then
--Down
select("down", unpack(opt))
elseif key == 28 then
--Enter
term.setCursorPos(1, 1)
term.clear()
ret = opt[getCurSel() + 1]
end
end
return ret
elseif type == "text" then
--Start text input
term.setCursorPos(4, 11)
local answer = read()
--Clear
term.setCursorPos(1, 1)
term.clear()
return answer
elseif type == "display" then
--Do nothing
else
local err = "Unrecognised Prompt Type!"
term.setCursorPos(center(err), 11)
write(err)
end
end
-- ----------END PROMPT SOFTWARE----------