I did something like this a while ago with an OS project thing I had. It was basically a function that you would use, and it'd "halt" the entire program and wait for a click, then return whatever option the user clicked, or nil if he clicked out of the menu.
So this function could just be called "popupMenu". The first two arguments would be the x and y positions of the rightclick menu, and you can feed it mouse coordinates from whatever loop runs your program. You could then give it the option to take variable arguments as menu options.
My logic here is that I'm going through the menu options, or the "args", and making an "object" for each one. Objects are really just tables with various values that makes mouse position checks easier. Every object is positioned under one another and dependent on the x and y values given to the function. Then just go ahead and draw all of the "objects", or menu options.
After that, we get a mouse click from the user, then check it against all of the menu options to see if the mouse x and y is within the area of that option. If it is, return the text of that object.
This is what that function would look like:
function popupMenu(x, y, ...)
local args = {...}
local objects = {}
for i,v in pairs(args) do
objects[i] = {
x = x;
y = i + y - 1;
text = v;
}
end
for _, option in pairs(objects) do
term.setCursorPos(option.x, option.y)
term.write(option.text)
end
local _, button, mx, my = os.pullEvent('mouse_click')
for _, option in pairs(objects) do
if mx >= option.x and mx < option.x + #option.text
and my == option.y then
return option.text
end
end
end
Here's an example of how to use it:
local input = popupMenu(2,2, 'Bacon', 'No Bacon')
if input == 'Bacon' then
print 'yay!'
elseif input == 'No Bacon' then
print 'ffs.'
end
This code makes a menu at position 2, 2, with the options "Bacon" and "No Bacon".