224 lines
7.1 KiB
Python
224 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
Qt环境 vs ShowBase环境 GUI对比测试
|
||
验证两种环境中DirectGUI的差异
|
||
"""
|
||
|
||
import warnings
|
||
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||
|
||
import sys
|
||
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QLabel, QPushButton
|
||
from PyQt5.QtCore import Qt
|
||
from QMeta3D.QMeta3DWidget import QMeta3DWidget
|
||
from QMeta3D.Meta3DWorld import Meta3DWorld
|
||
from direct.showbase.ShowBase import ShowBase
|
||
from direct.gui.DirectGui import *
|
||
from direct.gui.OnscreenText import OnscreenText
|
||
from panda3d.core import *
|
||
|
||
class QtTestWorld(Meta3DWorld):
|
||
"""Qt集成环境测试"""
|
||
def __init__(self):
|
||
super().__init__()
|
||
|
||
print("=== Qt环境GUI测试 ===")
|
||
self.setBackgroundColor(0.2, 0.2, 0.3)
|
||
|
||
# 检查环境
|
||
print(f"基类: {self.__class__.__bases__}")
|
||
print(f"render2d: {self.render2d}")
|
||
print(f"aspect2d: {self.aspect2d}")
|
||
print(f"窗口类型: {type(self.win) if hasattr(self, 'win') else 'None'}")
|
||
|
||
# 延迟创建GUI
|
||
self.taskMgr.doMethodLater(1.0, self.createTestGUI, "create-gui")
|
||
|
||
def createTestGUI(self, task=None):
|
||
"""创建测试GUI"""
|
||
print("\n--- Qt环境创建GUI ---")
|
||
|
||
# 完全模仿gui_3d_demo.py的创建方式
|
||
try:
|
||
self.testButton = DirectButton(
|
||
text="Qt测试按钮",
|
||
pos=(0, 0, 0),
|
||
scale=0.1,
|
||
command=self.onButtonClick,
|
||
frameColor=(1, 0, 0, 1), # 红色,更显眼
|
||
text_font=None
|
||
)
|
||
print(f"✓ Qt环境按钮创建成功: {self.testButton}")
|
||
print(f" 父节点: {self.testButton.getParent()}")
|
||
print(f" 位置: {self.testButton.getPos()}")
|
||
print(f" 隐藏状态: {self.testButton.isHidden()}")
|
||
print(f" 渲染状态: {self.testButton.node().isOnstage()}")
|
||
|
||
except Exception as e:
|
||
print(f"✗ Qt环境按钮创建失败: {str(e)}")
|
||
|
||
# 创建标题文本
|
||
try:
|
||
self.title = OnscreenText(
|
||
text="Qt Environment Test",
|
||
pos=(0, 0.8),
|
||
scale=0.08,
|
||
fg=(1, 1, 1, 1),
|
||
align=TextNode.ACenter
|
||
)
|
||
print(f"✓ Qt环境标题创建成功: {self.title}")
|
||
except Exception as e:
|
||
print(f"✗ Qt环境标题创建失败: {str(e)}")
|
||
|
||
# 检查render2d和aspect2d的状态
|
||
print(f"\nQt环境渲染状态:")
|
||
print(f" render2d子节点数: {self.render2d.getNumChildren()}")
|
||
print(f" aspect2d子节点数: {self.aspect2d.getNumChildren()}")
|
||
|
||
return task.done if task else None
|
||
|
||
def onButtonClick(self):
|
||
print("🎯 Qt环境按钮被点击了!")
|
||
|
||
class ShowBaseTestWorld(ShowBase):
|
||
"""ShowBase环境测试"""
|
||
def __init__(self):
|
||
ShowBase.__init__(self)
|
||
|
||
print("\n=== ShowBase环境GUI测试 ===")
|
||
self.setBackgroundColor(0.2, 0.2, 0.3)
|
||
|
||
# 检查环境
|
||
print(f"基类: {self.__class__.__bases__}")
|
||
print(f"render2d: {self.render2d}")
|
||
print(f"aspect2d: {self.aspect2d}")
|
||
print(f"窗口类型: {type(self.win)}")
|
||
|
||
# 立即创建GUI
|
||
self.createTestGUI()
|
||
|
||
def createTestGUI(self):
|
||
"""创建测试GUI"""
|
||
print("\n--- ShowBase环境创建GUI ---")
|
||
|
||
# 完全相同的GUI创建方式
|
||
try:
|
||
self.testButton = DirectButton(
|
||
text="ShowBase测试按钮",
|
||
pos=(0, 0, 0),
|
||
scale=0.1,
|
||
command=self.onButtonClick,
|
||
frameColor=(0, 1, 0, 1), # 绿色,区分
|
||
text_font=None
|
||
)
|
||
print(f"✓ ShowBase环境按钮创建成功: {self.testButton}")
|
||
print(f" 父节点: {self.testButton.getParent()}")
|
||
print(f" 位置: {self.testButton.getPos()}")
|
||
print(f" 隐藏状态: {self.testButton.isHidden()}")
|
||
print(f" 渲染状态: {self.testButton.node().isOnstage()}")
|
||
|
||
except Exception as e:
|
||
print(f"✗ ShowBase环境按钮创建失败: {str(e)}")
|
||
|
||
# 创建标题文本
|
||
try:
|
||
self.title = OnscreenText(
|
||
text="ShowBase Environment Test",
|
||
pos=(0, 0.8),
|
||
scale=0.08,
|
||
fg=(1, 1, 1, 1),
|
||
align=TextNode.ACenter
|
||
)
|
||
print(f"✓ ShowBase环境标题创建成功: {self.title}")
|
||
except Exception as e:
|
||
print(f"✗ ShowBase环境标题创建失败: {str(e)}")
|
||
|
||
# 检查render2d和aspect2d的状态
|
||
print(f"\nShowBase环境渲染状态:")
|
||
print(f" render2d子节点数: {self.render2d.getNumChildren()}")
|
||
print(f" aspect2d子节点数: {self.aspect2d.getNumChildren()}")
|
||
|
||
def onButtonClick(self):
|
||
print("🎯 ShowBase环境按钮被点击了!")
|
||
|
||
def testQtEnvironment():
|
||
"""测试Qt环境"""
|
||
print("启动Qt环境测试...")
|
||
|
||
app = QApplication(sys.argv)
|
||
|
||
# 创建主窗口
|
||
mainWindow = QMainWindow()
|
||
mainWindow.setWindowTitle("Qt环境测试")
|
||
mainWindow.setGeometry(100, 100, 600, 400)
|
||
|
||
# 创建布局
|
||
centralWidget = QWidget()
|
||
layout = QVBoxLayout(centralWidget)
|
||
|
||
# 添加说明
|
||
label = QLabel("Qt环境DirectGUI测试")
|
||
label.setAlignment(Qt.AlignCenter)
|
||
label.setStyleSheet("background-color: lightblue; padding: 10px;")
|
||
layout.addWidget(label)
|
||
|
||
# 创建世界
|
||
world = QtTestWorld()
|
||
|
||
# 创建Panda3D部件
|
||
pandaWidget = QMeta3DWidget(world)
|
||
layout.addWidget(pandaWidget)
|
||
|
||
mainWindow.setCentralWidget(centralWidget)
|
||
mainWindow.show()
|
||
|
||
print("Qt环境窗口已显示")
|
||
return app.exec_()
|
||
|
||
def testShowBaseEnvironment():
|
||
"""测试ShowBase环境"""
|
||
print("\n启动ShowBase环境测试...")
|
||
|
||
# 配置Panda3D窗口
|
||
loadPrcFileData("", """
|
||
win-size 600 400
|
||
window-title ShowBase Environment Test
|
||
""")
|
||
|
||
# 创建ShowBase应用
|
||
app = ShowBaseTestWorld()
|
||
app.run()
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("DirectGUI环境对比测试")
|
||
print("="*50)
|
||
|
||
choice = input("选择测试环境:\n1. Qt环境\n2. ShowBase环境\n3. 两个都测试\n请输入(1/2/3): ").strip()
|
||
|
||
if choice == "1":
|
||
testQtEnvironment()
|
||
elif choice == "2":
|
||
testShowBaseEnvironment()
|
||
elif choice == "3":
|
||
print("先测试ShowBase环境...")
|
||
import threading
|
||
import time
|
||
|
||
# 先启动ShowBase环境
|
||
showbase_thread = threading.Thread(target=testShowBaseEnvironment)
|
||
showbase_thread.daemon = True
|
||
showbase_thread.start()
|
||
|
||
# 等待一会
|
||
time.sleep(2)
|
||
|
||
print("\n然后测试Qt环境...")
|
||
testQtEnvironment()
|
||
else:
|
||
print("无效选择")
|
||
|
||
if __name__ == "__main__":
|
||
main() |