OnClientGUIClick: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Added example (similar to guiCreateButton) and see also)
(Change to resource root.)
 
(30 intermediate revisions by 20 users not shown)
Line 1: Line 1:
{{Client event}}
{{Client event}}
__NOTOC__
__NOTOC__
This event is fired when the user clicks a GUI element.
This event happens when any gui-element clicked.
{{Note|The '''player''' who clicked the gui-element is always the [[localPlayer]].}}


==Parameters==  
==Parameters==  
Line 7: Line 8:
string button, string state, int absoluteX, int absoluteY
string button, string state, int absoluteX, int absoluteY
</syntaxhighlight>
</syntaxhighlight>
*'''button:''' the name of the mouse button that the GUI element was clicked with, can be left, right, or middle.
*'''button:''' the name of the button which will be clicked, it can be ''left'', ''right'', ''middle''.
*'''state:''' the state of the mouse button. "down" or "up".
*'''state:''' the state of the mouse button, will be ''down'' if the mouse button was pushed, or ''up'' if it was released. '''Please note currently only the ''up'' state is supported.'''
*'''absoluteX:''' the X position of the mouse cursor, in pixels, measured from the left side of the screen.
*'''absoluteX:''' the X position of the mouse cursor, in pixels, measured from the left side of the screen.
*'''absoluteY:''' the Y position of the mouse cursor, in pixels, measured from the top of the screen.
*'''absoluteY:''' the Y position of the mouse cursor, in pixels, measured from the top of the screen.
Line 14: Line 15:
==Source==
==Source==
The [[event system#Event source|source]] of this event is the GUI element that was clicked.
The [[event system#Event source|source]] of this event is the GUI element that was clicked.
{{Note|If the GUI Element attached to this event has a parent element, this event will be triggered once the parent element of the attached element is clicked too. You can set the parameter '''propagate''' to ''false'' in the call to [[addEventHandler]] to prevent this.}}


==Example==  
==Example==  
This example creates an edit box alongside an "Output!" button. When the button is clicked with the left mouse button, it will output the message in the edit box into the Chat Box.
This example creates an edit box alongside an "Output!" button. When the button is clicked with the left mouse button, it will output the message in the edit box into the chat box.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--create our button
-- When client's resource starts, create the GUI
btnOutput = guiCreateButton( 0.7, 0.1, 0.2, 0.1, "Output!", true )
function initGUI( )
    -- Create our button
    btnOutput = guiCreateButton( 0.7, 0.1, 0.2, 0.1, "Output!", true )


--Create an edit box and define it as "editBox".
    -- And attach our button to the outputEditBox function
editBox = guiCreateEdit( 0.3, 0.1, 0.4, 0.1, "Type your message here!", true )
    addEventHandler ( "onClientGUIClick", btnOutput, outputEditBox, false )
guiEditSetMaxLength ( editBox, 128 ) --the max chatbox length is 128, so force this


--setup our function to output the message to the chatbox
    -- Create an edit box and define it as "editBox".
function outputEditBox (button)
    editBox = guiCreateEdit( 0.3, 0.1, 0.4, 0.1, "Type your message here!", true )
    guiEditSetMaxLength ( editBox, 128 ) -- The max chatbox text length is 128, so force this
end
addEventHandler( "onClientResourceStart", resourceRoot, initGUI )
 
-- Setup our function to output the message to the chatbox
function outputEditBox ( button )
     if button == "left" then
     if button == "left" then
         local text = guiGetText ( editBox )--get the text from the edit box
         local text = guiGetText ( editBox )-- Get the text from the edit box
         outputChatBox ( text ) --output that text
         outputChatBox ( text ) -- Output that text
     end
     end
end
end
--and attach our button to the outputEditBox function
addEventHandler ( "onClientGUIClick", btnOutput, outputEditBox )
</syntaxhighlight>
</syntaxhighlight>
[[pl:onClientGUIClick]]


==See Also==
==See Also==
===GUI events===
{{GUI_events}}
{{GUI_events}}
===Event functions===
===Client event functions===
{{Client_event_functions}}
{{Client_event_functions}}

Latest revision as of 16:40, 19 October 2022

This event happens when any gui-element clicked.

[[{{{image}}}|link=|]] Note: The player who clicked the gui-element is always the localPlayer.

Parameters

string button, string state, int absoluteX, int absoluteY
  • button: the name of the button which will be clicked, it can be left, right, middle.
  • state: the state of the mouse button, will be down if the mouse button was pushed, or up if it was released. Please note currently only the up state is supported.
  • absoluteX: the X position of the mouse cursor, in pixels, measured from the left side of the screen.
  • absoluteY: the Y position of the mouse cursor, in pixels, measured from the top of the screen.

Source

The source of this event is the GUI element that was clicked.

[[{{{image}}}|link=|]] Note: If the GUI Element attached to this event has a parent element, this event will be triggered once the parent element of the attached element is clicked too. You can set the parameter propagate to false in the call to addEventHandler to prevent this.

Example

This example creates an edit box alongside an "Output!" button. When the button is clicked with the left mouse button, it will output the message in the edit box into the chat box.

-- When client's resource starts, create the GUI
function initGUI( )
    -- Create our button
    btnOutput = guiCreateButton( 0.7, 0.1, 0.2, 0.1, "Output!", true )

    -- And attach our button to the outputEditBox function
    addEventHandler ( "onClientGUIClick", btnOutput, outputEditBox, false )

    -- Create an edit box and define it as "editBox".
    editBox = guiCreateEdit( 0.3, 0.1, 0.4, 0.1, "Type your message here!", true )
    guiEditSetMaxLength ( editBox, 128 ) -- The max chatbox text length is 128, so force this
end
addEventHandler( "onClientResourceStart", resourceRoot, initGUI )

-- Setup our function to output the message to the chatbox
function outputEditBox ( button )
    if button == "left" then
        local text = guiGetText ( editBox )-- Get the text from the edit box
        outputChatBox ( text ) -- Output that text
    end
end

See Also

Input

GUI


Client event functions