text = {} -- this will store the text you type
buttons = {} -- this will store all of your buttons (dont worry about it)
textBoxX = 1
textBoxY = 5
function addButton(text,x1,y1)
buttons[text] = {pos1 = {x = x1,y = y1},pos2 = {x = string.len(text)+x1,y = y1}}
end
function drawButtons()
for k,v in pairs(buttons) do
term.setCursorPos(buttons[k].pos1.x,buttons[k].pos1.y)
term.write(k)
end
end
-- Here you can add your buttons
addButton("Iron Ingot",10,6)
addButton("Gold Ingot",10,7)
while true do
event, keyNum, x, y = os.pullEvent()
term.setCursorPos(textBoxX,textBoxY)
term.clear()
drawButtons()
if (event == "key") then
event, char = os.pullEvent()
if (tonumber(char) == nil) then
table.insert(text,char)
elseif (keyNum == 14) then
table.remove(text,#text)
end
elseif (event == "mouse_up") then
if (keyNum == 1) then
for k,v in pairs(buttons) do
if (x >= buttons[k].pos1.x and x <= buttons[k].pos2.x) then
if (y >= buttons[k].pos1.y and y <= buttons[k].pos2.y) then
term.setCursorPos(buttons[k].pos1.x-1,buttons[k].pos1.y)
term.write("["..k.."]")
-- k is the name of the item you added, you can do whatever you want with it after this write statement
-- inside of this if.
end
end
end
elseif (keyNum == 2) then
for i = 1,#text do
if (x >= textBoxX and x <= (#text+textBoxX)) then
if (y == textBoxY) then
text = {}
end
end
end
end
end
term.setCursorPos(1,5)
for i = 1,#text do
term.write(text[i])
end
end
Comments