Jump to content

get(absolute)CursorPosition


UnchiuDawg

Recommended Posts

Hello, I am trying to make a scroll bar that can be dragged using the dx functions.

Long story short, want to compare the absoluteY returned by onClientClick with the absoluteY of the current frame (by using onClientRender). The only way to get the current cursor location this way is by using getCursorPosition, which does not return an absolute position.

I couldn't find any other function to do this and dividing the screen height by the float returned by getCursorPosition is not accurate.

Is there any way to do this? Am I missing something? plz halp lol

Edited by UnchiuDawg
Link to post
21 minutes ago, thisdp said:

local scw,sch = guiGetScreenSize()

local cursorX,cursorY = getCursorPosition()

local absX,absY = cursorX*scw,cursorY*sch

 

42 minutes ago, UnchiuDawg said:

dividing the screen height by the float returned by getCursorPosition is not accurate.

I realised my typo, I meant to say that multiplying the float returned by getCursorPosition with the screen height is not accurate. I'd like to know if there is a way to get a more accurate position ^^

Thanks for replying anyway.

Edited by UnchiuDawg
Link to post
2 hours ago, UnchiuDawg said:

I couldn't find any other function to do this and dividing the screen height by the float returned by getCursorPosition is not accurate.

Something else must be off in your script. For example the way you're drawing the GUI, the positions may be off, or you might have to adjust the mouse pos accordingly.

Show us how you're drawing the stuff, in e.x. the positions.

Multiplying the positions by the screen size makes it as accurate as it can be.

The relative positions that getCursorPosition gives you are just values between 0 and 1, they're fixed they never change. Each pixel you move to will have the same float. It's not a matter of accuracy.

Edited by Tails
  • Like 1
Link to post
9 hours ago, Tails said:

Something else must be off in your script. For example the way you're drawing the GUI, the positions may be off, or you might have to adjust the mouse pos accordingly.

Show us how you're drawing the stuff, in e.x. the positions.

Multiplying the positions by the screen size makes it as accurate as it can be.

The relative positions that getCursorPosition gives you are just values between 0 and 1, they're fixed they never change. Each pixel you move to will have the same float. It's not a matter of accuracy.

Refer to dgs.

https://forum.mtasa.com/topic/95964-releasethisdps

Edited by thisdp
  • Thanks 1
Link to post
local scw,sch = guiGetScreenSize()

local cursorX,cursorY = getCursorPosition()

local absX,absY = cursorX*scw,cursorY*sch

Code above will always return correct absolute coordinate. Maybe you have some problem on client click event, because it's dragging.

  • Like 1
Link to post
1 hour ago, Tails said:

What about it?

Since the problem comes from dx scrollbar, DGS(a dx lib) can help him to solve the problem and give him a reference.

  • Like 1
Link to post

Thank you everybody, I managed to make it as accurate as possible by using onClientCursorMove.

 

To answer a few questions: I would drag the scroll bar by comparing the absolute position clicked (by using  onClientClick) with the float position returned by  getCursorPosition multiplied by the screenHeight. I used math.ceil in order to move the scroll bar if there was even a slight difference to approximate the distance between those two positions to one unit.

The "problem" was that sometimes the absolute position clicked and the float multiplied by the scren height would not be the same (even though I haven't moved the cursor yet) perhaps by a few pixels, which ended up moving the scroll bar as if I had moved the cursor. This wasn't a huge problem and I know I could have fixed it by not using math.ceil for any value between 0 and 1, but before trying that I was curious if there was any function/event I missed (and it turns out there was). So thanks again ^^

  • Like 1
Link to post
mouseOverScrollbar = false
addEventHandler("onClientClick", root,
  function (button, state, cx, cy)
    local x, y -- your scrollbar position
    local width, height -- your scrollbar with and height
    if button == "left" then
      if state == "down" then
        if ( cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height ) then
          mouseOverScrollbar = true
        else
          mouseOverScrollbar = false
        end
      elseif state == "up" then
          mouseOverScrollbar = false
      end
    end
  end
)

addEventHandler( "onClientCursorMove", root,
    function ()
        if mouseOverScrollbar then
            -- your calculation here
        end
    end
)

 

Edited by idarrr
Link to post

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