[Python] Detect hovering over a widget and its children in Pyimgui

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    Is there a smart way to detect mouse events over a parent and all its children at once in PyImGui?

    Typically, if I hover a window that contains multiple items (child, button, input_text_multiline, text, ...), I have to track everyone either with imgui.is_item_hovered() or imgui.is_window_hovered() to make sure the event is triggered.

    This is what I ended up doing:

    import imgui

    def draw(self):

    hovered = []

    def check_hover():
    if imgui.is_item_hovered() or imgui.is_window_hovered():
    hovered.append(True)

    # Start a new ImGui window for the console
    with imgui.begin("Console"):
    check_hover()

    # Get the available size for the text editor region
    available_size = imgui.get_content_region_available()

    # Begin a child window for scroll synchronization between the line numbers and text
    with imgui.begin_child("##scrolling_region", available_size.x, available_size.y,
    border=False, flags=imgui.WINDOW_NO_BACKGROUND):
    imgui.columns(2)

    # Left column: line numbers
    imgui.set_column_width(0, 25)
    check_hover()

    for i in range(len(self.file_line_numbers)):
    imgui.text(str(i + 1)) # Display the line numbers

    # Right column: The multiline text editor
    imgui.next_column()
    text_changed, text_lines = imgui.input_text_multiline(
    "##editor", self.file_content, len(self.file_line_numbers),
    available_size.x, available_size.y)
    if text_changed: # If the text was changed, update the original list of lines
    self.file_content = text_lines #.splitlines(keepends=True) # Split back into lines
    check_hover()

    if imgui.button("Load"):
    self.load_file()
    imgui.same_line()
    if imgui.button("Save"):
    self.save_file(self.file_content)
    check_hover()


    if any(hovered): # if at least one of the items is hovered
    self.is_hovered = True
    print("console hovered")
    else:
    self.is_hovered = False

    Continue reading...

Compartilhe esta Página