173 lines
5.5 KiB
Python
173 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
简单的2D GUI测试
|
||
基于ShowBase直接测试2D GUI组件显示
|
||
"""
|
||
|
||
import warnings
|
||
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||
|
||
from direct.showbase.ShowBase import ShowBase
|
||
from direct.gui.DirectGui import *
|
||
from panda3d.core import *
|
||
import os
|
||
|
||
class SimpleGUITest(ShowBase):
|
||
def __init__(self):
|
||
ShowBase.__init__(self)
|
||
|
||
# 设置背景色
|
||
self.setBackgroundColor(0.2, 0.2, 0.3)
|
||
|
||
# 尝试加载中文字体
|
||
self.chinese_font = self.loadChineseFont()
|
||
|
||
# 创建测试GUI元素
|
||
self.createTestGUI()
|
||
|
||
print("简单GUI测试启动成功!")
|
||
print(f"中文字体加载状态: {'成功' if self.chinese_font else '失败,使用默认字体'}")
|
||
|
||
def loadChineseFont(self):
|
||
"""尝试加载中文字体"""
|
||
font_paths = [
|
||
'/usr/share/fonts/truetype/wqy/wqy-microhei.ttc',
|
||
'/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc'
|
||
]
|
||
|
||
for font_path in font_paths:
|
||
if os.path.exists(font_path):
|
||
try:
|
||
font = self.loader.loadFont(font_path)
|
||
if font:
|
||
return font
|
||
except:
|
||
continue
|
||
return None
|
||
|
||
def createTestGUI(self):
|
||
"""创建测试GUI元素"""
|
||
print("\n=== 创建2D GUI测试元素 ===")
|
||
|
||
# 标题
|
||
self.title = OnscreenText(
|
||
text="2D 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
|
||
)
|
||
|
||
# 测试修复后的坐标转换
|
||
test_positions = [
|
||
("中心", (0, 0, 0)),
|
||
("左侧", (-5, 0, 0)),
|
||
("右侧", (5, 0, 0)),
|
||
("上方", (0, 0, 5)),
|
||
("下方", (0, 0, -5))
|
||
]
|
||
|
||
self.buttons = []
|
||
self.labels = []
|
||
|
||
for i, (name, logical_pos) in enumerate(test_positions):
|
||
# 应用修复后的坐标转换
|
||
screen_pos = (logical_pos[0] * 0.1, 0, logical_pos[2] * 0.1)
|
||
|
||
print(f"创建 {name} GUI元素:")
|
||
print(f" 逻辑坐标: {logical_pos}")
|
||
print(f" 屏幕坐标: {screen_pos}")
|
||
|
||
# 创建按钮
|
||
button = DirectButton(
|
||
text=f"{name}按钮",
|
||
pos=screen_pos,
|
||
scale=0.08,
|
||
command=self.onButtonClick,
|
||
extraArgs=[name],
|
||
frameColor=(0.2, 0.6, 0.8, 1),
|
||
text_font=self.chinese_font if self.chinese_font else None,
|
||
rolloverSound=None,
|
||
clickSound=None
|
||
)
|
||
self.buttons.append(button)
|
||
|
||
# 创建标签(偏移位置)
|
||
label_pos = (screen_pos[0], 0, screen_pos[2] - 0.15)
|
||
label = DirectLabel(
|
||
text=f"{name}标签",
|
||
pos=label_pos,
|
||
scale=0.06,
|
||
frameColor=(0, 0, 0, 0),
|
||
text_fg=(1, 1, 0, 1),
|
||
text_font=self.chinese_font if self.chinese_font else None
|
||
)
|
||
self.labels.append(label)
|
||
|
||
print(f" 按钮已创建在: {screen_pos}")
|
||
print(f" 标签已创建在: {label_pos}")
|
||
|
||
# 创建输入框
|
||
self.entry = DirectEntry(
|
||
text="",
|
||
pos=(0, 0, -0.7),
|
||
scale=0.06,
|
||
command=self.onEntrySubmit,
|
||
initialText="测试输入框",
|
||
numLines=1,
|
||
width=15,
|
||
focus=0
|
||
)
|
||
|
||
print(f"输入框已创建在: (0, 0, -0.7)")
|
||
|
||
# 创建退出按钮
|
||
self.exitButton = DirectButton(
|
||
text="退出测试",
|
||
pos=(0, 0, -0.85),
|
||
scale=0.06,
|
||
command=self.exitTest,
|
||
frameColor=(0.8, 0.2, 0.2, 1),
|
||
text_font=self.chinese_font if self.chinese_font else None
|
||
)
|
||
|
||
# 状态信息
|
||
self.statusText = OnscreenText(
|
||
text="如果你能看到5个按钮和5个标签,说明2D GUI修复成功!",
|
||
pos=(0, -0.95),
|
||
scale=0.04,
|
||
fg=(0, 1, 0, 1),
|
||
align=TextNode.ACenter,
|
||
font=self.chinese_font if self.chinese_font else None
|
||
)
|
||
|
||
print("=== 所有GUI元素创建完成 ===")
|
||
print("如果修复成功,你应该看到:")
|
||
print("- 5个不同位置的按钮(中心、左侧、右侧、上方、下方)")
|
||
print("- 5个对应的标签")
|
||
print("- 1个输入框")
|
||
print("- 1个退出按钮")
|
||
|
||
def onButtonClick(self, name):
|
||
"""按钮点击事件"""
|
||
print(f"✓ 按钮点击测试成功: {name}")
|
||
self.statusText.setText(f"点击了 {name} 按钮 - 测试成功!")
|
||
|
||
def onEntrySubmit(self, text):
|
||
"""输入框提交事件"""
|
||
print(f"✓ 输入框测试成功: {text}")
|
||
self.statusText.setText(f"输入框内容: {text}")
|
||
|
||
def exitTest(self):
|
||
"""退出测试"""
|
||
print("退出简单GUI测试")
|
||
self.userExit()
|
||
|
||
if __name__ == "__main__":
|
||
print("启动简单2D GUI测试...")
|
||
print("这个测试使用ShowBase基类,应该能正确显示2D GUI组件")
|
||
app = SimpleGUITest()
|
||
app.run() |