import pygame
import ctypes

# 初始化pygame
pygame.init()
WIDTH, HEIGHT = 640, 420
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pygame 长度单位转换器")
clock = pygame.time.Clock()

# 字体
font = pygame.font.SysFont("simhei", 22)
small_font = pygame.font.SysFont("simhei", 18)

# 获取系统DPI Windows专用
def get_dpi():
    try:
        user32 = ctypes.windll.user32
        user32.SetProcessDPIAware()
        hdc = user32.GetDC(0)
        gdi32 = ctypes.WinDLL("gdi32", use_last_error=True)
        dpi = gdi32.GetDeviceCaps(hdc, 88)  # LOGPIXELSX
        user32.ReleaseDC(0, hdc)
        return dpi
    except:
        return 96  # 默认DPI

DPI = get_dpi()
INCH_TO_MM = 25.4
PIX_PER_MM = DPI / INCH_TO_MM  # 每毫米像素

# 单位转换核心函数
def px_to_mm(px):
    return px / PIX_PER_MM

def mm_to_px(mm):
    return mm * PIX_PER_MM

def mm_to_cm(mm):
    return mm / 10

def cm_to_mm(cm):
    return cm * 10

def mm_to_inch(mm):
    return mm / INCH_TO_MM

def inch_to_mm(inch):
    return inch * INCH_TO_MM

# 输入状态
input_text = "100"
active_type = 0  # 0:px 1:mm 2:cm 3:in
unit_names = ["像素(px)", "毫米(mm)", "厘米(cm)", "英寸(in)"]

running = True
while running:
    screen.fill((30, 30, 40))
    mouse_pos = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            # 切换输入单位
            for i in range(4):
                rect = pygame.Rect(40, 40 + i * 60, 160, 45)
                if rect.collidepoint(mouse_pos):
                    active_type = i

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_BACKSPACE:
                input_text = input_text[:-1]
            elif event.key == pygame.K_RETURN:
                pass
            else:
                if event.unicode in "0123456789.":
                    input_text += event.unicode

    # 解析输入数值
    try:
        val = float(input_text)
    except:
        val = 0

    # 统一转换成毫米作为中间量
    if active_type == 0:
        mm_val = px_to_mm(val)
    elif active_type == 1:
        mm_val = val
    elif active_type == 2:
        mm_val = cm_to_mm(val)
    else:
        mm_val = inch_to_mm(val)

    # 计算所有单位
    px_val = mm_to_px(mm_val)
    cm_val = mm_to_cm(mm_val)
    inch_val = mm_to_inch(mm_val)
    results = [px_val, mm_val, cm_val, inch_val]

    # 绘制选项
    for i in range(4):
        rect = pygame.Rect(40, 40 + i * 60, 160, 45)
        color = (60, 120, 200) if i == active_type else (50, 50, 70)
        pygame.draw.rect(screen, color, rect, border_radius=6)
        pygame.draw.rect(screen, (180, 180, 220), rect, 2, border_radius=6)
        txt = font.render(unit_names[i], True, (255, 255, 255))
        screen.blit(txt, (rect.x + 12, rect.y + 10))

    # 输入框
    input_rect = pygame.Rect(230, 40 + active_type * 60, 340, 45)
    pygame.draw.rect(screen, (20, 20, 30), input_rect, border_radius=6)
    pygame.draw.rect(screen, (100, 160, 255), input_rect, 2, border_radius=6)
    input_surf = font.render(input_text, True, (255, 255, 255))
    screen.blit(input_surf, (input_rect.x + 12, input_rect.y + 10))

    # 输出全部换算结果
    y_offset = 300
    tip = small_font.render(f"屏幕DPI:{DPI} | 点击左侧单位切换输入类型", True, (160,160,180))
    screen.blit(tip, (40, y_offset - 40))

    for i in range(4):
        text_str = f"{unit_names[i]} = {results[i]:.3f}"
        res_surf = font.render(text_str, True, (220, 220, 255))
        screen.blit(res_surf, (40, y_offset))
        y_offset += 36

    pygame.display.flip()
    clock.tick(60)

pygame.quit()