Skip to content

Port SDL_SetWindowHitTest #2582

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yunline
Copy link
Contributor

@yunline yunline commented Nov 22, 2023

Test code:

import pygame
from pygame._sdl2 import video

pygame.init()

font = pygame.font.Font(size=24)

win = video.Window(size=(640, 480), borderless=True)
win.resizable = True
win.borderless = True
win.minimum_size = (480,360)
screen = win.get_surface()


def updata_size():
    global WIN_SIZE, RECT_DRAG, RECT_TOP, RECT_TOPLEFT, RECT_LEFT
    global RECT_BOTTOMLEFT, RECT_BOTTOM, RECT_BOTTOMRIGHT, RECT_RIGHT, RECT_TOPRIGHT
    WIN_SIZE = win.size
    RECT_DRAG = pygame.Rect((0, 0), WIN_SIZE).inflate(-200, -200)
    RECT_TOP = pygame.Rect(WIN_SIZE[0] / 2 - 200 / 2, 0, 200, 50)
    RECT_TOPLEFT = pygame.Rect(0, 0, 60, 60)
    RECT_LEFT = pygame.Rect(0, WIN_SIZE[1] / 2 - 120 / 2, 70, 120)
    RECT_BOTTOMLEFT = pygame.Rect(0, WIN_SIZE[1] - 60, 60, 60)
    RECT_BOTTOM = pygame.Rect(WIN_SIZE[0] / 2 - 200 / 2, WIN_SIZE[1] - 50, 200, 50)
    RECT_BOTTOMRIGHT = pygame.Rect(WIN_SIZE[0] - 60, WIN_SIZE[1] - 60, 60, 60)
    RECT_RIGHT = pygame.Rect(WIN_SIZE[0] - 70, WIN_SIZE[1] / 2 - 120 / 2, 70, 120)
    RECT_TOPRIGHT = pygame.Rect(WIN_SIZE[0] - 60, 0, 60, 60)

    win.clear_hit_test()
    win.add_draggable_hit_test(RECT_DRAG)
    win.add_resize_hit_test(RECT_TOP, "top")
    win.add_resize_hit_test(RECT_TOPLEFT, "topleft")
    win.add_resize_hit_test(RECT_LEFT, "left")
    win.add_resize_hit_test(RECT_BOTTOMLEFT, "bottomleft")
    win.add_resize_hit_test(RECT_BOTTOM, "bottom")
    win.add_resize_hit_test(RECT_BOTTOMRIGHT, "bottomright")
    win.add_resize_hit_test(RECT_RIGHT, "right")
    win.add_resize_hit_test(RECT_TOPRIGHT, "topright")


updata_size()


def draw(screen):
    screen.fill((60, 60, 60))
    pygame.draw.rect(
        screen, "white", pygame.Rect((0, 0), WIN_SIZE).inflate(-30, -30), width=4
    )

    def draw_box(rect, text):
        pygame.draw.rect(screen, (100, 100, 100), rect)
        text_rect = rect.inflate(-20, -20)
        text_surf = font.render(text, True, "white", wraplength=max(1,text_rect.width))
        screen.blit(text_surf, text_rect.topleft)

    draw_box(RECT_DRAG, "drag me to move the window\nPress Esc to exit")
    draw_box(RECT_TOP, "drag me to resize")
    draw_box(RECT_TOPLEFT, "drag me")
    draw_box(RECT_LEFT, "drag me to resize")
    draw_box(RECT_BOTTOMLEFT, "drag me")
    draw_box(RECT_BOTTOM, "drag me to resize")
    draw_box(RECT_BOTTOMRIGHT, "drag me")
    draw_box(RECT_RIGHT, "drag me to resize")
    draw_box(RECT_TOPRIGHT, "drag me")


cnt = 0
while 1:
    draw(screen)
    win.flip()
    for event in pygame.event.get():
        if event.type == pygame.WINDOWHITTEST:
            print(f"hit test triggered {(cnt:=cnt+1)}")
        if event.type == pygame.WINDOWRESIZED:
            updata_size()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            raise SystemExit

图片

2023-11-22.10-01-04.mp4

@yunline yunline added the video pygame.video label Nov 22, 2023
@yunline yunline requested a review from a team as a code owner November 22, 2023 01:51
@Matiiss
Copy link
Member

Matiiss commented Nov 26, 2023

What about calling this a region or area instead of hit_test, I think it would be slightly more intuitive that way.
So, add_draggable_region, add_resize_region or add_draggable_area, add_resize_area.

@Starbuck5 Starbuck5 added the window pygame.Window label Dec 15, 2023
@yunline yunline marked this pull request as draft December 16, 2023 06:08
@yunline
Copy link
Contributor Author

yunline commented Dec 16, 2023

In electron, they use -webkit-app-region: drag to set a draggable region.
https://www.electronjs.org/docs/latest/tutorial/window-customization#set-custom-draggable-region

So Window.add_draggable_region(), Window.add_resize_region() and Window.clear_special_regions() could be better names (?
But the name of the hittest event is still WINDOWHITTEST, shall we also change the event name?

@Matiiss
Copy link
Member

Matiiss commented Dec 16, 2023

Yeah, region seems fine.

I don't think the event should be renamed, what would you rename it to then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
video pygame.video window pygame.Window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants