flamendless/Slab

GetMousePosition() gives the same result as GetMousePositionWindow()

Closed this issue · 6 comments

GetMousePosition() gives the same result as GetMousePositionWindow()

Will look on this. Thanks for reporting

@youssefsahli upon further investigation, it appears that there is actually no problem with this. Here are the descriptions of the API

GetMousePosition - Retrieves the current mouse position in the viewport.
GetMousePositionWindow - Retrieves the current mouse position within the current window. This position will include any transformations added to the window such as scrolling.

here is a sample test case:

        -- lets create a window that have contents that will make it overflow (to create the scrollbars)
	Slab.BeginWindow("sample", {Title = "Sample", W = 640, AutoSizeWindow = false})

        --print the values (these two should have the SAME values)
	local mx, my = Slab.GetMousePosition()
	Slab.Text("mx")
	Slab.SameLine()
	Slab.Input("mx", {Text = mx})
	Slab.Text("my")
	Slab.SameLine()
	Slab.Input("my", {Text = my})

	local wmx, wmy = Slab.GetMousePositionWindow()
	Slab.Text("wmx")
	Slab.SameLine()
	Slab.Input("wm", {Text = wmx})
	Slab.Text("wmy")
	Slab.SameLine()
	Slab.Input("wmy", {Text = wmy})
        Slab.EndWindow()

        -- make content bigger than the window (viewport)
	for i = 1, 30 do Slab.Text(i) end

        --print the values (these two should have different values)
	local mx2, my2 = Slab.GetMousePosition()
	Slab.Text("mx2")
	Slab.SameLine()
	Slab.Input("mx2", {Text = mx2})
	Slab.Text("my2")
	Slab.SameLine()
	Slab.Input("my2", {Text = my2})

	local wmx2, wmy2 = Slab.GetMousePositionWindow()
	Slab.Text("wmx2")
	Slab.SameLine()
	Slab.Input("wmx", {Text = wmx2})
	Slab.Text("wmy2")
	Slab.SameLine()
	Slab.Input("wmy2", {Text = wmy2})
        Slab.EndWindow()

if you test this, the first two functions will show you the same values because there's no change in transformation/viewport, but if you scroll down and check the two functions at the end, thus changing the viewport, you will see that the values returned are now different.

Note that using the two methods will get the current values from the current/active window.

I hope i explained this clearly, let me know if youre still confused :)

Oh sorry, I thought these were functions returning the coords of the mouse inside the window: by that I mean if the mouse is in the top left corner of the window, the function returns 0, 0

No, windows in this context are Slab windows. if you want of get the coordinates of the main window, you can use the love.mouse.getPosition function

What I mean is:

When the cursor is here (see screenshot), I thought GetMousePositionWindow gives the relative position of the cursor inside the Slab window

Screenshot from 2022-01-22 14-56-59

Hmmm what's the usecase? You can easily get that by doing:

local wx, wy = Slab.GetWindowPosition()
local mx, my = Slab.GetMousePosition()
local relative_x = mx - wx
local relative_y = my - wy

If i can know a good usecase for this, I will add this in the API :)