This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GravityScore's profile picture

Simple Prompt Software

Started by GravityScore, 22 June 2012 - 09:06 AM
GravityScore #1
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


-- ----------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----------
libraryaddict #2
Posted 23 June 2012 - 12:48 PM
Hmm, No offense but my menu API could easily do this.

You could work the API into your code :P/>/>
Cloudy #3
Posted 23 June 2012 - 01:02 PM
And? Just because you have an API which does something does not mean their accomplishment is any less.

Nice work OP :-)
libraryaddict #4
Posted 24 June 2012 - 01:47 PM
And? Just because you have an API which does something does not mean their accomplishment is any less.

Nice work OP :-)

Aie.
I know I came across as a jerk.

What I meant to say in the other post but somehow didn't say.
You should look at turning prompts into a table.
Unlimited prompts :P/>/>