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

"""
PANDA3D 简化版GUI功能测试
演示DirectGUI基本组件的使用
"""

from direct.showbase.ShowBase import ShowBase
from direct.gui.DirectGui import *
from direct.gui.OnscreenText import OnscreenText
from panda3d.core import *

class SimpleGUITest(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        
        # 设置窗口属性和背景
        wp = WindowProperties()
        wp.setTitle("PANDA3D 简化GUI测试")
        self.win.requestProperties(wp)
        self.setBackgroundColor(0.1, 0.2, 0.4)
        
        # 标题文本
        self.title = OnscreenText(
            text="PANDA3D DirectGUI 基础测试",
            pos=(0, 0.85),
            scale=0.08,
            fg=(1, 1, 1, 1),
            align=TextNode.ACenter
        )
        
        # 创建GUI组件
        self.setupGUI()
        
        # 尝试加载中文字体
        self.chinese_font = self.loadChineseFont()
        
        # 变量用于存储状态
        self.clickCount = 0
        
        print("="*50)
        print("PANDA3D GUI测试程序启动")
        print(f"中文字体加载状态: {'成功' if self.chinese_font else '失败，使用默认字体'}")
        print("尝试点击不同的GUI组件来测试功能")
        print("="*50)
    
    def setupGUI(self):
        """设置所有GUI组件"""
        
        # 1. 按钮测试
        self.createButtons()
        
        # 2. 文本输入框测试
        self.createTextEntry()
        
        # 3. 滑块测试
        self.createSlider()
        
        # 4. 复选框测试
        self.createCheckbox()
        
        # 5. 选项菜单测试
        self.createOptionMenu()
        
        # 6. 状态显示
        self.createStatusDisplay()
    
    def createButtons(self):
        """创建按钮组件"""
        # 主要测试按钮
        self.testButton = DirectButton(
            text="点击测试",
            pos=(-0.6, 0, 0.5),
            scale=0.08,
            command=self.onTestButtonClick,
            frameColor=(0.2, 0.7, 0.2, 1),
            text_fg=(1, 1, 1, 1),
            text_font=self.chinese_font if self.chinese_font else None
        )
        
        # 重置按钮
        self.resetButton = DirectButton(
            text="重置",
            pos=(0, 0, 0.5),
            scale=0.08,
            command=self.onResetClick,
            frameColor=(0.7, 0.7, 0.2, 1),
            text_fg=(1, 1, 1, 1),
            text_font=self.chinese_font if self.chinese_font else None
        )
        
        # 退出按钮
        self.exitButton = DirectButton(
            text="退出",
            pos=(0.6, 0, 0.5),
            scale=0.08,
            command=self.exitProgram,
            frameColor=(0.7, 0.2, 0.2, 1),
            text_fg=(1, 1, 1, 1),
            text_font=self.chinese_font if self.chinese_font else None
        )
    
    def createTextEntry(self):
        """创建文本输入框"""
        # 输入框标签
        self.entryLabel = DirectLabel(
            text="文本输入:",
            pos=(-0.8, 0, 0.2),
            scale=0.06,
            frameColor=(0, 0, 0, 0),
            text_fg=(1, 1, 1, 1)
        )
        
        # 文本输入框
        self.textEntry = DirectEntry(
            text="",
            pos=(-0.3, 0, 0.2),
            scale=0.06,
            command=self.onTextSubmit,
            initialText="在此输入...",
            numLines=1,
            width=15,
            focus=0
        )
    
    def createSlider(self):
        """创建滑块"""
        # 滑块标签
        self.sliderLabel = DirectLabel(
            text="滑块:",
            pos=(-0.8, 0, 0),
            scale=0.06,
            frameColor=(0, 0, 0, 0),
            text_fg=(1, 1, 1, 1)
        )
        
        # 滑块控件
        self.slider = DirectSlider(
            range=(0, 100),
            value=50,
            pos=(-0.2, 0, 0),
            scale=0.5,
            command=self.onSliderChange,
            orientation=DGG.HORIZONTAL
        )
        
        # 滑块值显示
        self.sliderValueLabel = DirectLabel(
            text="值: 50",
            pos=(0.4, 0, 0),
            scale=0.06,
            frameColor=(0, 0, 0, 0),
            text_fg=(0, 1, 1, 1)
        )
    
    def createCheckbox(self):
        """创建复选框"""
        self.checkbox = DirectCheckButton(
            text="启用特殊模式",
            pos=(-0.6, 0, -0.2),
            scale=0.06,
            command=self.onCheckboxToggle
        )
    
    def createOptionMenu(self):
        """创建选项菜单"""
        # 选项菜单标签
        self.menuLabel = DirectLabel(
            text="选择:",
            pos=(-0.8, 0, -0.4),
            scale=0.06,
            frameColor=(0, 0, 0, 0),
            text_fg=(1, 1, 1, 1)
        )
        
        # 选项列表
        options = ["选项1", "选项2", "选项3", "高级选项"]
        self.optionMenu = DirectOptionMenu(
            text="请选择",
            pos=(-0.3, 0, -0.4),
            scale=0.06,
            items=options,
            initialitem=0,
            command=self.onOptionSelect
        )
    
    def createStatusDisplay(self):
        """创建状态显示区域"""
        # 状态标题
        self.statusTitle = DirectLabel(
            text="状态信息:",
            pos=(-0.95, 0, -0.7),
            scale=0.05,
            frameColor=(0, 0, 0, 0),
            text_fg=(1, 1, 0, 1),
            text_align=TextNode.ALeft
        )
        
        # 状态显示文本
        self.statusText = OnscreenText(
            text="程序就绪",
            pos=(-0.95, -0.8),
            scale=0.05,
            fg=(0, 1, 0, 1),
            align=TextNode.ALeft
        )
        
        # 计数器显示
        self.counterText = OnscreenText(
            text="点击次数: 0",
            pos=(0.5, -0.8),
            scale=0.05,
            fg=(1, 0.5, 0, 1),
            align=TextNode.ALeft
        )
    
    # 事件处理函数
    def onTestButtonClick(self):
        """测试按钮点击事件"""
        self.clickCount += 1
        self.updateStatus(f"按钮被点击! 第{self.clickCount}次")
        self.counterText.setText(f"点击次数: {self.clickCount}")
        print(f"测试按钮被点击，当前点击次数: {self.clickCount}")
    
    def onResetClick(self):
        """重置按钮点击事件"""
        self.clickCount = 0
        self.slider['value'] = 50
        self.sliderValueLabel['text'] = "值: 50"
        self.textEntry.enterText("")
        self.textEntry.set("在此输入...")
        self.checkbox['indicatorValue'] = 0
        self.optionMenu.set(0)
        self.updateStatus("所有设置已重置")
        self.counterText.setText("点击次数: 0")
        print("界面已重置")
    
    def loadChineseFont(self):
        """尝试加载中文字体"""
        import os
        font_paths = [
            '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc',
            '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc',
            '/usr/share/fonts/truetype/arphic/ukai.ttc',
            '/usr/share/fonts/truetype/arphic/uming.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 onTextSubmit(self, text):
        """文本提交事件"""
        if text.strip():
            self.updateStatus(f"输入文本: {text}")
            print(f"用户输入: {text}")
        else:
            self.updateStatus("输入为空")
    
    def onSliderChange(self):
        """滑块变化事件"""
        value = int(self.slider['value'])
        self.sliderValueLabel['text'] = f"值: {value}"
        self.updateStatus(f"滑块值: {value}")
        print(f"滑块值变更为: {value}")
    
    def onCheckboxToggle(self, status):
        """复选框切换事件"""
        state = "开启" if status else "关闭"
        self.updateStatus(f"特殊模式: {state}")
        print(f"复选框状态: {state}")
    
    def onOptionSelect(self, selectedOption):
        """选项菜单选择事件"""
        self.updateStatus(f"选择: {selectedOption}")
        print(f"选项变更为: {selectedOption}")
    
    def updateStatus(self, message):
        """更新状态显示"""
        self.statusText.setText(message)
    
    def exitProgram(self):
        """退出程序"""
        print("用户点击退出按钮")
        print("程序即将关闭...")
        self.userExit()

# 程序入口
if __name__ == "__main__":
    print("启动PANDA3D GUI测试程序...")
    app = SimpleGUITest()
    app.run() 