OnClientGUIMouseUp: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Replace to predefined variables.)
 
(2 intermediate revisions by 2 users not shown)
Line 17: Line 17:
This example show how to add very basic ''click'n'drag'' feature for GUI elements (only for those which parent element is gui-root)
This example show how to add very basic ''click'n'drag'' feature for GUI elements (only for those which parent element is gui-root)
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler( "onClientGUIMouseDown", getRootElement( ),
addEventHandler( "onClientGUIMouseDown", root,
     function ( btn, x, y )
     function ( btn, x, y )
         if btn == "left" then
         if btn == "left" then
Line 27: Line 27:
);
);


addEventHandler( "onClientGUIMouseUp", getRootElement( ),
addEventHandler( "onClientGUIMouseUp", root,
     function ( btn, x, y )
     function ( btn, x, y )
         if btn == "left" then
         if btn == "left" then
Line 35: Line 35:
);
);


addEventHandler( "onClientCursorMove", getRootElement( ),
addEventHandler( "onClientCursorMove", root,
     function ( _, _, x, y )
     function ( _, _, x, y )
         if clickedElement then
         if clickedElement then
Line 45: Line 45:


==See Also==
==See Also==
===GUI events===
{{GUI_events}}
{{GUI_events}}
===Client event functions===
===Client event functions===
{{Client_event_functions}}
{{Client_event_functions}}
[[Category:Needs_Example]]

Latest revision as of 00:39, 12 April 2023

This event is fired when the user releases his mouse button when on top of a GUI element.

Parameters

string button, int absoluteX, int absoluteY
  • button: the name of the mouse button that was released on a GUI element, can be left, right, or middle.
  • 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 on top of which the mouse button was released.

Example

This example show how to add very basic click'n'drag feature for GUI elements (only for those which parent element is gui-root)

addEventHandler( "onClientGUIMouseDown", root,
    function ( btn, x, y )
        if btn == "left" then
            clickedElement = source; -- store the clicked element in a global variable
            local elementPos = { guiGetPosition( source, false ) };
            offsetPos = { x - elementPos[ 1 ], y - elementPos[ 2 ] }; -- get the offset position
        end
    end
);

addEventHandler( "onClientGUIMouseUp", root,
    function ( btn, x, y )
        if btn == "left" then
            clickedElement = nil;
        end
    end
);

addEventHandler( "onClientCursorMove", root,
    function ( _, _, x, y )
        if clickedElement then
            guiSetPosition( clickedElement, x - offsetPos[ 1 ], y - offsetPos[ 2 ], false );
        end
    end
);

See Also

Input

GUI


Client event functions