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

Mouse Event draws menu then clears it? [<SOLVED>]

Started by Mr_Programmer, 03 January 2016 - 01:14 AM
Mr_Programmer #1
Posted 03 January 2016 - 02:14 AM
Hi everyone,

i am working on 2 programs at the minute and i am using the same mouse event function to draw menus and what not, one of the programs i have been foring on for over 6+ months and the mouse events worked fine but now it doesnt work propper.

INFO: when the "menu" secection is clicked, it should draw a drop down menu, however, it does do that but when i let go of the mouse button, the menu goes away, i have checked the code and cant see anything wrong with it unless im just not noticing the problem.

Here is the 2 peices of code:

function drawLogMenu()
	term.setBackgroundColour(c_writing)
	term.setTextColour(c_menu)
	term.setCursorPos(33,2)
	print("===================")
	term.setTextColour(c_border)
	term.setCursorPos(33,3)
	print("   Reboot System   ")
	term.setCursorPos(33,4)
	print("				   ")
	term.setCursorPos(33,5)
	print("  Shutdown System  ")
	term.setCursorPos(33,6)
	term.setTextColour(c_menu)
	print("===================")
	term.setCursorPos(43,1)
	term.setTextColour(c_off)
	term.setBackgroundColour(c_error)
	print("System <<")
	term.setBackgroundColour(black)
end

This one is the draw menu and this one:

function login3()
	ls = 0
	logScreen = true

	while logScreen == true do
		local event, button, X, Y = os.pullEventRaw()
		if ls == 0 then
			if event == "mouse_click" then
				if X >=43 and X <=52 and Y == 1 and button == 1 then
					drawLogMenu()
					ls = 1
				elseif X >=21 and X <=38 and Y == 8 and button == 1 then
					logScreen = false
					term.setCursorPos(21,8)
				elseif X >=20 and X <=33 and Y == 15 and button == 1 then
					logScreen = false
					accountCreation()
				elseif X >=38 and X <=51 and Y == 15 and button == 1 then
					logscreen = false
					accountDeletion()
				else
					login()
				end
			end
		elseif ls == 1 then
			if X >=35 and X <=50 and Y == 3 and button == 1 then
				ls = 0
				rednet.send(ServerID, "systemReboot")
				os.reboot()
			elseif X >=34  and X <=51 and Y == 5 and button == 1 then
				ls = 0
				os.shutdown()
			else
				ls = 0
				login()
			end
		end
	end
end

This one is the Mouse event code.


Any help would be muchly appreciated,

Thanks, MrProgrammer…
Edited by
Creator #2
Posted 03 January 2016 - 02:30 AM
I think it is because letting go of the mouse queues a "mouse_release" event. Since there is a new event, the whole thing gets drawn again without the drop down part. You have to add a variable isPressed and when it is true then draw the drop down part too.
Mr_Programmer #3
Posted 03 January 2016 - 02:36 AM
I think it is because letting go of the mouse queues a "mouse_release" event. Since there is a new event, the whole thing gets drawn again without the drop down part. You have to add a variable isPressed and when it is true then draw the drop down part too.
There is no "mouse_release" on the wiki? im on computercraft 1.7 also if there is a "mouse_release" event there, then there is no where for it to go? it shouldnt wipe the dropDownMenu off the screen? if anything it shouldnt do anything becouse the program hasnt required the event to do anything? does that make sence or does everything i just said wrong?
Edited on 03 January 2016 - 01:56 AM
KingofGamesYami #4
Posted 03 January 2016 - 02:41 AM
He's referring to mouse_up
Mr_Programmer #5
Posted 03 January 2016 - 02:45 AM
He's referring to mouse_up
Ah, ok but still why is the event taking effect on the program if the program is only asking for "mouse_click" it shouldnt undraw the menu upon "mouse_up"? or am i incorrect?
Mr_Programmer #6
Posted 03 January 2016 - 02:54 AM
Would i just be able to change
 if event == "mouse_click" then 

to this? or would it not work?
 if event == "mouse_click" or "mouse_up" then 

or would it be better to just have
 if event == "mouse_up" then 
Edited on 03 January 2016 - 01:55 AM
Bomb Bloke #7
Posted 03 January 2016 - 03:04 AM
This certainly wouldn't work:

 if event == "mouse_click" or "mouse_up" then 

You meant:

 if event == "mouse_click" or event == "mouse_up" then 

In your current code, you only check the event type if ls is 0. If ls is 1, you perform no such check.

Seems easiest to simply apply a filter to this line:

local event, button, X, Y = os.pullEventRaw("mouse_click")

os.pullEventRaw() will then ignore all events other than the type you're interested in, and will only return once one of those enters the queue.
Mr_Programmer #8
Posted 03 January 2016 - 03:34 AM
This certainly wouldn't work:

 if event == "mouse_click" or "mouse_up" then 

You meant:

 if event == "mouse_click" or event == "mouse_up" then 

In your current code, you only check the event type if ls is 0. If ls is 1, you perform no such check.

Seems easiest to simply apply a filter to this line:

local event, button, X, Y = os.pullEventRaw("mouse_click")

os.pullEventRaw() will then ignore all events other than the type you're interested in, and will only return once one of those enters the queue.

Thanks, Bomb Bloke, all fixed, thanks a bunch!