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

"""
GUI文本编辑功能测试脚本
"""

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

from direct.showbase.ShowBase import ShowBase
from direct.gui.DirectGui import DirectButton, DirectLabel
from panda3d.core import TextNode, WindowProperties

class GUIEditTest(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        
        # 设置窗口
        wp = WindowProperties()
        wp.setTitle("GUI文本编辑测试")
        self.win.requestProperties(wp)
        self.setBackgroundColor(0.2, 0.3, 0.5)
        
        # 创建GUI元素列表
        self.gui_elements = []
        
        # 创建测试按钮
        self.button = DirectButton(
            text="测试按钮",
            pos=(0, 0, 0.3),
            scale=0.1,
            command=self.testButtonEdit,
            frameColor=(0.2, 0.6, 0.8, 1)
        )
        self.button.setTag("gui_type", "button")
        self.button.setTag("gui_text", "测试按钮")
        self.gui_elements.append(self.button)
        
        # 创建测试标签
        self.label = DirectLabel(
            text="测试标签",
            pos=(0, 0, 0),
            scale=0.08,
            frameColor=(0, 0, 0, 0),
            text_fg=(1, 1, 1, 1)
        )
        self.label.setTag("gui_type", "label")
        self.label.setTag("gui_text", "测试标签")
        self.gui_elements.append(self.label)
        
        # 创建3D文本
        textNode = TextNode('3d-test-text')
        textNode.setText("3D测试文本")
        textNode.setAlign(TextNode.ACenter)
        self.text3d = self.render.attachNewNode(textNode)
        self.text3d.setPos(0, 5, 0)
        self.text3d.setScale(0.5)
        self.text3d.setColor(1, 1, 0, 1)
        self.text3d.setBillboardAxis()
        self.text3d.setTag("gui_type", "3d_text")
        self.text3d.setTag("gui_text", "3D测试文本")
        self.gui_elements.append(self.text3d)
        
        # 测试计数器
        self.test_count = 0
        
        print("GUI编辑测试启动")
        print("按任意键开始测试文本编辑功能")
        
        # 绑定键盘事件
        self.accept("space", self.testGUIEdit)
        self.accept("1", self.testButtonTextEdit)
        self.accept("2", self.testLabelTextEdit)
        self.accept("3", self.test3DTextEdit)
        
        # 显示说明
        self.instructions = DirectLabel(
            text="按键说明:\nSpace - 循环测试所有GUI\n1 - 测试按钮\n2 - 测试标签\n3 - 测试3D文本",
            pos=(-0.9, 0, -0.5),
            scale=0.05,
            frameColor=(0, 0, 0, 0),
            text_fg=(1, 1, 1, 1),
            text_align=TextNode.ALeft
        )
    
    def editGUIElement(self, gui_element, property_name, value):
        """编辑GUI元素属性"""
        try:
            from panda3d.core import TextNode
            
            gui_type = gui_element.getTag("gui_type") if hasattr(gui_element, 'getTag') else "unknown"
            print(f"编辑 {gui_type}: {property_name} = {value}")
            
            if property_name == "text":
                if gui_type in ["button", "label"]:
                    gui_element['text'] = value
                    print(f"成功更新2D GUI文本: {value}")
                elif gui_type == "3d_text":
                    if isinstance(gui_element.node(), TextNode):
                        gui_element.node().setText(value)
                        print(f"成功更新3D文本: {value}")
                    else:
                        print(f"警告: {gui_type}不是TextNode类型")
                        
                gui_element.setTag("gui_text", value)
                return True
                
        except Exception as e:
            print(f"编辑GUI元素失败: {str(e)}")
            import traceback
            traceback.print_exc()
        return False
    
    def testGUIEdit(self):
        """循环测试所有GUI元素"""
        self.test_count += 1
        
        for i, gui_element in enumerate(self.gui_elements):
            gui_type = gui_element.getTag("gui_type")
            new_text = f"{gui_type}_测试_{self.test_count}"
            print(f"\n测试编辑GUI元素 {i}: {gui_type}")
            success = self.editGUIElement(gui_element, "text", new_text)
            print(f"结果: {'成功' if success else '失败'}")
        
        print(f"\n第 {self.test_count} 轮测试完成")
    
    def testButtonTextEdit(self):
        """测试按钮文本编辑"""
        new_text = f"按钮_修改_{self.test_count}"
        self.test_count += 1
        success = self.editGUIElement(self.button, "text", new_text)
        print(f"按钮文本编辑: {'成功' if success else '失败'}")
    
    def testLabelTextEdit(self):
        """测试标签文本编辑"""
        new_text = f"标签_修改_{self.test_count}"
        self.test_count += 1
        success = self.editGUIElement(self.label, "text", new_text)
        print(f"标签文本编辑: {'成功' if success else '失败'}")
    
    def test3DTextEdit(self):
        """测试3D文本编辑"""
        new_text = f"3D文本_修改_{self.test_count}"
        self.test_count += 1
        success = self.editGUIElement(self.text3d, "text", new_text)
        print(f"3D文本编辑: {'成功' if success else '失败'}")
    
    def testButtonEdit(self):
        """按钮点击测试"""
        print("测试按钮被点击!")
        self.testGUIEdit()

if __name__ == "__main__":
    print("启动GUI文本编辑测试...")
    test = GUIEditTest()
    test.run() 