Jump to content

Gui Click Problem


Recommended Posts

Problem with a login panel, what happens is that when you click anywhere in the window it is as if you were clicking on any of the buttons, the client code is as follows:

local screenW, screenH = guiGetScreenSize()

function regPanel()
        winRegister = guiCreateWindow((screenW - 293) / 2, (screenH - 399) / 2, 293, 399, "Registro", false)
        guiWindowSetSizable(winRegister, false)
		guiSetVisible(winRegister, false)

        lblUser = guiCreateLabel(27, 48, 74, 22, "Usuario:", false, winRegister)
        guiLabelSetHorizontalAlign(lblUser, "right", false)
        guiLabelSetVerticalAlign(lblUser, "center")
        lblPass = guiCreateLabel(27, 86, 74, 22, "Contraseña:", false, winRegister)
        guiLabelSetHorizontalAlign(lblPass, "right", false)
        guiLabelSetVerticalAlign(lblPass, "center")
        userBox = guiCreateEdit(107, 48, 147, 24, "", false, winRegister)
        pass1Box = guiCreateEdit(107, 86, 147, 24, "", false, winRegister)
		guiEditSetMasked(pass1Box, true)
        pass2Box = guiCreateEdit(107, 125, 147, 24, "", false, winRegister)
		guiEditSetMasked(pass2Box, true)
        btnRegister = guiCreateButton(60, 213, 173, 57, "Registrarse", false, winRegister)
		rbtRegresar = guiCreateButton(60, 292, 173, 57, "Regresar", false, winRegister)
        lblPass2 = guiCreateLabel(27, 125, 74, 22, "Contraseña:", false, winRegister)
        guiLabelSetHorizontalAlign(lblPass2, "right", false)
        guiLabelSetVerticalAlign(lblPass2, "center")
	
	addEventHandler("onClientGUIClick", rbtRegresar, function()
		guiSetVisible(winRegister, false)
		guiSetVisible(winLogin, true)
		end)
		
	addEventHandler("onClientGUIClick", btnRegister, function()
		usuario = guiGetText(userBox)
		c = guiGetText(pass1Box)
		c1 = guiGetText(pass2Box)
		
		
		if(c==c1)then
			triggerServerEvent("rg", getLocalPlayer(),usuario,c)
		else
		end
		end)
		
		
end

addEvent("cerrar", true)
addEventHandler("cerrar", getLocalPlayer(), function()
guiSetVisible(winRegister, false)
showCursor(false)
end)

addEvent("cerrar2", true)
addEventHandler("cerrar2", getLocalPlayer(), function()
guiSetVisible(winLogin, false)
showCursor(false)
end)


function logPanel()
		regPanel()
		showCursor(true)
        winLogin = guiCreateWindow((screenW - 510) / 2, (screenH - 524) / 2, 510, 524, "Login", false)
        guiWindowSetSizable(winLogin, false)

        lblUser = guiCreateLabel(48, 76, 133, 33, "User:", false, winLogin)
        guiLabelSetHorizontalAlign(lblUser, "right", false)
        guiLabelSetVerticalAlign(lblUser, "center")
        lblPass = guiCreateLabel(48, 135, 133, 33, "Password:", false, winLogin)
        guiLabelSetHorizontalAlign(lblPass, "right", false)
        guiLabelSetVerticalAlign(lblPass, "center")
        UserBox = guiCreateEdit(191, 76, 174, 33, "", false, winLogin)
        passBox = guiCreateEdit(191, 135, 174, 33, "", false, winLogin)
		guiEditSetMasked(passBox, true)
        btnLogin = guiCreateButton(48, 391, 175, 61, "Login", false, winLogin)
        btnRegister = guiCreateButton(288, 391, 175, 61, "Register", false, winLogin) 

		addEventHandler("onClientGUIClick", btnRegister, function()
		guiSetVisible(winLogin, false)
		guiSetVisible(winRegister, true)
		end)
		
		addEventHandler("onClientGUIClick", btnLogin, function()
		user = guiGetText(UserBox)
		clave = guiGetText(passBox)
		
		triggerServerEvent("lg", getLocalPlayer(),user,clave)
		end)
end

	
addEvent("abrirLogin", true)
addEventHandler("abrirLogin", getLocalPlayer(), logPanel)

 

and the server code is this: 

function rg(user, pass)

	if(addAccount(user,pass))then
	outputChatBox("se registro con exito")
	triggerClientEvent(source,"cerrar",source)
	logIn(source,getAccount(user,pass), pass)
	else
	outputChatBox("esta cuenta ya existe")
	end

end

function lg(usuario, clave)
	cuenta = getAccount(usuario,clave)
	if(cuenta)then
		logIn(source,getAccount(usuario,clave), clave)
		triggerClientEvent(source,"cerrar2",source)
	else
	outputChatBox("la cuenta no existe o usuario y contraseña incorrectos")
	end

end

addEvent("rg",true)
addEventHandler("rg", getRootElement(), rg)
addEvent("lg",true)
addEventHandler("lg", getRootElement(), lg)

addEventHandler("onPlayerJoin",getRootElement(), function()
triggerClientEvent(source,"abrirLogin",source)
end)

addEventHandler("onPlayerLogout",getRootElement(), function()
triggerClientEvent(source,"abrirLogin",source)
end)

 

Link to comment

Every onClientGUIClick event handler's attachment needs propagate set to false:

addEventHandler("onClientGUIClick", theButton, function()
    -- ...
  end, false -- add a false after the function's end (4th argument of addEventHandler)
)

Otherwise the event is attached to the given element's/button's element tree, meaning the event being triggered on any child element or parent element will trigger the handler, which is not what you want with GUIs.

The other workaround is, in each function, adding an if-block with condition checking the event's source (this isn't the most efficient though, since the function will still be called unnecessarily, just that it won't do anything.

addEventHandler("onClientGUIClick", theButton, function()
  if source == theButton then -- verify the event's source is the button you attached this function to
    -- do whatever the function does
  end
end)

 

Edited by Addlibs
  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...