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

How to make text into a clickable button?

Started by AnthonyD98™, 27 January 2013 - 04:47 PM
AnthonyD98™ #1
Posted 27 January 2013 - 05:47 PM
Hello, I have a question:

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
GravityScore #2
Posted 27 January 2013 - 07:42 PM
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() and cy == ty then
	  print("\nClicked!")
	end
  end
end
remiX #3
Posted 27 January 2013 - 09:08 PM
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
AnthonyD98™ #4
Posted 28 January 2013 - 10:45 AM
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

I will keep both these codes in mind which I will try using later :D/>

+1 to both of you

Thanks, AnthonyD98
Doyle3694 #5
Posted 28 January 2013 - 11:16 AM
Though you should be aware GravityScore's code doesn't detect on the y axis :P/>
AnthonyD98™ #6
Posted 28 January 2013 - 11:24 AM
Though you should be aware GravityScore's code doesn't detect on the y axis :P/>

Hmm, I haven't looked at the codes yet . . .
GravityScore #7
Posted 28 January 2013 - 02:39 PM
Though you should be aware GravityScore's code doesn't detect on the y axis :P/>

Whoop! I forgot to add that part of the and statement! Thanks! I'll edit my post…
Renee #8
Posted 31 January 2013 - 04:19 PM
RemiX, could show me how to apply your code to something like toggling on/off different bundle cable colors according to different buttons you press on the screen?