Jump to content

[HELP] DxDrawImage over event


Hayakii

Recommended Posts

---------------------------------------------------------------------------------------------------------------------------------------------

Hi everyone!

Im working on a login panel since 1 day and i couldn't find how to do this:

I've made an image with dxDrawImage and i want when i place my cursor over the image, it is replace by another one.

Not over => Image 1

Over it => Image 2

And also that you can click on it.

I've tried many things without success. I need you help guys, thank you!

-Hayakii-

---------------------------------------------------------------------------------------------------------------------------------------------

Link to comment

You must compare the cursor position to the image draw position and size.

You can use this function to avoid all the math calculations:

function isMouseInPosition ( x, y, width, height ) 
    if ( not isCursorShowing ( ) ) then 
        return false 
    end 
  
    local sx, sy = guiGetScreenSize ( ) 
    local cx, cy = getCursorPosition ( ) 
    local cx, cy = ( cx * sx ), ( cy * sy ) 
    if ( cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height ) then 
        return true 
    else 
        return false 
    end 
end 

Example:

addEventHandler ( "onClientRender", root, 
    function ( ) 
        local imgX, imgY, imgWidth, imgHeight = 50, 50, 200, 200 
        dxDrawImage ( imgX, imgY, imgWidth, imgHeight, ( isMouseInPosition ( imgX, imgY, imgWidth, imgHeight ) and "myImage2.png" or "myImage.png" ), tocolor ( 255, 255, 255 ) ) 
    end 
) 

Link to comment

Yea, i already find how to do it before you told me. Thanks anyway.

But i have a probleme (yes, another...) with a function. I want the players, when they click a little cross i've made, all the page on the menu panel is close. Its working, but we can still click on it even though the pages are close. We can still ear the sound (click.mp3).

So, i imagine that i cant remove the EventHandler onClickCross in the OnClickCross function . Let me paste my code below:

function onClickCross(button,state)  
    if button=="left" and state=="down" then 
        local imgX, imgY, imgWidth, imgHeight = (810/1024)*sx, (200/768)*sy , (20/1024)*sx, (20/768)*sy  --- Button Location on the screen 
        if isMouseInPosition ( imgX, imgY, imgWidth, imgHeight ) then 
            playSound("click.mp3") --- The click sound when you click on the button 
            --- Close all the pages 
            removeEventHandler("onClientRender",getRootElement(),playPanel) 
            removeEventHandler("onClientRender",getRootElement(),registerPanel) 
            removeEventHandler("onClientRender",getRootElement(),updatesPanel) 
            --- Close the onClickCross function (this function) 
            removeEventHandler("onClientRender",getRootElement(),onClickCross) 
        end 
    else 
        return false 
    end 
end 

That would be awesome if you can help me with that! Because im learning lua at the same time :P

(Sorry for my bad english by the way, french are not good at english you should know that ^^)

Link to comment

That's because it will always be true so long as your cursor is within the position you defined.

if isMouseInPosition ( imgX, imgY, imgWidth, imgHeight ) then 
            playSound("click.mp3") 

One way to remedy this would be to use a variable, for example;

local shouldPlaySound = true 
  
function onClickCross(button,state) 
    if button=="left" and state=="down" then 
        local imgX, imgY, imgWidth, imgHeight = (810/1024)*sx, (200/768)*sy , (20/1024)*sx, (20/768)*sy  --- Button Location on the screen 
        if isMouseInPosition ( imgX, imgY, imgWidth, imgHeight ) then 
            if(shouldPlaySound == true) then 
                playSound("click.mp3") --- The click sound when you click on the button 
            end 
            --- Close all the pages 
            removeEventHandler("onClientRender",getRootElement(),playPanel) 
            removeEventHandler("onClientRender",getRootElement(),registerPanel) 
            removeEventHandler("onClientRender",getRootElement(),updatesPanel) 
            --- Close the onClickCross function (this function) 
            removeEventHandler("onClientRender",getRootElement(),onClickCross) 
        end 
    else 
        return false 
    end 
end 

And then just change this variable between true and false depending on whether the interface is showing or not.

Link to comment

I've done it this way, its working. Thanks man, I appreciate you help! :D

local shouldPlaySound = false 

function onClickCross(button,state) 
    if button=="left" and state=="down" then 
        local imgX, imgY, imgWidth, imgHeight = (810/1024)*sx, (200/768)*sy , (20/1024)*sx, (20/768)*sy 
        if isMouseInPosition ( imgX, imgY, imgWidth, imgHeight ) then 
            if (shouldPlaySound == true) then 
                playSound("click.mp3") 
                removeEventHandler("onClientRender",getRootElement(),registerPanel) 
                removeEventHandler("onClientRender",getRootElement(),updatesPanel) 
                removeEventHandler("onClientRender",getRootElement(),playPanel) 
                removeEventHandler("onClientRender",getRootElement(),onClickCross) 
            end 
        shouldPlaySound = false 
        end 
    else 
        return false 
    end 
end 

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...