#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
工具栏GUI测试
模拟用户点击工具栏按钮创建GUI元素
"""

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QTimer

# 导入主程序
from test import MyWorld, CustomPanda3DWidget

def testToolbarGUI():
    """测试工具栏GUI创建功能"""
    print("=== 工具栏GUI创建测试 ===")
    
    app = QApplication(sys.argv)
    
    # 创建主程序的世界对象
    world = MyWorld()
    
    # 创建窗口
    mainWindow = QMainWindow()
    mainWindow.setWindowTitle("工具栏GUI测试")
    mainWindow.setGeometry(100, 100, 800, 600)
    
    # 创建Panda3D部件
    pandaWidget = CustomPanda3DWidget(world)
    mainWindow.setCentralWidget(pandaWidget)
    
    # 显示窗口
    mainWindow.show()
    
    # 模拟工具栏按钮点击
    def createGUIElements():
        print("\n模拟工具栏按钮点击...")
        
        # 创建一些GUI元素，使用合理的坐标
        print("创建GUI按钮...")
        button1 = world.createGUIButton((0, 0, 0), "中心按钮", 0.08)
        print(f"按钮1屏幕位置: {button1.getPos()}")
        
        button2 = world.createGUIButton((-10, 0, 0), "左按钮", 0.08)
        print(f"按钮2屏幕位置: {button2.getPos()}")
        
        button3 = world.createGUIButton((10, 0, 0), "右按钮", 0.08)
        print(f"按钮3屏幕位置: {button3.getPos()}")
        
        print("创建GUI标签...")
        label1 = world.createGUILabel((0, 0, -10), "测试标签", 0.06)
        print(f"标签1屏幕位置: {label1.getPos()}")
        
        print("创建GUI输入框...")
        entry1 = world.createGUIEntry((0, 0, -15), "输入测试", 0.05)
        print(f"输入框1屏幕位置: {entry1.getPos()}")
        
        print("创建3D文本...")
        text3d = world.createGUI3DText((0, 5, 2), "3D测试", 0.5)
        print(f"3D文本位置: {text3d.getPos()}")
        
        print(f"\n总共创建了 {len(world.gui_elements)} 个GUI元素")
        
        # 检查所有坐标是否在有效范围内
        print("\n检查2D GUI坐标范围:")
        for i, element in enumerate(world.gui_elements):
            if hasattr(element, 'getTag'):
                gui_type = element.getTag("gui_type")
                if gui_type in ["button", "label", "entry"]:
                    pos = element.getPos()
                    x, z = pos.getX(), pos.getZ()
                    in_range = (-1 <= x <= 1) and (-1 <= z <= 1)
                    print(f"  {gui_type}: ({x:.3f}, {z:.3f}) - {'✓' if in_range else '✗ 超出范围'}")
        
        print("\n如果你能看到蓝色按钮、白色标签和输入框，说明修复成功！")
        print("如果只能看到黄色3D文本，说明2D GUI坐标可能仍有问题。")
    
    # 1秒后创建GUI元素
    QTimer.singleShot(1000, createGUIElements)
    
    print("窗口已显示，即将测试GUI创建...")
    
    return app.exec_()

if __name__ == "__main__":
    testToolbarGUI() 