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

Bundled Cable + Click Event

Started by The_Cat, 02 February 2015 - 11:02 PM
The_Cat #1
Posted 03 February 2015 - 12:02 AM
Hi Everyone,

I am having some trouble trying to figue out somethings….

Here is the code:

term.clear()
term.setCursorPos(1,1)

isWhiteOn="OFF"
isOrangeOn="OFF"

while true do
    term.clear()
    term.setCursorPos(1,1)
    print("Toggle white light. [".. isWhiteOn .."]")
    print("Toggle orange light. [".. isOrangeOn .."]")
    local event, button, xPos, yPos = os.pullEvent("mouse_click")
    if xPos >= 20 and xPos <= 23 and yPos == 1 then
        if rs.testBundledInput("back", 1) == false then
            rs.setBundledOutput("back", 1)
            isWhiteOn="ON"
            isOrangeOn="OFF"
        else
            rs.setBundledOutput("back", 0)
            isWhiteOn="OFF"
        end
    elseif xPos >= 22 and xPos <= 25 and yPos == 2 then
        if rs.testBundledInput("back", 2) == false then
            rs.setBundledOutput("back", 2)
            isWhiteOn="OFF"
            isOrangeOn="ON"
        else
            rs.setBundledOutput("back", 0)
            isOrangeOn="OFF"
        end
    end
end

So basicly when you click in the area of the [ON] or [OFF] it either turns on or off the redstone signal

I would like to make it so it can automaticly find the xPos and yPos of the [ON] when i create it. So i dont have to guess he xPos myself every time i create a new ON or OFF button.
Edited on 02 February 2015 - 11:05 PM
InDieTasten #2
Posted 03 February 2015 - 12:52 AM
For a simple implementation you can write to the screen, stop at the point you want to draw a button, get the cursor position, then actually draw the button and voila, you've got the button coordinates in the cursor position you retrieved before. Example code following…

term.clear()
term.setCursorPos(1,1)

isWhiteOn="OFF"
isOrangeOn="OFF"

while true do
	term.clear()
	term.setCursorPos(1,1)

	write("Toggle white light. [") --# write the first part
	button1x, button1y = term.getCursorPos() --# retrieve starting position
	print(isWhiteOn.."]") --# print status and rest

	write("Toggle orange light. [") --# same thing with button2
	button2x, button2y = term.getCursorPos()
	print(isOrangeOn.."]")

	local event, button, xPos, yPos = os.pullEvent("mouse_click")
	if xPos >= button1x and xPos  <= button1x+#isWhiteOn-1 and yPos == button1y then --# can now use the button coords, rather than hardcoded values
		if rs.testBundledInput("back", 1) == false then
			rs.setBundledOutput("back", 1)
			isWhiteOn="ON"
			isOrangeOn="OFF"
		else
			rs.setBundledOutput("back", 0)
			isWhiteOn="OFF"
		end
	elseif xPos >= button2x and xPos <= button2x+#isOrangeOn-1 and yPos == button2y then
		if rs.testBundledInput("back", 2) == false then
			rs.setBundledOutput("back", 2)
			isWhiteOn="OFF"
			isOrangeOn="ON"
		else
			rs.setBundledOutput("back", 0)
			isOrangeOn="OFF"
		end
	end
end
Edited on 03 February 2015 - 12:10 AM
The_Cat #3
Posted 03 February 2015 - 01:50 AM

Thanks this has helped allot.