Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add desktop argument to mouse.get_pos and mouse.get_pressed #3105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

damusss
Copy link
Contributor

@damusss damusss commented Sep 16, 2024

This pull request combines the following PRs adopting a "better" API (suggested by @ankith26 ) while retaining the functionality. To know about usecases or why please check them both:

Combined Sample Code

import pygame

win = pygame.Window("desktop pressed test", (200, 200))
win.always_on_top = True

# if a member of the SC is reading this, Window.surface >>>> Window.get_surface!
screen = win.get_surface()

clock = pygame.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.WINDOWCLOSE and event.window == win:
            pygame.quit()
            raise SystemExit

    screen.fill(0)

    # DESKTOP PRESSED
    dmouse = pygame.mouse.get_pressed(desktop=True)[0]
    mouse = pygame.mouse.get_pressed()[0]
    if mouse and dmouse:
        print("Pressing LEFT with focus")
    elif dmouse:
        print("Pressing LEFT without focus")

        # click other windows, click the taskbar, you'll always get the left button pressed
        # also test this: after clicking on another window,
        # hold the mouse on the window. You'll see it says "pressing without focus" until you click again

    # DESKTOP POS
    if not mouse and not dmouse:
        dpos = pygame.mouse.get_pos(desktop=True)
        win_rect = pygame.Rect(win.position, win.size)
        if win_rect.collidepoint(dpos):
            print(f"MOUSE HOVERING! desktop pos: {dpos}")
        else:
            print(f"NO HOVER. desktop pos: {dpos}")

        # you will get the hover status regardless of focus

    win.flip()
    clock.tick(60)

@damusss damusss added New API This pull request may need extra debate as it adds a new class or function to pygame mouse pygame.mouse labels Sep 16, 2024
@damusss damusss requested a review from a team as a code owner September 16, 2024 12:46
@damusss damusss changed the title Add desktop argument to mouse.get_posand mouse.get_pressed Add desktop argument to mouse.get_pos and mouse.get_pressed Sep 16, 2024
Copy link
Contributor

@yunline yunline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the position of get_pos(desktop=True) is not scaled in SCALED mode.
Will this cause a problem (?

@damusss
Copy link
Contributor Author

damusss commented Sep 22, 2024

I found the position of get_pos(desktop=True) is not scaled in SCALED mode.
Will this cause a problem (?

Hi :)
as far as I know SCALED is related to a window setting, but the desktop position is relative to the monitor, so by my understanding the monitor position should not be affected by the window size or fullscreen status, so I believe it's not a problem. The only thing that comes to mind is Wayland, but I'm not sure it's possible to check for it or if it's worth it.

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I prefer this API over the previous 2 PRs that were adding this functionality with a new function.

One thing that could be potentially discussed further is the keyword argument naming. Is desktop good enough or is there something better? I am fine with desktop, but I'm open to other suggestions.

@ankith26
Copy link
Member

We could also make a note about the SCALED thing in the docs

@ankith26 ankith26 added this to the 2.5.2 milestone Sep 23, 2024
@damusss
Copy link
Contributor Author

damusss commented Sep 23, 2024

One thing that could be potentially discussed further is the keyword argument naming. Is desktop good enough or is there something better? I am fine with desktop, but I'm open to other suggestions.

I used Andrew's suggestion. SDL calls it global but I don't think it conveys enough information. absolute was an option but it can be confused with clamped or abs()

@damusss
Copy link
Contributor Author

damusss commented Sep 23, 2024

We could also make a note about the SCALED thing in the docs

Is it necessary to specify that it's not effected by the SCALED flag?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mouse pygame.mouse New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants