How do you make text into a clickable button?
Lets say I have this [ Programs ] and I want it to be clickable and to run a program or do a function. How would I make the [ Programs ] A button?
Thanks, AnthonyD98
-- Draw the text
local tx, ty = 3, 3
local text = "[ Hello there! ]"
term.setCursorPos(tx, ty)
write(text)
-- Event loop
while true do
local e, but, cx, cy = os.pullEvent()
if e == "mouse_click" then
if cx >= tx and cx <= tx + text:len() and cy == ty then
print("\nClicked!")
end
end
end
screenX, screenY = term.getSize()
t_stuff = {
{text = "hello", x = math.floor(screenX/2), y = math.floor(screenY/2-5), textCol = colours.black, bgCol = colours.blue},
{text = "bye", x = math.floor(screenX/2), y = math.floor(screenY/2-4), textCol = colours.black, bgCol = colours.blue}
}
function isValidMouseClick(_table, mx, my)
for _, v in pairs(_table) do
if mx >= v.x and mx < v.x + #v.text
and my == v.y then
return true, v.text
end
end
return false, nil
end
term.clear()
while true do
for _, v in pairs(t_stuff) do
term.setCursorPos(v.x, v.y)
term.setBackgroundColour(v.bgCol or colours.black)
term.setTextColour(v.textCol or colours.white)
write(v.text)
end
e = { os.pullEvent() }
if e[1] == "mouse_click" then
if e[2] == 1 then -- left click
bValid, sOption = isValidMouseClick(t_stuff, e[3], e[4])
if bValid then
term.setCursorPos(1, 1)
term.clearLine()
write("You clicked " .. sOption)
end
end
end
end
What you have to do is draw the text at a certain x and y position, remember that position, then in your main event loop, every time there is a mouse_click event, check if the clicked x and y positions are inside your text's position. Here:-- Draw the text local tx, ty = 3, 3 local text = "[ Hello there! ]" term.setCursorPos(tx, ty) write(text) -- Event loop while true do local e, but, cx, cy = os.pullEvent() if e == "mouse_click" then if cx >= tx and cx <= tx + text:len() then print("\nClicked!") end end end
What I usually do is have the co-ordinates of where the text will be written inside the table so it is easier to check if something was clicked, take this for example:screenX, screenY = term.getSize() t_stuff = { {text = "hello", x = math.floor(screenX/2), y = math.floor(screenY/2-5), textCol = colours.black, bgCol = colours.blue}, {text = "bye", x = math.floor(screenX/2), y = math.floor(screenY/2-4), textCol = colours.black, bgCol = colours.blue} } function isValidMouseClick(_table, mx, my) for _, v in pairs(_table) do if mx >= v.x and mx < v.x + #v.text and my == v.y then return true, v.text end end return false, nil end term.clear() while true do for _, v in pairs(t_stuff) do term.setCursorPos(v.x, v.y) term.setBackgroundColour(v.bgCol or colours.black) term.setTextColour(v.textCol or colours.white) write(v.text) end e = { os.pullEvent() } if e[1] == "mouse_click" then if e[2] == 1 then -- left click bValid, sOption = isValidMouseClick(t_stuff, e[3], e[4]) if bValid then term.setCursorPos(1, 1) term.clearLine() write("You clicked " .. sOption) end end end end
Though you should be aware GravityScore's code doesn't detect on the y axis :P/>
Though you should be aware GravityScore's code doesn't detect on the y axis :P/>