hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en
MIT License
13.03k stars 677 forks source link

get_mouse_pos(local=True) returns wrong values when no_title_bar = True #2243

Open coz-eduardo-hernandez opened 9 months ago

coz-eduardo-hernandez commented 9 months ago

Version of Dear PyGui

Version: 1.10 Operating System: Windows 10

My Issue/Question

get_mouse_pos() value turns negative when going where into the title bar area, which makes sense when there's a title bar, but when where's none either explicitly or by assigning a primary window the same calculations are given A clear and concise description of what the issue/question is. Please provide as much context as possible.

To Reproduce

  1. Create a window with no_title_bar = True
  2. Place the mouse where the title would be
  3. Issue a call to get_mouse_pos()
  4. Notice the negative value

Expected behavior

Get coordinates relative to 0,0 in the window

Standalone, minimal, complete and verifiable example

import dearpygui.dearpygui as dpg

def log( text ):
    config = dpg.get_item_configuration( "Listbox" )
    dpg.configure_item( "Listbox", items = config[ "items" ] + [ text ], num_items = config[ "num_items" ] + 1 )

def moveCallback(sender,app_data):
    log(dpg.get_mouse_pos(local=True))
    log(app_data)

dpg.create_context()
dpg.create_viewport()

with dpg.window( tag = "Primary Window", pos=[0,0], width=600,height=600, no_title_bar=True ):
    dpg.add_listbox(
        [],
        tag = "Listbox",
        pos = [ 100,100],
        width = 400,
        tracked=True,
        track_offset=1.0,
        num_items = 0
    )

    with dpg.handler_registry():
        dpg.add_mouse_move_handler(callback=moveCallback)

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
coz-eduardo-hernandez commented 9 months ago

Might be related to https://github.com/hoffstadt/DearPyGui/issues/1550

@v-ein wrote on Discord:

It does indeed subtract title bar height unconditionally:

if (IsItemFocused(item.state))
{
    float titleBarHeight = ImGui::GetStyle().FramePadding.y * 2 + ImGui::GetFontSize();

    // update mouse
    ImVec2 mousePos = ImGui::GetMousePos();
    float x = mousePos.x - ImGui::GetWindowPos().x;
    float y = mousePos.y - ImGui::GetWindowPos().y - titleBarHeight;
    GContext->input.mousePos.x = (int)x;
    GContext->input.mousePos.y = (int)y;

So we need an extra "if" around titleBarHeight (or a ?:).