pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Port `SDL_SetWindowHitTest`

Open yunline opened this issue 1 year ago • 3 comments
trafficstars

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

图片

https://github.com/pygame-community/pygame-ce/assets/31395137/eff834e2-932d-480a-8ecd-42ff8bb20b64

yunline avatar Nov 22 '23 01:11 yunline