IsMouseOnGuiElement: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Client function}} This function allows you to check whether or not your mouse is over a specific gui element, this is especially useful if the gui element has a p...")
 
Line 6: Line 6:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function isMouseOnGuiElement(guiElement,guiElementParent)
function isMouseOnGuiElement(guiElement,guiElementParent)
     local sx,sy = guiGetScreenSize()
     if isCursorShowing() then
    local x,y = getCursorPosition()
        local sx,sy = guiGetScreenSize()
    local wx,wy = 0,0
        local x,y = getCursorPosition()
    x,y = x*sx, y*sy
        local wx,wy = 0,0
    if guiElementParent then
        x,y = x*sx, y*sy
        wx,wy = guiGetPosition(guiElementParent,false)
        if guiElementParent then
            wx,wy = guiGetPosition(guiElementParent,false)
        end
        local bx,by = guiGetPosition(guiElement,false)
        local ex,ey = guiGetSize(guiElement,false)
        if x >= wx+bx and x <= wx+bx+ex and y >= wy+by and y <= wy+by+ey then
            return true
        end
        return false
     end
     end
    local bx,by = guiGetPosition(guiElement,false)
    local ex,ey = guiGetSize(guiElement,false)
    if x >= wx+bx and x <= wx+bx+ex and y >= wy+by and y <= wy+by+ey then
        return true
    end
    return false
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 00:35, 6 April 2019

This function allows you to check whether or not your mouse is over a specific gui element, this is especially useful if the gui element has a parent.

Code

function isMouseOnGuiElement(guiElement,guiElementParent)
    if isCursorShowing() then
        local sx,sy = guiGetScreenSize()
        local x,y = getCursorPosition()
        local wx,wy = 0,0
        x,y = x*sx, y*sy
        if guiElementParent then
            wx,wy = guiGetPosition(guiElementParent,false)
        end
        local bx,by = guiGetPosition(guiElement,false)
        local ex,ey = guiGetSize(guiElement,false)
        if x >= wx+bx and x <= wx+bx+ex and y >= wy+by and y <= wy+by+ey then
            return true
        end
        return false
    end
end

Syntax

element isMouseOnGuiElement(element guiElement, element guiElementParent)

Required Arguments

  • guiElement: The GUI element you want to check if the mouse is over it.
  • guiElementParent: The parent element of the element you want to check.

Returns

Returns true if the mouse is over the element, otherwise it returns false.

Example

local window = guiCreateWindow(500,500,200,200,"Test",false)
local image = guiCreateStaticImage(25,25,50,50,"image.png",false,window)

addEventHandler("onClientRender",root,function()
    if isMouseOnGuiElement(image,window) then
        guiStaticImageLoadImage(image,"image2.png")
    else
        guiStaticImageLoadImage(image,"image.png")
    end
end)

See Also

General functions

Browsers

Buttons

Checkboxes

Comboboxes

Edit Boxes

Gridlists

Memos

Progressbars

Radio Buttons

Scrollbars

Scrollpanes

Static Images

Tab Panels

Tabs

Text Labels

Windows

Input

GUI