Imgui get / set cursor position

I am using the @britzl imgui extension for Defold. Maybe I am missing something. But it seems to me that the extension implements the lua version of: (1) set_cursor_pos and (2) get_cursor_screen_pos.

One can use these in combination with get_window_pos to get also get_cursor_pos. But it seems that I need also imgui.get_scroll_x / imgui.get_scroll_y in order to convert from window to screen pos (in imgui space). Indeed my code seems to work as far as there is no scroll bar…

Is there some workaround I am missing? or have I misunderstood something?

Many thanks!

I have not really considered this usecase and I can’t say if this is the way or if there is a workaround. Maybe there’s some additional functions in the imgui api to help with this?

I reread your question and I’m not sure I understand what problem you are having. What do you want to do?

I am sorry, I was not good at explaining. And thank you for your answer!

I want to implement some custom widget and, for this, I need to “move” the cursor. Looking at the source code, it seems to me that the extension is providing a lua binding only for set_cursor_pos and get_cursor_screen_pos. There is no binding for get_cursor_pos.

This last function can be implemented in lua by using get_cursor_screen_pos, get_window_pos and get_scroll_x / get_scroll_y. And this is the point, the extension does not bind these last two functions either.

Without get_scroll_x/y I have a get_cursor_pos in lua that works ONLY if there is no scroll bar

I have tried to look at the API and it seems to me that there is no solution. So, I would like to ask if you could implement get_cursor_pos.

I hope I have been able to explain better my problem.

Thank you for the support as always!!!

Have you tried making a fork of the extension, and adding this example yourself?
We’d be happy to accept a Pull Request.

Dear Mathias, thanks!

I think I know how to code the binding I need. But I am completely ignorant about “making a fork” and “pull request”. It would be a very inefficient way of keeping the entropy of the universe low :slight_smile: :slight_smile: :slight_smile:

I will try anyway…

I have created a pull request for the binding of imgui.get_cursor_pos. First pull request in my life, so be patient… It was not hard :slight_smile:

1 Like

Thanks. I have added a review with a small change.

I don’t understand how to do it. Some lines above there is imgui_GetCursorScreenPos (already there, not written by me) and I have copied the same format of the documentation. Could you please explain to me what to do?

The function returns two values right? The documentation/annotation above the function should reflect this (and yes, imgui_GetCursorScreenPos is also wrong):

/** GetCursorPos
 * @name get_cursor_pos
 * @treturn number x Cursor x position
 * @treturn number y Cursor y position
 */
static int imgui_GetCursorPos(lua_State* L)
{
    DM_LUA_STACK_CHECK(L, 2);

    ImVec2 p = ImGui::GetCursorPos();
    lua_pushnumber(L, p.x);
    lua_pushnumber(L, p.y);
    return 2;
}
1 Like

Thanks for this! Done for get_cursor_pos and get_cursor_screen_pos; it should be correct.

1 Like