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

creating a simple touchscreen button to activate a redstone signal

Started by Exanadu, 12 February 2013 - 02:53 AM
Exanadu #1
Posted 12 February 2013 - 03:53 AM
Seeking help creating a simple touchscreen button to activate a redstone signal, I've been browsing the help section and have read similar posts but I still am unable to get something to work. Any help would be appreciated. Very basic not too complex lol.
Cranium #2
Posted 12 February 2013 - 03:57 AM
Split to new topic.

You might want to take a look at the wiki. Some useful areas are the os API (os.pullEvent()), the peripheral API (monitors), as well as the redstone API (redstone signals)
You can search either of these in the wiki, and it should get you what you're looking for.
Exanadu #3
Posted 12 February 2013 - 04:16 AM
ty, I know how to make the redstone signals go where i want, i just do not know how to make a touch screen button set it off, I'm looking at the wiki but it made me even more confused :P/>
Cranium #4
Posted 12 February 2013 - 04:25 AM
Well, if you are using monitors, then you do something similar to this:

while true do
    local events = {os.pullEvent()}
    if events[1] == "monitor_touch" then
	    --do whatever with the monitor touch event
    end
end
remiX #5
Posted 12 February 2013 - 04:35 AM
Have a table with information about the buttons (text, x and y values - Optional: colours)


tButtons = {
	{ text = "Redstone ON", x = 1, y = 1, col = colours.lime },
	{ text = "Redstone OFF", x = 1, y = 1, col = colours.red }
}

local function isValidClick( tab, mx, my )
	for _, v in pairs( tab ) do
		if mx >= v.x and mx <= v.x + #v.text - 1 -- verifies the X position
		and my == v.y then -- Y position
			return true, v.text -- valid click so it returns true and what text was clicked
		end
	end
	return false, nil -- If the button is invalid, return false and nil
end

local function printTable( tab ) -- Function to print any table
	for _, v in pairs( tab ) do
		term.setCursorPos( v.x, v.y )
		term.setBackgroundColour( v.col )
		write( v.text )
	end
end

printTable( tButtons )

while true do
	local event, but, X, Y = os.pullEvent()
	if event == "monitor_touch" then
		bValidClick, sText = isValidClick( tButtons, X, Y ) -- calls the function to check if it's a valid click
		if bValidClick then -- if it's a valid click
			if sText == "Redstone ON" then -- if the text returned is 'Redstone ON'
				redstone.setOutput( "back", true )
			elseif sText == "Redstone OFF" -- if the text returned is 'Redstone OFF'
				redstone.setOutput( "back", false )
			end
		end
	end
end
Exanadu #6
Posted 12 February 2013 - 05:14 AM
how do you make your table setup show up on the monitor as it is "after a few typo fixes" it shows up on the computer and i can click the coordinates on the monitor and it works but you cant see it on the monitor
AnDwHaT5 #7
Posted 12 February 2013 - 05:17 AM
Try this

event, x, y = os.pullEvent("mouse_click")
if posX = x and posY = y then
rs.setOutput("right", true)
sleep(x)
rs.setOutput("right", false)
else
shell.run("yourprogramhere")
end
end




Dont quote me but thats a basic redstone output upon click. Do double check the codeing for i forgot some and the posX = x and posY = y might be wrong. also the one above may be wrong but hey its the closest youve got so far. I gave you the basics to look for at least. Anything under should be right. Happy coding:)
remiX #8
Posted 12 February 2013 - 05:47 AM
how do you make your table setup show up on the monitor as it is "after a few typo fixes" it shows up on the computer and i can click the coordinates on the monitor and it works but you cant see it on the monitor

Oh woops, forgot it must print on the monitor :D/> … Also, what typos did I make :o/>?


--Variables
monitorSide = "right"
redstoneSide = "left"
mon = peripheral.wrap(monitorSide)

tButtons = {
	{ text = "Redstone ON", x = 1, y = 1, col = colours.lime },
	{ text = "Redstone OFF", x = 1, y = 1, col = colours.red }
}

local function isValidClick( tab, mx, my )
	for _, v in pairs( tab ) do
		if mx >= v.x and mx <= v.x + #v.text - 1 -- verifies the X position
		and my == v.y then -- Y position
			return true, v.text -- valid click so it returns true and what text was clicked
		end
	end
	return false, nil -- If the button is invalid, return false and nil
end

local function printTable( tab ) -- Function to print any table
	print("Printing the options for table onto the monitor...")
	for _, v in pairs( tab ) do
		term.setCursorPos( v.x, v.y )
		term.setBackgroundColour( v.col )
		mon.write( v.text )
	end
end

printTable( tButtons )

while true do
	local event, but, X, Y = os.pullEvent()
	if event == "monitor_touch" then
		bValidClick, sText = isValidClick( tButtons, X, Y ) -- calls the function to check if it's a valid click
		if bValidClick then -- if it's a valid click
			if sText == "Redstone ON" then -- if the text returned is 'Redstone ON'
				redstone.setOutput( "back", true )
				print( "Redstone signal has been turned ON" )
			elseif sText == "Redstone OFF" -- if the text returned is 'Redstone OFF'
				redstone.setOutput( "back", false )
				print( "Redstone signal has been turned OFF" )
			end
		end
	end
end
Exanadu #9
Posted 12 February 2013 - 11:54 AM
thx rimi this really helped get me going in the right direction. I ended up going a different route other than a table so i could have more control over the touch area but it defiantly helped. as far as the typos i was talking bout me lol, but yea thx guys ill post my setup when i'm finished so it might help others