"""
屏幕坐标测量工具
用于确定手机屏幕的实际触摸坐标范围
"""
import os
import sys
import time
import subprocess

# ADB 路径
ADB_PATH = r"C:\Users\DYB\Desktop\ADB\platform-tools\platform-tools\adb.exe"

def adb_shell(cmd):
    """执行 adb shell 命令"""
    try:
        result = subprocess.run(
            [ADB_PATH, "shell", cmd],
            capture_output=True,
            text=True,
            timeout=10
        )
        return result.stdout.strip()
    except Exception as e:
        print(f"Error: {e}")
        return None

def main():
    print("=" * 60)
    print("  Screen Coordinate Measurement Tool")
    print("=" * 60)
    
    # 检查设备
    print("\nChecking devices...")
    result = subprocess.run([ADB_PATH, "devices"], capture_output=True, text=True)
    print(result.stdout)
    
    if "device" not in result.stdout.split("\n")[-2] if "\n" in result.stdout else "device" not in result.stdout:
        print("No device found!")
        return
    
    print("Device connected!\n")
    
    # 获取物理分辨率
    print("Getting physical resolution...")
    result = adb_shell("wm size")
    print(f"  Raw: {result}")
    
    # 获取物理尺寸
    result = adb_shell("dumpsys window displays | grep -E 'mCurrentFocus|mFocusedApp'")
    print(f"  Window: {result}")
    
    print("\n" + "=" * 60)
    print("  Now testing corner points...")
    print("  Watch the game's touch indicator!")
    print("=" * 60 + "\n")
    
    # 使用不同的分辨率测试
    # 测试1: 使用较大的值
    test_configs = [
        (1080, 2400, "1080x2400"),
        (2400, 1080, "2400x1080"),
        (1080, 1080, "1080x1080"),
        (2400, 2400, "2400x2400"),
    ]
    
    for width, height, name in test_configs:
        print(f"\n--- Testing {name} ---")
        
        # 点击四个角
        corners = [
            (0, 0, "Top-Left corner (0, 0)"),
            (width - 1, 0, f"Top-Right corner ({width-1}, 0)"),
            (width - 1, height - 1, f"Bottom-Right corner ({width-1}, {height-1})"),
            (0, height - 1, f"Bottom-Left corner (0, {height-1})"),
        ]
        
        for x, y, desc in corners:
            print(f"  Tapping {desc}...")
            adb_shell(f"input tap {x} {y}")
            time.sleep(0.8)
    
    print("\n" + "=" * 60)
    print("  Test complete!")
    print("  Tell me which corners were actually touched!")
    print("  Example: 'Top-left was at (500, 500) in game'")
    print("=" * 60)

if __name__ == "__main__":
    main()
