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

"""
Qt环境下的2D GUI测试
专门测试在Qt集成环境中的GUI显示问题
"""

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

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt
from QMeta3D.QMeta3DWidget import QMeta3DWidget
from QMeta3D.Meta3DWorld import Meta3DWorld
from direct.gui.DirectGui import *
from direct.gui.OnscreenText import OnscreenText
from panda3d.core import *
import os

class QtGUITestWorld(Meta3DWorld):
    def __init__(self):
        super().__init__()
        
        # 设置背景色
        self.setBackgroundColor(0.2, 0.2, 0.3)
        
        # 尝试加载中文字体
        try:
            self.chinese_font = self.loader.loadFont('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc')
        except:
            self.chinese_font = None
        
        # 延迟创建GUI，确保渲染系统就绪
        self.taskMgr.doMethodLater(0.5, self.createTestGUI, "create-gui")
    
    def createTestGUI(self, task=None):
        """创建测试GUI"""
        print("\n=== Qt环境GUI测试 ===")
        
        # 检查render2d状态
        print(f"render2d: {self.render2d}")
        print(f"aspect2d: {self.aspect2d}")
        print(f"pixel2d: {self.pixel2d}")
        
        # 创建标题
        self.title = OnscreenText(
            text="Qt环境GUI测试",
            pos=(0, 0.9),
            scale=0.08,
            fg=(1, 1, 1, 1),
            align=TextNode.ACenter,
            font=self.chinese_font if self.chinese_font else None
        )
        print(f"标题创建成功: {self.title}")
        
        # 创建简单的按钮
        self.testButton = DirectButton(
            text="测试按钮",
            pos=(0, 0, 0),
            scale=0.1,
            command=self.onButtonClick,
            frameColor=(1, 0, 0, 1),  # 使用红色，更容易看见
            text_font=self.chinese_font if self.chinese_font else None,
            rolloverSound=None,
            clickSound=None
        )
        print(f"按钮创建成功: {self.testButton}")
        print(f"按钮父节点: {self.testButton.getParent()}")
        print(f"按钮位置: {self.testButton.getPos()}")
        print(f"按钮隐藏状态: {self.testButton.isHidden()}")
        
        # 创建标签
        self.testLabel = DirectLabel(
            text="测试标签",
            pos=(0, 0, -0.3),
            scale=0.08,
            frameColor=(0, 1, 0, 1),  # 使用绿色背景
            text_fg=(0, 0, 0, 1),     # 黑色文字
            text_font=self.chinese_font if self.chinese_font else None
        )
        print(f"标签创建成功: {self.testLabel}")
        
        # 检查render2d的子节点
        print(f"\nrender2d子节点数量: {self.render2d.getNumChildren()}")
        for i in range(self.render2d.getNumChildren()):
            child = self.render2d.getChild(i)
            print(f"  子节点 {i}: {child}")
        
        print(f"\naspect2d子节点数量: {self.aspect2d.getNumChildren()}")
        for i in range(self.aspect2d.getNumChildren()):
            child = self.aspect2d.getChild(i)
            print(f"  子节点 {i}: {child}")
        
        # 检查相机设置
        print(f"\nrender2d相机: {self.camera2d}")
        print(f"render2d相机位置: {self.camera2d.getPos()}")
        print(f"render2d相机HPR: {self.camera2d.getHpr()}")
        
        # 尝试手动刷新2D渲染
        if hasattr(self, 'graphicsEngine'):
            print("\n尝试刷新图形引擎...")
            self.graphicsEngine.renderFrame()
        
        return task.done if task else None
    
    def onButtonClick(self):
        """按钮点击事件"""
        print("✓ 按钮被点击了！")

class QtGUITestWidget(QMeta3DWidget):
    def __init__(self, world, parent=None):
        super().__init__(world, parent)
        self.world = world

def main():
    """主函数"""
    print("启动Qt环境GUI测试...")
    
    app = QApplication(sys.argv)
    
    # 创建测试世界
    world = QtGUITestWorld()
    
    # 创建主窗口
    mainWindow = QMainWindow()
    mainWindow.setWindowTitle("Qt环境GUI测试")
    mainWindow.setGeometry(100, 100, 800, 600)
    
    # 创建Panda3D部件
    pandaWidget = QtGUITestWidget(world)
    mainWindow.setCentralWidget(pandaWidget)
    
    # 显示窗口
    mainWindow.show()
    
    print("窗口已显示，检查控制台输出...")
    print("如果看到红色按钮和绿色标签，说明Qt环境GUI工作正常")
    
    # 运行应用
    return app.exec_()

if __name__ == "__main__":
    exit_code = main() 